##// END OF EJS Templates
Moved login view to a separate class. Refactored thread and all threads views
neko259 -
r544:5f217adc 1.7-dev
parent child Browse files
Show More
@@ -0,0 +1,30 b''
1 from django.shortcuts import render, redirect
2 from boards.forms import LoginForm, PlainErrorList
3 from boards.models import User
4 from boards.views.base import BaseBoardView, PARAMETER_FORM
5
6 __author__ = 'neko259'
7
8
9 class LoginView(BaseBoardView):
10
11 def get(self, request, form=None):
12 context = self.get_context_data(request=request)
13
14 if not form:
15 form = LoginForm()
16 context[PARAMETER_FORM] = form
17
18 return render(request, 'boards/login.html', context)
19
20 def post(self, request):
21 form = LoginForm(request.POST, request.FILES,
22 error_class=PlainErrorList)
23 form.session = request.session
24
25 if form.is_valid():
26 user = User.objects.get(user_id=form.cleaned_data['user_id'])
27 request.session['user_id'] = user.id
28 return redirect('index')
29 else:
30 return self.get(request, form)
@@ -4,7 +4,7 b''
4
4
5 {% get_current_language as LANGUAGE_CODE %}
5 {% get_current_language as LANGUAGE_CODE %}
6
6
7 {% cache 60 post post.id post.thread_new.last_edit_time truncated moderator LANGUAGE_CODE %}
7 {% cache 300 post post.id post.thread_new.last_edit_time truncated moderator LANGUAGE_CODE need_open_link %}
8 {% spaceless %}
8 {% spaceless %}
9 {% with thread=post.thread_new %}
9 {% with thread=post.thread_new %}
10 {% if thread.archived %}
10 {% if thread.archived %}
@@ -93,4 +93,4 b''
93 </div>
93 </div>
94 {% endwith %}
94 {% endwith %}
95 {% endspaceless %}
95 {% endspaceless %}
96 {% endcache %} No newline at end of file
96 {% endcache %}
@@ -1,7 +1,7 b''
1 from django.conf.urls import patterns, url, include
1 from django.conf.urls import patterns, url, include
2 from boards import views
2 from boards import views
3 from boards.rss import AllThreadsFeed, TagThreadsFeed, ThreadPostsFeed
3 from boards.rss import AllThreadsFeed, TagThreadsFeed, ThreadPostsFeed
4 from boards.views import api, tag_threads, all_threads, archived_threads
4 from boards.views import api, tag_threads, all_threads, archived_threads, login
5
5
6 js_info_dict = {
6 js_info_dict = {
7 'packages': ('boards',),
7 'packages': ('boards',),
@@ -20,7 +20,7 b" urlpatterns = patterns('',"
20 archived_threads.ArchiveView.as_view(), name='archive'),
20 archived_threads.ArchiveView.as_view(), name='archive'),
21
21
22 # login page
22 # login page
23 url(r'^login/$', views.login, name='login'),
23 url(r'^login/$', login.LoginView.as_view(), name='login'),
24
24
25 # /boards/tag/tag_name/
25 # /boards/tag/tag_name/
26 url(r'^tag/(?P<tag_name>\w+)/$', tag_threads.TagView.as_view(),
26 url(r'^tag/(?P<tag_name>\w+)/$', tag_threads.TagView.as_view(),
@@ -1,12 +1,6 b''
1 from datetime import datetime, timedelta
2
3 from django.db.models import Count
4
5 __author__ = 'neko259'
1 __author__ = 'neko259'
6
2
7 import hashlib
3 import hashlib
8 import string
9 import re
10
4
11 from django.core import serializers
5 from django.core import serializers
12 from django.core.urlresolvers import reverse
6 from django.core.urlresolvers import reverse
@@ -20,15 +14,13 b' from django.views.decorators.cache impor'
20 from django.views.i18n import javascript_catalog
14 from django.views.i18n import javascript_catalog
21
15
22 import boards
16 import boards
23 from boards import utils
24 from boards.forms import SettingsForm, PlainErrorList, \
17 from boards.forms import SettingsForm, PlainErrorList, \
25 LoginForm, ModeratorSettingsForm
18 ModeratorSettingsForm
26 from boards.models import Post, Tag, Ban, User
19 from boards.models import Post, Tag, Ban, User
27 from boards.models.post import SETTING_MODERATE, REGEX_REPLY
20 from boards.models.post import SETTING_MODERATE
28 from boards.models.user import RANK_USER
21 from boards.models.user import RANK_USER
29 from boards import authors
22 from boards import authors
30 import neboard
23 import neboard
31
32 import all_threads
24 import all_threads
33
25
34
26
@@ -37,29 +29,6 b" BAN_REASON_SPAM = 'Autoban: spam bot'"
37 DEFAULT_PAGE = 1
29 DEFAULT_PAGE = 1
38
30
39
31
40 def login(request):
41 """Log in with user id"""
42
43 context = _init_default_context(request)
44
45 if request.method == 'POST':
46 form = LoginForm(request.POST, request.FILES,
47 error_class=PlainErrorList)
48 form.session = request.session
49
50 if form.is_valid():
51 user = User.objects.get(user_id=form.cleaned_data['user_id'])
52 request.session['user_id'] = user.id
53 return redirect('index')
54
55 else:
56 form = LoginForm()
57
58 context['form'] = form
59
60 return render(request, 'boards/login.html', context)
61
62
63 def settings(request):
32 def settings(request):
64 """User's settings"""
33 """User's settings"""
65
34
@@ -311,46 +280,4 b' def _redirect_to_next(request):'
311 next_page = request.GET['next']
280 next_page = request.GET['next']
312 return HttpResponseRedirect(next_page)
281 return HttpResponseRedirect(next_page)
313 else:
282 else:
314 return redirect('index')
283 return redirect('index') No newline at end of file
315
316
317 @transaction.atomic
318 def _ban_current_user(request):
319 """Add current user to the IP ban list"""
320
321 ip = utils.get_client_ip(request)
322 ban, created = Ban.objects.get_or_create(ip=ip)
323 if created:
324 ban.can_read = False
325 ban.reason = BAN_REASON_SPAM
326 ban.save()
327
328
329 def _remove_invalid_links(text):
330 """
331 Replace invalid links in posts so that they won't be parsed.
332 Invalid links are links to non-existent posts
333 """
334
335 for reply_number in re.finditer(REGEX_REPLY, text):
336 post_id = reply_number.group(1)
337 post = Post.objects.filter(id=post_id)
338 if not post.exists():
339 text = string.replace(text, '>>' + post_id, post_id)
340
341 return text
342
343
344 def _get_template_thread(thread_to_show):
345 """Get template values for thread"""
346
347 last_replies = thread_to_show.get_last_replies()
348 skipped_replies_count = thread_to_show.get_replies().count() \
349 - len(last_replies) - 1
350 return {
351 'thread': thread_to_show,
352 'op': thread_to_show.get_replies()[0],
353 'bumpable': thread_to_show.can_bump(),
354 'last_replies': last_replies,
355 'skipped_replies': skipped_replies_count,
356 }
@@ -25,10 +25,11 b' DEFAULT_PAGE = 1'
25
25
26 class AllThreadsView(PostMixin, BaseBoardView):
26 class AllThreadsView(PostMixin, BaseBoardView):
27
27
28 def get(self, request, page=DEFAULT_PAGE):
28 def get(self, request, page=DEFAULT_PAGE, form=None):
29 context = self.get_context_data(request=request)
29 context = self.get_context_data(request=request)
30
30
31 form = ThreadForm(error_class=PlainErrorList)
31 if not form:
32 form = ThreadForm(error_class=PlainErrorList)
32
33
33 paginator = Paginator(self.get_threads(),
34 paginator = Paginator(self.get_threads(),
34 neboard.settings.THREADS_PER_PAGE)
35 neboard.settings.THREADS_PER_PAGE)
@@ -55,17 +56,7 b' class AllThreadsView(PostMixin, BaseBoar'
55 # Ban user because he is suspected to be a bot
56 # Ban user because he is suspected to be a bot
56 self._ban_current_user(request)
57 self._ban_current_user(request)
57
58
58 paginator = Paginator(self.get_threads(),
59 return self.get(request, page, form)
59 neboard.settings.THREADS_PER_PAGE)
60
61 threads = paginator.page(page).object_list
62
63 context[PARAMETER_THREADS] = threads
64 context[PARAMETER_FORM] = form
65
66 self._get_page_context(paginator, context, page)
67
68 return render(request, TEMPLATE, context)
69
60
70 @staticmethod
61 @staticmethod
71 def _get_page_context(paginator, context, page):
62 def _get_page_context(paginator, context, page):
@@ -131,4 +122,4 b' class AllThreadsView(PostMixin, BaseBoar'
131 return redirect('thread', post_id=thread_to_show)
122 return redirect('thread', post_id=thread_to_show)
132
123
133 def get_threads(self):
124 def get_threads(self):
134 return Thread.objects.filter(archived=False) No newline at end of file
125 return Thread.objects.filter(archived=False)
@@ -17,14 +17,15 b" MODE_NORMAL = 'normal'"
17
17
18 class ThreadView(BaseBoardView, PostMixin):
18 class ThreadView(BaseBoardView, PostMixin):
19
19
20 def get(self, request, post_id, mode=MODE_NORMAL):
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 form = PostForm(error_class=PlainErrorList)
27 if not form:
28 form = PostForm(error_class=PlainErrorList)
28
29
29 thread_to_show = opening_post.thread_new
30 thread_to_show = opening_post.thread_new
30
31
@@ -75,36 +76,7 b' class ThreadView(BaseBoardView, PostMixi'
75 # Ban user because he is suspected to be a bot
76 # Ban user because he is suspected to be a bot
76 self._ban_current_user(request)
77 self._ban_current_user(request)
77
78
78 thread_to_show = opening_post.thread_new
79 return self.get(request, post_id, mode, form)
79
80 context = self.get_context_data(request=request)
81
82 posts = thread_to_show.get_replies()
83 context[PARAMETER_FORM] = form
84 context["last_update"] = utils.datetime_to_epoch(
85 thread_to_show.last_edit_time)
86 context["thread"] = thread_to_show
87
88 if MODE_NORMAL == mode:
89 context['bumpable'] = thread_to_show.can_bump()
90 if context['bumpable']:
91 context['posts_left'] = neboard.settings.MAX_POSTS_PER_THREAD - posts \
92 .count()
93 context['bumplimit_progress'] = str(
94 float(context['posts_left']) /
95 neboard.settings.MAX_POSTS_PER_THREAD * 100)
96
97 context['posts'] = posts
98
99 document = 'boards/thread.html'
100 elif MODE_GALLERY == mode:
101 context['posts'] = posts.filter(image_width__gt=0)
102
103 document = 'boards/thread_gallery.html'
104 else:
105 raise Http404
106
107 return render(request, document, context)
108
80
109 @transaction.atomic
81 @transaction.atomic
110 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):
@@ -146,4 +118,4 b' class ThreadView(BaseBoardView, PostMixi'
146 if opening_post:
118 if opening_post:
147 return redirect(reverse('thread',
119 return redirect(reverse('thread',
148 kwargs={'post_id': thread_to_show}) + '#'
120 kwargs={'post_id': thread_to_show}) + '#'
149 + str(post.id)) No newline at end of file
121 + str(post.id))
General Comments 0
You need to be logged in to leave comments. Login now