##// END OF EJS Templates
Added a todo to the post api method
neko259 -
r546:c72e67b3 1.7-dev
parent child Browse files
Show More
@@ -1,283 +1,284 b''
1 __author__ = 'neko259'
1 __author__ = 'neko259'
2
2
3 import hashlib
3 import hashlib
4
4
5 from django.core import serializers
5 from django.core import serializers
6 from django.core.urlresolvers import reverse
6 from django.core.urlresolvers import reverse
7 from django.http import HttpResponseRedirect
7 from django.http import HttpResponseRedirect
8 from django.http.response import HttpResponse
8 from django.http.response import HttpResponse
9 from django.template import RequestContext
9 from django.template import RequestContext
10 from django.shortcuts import render, redirect, get_object_or_404
10 from django.shortcuts import render, redirect, get_object_or_404
11 from django.utils import timezone
11 from django.utils import timezone
12 from django.db import transaction
12 from django.db import transaction
13 from django.views.decorators.cache import cache_page
13 from django.views.decorators.cache import cache_page
14 from django.views.i18n import javascript_catalog
14 from django.views.i18n import javascript_catalog
15
15
16 import boards
16 import boards
17 from boards.forms import SettingsForm, PlainErrorList, \
17 from boards.forms import SettingsForm, PlainErrorList, \
18 ModeratorSettingsForm
18 ModeratorSettingsForm
19 from boards.models import Post, Tag, Ban, User
19 from boards.models import Post, Tag, Ban, User
20 from boards.models.post import SETTING_MODERATE
20 from boards.models.post import SETTING_MODERATE
21 from boards.models.user import RANK_USER
21 from boards.models.user import RANK_USER
22 from boards import authors
22 from boards import authors
23 import neboard
23 import neboard
24 import all_threads
24 import all_threads
25
25
26
26
27 BAN_REASON_SPAM = 'Autoban: spam bot'
27 BAN_REASON_SPAM = 'Autoban: spam bot'
28
28
29 DEFAULT_PAGE = 1
29 DEFAULT_PAGE = 1
30
30
31
31
32 def settings(request):
32 def settings(request):
33 """User's settings"""
33 """User's settings"""
34
34
35 context = _init_default_context(request)
35 context = _init_default_context(request)
36 user = _get_user(request)
36 user = _get_user(request)
37 is_moderator = user.is_moderator()
37 is_moderator = user.is_moderator()
38
38
39 if request.method == 'POST':
39 if request.method == 'POST':
40 with transaction.atomic():
40 with transaction.atomic():
41 if is_moderator:
41 if is_moderator:
42 form = ModeratorSettingsForm(request.POST,
42 form = ModeratorSettingsForm(request.POST,
43 error_class=PlainErrorList)
43 error_class=PlainErrorList)
44 else:
44 else:
45 form = SettingsForm(request.POST, error_class=PlainErrorList)
45 form = SettingsForm(request.POST, error_class=PlainErrorList)
46
46
47 if form.is_valid():
47 if form.is_valid():
48 selected_theme = form.cleaned_data['theme']
48 selected_theme = form.cleaned_data['theme']
49
49
50 user.save_setting('theme', selected_theme)
50 user.save_setting('theme', selected_theme)
51
51
52 if is_moderator:
52 if is_moderator:
53 moderate = form.cleaned_data['moderate']
53 moderate = form.cleaned_data['moderate']
54 user.save_setting(SETTING_MODERATE, moderate)
54 user.save_setting(SETTING_MODERATE, moderate)
55
55
56 return redirect(settings)
56 return redirect(settings)
57 else:
57 else:
58 selected_theme = _get_theme(request)
58 selected_theme = _get_theme(request)
59
59
60 if is_moderator:
60 if is_moderator:
61 form = ModeratorSettingsForm(initial={'theme': selected_theme,
61 form = ModeratorSettingsForm(initial={'theme': selected_theme,
62 'moderate': context['moderator']},
62 'moderate': context['moderator']},
63 error_class=PlainErrorList)
63 error_class=PlainErrorList)
64 else:
64 else:
65 form = SettingsForm(initial={'theme': selected_theme},
65 form = SettingsForm(initial={'theme': selected_theme},
66 error_class=PlainErrorList)
66 error_class=PlainErrorList)
67
67
68 context['form'] = form
68 context['form'] = form
69
69
70 return render(request, 'boards/settings.html', context)
70 return render(request, 'boards/settings.html', context)
71
71
72
72
73 def all_tags(request):
73 def all_tags(request):
74 """All tags list"""
74 """All tags list"""
75
75
76 context = _init_default_context(request)
76 context = _init_default_context(request)
77 context['all_tags'] = Tag.objects.get_not_empty_tags()
77 context['all_tags'] = Tag.objects.get_not_empty_tags()
78
78
79 return render(request, 'boards/tags.html', context)
79 return render(request, 'boards/tags.html', context)
80
80
81
81
82 def jump_to_post(request, post_id):
82 def jump_to_post(request, post_id):
83 """Determine thread in which the requested post is and open it's page"""
83 """Determine thread in which the requested post is and open it's page"""
84
84
85 post = get_object_or_404(Post, id=post_id)
85 post = get_object_or_404(Post, id=post_id)
86
86
87 if not post.thread:
87 if not post.thread:
88 return redirect('thread', post_id=post.id)
88 return redirect('thread', post_id=post.id)
89 else:
89 else:
90 return redirect(reverse('thread', kwargs={'post_id': post.thread.id})
90 return redirect(reverse('thread', kwargs={'post_id': post.thread.id})
91 + '#' + str(post.id))
91 + '#' + str(post.id))
92
92
93
93
94 def authors(request):
94 def authors(request):
95 """Show authors list"""
95 """Show authors list"""
96
96
97 context = _init_default_context(request)
97 context = _init_default_context(request)
98 context['authors'] = boards.authors.authors
98 context['authors'] = boards.authors.authors
99
99
100 return render(request, 'boards/authors.html', context)
100 return render(request, 'boards/authors.html', context)
101
101
102
102
103 @transaction.atomic
103 @transaction.atomic
104 def delete(request, post_id):
104 def delete(request, post_id):
105 """Delete post"""
105 """Delete post"""
106
106
107 user = _get_user(request)
107 user = _get_user(request)
108 post = get_object_or_404(Post, id=post_id)
108 post = get_object_or_404(Post, id=post_id)
109
109
110 if user.is_moderator():
110 if user.is_moderator():
111 # TODO Show confirmation page before deletion
111 # TODO Show confirmation page before deletion
112 Post.objects.delete_post(post)
112 Post.objects.delete_post(post)
113
113
114 if not post.thread:
114 if not post.thread:
115 return _redirect_to_next(request)
115 return _redirect_to_next(request)
116 else:
116 else:
117 return redirect('thread', post_id=post.thread.id)
117 return redirect('thread', post_id=post.thread.id)
118
118
119
119
120 @transaction.atomic
120 @transaction.atomic
121 def ban(request, post_id):
121 def ban(request, post_id):
122 """Ban user"""
122 """Ban user"""
123
123
124 user = _get_user(request)
124 user = _get_user(request)
125 post = get_object_or_404(Post, id=post_id)
125 post = get_object_or_404(Post, id=post_id)
126
126
127 if user.is_moderator():
127 if user.is_moderator():
128 # TODO Show confirmation page before ban
128 # TODO Show confirmation page before ban
129 ban, created = Ban.objects.get_or_create(ip=post.poster_ip)
129 ban, created = Ban.objects.get_or_create(ip=post.poster_ip)
130 if created:
130 if created:
131 ban.reason = 'Banned for post ' + str(post_id)
131 ban.reason = 'Banned for post ' + str(post_id)
132 ban.save()
132 ban.save()
133
133
134 return _redirect_to_next(request)
134 return _redirect_to_next(request)
135
135
136
136
137 def page_404(request):
137 def page_404(request):
138 """Show page 404 (not found error)"""
138 """Show page 404 (not found error)"""
139
139
140 context = _init_default_context(request)
140 context = _init_default_context(request)
141 return render(request, 'boards/404.html', context)
141 return render(request, 'boards/404.html', context)
142
142
143
143
144 @transaction.atomic
144 @transaction.atomic
145 def tag_subscribe(request, tag_name):
145 def tag_subscribe(request, tag_name):
146 """Add tag to favorites"""
146 """Add tag to favorites"""
147
147
148 user = _get_user(request)
148 user = _get_user(request)
149 tag = get_object_or_404(Tag, name=tag_name)
149 tag = get_object_or_404(Tag, name=tag_name)
150
150
151 if not tag in user.fav_tags.all():
151 if not tag in user.fav_tags.all():
152 user.add_tag(tag)
152 user.add_tag(tag)
153
153
154 return _redirect_to_next(request)
154 return _redirect_to_next(request)
155
155
156
156
157 @transaction.atomic
157 @transaction.atomic
158 def tag_unsubscribe(request, tag_name):
158 def tag_unsubscribe(request, tag_name):
159 """Remove tag from favorites"""
159 """Remove tag from favorites"""
160
160
161 user = _get_user(request)
161 user = _get_user(request)
162 tag = get_object_or_404(Tag, name=tag_name)
162 tag = get_object_or_404(Tag, name=tag_name)
163
163
164 if tag in user.fav_tags.all():
164 if tag in user.fav_tags.all():
165 user.remove_tag(tag)
165 user.remove_tag(tag)
166
166
167 return _redirect_to_next(request)
167 return _redirect_to_next(request)
168
168
169
169
170 def static_page(request, name):
170 def static_page(request, name):
171 """Show a static page that needs only tags list and a CSS"""
171 """Show a static page that needs only tags list and a CSS"""
172
172
173 context = _init_default_context(request)
173 context = _init_default_context(request)
174 return render(request, 'boards/staticpages/' + name + '.html', context)
174 return render(request, 'boards/staticpages/' + name + '.html', context)
175
175
176
176
177 # TODO This has to be moved under the api module
177 def api_get_post(request, post_id):
178 def api_get_post(request, post_id):
178 """
179 """
179 Get the JSON of a post. This can be
180 Get the JSON of a post. This can be
180 used as and API for external clients.
181 used as and API for external clients.
181 """
182 """
182
183
183 post = get_object_or_404(Post, id=post_id)
184 post = get_object_or_404(Post, id=post_id)
184
185
185 json = serializers.serialize("json", [post], fields=(
186 json = serializers.serialize("json", [post], fields=(
186 "pub_time", "_text_rendered", "title", "text", "image",
187 "pub_time", "_text_rendered", "title", "text", "image",
187 "image_width", "image_height", "replies", "tags"
188 "image_width", "image_height", "replies", "tags"
188 ))
189 ))
189
190
190 return HttpResponse(content=json)
191 return HttpResponse(content=json)
191
192
192
193
193 @cache_page(86400)
194 @cache_page(86400)
194 def cached_js_catalog(request, domain='djangojs', packages=None):
195 def cached_js_catalog(request, domain='djangojs', packages=None):
195 return javascript_catalog(request, domain, packages)
196 return javascript_catalog(request, domain, packages)
196
197
197
198
198 # TODO This method is deprecated and should be removed after switching to
199 # TODO This method is deprecated and should be removed after switching to
199 # class-based view
200 # class-based view
200 def _get_theme(request, user=None):
201 def _get_theme(request, user=None):
201 """Get user's CSS theme"""
202 """Get user's CSS theme"""
202
203
203 if not user:
204 if not user:
204 user = _get_user(request)
205 user = _get_user(request)
205 theme = user.get_setting('theme')
206 theme = user.get_setting('theme')
206 if not theme:
207 if not theme:
207 theme = neboard.settings.DEFAULT_THEME
208 theme = neboard.settings.DEFAULT_THEME
208
209
209 return theme
210 return theme
210
211
211
212
212 # TODO This method is deprecated and should be removed after switching to
213 # TODO This method is deprecated and should be removed after switching to
213 # class-based view
214 # class-based view
214 def _init_default_context(request):
215 def _init_default_context(request):
215 """Create context with default values that are used in most views"""
216 """Create context with default values that are used in most views"""
216
217
217 context = RequestContext(request)
218 context = RequestContext(request)
218
219
219 user = _get_user(request)
220 user = _get_user(request)
220 context['user'] = user
221 context['user'] = user
221 context['tags'] = user.get_sorted_fav_tags()
222 context['tags'] = user.get_sorted_fav_tags()
222 context['posts_per_day'] = float(Post.objects.get_posts_per_day())
223 context['posts_per_day'] = float(Post.objects.get_posts_per_day())
223
224
224 theme = _get_theme(request, user)
225 theme = _get_theme(request, user)
225 context['theme'] = theme
226 context['theme'] = theme
226 context['theme_css'] = 'css/' + theme + '/base_page.css'
227 context['theme_css'] = 'css/' + theme + '/base_page.css'
227
228
228 # This shows the moderator panel
229 # This shows the moderator panel
229 moderate = user.get_setting(SETTING_MODERATE)
230 moderate = user.get_setting(SETTING_MODERATE)
230 if moderate == 'True':
231 if moderate == 'True':
231 context['moderator'] = user.is_moderator()
232 context['moderator'] = user.is_moderator()
232 else:
233 else:
233 context['moderator'] = False
234 context['moderator'] = False
234
235
235 return context
236 return context
236
237
237
238
238 # TODO This method is deprecated and should be removed after switching to
239 # TODO This method is deprecated and should be removed after switching to
239 # class-based view
240 # class-based view
240 def _get_user(request):
241 def _get_user(request):
241 """
242 """
242 Get current user from the session. If the user does not exist, create
243 Get current user from the session. If the user does not exist, create
243 a new one.
244 a new one.
244 """
245 """
245
246
246 session = request.session
247 session = request.session
247 if not 'user_id' in session:
248 if not 'user_id' in session:
248 request.session.save()
249 request.session.save()
249
250
250 md5 = hashlib.md5()
251 md5 = hashlib.md5()
251 md5.update(session.session_key)
252 md5.update(session.session_key)
252 new_id = md5.hexdigest()
253 new_id = md5.hexdigest()
253
254
254 while User.objects.filter(user_id=new_id).exists():
255 while User.objects.filter(user_id=new_id).exists():
255 md5.update(str(timezone.now()))
256 md5.update(str(timezone.now()))
256 new_id = md5.hexdigest()
257 new_id = md5.hexdigest()
257
258
258 time_now = timezone.now()
259 time_now = timezone.now()
259 user = User.objects.create(user_id=new_id, rank=RANK_USER,
260 user = User.objects.create(user_id=new_id, rank=RANK_USER,
260 registration_time=time_now)
261 registration_time=time_now)
261
262
262 # TODO This is just a test. This method should be removed
263 # TODO This is just a test. This method should be removed
263 # _delete_old_users()
264 # _delete_old_users()
264
265
265 session['user_id'] = user.id
266 session['user_id'] = user.id
266 else:
267 else:
267 user = User.objects.get(id=session['user_id'])
268 user = User.objects.get(id=session['user_id'])
268
269
269 return user
270 return user
270
271
271
272
272 def _redirect_to_next(request):
273 def _redirect_to_next(request):
273 """
274 """
274 If a 'next' parameter was specified, redirect to the next page. This is
275 If a 'next' parameter was specified, redirect to the next page. This is
275 used when the user is required to return to some page after the current
276 used when the user is required to return to some page after the current
276 view has finished its work.
277 view has finished its work.
277 """
278 """
278
279
279 if 'next' in request.GET:
280 if 'next' in request.GET:
280 next_page = request.GET['next']
281 next_page = request.GET['next']
281 return HttpResponseRedirect(next_page)
282 return HttpResponseRedirect(next_page)
282 else:
283 else:
283 return redirect('index') No newline at end of file
284 return redirect('index')
General Comments 0
You need to be logged in to leave comments. Login now