##// END OF EJS Templates
Removed user last access time update to speed up page load.
neko259 -
r196:005f6ba5 default
parent child Browse files
Show More
@@ -1,356 +1,358 b''
1 import hashlib
1 import hashlib
2 from django.core.urlresolvers import reverse
2 from django.core.urlresolvers import reverse
3 from django.http import HttpResponseRedirect
3 from django.http import HttpResponseRedirect
4 from django.template import RequestContext
4 from django.template import RequestContext
5 from django.shortcuts import render, redirect, get_object_or_404
5 from django.shortcuts import render, redirect, get_object_or_404
6 from django.utils import timezone
6 from django.utils import timezone
7
7
8 from boards import forms
8 from boards import forms
9 import boards
9 import boards
10 from boards import utils
10 from boards import utils
11 from boards.forms import ThreadForm, PostForm, SettingsForm, PlainErrorList, \
11 from boards.forms import ThreadForm, PostForm, SettingsForm, PlainErrorList, \
12 ThreadCaptchaForm, PostCaptchaForm, LoginForm
12 ThreadCaptchaForm, PostCaptchaForm, LoginForm
13
13
14 from boards.models import Post, Tag, Ban, User, RANK_USER, NO_PARENT
14 from boards.models import Post, Tag, Ban, User, RANK_USER, NO_PARENT
15 from boards import authors
15 from boards import authors
16 import neboard
16 import neboard
17
17
18
18
19 def index(request, page=0):
19 def index(request, page=0):
20 context = _init_default_context(request)
20 context = _init_default_context(request)
21
21
22 if utils.need_include_captcha(request):
22 if utils.need_include_captcha(request):
23 threadFormClass = ThreadCaptchaForm
23 threadFormClass = ThreadCaptchaForm
24 kwargs = {'request': request}
24 kwargs = {'request': request}
25 else:
25 else:
26 threadFormClass = ThreadForm
26 threadFormClass = ThreadForm
27 kwargs = {}
27 kwargs = {}
28
28
29 if request.method == 'POST':
29 if request.method == 'POST':
30 form = threadFormClass(request.POST, request.FILES,
30 form = threadFormClass(request.POST, request.FILES,
31 error_class=PlainErrorList, **kwargs)
31 error_class=PlainErrorList, **kwargs)
32 form.session = request.session
32 form.session = request.session
33
33
34 if form.is_valid():
34 if form.is_valid():
35 return _new_post(request, form)
35 return _new_post(request, form)
36 else:
36 else:
37 form = threadFormClass(error_class=PlainErrorList, **kwargs)
37 form = threadFormClass(error_class=PlainErrorList, **kwargs)
38
38
39 threads = []
39 threads = []
40 for thread in Post.objects.get_threads(page=int(page)):
40 for thread in Post.objects.get_threads(page=int(page)):
41 threads.append({'thread': thread,
41 threads.append({'thread': thread,
42 'bumpable': thread.can_bump()})
42 'bumpable': thread.can_bump()})
43
43
44 context['threads'] = None if len(threads) == 0 else threads
44 context['threads'] = None if len(threads) == 0 else threads
45 context['form'] = form
45 context['form'] = form
46 context['pages'] = range(Post.objects.get_thread_page_count())
46 context['pages'] = range(Post.objects.get_thread_page_count())
47
47
48 return render(request, 'boards/posting_general.html',
48 return render(request, 'boards/posting_general.html',
49 context)
49 context)
50
50
51
51
52 def _new_post(request, form, thread_id=boards.models.NO_PARENT):
52 def _new_post(request, form, thread_id=boards.models.NO_PARENT):
53 """Add a new post (in thread or as a reply)."""
53 """Add a new post (in thread or as a reply)."""
54
54
55 ip = _get_client_ip(request)
55 ip = _get_client_ip(request)
56 is_banned = Ban.objects.filter(ip=ip).count() > 0
56 is_banned = Ban.objects.filter(ip=ip).count() > 0
57
57
58 if is_banned:
58 if is_banned:
59 return redirect(you_are_banned)
59 return redirect(you_are_banned)
60
60
61 data = form.cleaned_data
61 data = form.cleaned_data
62
62
63 title = data['title']
63 title = data['title']
64 text = data['text']
64 text = data['text']
65
65
66 if 'image' in data.keys():
66 if 'image' in data.keys():
67 image = data['image']
67 image = data['image']
68 else:
68 else:
69 image = None
69 image = None
70
70
71 tags = []
71 tags = []
72
72
73 new_thread = thread_id == boards.models.NO_PARENT
73 new_thread = thread_id == boards.models.NO_PARENT
74 if new_thread:
74 if new_thread:
75 tag_strings = data['tags']
75 tag_strings = data['tags']
76
76
77 if tag_strings:
77 if tag_strings:
78 tag_strings = tag_strings.split(' ')
78 tag_strings = tag_strings.split(' ')
79 for tag_name in tag_strings:
79 for tag_name in tag_strings:
80 tag_name = tag_name.strip()
80 tag_name = tag_name.strip()
81 if len(tag_name) > 0:
81 if len(tag_name) > 0:
82 tag, created = Tag.objects.get_or_create(name=tag_name)
82 tag, created = Tag.objects.get_or_create(name=tag_name)
83 tags.append(tag)
83 tags.append(tag)
84
84
85 op = None if thread_id == boards.models.NO_PARENT else \
85 op = None if thread_id == boards.models.NO_PARENT else \
86 get_object_or_404(Post, id=thread_id)
86 get_object_or_404(Post, id=thread_id)
87 post = Post.objects.create_post(title=title, text=text, ip=ip,
87 post = Post.objects.create_post(title=title, text=text, ip=ip,
88 thread=op, image=image,
88 thread=op, image=image,
89 tags=tags, user=_get_user(request))
89 tags=tags, user=_get_user(request))
90
90
91 thread_to_show = (post.id if new_thread else thread_id)
91 thread_to_show = (post.id if new_thread else thread_id)
92
92
93 if new_thread:
93 if new_thread:
94 return redirect(thread, post_id=thread_to_show)
94 return redirect(thread, post_id=thread_to_show)
95 else:
95 else:
96 return redirect(reverse(thread, kwargs={'post_id': thread_to_show}) +
96 return redirect(reverse(thread, kwargs={'post_id': thread_to_show}) +
97 '#' + str(post.id))
97 '#' + str(post.id))
98
98
99
99
100 def tag(request, tag_name, page=0):
100 def tag(request, tag_name, page=0):
101 """Get all tag threads (posts without a parent)."""
101 """Get all tag threads (posts without a parent)."""
102
102
103 tag = get_object_or_404(Tag, name=tag_name)
103 tag = get_object_or_404(Tag, name=tag_name)
104 threads = []
104 threads = []
105 for thread in Post.objects.get_threads(tag=tag, page=int(page)):
105 for thread in Post.objects.get_threads(tag=tag, page=int(page)):
106 threads.append({'thread': thread,
106 threads.append({'thread': thread,
107 'bumpable': thread.can_bump()})
107 'bumpable': thread.can_bump()})
108
108
109 if request.method == 'POST':
109 if request.method == 'POST':
110 form = ThreadForm(request.POST, request.FILES,
110 form = ThreadForm(request.POST, request.FILES,
111 error_class=PlainErrorList)
111 error_class=PlainErrorList)
112 if form.is_valid():
112 if form.is_valid():
113 return _new_post(request, form)
113 return _new_post(request, form)
114 else:
114 else:
115 form = forms.ThreadForm(initial={'tags': tag_name},
115 form = forms.ThreadForm(initial={'tags': tag_name},
116 error_class=PlainErrorList)
116 error_class=PlainErrorList)
117
117
118 context = _init_default_context(request)
118 context = _init_default_context(request)
119 context['threads'] = None if len(threads) == 0 else threads
119 context['threads'] = None if len(threads) == 0 else threads
120 context['tag'] = tag
120 context['tag'] = tag
121 context['pages'] = range(Post.objects.get_thread_page_count(tag=tag))
121 context['pages'] = range(Post.objects.get_thread_page_count(tag=tag))
122
122
123 context['form'] = form
123 context['form'] = form
124
124
125 return render(request, 'boards/posting_general.html',
125 return render(request, 'boards/posting_general.html',
126 context)
126 context)
127
127
128
128
129 def thread(request, post_id):
129 def thread(request, post_id):
130 """Get all thread posts"""
130 """Get all thread posts"""
131
131
132 if utils.need_include_captcha(request):
132 if utils.need_include_captcha(request):
133 postFormClass = PostCaptchaForm
133 postFormClass = PostCaptchaForm
134 kwargs = {'request': request}
134 kwargs = {'request': request}
135 else:
135 else:
136 postFormClass = PostForm
136 postFormClass = PostForm
137 kwargs = {}
137 kwargs = {}
138
138
139 if request.method == 'POST':
139 if request.method == 'POST':
140 form = postFormClass(request.POST, request.FILES,
140 form = postFormClass(request.POST, request.FILES,
141 error_class=PlainErrorList, **kwargs)
141 error_class=PlainErrorList, **kwargs)
142 form.session = request.session
142 form.session = request.session
143
143
144 if form.is_valid():
144 if form.is_valid():
145 return _new_post(request, form, post_id)
145 return _new_post(request, form, post_id)
146 else:
146 else:
147 form = postFormClass(error_class=PlainErrorList, **kwargs)
147 form = postFormClass(error_class=PlainErrorList, **kwargs)
148
148
149 posts = Post.objects.get_thread(post_id)
149 posts = Post.objects.get_thread(post_id)
150
150
151 context = _init_default_context(request)
151 context = _init_default_context(request)
152
152
153 context['posts'] = posts
153 context['posts'] = posts
154 context['form'] = form
154 context['form'] = form
155 context['bumpable'] = posts[0].can_bump()
155 context['bumpable'] = posts[0].can_bump()
156
156
157 return render(request, 'boards/thread.html', context)
157 return render(request, 'boards/thread.html', context)
158
158
159
159
160 def login(request):
160 def login(request):
161 """Log in with user id"""
161 """Log in with user id"""
162
162
163 context = _init_default_context(request)
163 context = _init_default_context(request)
164
164
165 if request.method == 'POST':
165 if request.method == 'POST':
166 form = LoginForm(request.POST, request.FILES,
166 form = LoginForm(request.POST, request.FILES,
167 error_class=PlainErrorList)
167 error_class=PlainErrorList)
168 if form.is_valid():
168 if form.is_valid():
169 user = User.objects.get(user_id=form.cleaned_data['user_id'])
169 user = User.objects.get(user_id=form.cleaned_data['user_id'])
170 request.session['user_id'] = user.id
170 request.session['user_id'] = user.id
171 return redirect(index)
171 return redirect(index)
172
172
173 else:
173 else:
174 form = LoginForm()
174 form = LoginForm()
175
175
176 context['form'] = form
176 context['form'] = form
177
177
178 return render(request, 'boards/login.html', context)
178 return render(request, 'boards/login.html', context)
179
179
180
180
181 def settings(request):
181 def settings(request):
182 """User's settings"""
182 """User's settings"""
183
183
184 context = _init_default_context(request)
184 context = _init_default_context(request)
185
185
186 if request.method == 'POST':
186 if request.method == 'POST':
187 form = SettingsForm(request.POST)
187 form = SettingsForm(request.POST)
188 if form.is_valid():
188 if form.is_valid():
189 selected_theme = form.cleaned_data['theme']
189 selected_theme = form.cleaned_data['theme']
190
190
191 user = _get_user(request)
191 user = _get_user(request)
192 user.save_setting('theme', selected_theme)
192 user.save_setting('theme', selected_theme)
193
193
194 return redirect(settings)
194 return redirect(settings)
195 else:
195 else:
196 selected_theme = _get_theme(request)
196 selected_theme = _get_theme(request)
197 form = SettingsForm(initial={'theme': selected_theme})
197 form = SettingsForm(initial={'theme': selected_theme})
198 context['form'] = form
198 context['form'] = form
199
199
200 return render(request, 'boards/settings.html', context)
200 return render(request, 'boards/settings.html', context)
201
201
202
202
203 def all_tags(request):
203 def all_tags(request):
204 """All tags list"""
204 """All tags list"""
205
205
206 context = _init_default_context(request)
206 context = _init_default_context(request)
207 context['all_tags'] = Tag.objects.get_not_empty_tags()
207 context['all_tags'] = Tag.objects.get_not_empty_tags()
208
208
209 return render(request, 'boards/tags.html', context)
209 return render(request, 'boards/tags.html', context)
210
210
211
211
212 def jump_to_post(request, post_id):
212 def jump_to_post(request, post_id):
213 """Determine thread in which the requested post is and open it's page"""
213 """Determine thread in which the requested post is and open it's page"""
214
214
215 post = get_object_or_404(Post, id=post_id)
215 post = get_object_or_404(Post, id=post_id)
216
216
217 if not post.thread:
217 if not post.thread:
218 return redirect(thread, post_id=post.id)
218 return redirect(thread, post_id=post.id)
219 else:
219 else:
220 return redirect(reverse(thread, kwargs={'post_id': post.thread.id})
220 return redirect(reverse(thread, kwargs={'post_id': post.thread.id})
221 + '#' + str(post.id))
221 + '#' + str(post.id))
222
222
223
223
224 def authors(request):
224 def authors(request):
225 context = _init_default_context(request)
225 context = _init_default_context(request)
226 context['authors'] = boards.authors.authors
226 context['authors'] = boards.authors.authors
227
227
228 return render(request, 'boards/authors.html', context)
228 return render(request, 'boards/authors.html', context)
229
229
230
230
231 def delete(request, post_id):
231 def delete(request, post_id):
232 user = _get_user(request)
232 user = _get_user(request)
233 post = get_object_or_404(Post, id=post_id)
233 post = get_object_or_404(Post, id=post_id)
234
234
235 if user.is_moderator():
235 if user.is_moderator():
236 # TODO Show confirmation page before deletion
236 # TODO Show confirmation page before deletion
237 Post.objects.delete_post(post)
237 Post.objects.delete_post(post)
238
238
239 if not post.thread:
239 if not post.thread:
240 return _redirect_to_next(request)
240 return _redirect_to_next(request)
241 else:
241 else:
242 return redirect(thread, post_id=post.thread.id)
242 return redirect(thread, post_id=post.thread.id)
243
243
244
244
245 def ban(request, post_id):
245 def ban(request, post_id):
246 user = _get_user(request)
246 user = _get_user(request)
247 post = get_object_or_404(Post, id=post_id)
247 post = get_object_or_404(Post, id=post_id)
248
248
249 if user.is_moderator():
249 if user.is_moderator():
250 # TODO Show confirmation page before ban
250 # TODO Show confirmation page before ban
251 Ban.objects.get_or_create(ip=post.poster_ip)
251 Ban.objects.get_or_create(ip=post.poster_ip)
252
252
253 return _redirect_to_next(request)
253 return _redirect_to_next(request)
254
254
255
255
256 def you_are_banned(request):
256 def you_are_banned(request):
257 context = _init_default_context(request)
257 context = _init_default_context(request)
258 return render(request, 'boards/staticpages/banned.html', context)
258 return render(request, 'boards/staticpages/banned.html', context)
259
259
260
260
261 def page_404(request):
261 def page_404(request):
262 context = _init_default_context(request)
262 context = _init_default_context(request)
263 return render(request, 'boards/404.html', context)
263 return render(request, 'boards/404.html', context)
264
264
265
265
266 def tag_subscribe(request, tag_name):
266 def tag_subscribe(request, tag_name):
267 user = _get_user(request)
267 user = _get_user(request)
268 tag = get_object_or_404(Tag, name=tag_name)
268 tag = get_object_or_404(Tag, name=tag_name)
269
269
270 if not tag in user.fav_tags.all():
270 if not tag in user.fav_tags.all():
271 user.fav_tags.add(tag)
271 user.fav_tags.add(tag)
272
272
273 return _redirect_to_next(request)
273 return _redirect_to_next(request)
274
274
275
275
276 def tag_unsubscribe(request, tag_name):
276 def tag_unsubscribe(request, tag_name):
277 user = _get_user(request)
277 user = _get_user(request)
278 tag = get_object_or_404(Tag, name=tag_name)
278 tag = get_object_or_404(Tag, name=tag_name)
279
279
280 if tag in user.fav_tags.all():
280 if tag in user.fav_tags.all():
281 user.fav_tags.remove(tag)
281 user.fav_tags.remove(tag)
282
282
283 return _redirect_to_next(request)
283 return _redirect_to_next(request)
284
284
285
285
286 def static_page(request, name):
286 def static_page(request, name):
287 context = _init_default_context(request)
287 context = _init_default_context(request)
288 return render(request, 'boards/staticpages/' + name + '.html', context)
288 return render(request, 'boards/staticpages/' + name + '.html', context)
289
289
290
290
291 def _get_theme(request, user=None):
291 def _get_theme(request, user=None):
292 """Get user's CSS theme"""
292 """Get user's CSS theme"""
293
293
294 if not user:
294 if not user:
295 user = _get_user(request)
295 user = _get_user(request)
296 theme = user.get_setting('theme')
296 theme = user.get_setting('theme')
297 if not theme:
297 if not theme:
298 theme = neboard.settings.DEFAULT_THEME
298 theme = neboard.settings.DEFAULT_THEME
299
299
300 return theme
300 return theme
301
301
302
302
303 def _get_client_ip(request):
303 def _get_client_ip(request):
304 x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
304 x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
305 if x_forwarded_for:
305 if x_forwarded_for:
306 ip = x_forwarded_for.split(',')[-1].strip()
306 ip = x_forwarded_for.split(',')[-1].strip()
307 else:
307 else:
308 ip = request.META.get('REMOTE_ADDR')
308 ip = request.META.get('REMOTE_ADDR')
309 return ip
309 return ip
310
310
311
311
312 def _init_default_context(request):
312 def _init_default_context(request):
313 """Create context with default values that are used in most views"""
313 """Create context with default values that are used in most views"""
314
314
315 context = RequestContext(request)
315 context = RequestContext(request)
316
316
317 user = _get_user(request)
317 user = _get_user(request)
318 context['user'] = user
318 context['user'] = user
319 context['tags'] = user.get_sorted_fav_tags()
319 context['tags'] = user.get_sorted_fav_tags()
320 context['theme'] = _get_theme(request, user)
320 context['theme'] = _get_theme(request, user)
321 context['moderator'] = user.is_moderator()
321 context['moderator'] = user.is_moderator()
322
322
323 return context
323 return context
324
324
325
325
326 def _get_user(request):
326 def _get_user(request):
327 """Get current user from the session"""
327 """Get current user from the session"""
328
328
329 session = request.session
329 session = request.session
330 if not 'user_id' in session:
330 if not 'user_id' in session:
331 request.session.save()
331 request.session.save()
332
332
333 md5 = hashlib.md5()
333 md5 = hashlib.md5()
334 md5.update(session.session_key)
334 md5.update(session.session_key)
335 new_id = md5.hexdigest()
335 new_id = md5.hexdigest()
336
336
337 time_now = timezone.now()
337 time_now = timezone.now()
338 user = User.objects.create(user_id=new_id, rank=RANK_USER,
338 user = User.objects.create(user_id=new_id, rank=RANK_USER,
339 registration_time=time_now,
339 registration_time=time_now,
340 last_access_time=time_now)
340 last_access_time=time_now)
341
341
342 session['user_id'] = user.id
342 session['user_id'] = user.id
343 else:
343 else:
344 user = User.objects.get(id=session['user_id'])
344 user = User.objects.get(id=session['user_id'])
345 user.last_access_time = timezone.now()
345
346 user.save()
346 # TODO We need to determine if we need this and increas the performance
347 #user.last_access_time = timezone.now()
348 #user.save()
347
349
348 return user
350 return user
349
351
350
352
351 def _redirect_to_next(request):
353 def _redirect_to_next(request):
352 if 'next' in request.GET:
354 if 'next' in request.GET:
353 next_page = request.GET['next']
355 next_page = request.GET['next']
354 return HttpResponseRedirect(next_page)
356 return HttpResponseRedirect(next_page)
355 else:
357 else:
356 return redirect(index)
358 return redirect(index)
General Comments 0
You need to be logged in to leave comments. Login now