##// END OF EJS Templates
Minor style fixes to view classes. Fixed ban view
neko259 -
r561:2045aa89 1.7-dev
parent child Browse files
Show More
@@ -1,12 +1,13
1 from django.shortcuts import render
1 from django.shortcuts import render
2
2
3 from boards.authors import authors
3 from boards.authors import authors
4 from boards.views.base import BaseBoardView
4 from boards.views.base import BaseBoardView
5
5
6
6 class AuthorsView(BaseBoardView):
7 class AuthorsView(BaseBoardView):
7
8
8 def get(self, request):
9 def get(self, request):
9 context = self.get_context_data(request=request)
10 context = self.get_context_data(request=request)
10 context['authors'] = authors
11 context['authors'] = authors
11
12
12 return render(request, 'boards/authors.html', context)
13 return render(request, 'boards/authors.html', context)
@@ -1,20 +1,22
1 from django.db import transaction
1 from django.db import transaction
2 from django.shortcuts import get_object_or_404
2
3
3 from boards.views.base import BaseBoardView
4 from boards.views.base import BaseBoardView
4 from boards.models import Post, Ban
5 from boards.models import Post, Ban
5
6
7
6 class BanUserView(BaseBoardView):
8 class BanUserView(BaseBoardView):
7
9
8 @transaction.atomic
10 @transaction.atomic
9 def get(self, request, post_id):
11 def get(self, request, post_id):
10 user = self._get_user(request)
12 user = self._get_user(request)
11 post = get_object_or_404(Post, id=post_id)
13 post = get_object_or_404(Post, id=post_id)
12
14
13 if user.is_moderator():
15 if user.is_moderator():
14 # TODO Show confirmation page before ban
16 # TODO Show confirmation page before ban
15 ban, created = Ban.objects.get_or_create(ip=post.poster_ip)
17 ban, created = Ban.objects.get_or_create(ip=post.poster_ip)
16 if created:
18 if created:
17 ban.reason = 'Banned for post ' + str(post_id)
19 ban.reason = 'Banned for post ' + str(post_id)
18 ban.save()
20 ban.save()
19
21
20 return self.redirect_to_next(request)
22 return self.redirect_to_next(request)
@@ -1,25 +1,26
1 from django.shortcuts import redirect, get_object_or_404
1 from django.shortcuts import redirect, get_object_or_404
2 from django.db import transaction
2 from django.db import transaction
3
3
4 from boards.views.base import BaseBoardView
4 from boards.views.base import BaseBoardView
5 from boards.views.redirect_next_mixin import RedirectNextMixin
5 from boards.views.redirect_next_mixin import RedirectNextMixin
6 from boards.models import Post
6 from boards.models import Post
7
7
8
8 class DeletePostView(BaseBoardView, RedirectNextMixin):
9 class DeletePostView(BaseBoardView, RedirectNextMixin):
9
10
10 @transaction.atomic
11 @transaction.atomic
11 def get(self, request, post_id):
12 def get(self, request, post_id):
12 user = self._get_user(request)
13 user = self._get_user(request)
13 post = get_object_or_404(Post, id=post_id)
14 post = get_object_or_404(Post, id=post_id)
14
15
15 opening_post = post.is_opening()
16 opening_post = post.is_opening()
16
17
17 if user.is_moderator():
18 if user.is_moderator():
18 # TODO Show confirmation page before deletion
19 # TODO Show confirmation page before deletion
19 Post.objects.delete_post(post)
20 Post.objects.delete_post(post)
20
21
21 if not opening_post:
22 if not opening_post:
22 thread = post.thread_new
23 thread = post.thread_new
23 return redirect('thread', post_id=thread.get_opening_post().id)
24 return redirect('thread', post_id=thread.get_opening_post().id)
24 else:
25 else:
25 return self.redirect_to_next(request)
26 return self.redirect_to_next(request)
@@ -1,12 +1,13
1 from django.shortcuts import render
1 from django.shortcuts import render
2
2
3 from boards.views.base import BaseBoardView
3 from boards.views.base import BaseBoardView
4
4
5
5 class NotFoundView(BaseBoardView):
6 class NotFoundView(BaseBoardView):
6 '''
7 """
7 Page 404 (not found)
8 Page 404 (not found)
8 '''
9 """
9
10
10 def get(self, request):
11 def get(self, request):
11 context = self.get_context_data(request=request)
12 context = self.get_context_data(request=request)
12 return render(request, 'boards/404.html', context)
13 return render(request, 'boards/404.html', context)
@@ -1,17 +1,18
1 from django.shortcuts import redirect
1 from django.shortcuts import redirect
2 from django.http import HttpResponseRedirect
2 from django.http import HttpResponseRedirect
3
3
4
4 class RedirectNextMixin:
5 class RedirectNextMixin:
5
6
6 def redirect_to_next(self, request):
7 def redirect_to_next(self, request):
7 """
8 """
8 If a 'next' parameter was specified, redirect to the next page. This
9 If a 'next' parameter was specified, redirect to the next page. This
9 is used when the user is required to return to some page after the
10 is used when the user is required to return to some page after the
10 current view has finished its work.
11 current view has finished its work.
11 """
12 """
12
13
13 if 'next' in request.GET:
14 if 'next' in request.GET:
14 next_page = request.GET['next']
15 next_page = request.GET['next']
15 return HttpResponseRedirect(next_page)
16 return HttpResponseRedirect(next_page)
16 else:
17 else:
17 return redirect('index')
18 return redirect('index')
@@ -1,50 +1,52
1 from django.db import transaction
1 from django.db import transaction
2 from django.shortcuts import render, redirect
2 from django.shortcuts import render, redirect
3
3
4 from boards.views.base import BaseBoardView, PARAMETER_FORM
4 from boards.views.base import BaseBoardView, PARAMETER_FORM
5 from boards.forms import SettingsForm, ModeratorSettingsForm, PlainErrorList
5 from boards.forms import SettingsForm, ModeratorSettingsForm, PlainErrorList
6 from boards.views import SETTING_MODERATE
6 from boards.views import SETTING_MODERATE
7
7
8
8 class SettingsView(BaseBoardView):
9 class SettingsView(BaseBoardView):
9
10
10 def get(self, request):
11 def get(self, request):
11 context = self.get_context_data(request=request)
12 context = self.get_context_data(request=request)
12 user = context['user']
13 user = context['user']
13 is_moderator = user.is_moderator()
14 is_moderator = user.is_moderator()
14
15
15 selected_theme = context['theme']
16 selected_theme = context['theme']
16
17
17 if is_moderator:
18 if is_moderator:
18 form = ModeratorSettingsForm(initial={'theme': selected_theme,
19 form = ModeratorSettingsForm(initial={
19 'moderate': context['moderator']},
20 'theme': selected_theme,
20 error_class=PlainErrorList)
21 'moderate': context['moderator']
22 }, error_class=PlainErrorList)
21 else:
23 else:
22 form = SettingsForm(initial={'theme': selected_theme},
24 form = SettingsForm(initial={'theme': selected_theme},
23 error_class=PlainErrorList)
25 error_class=PlainErrorList)
24
26
25 context[PARAMETER_FORM] = form
27 context[PARAMETER_FORM] = form
26
28
27 return render(request, 'boards/settings.html', context)
29 return render(request, 'boards/settings.html', context)
28
30
29 def post(self, request):
31 def post(self, request):
30 context = self.get_context_data(request=request)
32 context = self.get_context_data(request=request)
31 user = context['user']
33 user = context['user']
32 is_moderator = user.is_moderator()
34 is_moderator = user.is_moderator()
33
35
34 with transaction.atomic():
36 with transaction.atomic():
35 if is_moderator:
37 if is_moderator:
36 form = ModeratorSettingsForm(request.POST,
38 form = ModeratorSettingsForm(request.POST,
37 error_class=PlainErrorList)
39 error_class=PlainErrorList)
38 else:
40 else:
39 form = SettingsForm(request.POST, error_class=PlainErrorList)
41 form = SettingsForm(request.POST, error_class=PlainErrorList)
40
42
41 if form.is_valid():
43 if form.is_valid():
42 selected_theme = form.cleaned_data['theme']
44 selected_theme = form.cleaned_data['theme']
43
45
44 user.save_setting('theme', selected_theme)
46 user.save_setting('theme', selected_theme)
45
47
46 if is_moderator:
48 if is_moderator:
47 moderate = form.cleaned_data['moderate']
49 moderate = form.cleaned_data['moderate']
48 user.save_setting(SETTING_MODERATE, moderate)
50 user.save_setting(SETTING_MODERATE, moderate)
49
51
50 return redirect('settings')
52 return redirect('settings')
@@ -1,121 +1,121
1 import string
1 import string
2 from django.core.urlresolvers import reverse
2 from django.core.urlresolvers import reverse
3 from django.db import transaction
3 from django.db import transaction
4 from django.http import Http404
4 from django.http import Http404
5 from django.shortcuts import get_object_or_404, render, redirect
5 from django.shortcuts import get_object_or_404, render, redirect
6 from boards import utils
6 from boards import utils
7 from boards.forms import PostForm, PlainErrorList
7 from boards.forms import PostForm, PlainErrorList
8 from boards.models import Post, Ban, Tag
8 from boards.models import Post, Ban, Tag
9 from boards.views.banned import BannedView
9 from boards.views.banned import BannedView
10 from boards.views.base import BaseBoardView, PARAMETER_FORM
10 from boards.views.base import BaseBoardView, PARAMETER_FORM
11 from boards.views.posting_mixin import PostMixin
11 from boards.views.posting_mixin import PostMixin
12 import neboard
12 import neboard
13
13
14 MODE_GALLERY = 'gallery'
14 MODE_GALLERY = 'gallery'
15 MODE_NORMAL = 'normal'
15 MODE_NORMAL = 'normal'
16
16
17
17
18 class ThreadView(BaseBoardView, PostMixin):
18 class ThreadView(BaseBoardView, PostMixin):
19
19
20 def get(self, request, post_id, mode=MODE_NORMAL, form=None):
20 def get(self, request, post_id, mode=MODE_NORMAL, form=None):
21 opening_post = get_object_or_404(Post, id=post_id)
21 opening_post = get_object_or_404(Post, id=post_id)
22
22
23 # If this is not OP, don't show it as it is
23 # If this is not OP, don't show it as it is
24 if not opening_post.is_opening():
24 if not opening_post.is_opening():
25 raise Http404
25 raise Http404
26
26
27 if not form:
27 if not form:
28 form = PostForm(error_class=PlainErrorList)
28 form = PostForm(error_class=PlainErrorList)
29
29
30 thread_to_show = opening_post.thread_new
30 thread_to_show = opening_post.thread_new
31
31
32 context = self.get_context_data(request=request)
32 context = self.get_context_data(request=request)
33
33
34 posts = thread_to_show.get_replies()
34 posts = thread_to_show.get_replies()
35 context[PARAMETER_FORM] = form
35 context[PARAMETER_FORM] = form
36 context["last_update"] = utils.datetime_to_epoch(
36 context["last_update"] = utils.datetime_to_epoch(
37 thread_to_show.last_edit_time)
37 thread_to_show.last_edit_time)
38 context["thread"] = thread_to_show
38 context["thread"] = thread_to_show
39
39
40 if MODE_NORMAL == mode:
40 if MODE_NORMAL == mode:
41 context['bumpable'] = thread_to_show.can_bump()
41 context['bumpable'] = thread_to_show.can_bump()
42 if context['bumpable']:
42 if context['bumpable']:
43 context['posts_left'] = neboard.settings.MAX_POSTS_PER_THREAD - posts \
43 context['posts_left'] = neboard.settings.MAX_POSTS_PER_THREAD - posts \
44 .count()
44 .count()
45 context['bumplimit_progress'] = str(
45 context['bumplimit_progress'] = str(
46 float(context['posts_left']) /
46 float(context['posts_left']) /
47 neboard.settings.MAX_POSTS_PER_THREAD * 100)
47 neboard.settings.MAX_POSTS_PER_THREAD * 100)
48
48
49 context['posts'] = posts
49 context['posts'] = posts
50
50
51 document = 'boards/thread.html'
51 document = 'boards/thread.html'
52 elif MODE_GALLERY == mode:
52 elif MODE_GALLERY == mode:
53 context['posts'] = posts.filter(image_width__gt=0)
53 context['posts'] = posts.filter(image_width__gt=0)
54
54
55 document = 'boards/thread_gallery.html'
55 document = 'boards/thread_gallery.html'
56 else:
56 else:
57 raise Http404
57 raise Http404
58
58
59 return render(request, document, context)
59 return render(request, document, context)
60
60
61 def post(self, request, post_id, mode=MODE_NORMAL):
61 def post(self, request, post_id, mode=MODE_NORMAL):
62 opening_post = get_object_or_404(Post, id=post_id)
62 opening_post = get_object_or_404(Post, id=post_id)
63
63
64 # If this is not OP, don't show it as it is
64 # If this is not OP, don't show it as it is
65 if not opening_post.is_opening():
65 if not opening_post.is_opening():
66 raise Http404
66 raise Http404
67
67
68 if not opening_post.thread_new.archived:
68 if not opening_post.thread_new.archived:
69 form = PostForm(request.POST, request.FILES,
69 form = PostForm(request.POST, request.FILES,
70 error_class=PlainErrorList)
70 error_class=PlainErrorList)
71 form.session = request.session
71 form.session = request.session
72
72
73 if form.is_valid():
73 if form.is_valid():
74 return self.new_post(request, form, opening_post)
74 return self.new_post(request, form, opening_post)
75 if form.need_to_ban:
75 if form.need_to_ban:
76 # Ban user because he is suspected to be a bot
76 # Ban user because he is suspected to be a bot
77 self._ban_current_user(request)
77 self._ban_current_user(request)
78
78
79 return self.get(request, post_id, mode, form)
79 return self.get(request, post_id, mode, form)
80
80
81 @transaction.atomic
81 @transaction.atomic
82 def new_post(self, request, form, opening_post=None, html_response=True):
82 def new_post(self, request, form, opening_post=None, html_response=True):
83 """Add a new post (in thread or as a reply)."""
83 """Add a new post (in thread or as a reply)."""
84
84
85 ip = utils.get_client_ip(request)
85 ip = utils.get_client_ip(request)
86 is_banned = Ban.objects.filter(ip=ip).exists()
86 is_banned = Ban.objects.filter(ip=ip).exists()
87
87
88 if is_banned:
88 if is_banned:
89 if html_response:
89 if html_response:
90 return redirect(BannedView().as_view())
90 return redirect(BannedView().as_view())
91 else:
91 else:
92 return
92 return
93
93
94 data = form.cleaned_data
94 data = form.cleaned_data
95
95
96 title = data['title']
96 title = data['title']
97 text = data['text']
97 text = data['text']
98
98
99 text = self._remove_invalid_links(text)
99 text = self._remove_invalid_links(text)
100
100
101 if 'image' in data.keys():
101 if 'image' in data.keys():
102 image = data['image']
102 image = data['image']
103 else:
103 else:
104 image = None
104 image = None
105
105
106 tags = []
106 tags = []
107
107
108 post_thread = opening_post.thread_new
108 post_thread = opening_post.thread_new
109
109
110 post = Post.objects.create_post(title=title, text=text, ip=ip,
110 post = Post.objects.create_post(title=title, text=text, ip=ip,
111 thread=post_thread, image=image,
111 thread=post_thread, image=image,
112 tags=tags,
112 tags=tags,
113 user=self._get_user(request))
113 user=self._get_user(request))
114
114
115 thread_to_show = (opening_post.id if opening_post else post.id)
115 thread_to_show = (opening_post.id if opening_post else post.id)
116
116
117 if html_response:
117 if html_response:
118 if opening_post:
118 if opening_post:
119 return redirect(reverse('thread',
119 return redirect(reverse(
120 kwargs={'post_id': thread_to_show}) + '#'
120 'thread',
121 + str(post.id))
121 kwargs={'post_id': thread_to_show}) + '#' + str(post.id))
General Comments 0
You need to be logged in to leave comments. Login now