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