##// END OF EJS Templates
Fixed calls to render shortcut to use dict instead of context instance
neko259 -
r918:06775760 default
parent child Browse files
Show More
@@ -1,14 +1,14 b''
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 from boards.models.tag import Tag
4 from boards.models.tag import Tag
5
5
6
6
7 class AllTagsView(BaseBoardView):
7 class AllTagsView(BaseBoardView):
8
8
9 def get(self, request):
9 def get(self, request):
10 context = self.get_context_data(request=request)
10 params = dict()
11 context['all_tags'] = Tag.objects.get_not_empty_tags()
12
11
13 # TODO Use dict here
12 params['all_tags'] = Tag.objects.get_not_empty_tags()
14 return render(request, 'boards/tags.html', context_instance=context)
13
14 return render(request, 'boards/tags.html', params)
@@ -1,139 +1,137 b''
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 import utils, settings
4 from boards import utils, settings
5 from boards.abstracts.paginator import get_paginator
5 from boards.abstracts.paginator import get_paginator
6 from boards.abstracts.settingsmanager import get_settings_manager
6 from boards.abstracts.settingsmanager import get_settings_manager
7 from boards.forms import ThreadForm, PlainErrorList
7 from boards.forms import ThreadForm, PlainErrorList
8 from boards.models import Post, Thread, Ban, Tag
8 from boards.models import Post, Thread, Ban, Tag
9 from boards.views.banned import BannedView
9 from boards.views.banned import BannedView
10 from boards.views.base import BaseBoardView, CONTEXT_FORM
10 from boards.views.base import BaseBoardView, CONTEXT_FORM
11 from boards.views.posting_mixin import PostMixin
11 from boards.views.posting_mixin import PostMixin
12
12
13
13
14 FORM_TAGS = 'tags'
14 FORM_TAGS = 'tags'
15 FORM_TEXT = 'text'
15 FORM_TEXT = 'text'
16 FORM_TITLE = 'title'
16 FORM_TITLE = 'title'
17 FORM_IMAGE = 'image'
17 FORM_IMAGE = 'image'
18
18
19 TAG_DELIMITER = ' '
19 TAG_DELIMITER = ' '
20
20
21 PARAMETER_CURRENT_PAGE = 'current_page'
21 PARAMETER_CURRENT_PAGE = 'current_page'
22 PARAMETER_PAGINATOR = 'paginator'
22 PARAMETER_PAGINATOR = 'paginator'
23 PARAMETER_THREADS = 'threads'
23 PARAMETER_THREADS = 'threads'
24
24
25 TEMPLATE = 'boards/posting_general.html'
25 TEMPLATE = 'boards/posting_general.html'
26 DEFAULT_PAGE = 1
26 DEFAULT_PAGE = 1
27
27
28
28
29 class AllThreadsView(PostMixin, BaseBoardView):
29 class AllThreadsView(PostMixin, BaseBoardView):
30
30
31 def __init__(self):
31 def __init__(self):
32 self.settings_manager = None
32 self.settings_manager = None
33 super(AllThreadsView, self).__init__()
33 super(AllThreadsView, self).__init__()
34
34
35 def get(self, request, page=DEFAULT_PAGE, form=None):
35 def get(self, request, page=DEFAULT_PAGE, form=None):
36 context = self.get_context_data(request=request)
36 params = dict()
37
37
38 if not form:
38 if not form:
39 form = ThreadForm(error_class=PlainErrorList)
39 form = ThreadForm(error_class=PlainErrorList)
40
40
41 self.settings_manager = get_settings_manager(request)
41 self.settings_manager = get_settings_manager(request)
42 paginator = get_paginator(self.get_threads(),
42 paginator = get_paginator(self.get_threads(),
43 settings.THREADS_PER_PAGE)
43 settings.THREADS_PER_PAGE)
44 paginator.current_page = int(page)
44 paginator.current_page = int(page)
45
45
46 threads = paginator.page(page).object_list
46 threads = paginator.page(page).object_list
47
47
48 context[PARAMETER_THREADS] = threads
48 params[PARAMETER_THREADS] = threads
49 context[CONTEXT_FORM] = form
49 params[CONTEXT_FORM] = form
50
50
51 self._get_page_context(paginator, context, page)
51 self._get_page_context(paginator, params, page)
52
52
53 # TODO Use dict here
53 return render(request, TEMPLATE, params)
54 return render(request, TEMPLATE, context_instance=context)
55
54
56 def post(self, request, page=DEFAULT_PAGE):
55 def post(self, request, page=DEFAULT_PAGE):
57 form = ThreadForm(request.POST, request.FILES,
56 form = ThreadForm(request.POST, request.FILES,
58 error_class=PlainErrorList)
57 error_class=PlainErrorList)
59 form.session = request.session
58 form.session = request.session
60
59
61 if form.is_valid():
60 if form.is_valid():
62 return self.create_thread(request, form)
61 return self.create_thread(request, form)
63 if form.need_to_ban:
62 if form.need_to_ban:
64 # Ban user because he is suspected to be a bot
63 # Ban user because he is suspected to be a bot
65 self._ban_current_user(request)
64 self._ban_current_user(request)
66
65
67 return self.get(request, page, form)
66 return self.get(request, page, form)
68
67
69 @staticmethod
68 def _get_page_context(self, paginator, params, page):
70 def _get_page_context(paginator, context, page):
71 """
69 """
72 Get pagination context variables
70 Get pagination context variables
73 """
71 """
74
72
75 context[PARAMETER_PAGINATOR] = paginator
73 params[PARAMETER_PAGINATOR] = paginator
76 context[PARAMETER_CURRENT_PAGE] = paginator.page(int(page))
74 params[PARAMETER_CURRENT_PAGE] = paginator.page(int(page))
77
75
78 @staticmethod
76 @staticmethod
79 def parse_tags_string(tag_strings):
77 def parse_tags_string(tag_strings):
80 """
78 """
81 Parses tag list string and returns tag object list.
79 Parses tag list string and returns tag object list.
82 """
80 """
83
81
84 tags = []
82 tags = []
85
83
86 if tag_strings:
84 if tag_strings:
87 tag_strings = tag_strings.split(TAG_DELIMITER)
85 tag_strings = tag_strings.split(TAG_DELIMITER)
88 for tag_name in tag_strings:
86 for tag_name in tag_strings:
89 tag_name = tag_name.strip().lower()
87 tag_name = tag_name.strip().lower()
90 if len(tag_name) > 0:
88 if len(tag_name) > 0:
91 tag, created = Tag.objects.get_or_create(name=tag_name)
89 tag, created = Tag.objects.get_or_create(name=tag_name)
92 tags.append(tag)
90 tags.append(tag)
93
91
94 return tags
92 return tags
95
93
96 @transaction.atomic
94 @transaction.atomic
97 def create_thread(self, request, form, html_response=True):
95 def create_thread(self, request, form, html_response=True):
98 """
96 """
99 Creates a new thread with an opening post.
97 Creates a new thread with an opening post.
100 """
98 """
101
99
102 ip = utils.get_client_ip(request)
100 ip = utils.get_client_ip(request)
103 is_banned = Ban.objects.filter(ip=ip).exists()
101 is_banned = Ban.objects.filter(ip=ip).exists()
104
102
105 if is_banned:
103 if is_banned:
106 if html_response:
104 if html_response:
107 return redirect(BannedView().as_view())
105 return redirect(BannedView().as_view())
108 else:
106 else:
109 return
107 return
110
108
111 data = form.cleaned_data
109 data = form.cleaned_data
112
110
113 title = data[FORM_TITLE]
111 title = data[FORM_TITLE]
114 text = data[FORM_TEXT]
112 text = data[FORM_TEXT]
115 image = data.get(FORM_IMAGE)
113 image = data.get(FORM_IMAGE)
116
114
117 text = self._remove_invalid_links(text)
115 text = self._remove_invalid_links(text)
118
116
119 tag_strings = data[FORM_TAGS]
117 tag_strings = data[FORM_TAGS]
120
118
121 tags = self.parse_tags_string(tag_strings)
119 tags = self.parse_tags_string(tag_strings)
122
120
123 post = Post.objects.create_post(title=title, text=text, image=image,
121 post = Post.objects.create_post(title=title, text=text, image=image,
124 ip=ip, tags=tags)
122 ip=ip, tags=tags)
125
123
126 # This is required to update the threads to which posts we have replied
124 # This is required to update the threads to which posts we have replied
127 # when creating this one
125 # when creating this one
128 post.send_to_websocket(request)
126 post.send_to_websocket(request)
129
127
130 if html_response:
128 if html_response:
131 return redirect(post.get_url())
129 return redirect(post.get_url())
132
130
133 def get_threads(self):
131 def get_threads(self):
134 """
132 """
135 Gets list of threads that will be shown on a page.
133 Gets list of threads that will be shown on a page.
136 """
134 """
137
135
138 return Thread.objects.all().order_by('-bump_time')\
136 return Thread.objects.all().order_by('-bump_time')\
139 .exclude(tags__in=self.settings_manager.get_hidden_tags())
137 .exclude(tags__in=self.settings_manager.get_hidden_tags())
@@ -1,14 +1,13 b''
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
7 class AuthorsView(BaseBoardView):
7 class AuthorsView(BaseBoardView):
8
8
9 def get(self, request):
9 def get(self, request):
10 context = self.get_context_data(request=request)
10 params = dict()
11 context['authors'] = authors
11 params['authors'] = authors
12
12
13 # TODO Use dict here
13 return render(request, 'boards/authors.html', params)
14 return render(request, 'boards/authors.html', context_instance=context)
@@ -1,19 +1,17 b''
1 from django.shortcuts import get_object_or_404, render
1 from django.shortcuts import get_object_or_404, render
2 from boards import utils
2 from boards import utils
3 from boards.models import Ban
3 from boards.models import Ban
4 from boards.views.base import BaseBoardView
4 from boards.views.base import BaseBoardView
5
5
6
6
7 class BannedView(BaseBoardView):
7 class BannedView(BaseBoardView):
8
8
9 def get(self, request):
9 def get(self, request):
10 """Show the page that notifies that user is banned"""
10 """Show the page that notifies that user is banned"""
11
11
12 context = self.get_context_data(request=request)
12 params = dict()
13
13
14 ban = get_object_or_404(Ban, ip=utils.get_client_ip(request))
14 ban = get_object_or_404(Ban, ip=utils.get_client_ip(request))
15 context['ban_reason'] = ban.reason
15 params['ban_reason'] = ban.reason
16
16
17 # TODO Use dict here
17 return render(request, 'boards/staticpages/banned.html', params)
18 return render(request, 'boards/staticpages/banned.html',
19 context_instance=context)
@@ -1,143 +1,142 b''
1 from django.core.urlresolvers import reverse
1 from django.core.urlresolvers import reverse
2 from django.db import transaction
2 from django.db import transaction
3 from django.http import Http404
3 from django.http import Http404
4 from django.shortcuts import get_object_or_404, render, redirect
4 from django.shortcuts import get_object_or_404, render, redirect
5 from django.views.generic.edit import FormMixin
5 from django.views.generic.edit import FormMixin
6
6
7 from boards import utils, settings
7 from boards import utils, settings
8 from boards.forms import PostForm, PlainErrorList
8 from boards.forms import PostForm, PlainErrorList
9 from boards.models import Post, Ban
9 from boards.models import Post, Ban
10 from boards.views.banned import BannedView
10 from boards.views.banned import BannedView
11 from boards.views.base import BaseBoardView, CONTEXT_FORM
11 from boards.views.base import BaseBoardView, CONTEXT_FORM
12 from boards.views.posting_mixin import PostMixin
12 from boards.views.posting_mixin import PostMixin
13 import neboard
13 import neboard
14
14
15 TEMPLATE_GALLERY = 'boards/thread_gallery.html'
15 TEMPLATE_GALLERY = 'boards/thread_gallery.html'
16 TEMPLATE_NORMAL = 'boards/thread.html'
16 TEMPLATE_NORMAL = 'boards/thread.html'
17
17
18 CONTEXT_POSTS = 'posts'
18 CONTEXT_POSTS = 'posts'
19 CONTEXT_OP = 'opening_post'
19 CONTEXT_OP = 'opening_post'
20 CONTEXT_BUMPLIMIT_PRG = 'bumplimit_progress'
20 CONTEXT_BUMPLIMIT_PRG = 'bumplimit_progress'
21 CONTEXT_POSTS_LEFT = 'posts_left'
21 CONTEXT_POSTS_LEFT = 'posts_left'
22 CONTEXT_LASTUPDATE = "last_update"
22 CONTEXT_LASTUPDATE = "last_update"
23 CONTEXT_MAX_REPLIES = 'max_replies'
23 CONTEXT_MAX_REPLIES = 'max_replies'
24 CONTEXT_THREAD = 'thread'
24 CONTEXT_THREAD = 'thread'
25 CONTEXT_BUMPABLE = 'bumpable'
25 CONTEXT_BUMPABLE = 'bumpable'
26 CONTEXT_WS_TOKEN = 'ws_token'
26 CONTEXT_WS_TOKEN = 'ws_token'
27 CONTEXT_WS_PROJECT = 'ws_project'
27 CONTEXT_WS_PROJECT = 'ws_project'
28 CONTEXT_WS_HOST = 'ws_host'
28 CONTEXT_WS_HOST = 'ws_host'
29 CONTEXT_WS_PORT = 'ws_port'
29 CONTEXT_WS_PORT = 'ws_port'
30
30
31 FORM_TITLE = 'title'
31 FORM_TITLE = 'title'
32 FORM_TEXT = 'text'
32 FORM_TEXT = 'text'
33 FORM_IMAGE = 'image'
33 FORM_IMAGE = 'image'
34
34
35 MODE_GALLERY = 'gallery'
35 MODE_GALLERY = 'gallery'
36 MODE_NORMAL = 'normal'
36 MODE_NORMAL = 'normal'
37
37
38
38
39 class ThreadView(BaseBoardView, PostMixin, FormMixin):
39 class ThreadView(BaseBoardView, PostMixin, FormMixin):
40
40
41 def get(self, request, post_id, mode=MODE_NORMAL, form=None):
41 def get(self, request, post_id, mode=MODE_NORMAL, form=None):
42 try:
42 try:
43 opening_post = Post.objects.filter(id=post_id).only('thread_new')[0]
43 opening_post = Post.objects.filter(id=post_id).only('thread_new')[0]
44 except IndexError:
44 except IndexError:
45 raise Http404
45 raise Http404
46
46
47 # If this is not OP, don't show it as it is
47 # If this is not OP, don't show it as it is
48 if not opening_post or not opening_post.is_opening():
48 if not opening_post or not opening_post.is_opening():
49 raise Http404
49 raise Http404
50
50
51 if not form:
51 if not form:
52 form = PostForm(error_class=PlainErrorList)
52 form = PostForm(error_class=PlainErrorList)
53
53
54 thread_to_show = opening_post.get_thread()
54 thread_to_show = opening_post.get_thread()
55
55
56 context = self.get_context_data(request=request)
56 params = dict()
57
57
58 context[CONTEXT_FORM] = form
58 params[CONTEXT_FORM] = form
59 context[CONTEXT_LASTUPDATE] = str(utils.datetime_to_epoch(
59 params[CONTEXT_LASTUPDATE] = str(utils.datetime_to_epoch(
60 thread_to_show.last_edit_time))
60 thread_to_show.last_edit_time))
61 context[CONTEXT_THREAD] = thread_to_show
61 params[CONTEXT_THREAD] = thread_to_show
62 context[CONTEXT_MAX_REPLIES] = settings.MAX_POSTS_PER_THREAD
62 params[CONTEXT_MAX_REPLIES] = settings.MAX_POSTS_PER_THREAD
63
63
64 if settings.WEBSOCKETS_ENABLED:
64 if settings.WEBSOCKETS_ENABLED:
65 context[CONTEXT_WS_TOKEN] = utils.get_websocket_token(
65 params[CONTEXT_WS_TOKEN] = utils.get_websocket_token(
66 timestamp=context[CONTEXT_LASTUPDATE])
66 timestamp=params[CONTEXT_LASTUPDATE])
67 context[CONTEXT_WS_PROJECT] = neboard.settings.CENTRIFUGE_PROJECT_ID
67 params[CONTEXT_WS_PROJECT] = neboard.settings.CENTRIFUGE_PROJECT_ID
68 context[CONTEXT_WS_HOST] = request.get_host().split(':')[0]
68 params[CONTEXT_WS_HOST] = request.get_host().split(':')[0]
69 context[CONTEXT_WS_PORT] = neboard.settings.CENTRIFUGE_PORT
69 params[CONTEXT_WS_PORT] = neboard.settings.CENTRIFUGE_PORT
70
70
71 # TODO Move this to subclasses: NormalThreadView, GalleryThreadView etc
71 # TODO Move this to subclasses: NormalThreadView, GalleryThreadView etc
72 if MODE_NORMAL == mode:
72 if MODE_NORMAL == mode:
73 bumpable = thread_to_show.can_bump()
73 bumpable = thread_to_show.can_bump()
74 context[CONTEXT_BUMPABLE] = bumpable
74 params[CONTEXT_BUMPABLE] = bumpable
75 if bumpable:
75 if bumpable:
76 left_posts = settings.MAX_POSTS_PER_THREAD \
76 left_posts = settings.MAX_POSTS_PER_THREAD \
77 - thread_to_show.get_reply_count()
77 - thread_to_show.get_reply_count()
78 context[CONTEXT_POSTS_LEFT] = left_posts
78 params[CONTEXT_POSTS_LEFT] = left_posts
79 context[CONTEXT_BUMPLIMIT_PRG] = str(
79 params[CONTEXT_BUMPLIMIT_PRG] = str(
80 float(left_posts) / settings.MAX_POSTS_PER_THREAD * 100)
80 float(left_posts) / settings.MAX_POSTS_PER_THREAD * 100)
81
81
82 context[CONTEXT_OP] = opening_post
82 params[CONTEXT_OP] = opening_post
83
83
84 document = TEMPLATE_NORMAL
84 document = TEMPLATE_NORMAL
85 elif MODE_GALLERY == mode:
85 elif MODE_GALLERY == mode:
86 context[CONTEXT_POSTS] = thread_to_show.get_replies_with_images(
86 params[CONTEXT_POSTS] = thread_to_show.get_replies_with_images(
87 view_fields_only=True)
87 view_fields_only=True)
88
88
89 document = TEMPLATE_GALLERY
89 document = TEMPLATE_GALLERY
90 else:
90 else:
91 raise Http404
91 raise Http404
92
92
93 # TODO Use dict here
93 return render(request, document, params)
94 return render(request, document, context_instance=context)
95
94
96 def post(self, request, post_id, mode=MODE_NORMAL):
95 def post(self, request, post_id, mode=MODE_NORMAL):
97 opening_post = get_object_or_404(Post, id=post_id)
96 opening_post = get_object_or_404(Post, id=post_id)
98
97
99 # If this is not OP, don't show it as it is
98 # If this is not OP, don't show it as it is
100 if not opening_post.is_opening():
99 if not opening_post.is_opening():
101 raise Http404
100 raise Http404
102
101
103 if not opening_post.get_thread().archived:
102 if not opening_post.get_thread().archived:
104 form = PostForm(request.POST, request.FILES,
103 form = PostForm(request.POST, request.FILES,
105 error_class=PlainErrorList)
104 error_class=PlainErrorList)
106 form.session = request.session
105 form.session = request.session
107
106
108 if form.is_valid():
107 if form.is_valid():
109 return self.new_post(request, form, opening_post)
108 return self.new_post(request, form, opening_post)
110 if form.need_to_ban:
109 if form.need_to_ban:
111 # Ban user because he is suspected to be a bot
110 # Ban user because he is suspected to be a bot
112 self._ban_current_user(request)
111 self._ban_current_user(request)
113
112
114 return self.get(request, post_id, mode, form)
113 return self.get(request, post_id, mode, form)
115
114
116 def new_post(self, request, form, opening_post=None, html_response=True):
115 def new_post(self, request, form, opening_post=None, html_response=True):
117 """Add a new post (in thread or as a reply)."""
116 """Add a new post (in thread or as a reply)."""
118
117
119 ip = utils.get_client_ip(request)
118 ip = utils.get_client_ip(request)
120
119
121 data = form.cleaned_data
120 data = form.cleaned_data
122
121
123 title = data[FORM_TITLE]
122 title = data[FORM_TITLE]
124 text = data[FORM_TEXT]
123 text = data[FORM_TEXT]
125 image = data.get(FORM_IMAGE)
124 image = data.get(FORM_IMAGE)
126
125
127 text = self._remove_invalid_links(text)
126 text = self._remove_invalid_links(text)
128
127
129 post_thread = opening_post.get_thread()
128 post_thread = opening_post.get_thread()
130
129
131 post = Post.objects.create_post(title=title, text=text, image=image,
130 post = Post.objects.create_post(title=title, text=text, image=image,
132 thread=post_thread, ip=ip)
131 thread=post_thread, ip=ip)
133 post.send_to_websocket(request)
132 post.send_to_websocket(request)
134
133
135 thread_to_show = (opening_post.id if opening_post else post.id)
134 thread_to_show = (opening_post.id if opening_post else post.id)
136
135
137 if html_response:
136 if html_response:
138 if opening_post:
137 if opening_post:
139 return redirect(
138 return redirect(
140 reverse('thread', kwargs={'post_id': thread_to_show})
139 reverse('thread', kwargs={'post_id': thread_to_show})
141 + '#' + str(post.id))
140 + '#' + str(post.id))
142 else:
141 else:
143 return post
142 return post
General Comments 0
You need to be logged in to leave comments. Login now