##// END OF EJS Templates
Refactored code. Added thread title if the opening post has no title.
neko259 -
r147:b7ff6a14 default
parent child Browse files
Show More
@@ -57,14 +57,13 b' class PostManager(models.Manager):'
57
57
58 def delete_post(self, post):
58 def delete_post(self, post):
59 children = self.filter(parent=post.id)
59 children = self.filter(parent=post.id)
60 for child in children:
60
61 self.delete_post(child)
61 map(self.delete_post, children)
62 post.delete()
62 post.delete()
63
63
64 def delete_posts_by_ip(self, ip):
64 def delete_posts_by_ip(self, ip):
65 posts = self.filter(poster_ip=ip)
65 posts = self.filter(poster_ip=ip)
66 for post in posts:
66 map(self.delete_post, posts)
67 self.delete_post(post)
68
67
69 def get_threads(self, tag=None, page=ALL_PAGES,
68 def get_threads(self, tag=None, page=ALL_PAGES,
70 order_by='-last_edit_time'):
69 order_by='-last_edit_time'):
@@ -133,8 +132,7 b' class PostManager(models.Manager):'
133 num_threads_to_delete = thread_count - settings.MAX_THREAD_COUNT
132 num_threads_to_delete = thread_count - settings.MAX_THREAD_COUNT
134 old_threads = threads[thread_count - num_threads_to_delete:]
133 old_threads = threads[thread_count - num_threads_to_delete:]
135
134
136 for thread in old_threads:
135 map(self.delete_post, old_threads)
137 self.delete_post(thread)
138
136
139 def _bump_thread(self, thread_id):
137 def _bump_thread(self, thread_id):
140 thread = self.get(id=thread_id)
138 thread = self.get(id=thread_id)
@@ -172,8 +170,6 b' class Tag(models.Model):'
172 objects = TagManager()
170 objects = TagManager()
173
171
174 name = models.CharField(max_length=100)
172 name = models.CharField(max_length=100)
175 # TODO Connect the tag to its posts to check the number of threads for
176 # the tag.
177
173
178 def __unicode__(self):
174 def __unicode__(self):
179 return self.name
175 return self.name
@@ -235,6 +231,13 b' class Post(models.Model):'
235 return '#' + str(self.id) + ' ' + self.title + ' (' + \
231 return '#' + str(self.id) + ' ' + self.title + ' (' + \
236 self.text.raw[:50] + ')'
232 self.text.raw[:50] + ')'
237
233
234 def get_title(self):
235 title = self.title
236 if len(title) == 0:
237 title = self.text.raw[:20]
238
239 return title
240
238 def _get_replies(self):
241 def _get_replies(self):
239 return Post.objects.filter(parent=self.id)
242 return Post.objects.filter(parent=self.id)
240
243
@@ -249,14 +252,6 b' class Post(models.Model):'
249
252
250 return images_count
253 return images_count
251
254
252 def get_gets_count(self):
253 gets_count = 1 if self.is_get() else 0
254 for reply in self._get_replies():
255 if reply.is_get():
256 gets_count += 1
257
258 return gets_count
259
260 def can_bump(self):
255 def can_bump(self):
261 """Check if the thread can be bumped by replying"""
256 """Check if the thread can be bumped by replying"""
262
257
@@ -314,7 +309,7 b' class User(models.Model):'
314 return RANK_MODERATOR >= self.rank
309 return RANK_MODERATOR >= self.rank
315
310
316 def __unicode__(self):
311 def __unicode__(self):
317 return self.user_id
312 return self.user_id + '(' + self.rank + ')'
318
313
319
314
320 class Setting(models.Model):
315 class Setting(models.Model):
@@ -28,10 +28,11 b''
28 <div class="navigation_panel">
28 <div class="navigation_panel">
29 <a class="link" href="{% url 'index' %}">{% trans "All threads" %}</a>
29 <a class="link" href="{% url 'index' %}">{% trans "All threads" %}</a>
30 {% for tag in tags %}
30 {% for tag in tags %}
31 <a class="tag" href="{% url 'tag' tag_name=tag.name %}">
31 <a class="tag" href="{% url 'tag' tag_name=tag.name %}"
32 {{ tag.name }}</a>
32 >{{ tag.name }}</a>
33 {% endfor %}
33 {% endfor %}
34 <a class="tag" href="{% url 'tags' %}">[...]</a>
34 <a class="tag" href="{% url 'tags' %}" alt="{% trans 'Tag management' %}"
35 >[...]</a>
35 <a class="link" href="{% url 'settings' %}">{% trans 'Settings' %}</a>
36 <a class="link" href="{% url 'settings' %}">{% trans 'Settings' %}</a>
36 </div>
37 </div>
37
38
@@ -160,7 +160,7 b''
160 {% block metapanel %}
160 {% block metapanel %}
161
161
162 <span class="metapanel">
162 <span class="metapanel">
163 <b><a href="{% url "authors" %}">Neboard</a> pre1.0</b>
163 <b><a href="{% url "authors" %}">Neboard</a> 1.1</b>
164 {% trans "Pages:" %}
164 {% trans "Pages:" %}
165 {% for page in pages %}
165 {% for page in pages %}
166 [<a href="
166 [<a href="
@@ -4,11 +4,11 b''
4 {% load markup %}
4 {% load markup %}
5
5
6 {% block head %}
6 {% block head %}
7 <title>Neboard - {{ posts.0.title }}</title>
7 <title>Neboard - {{ posts.0.get_title }}</title>
8 <script src="{{ STATIC_URL }}js/thread.js"></script>
8 {% endblock %}
9 {% endblock %}
9
10
10 {% block content %}
11 {% block content %}
11 <script src="{{ STATIC_URL }}js/thread.js"></script>
12
12
13 {% if posts %}
13 {% if posts %}
14 <div id="posts">
14 <div id="posts">
@@ -2,7 +2,6 b' import hashlib'
2 from django.core.urlresolvers import reverse
2 from django.core.urlresolvers import reverse
3 from django.template import RequestContext
3 from django.template import RequestContext
4 from django.shortcuts import render, redirect, get_object_or_404
4 from django.shortcuts import render, redirect, get_object_or_404
5 from django.http import HttpResponseRedirect
6 from django.utils import timezone
5 from django.utils import timezone
7
6
8 from boards import forms
7 from boards import forms
@@ -11,7 +10,7 b' from boards import utils'
11 from boards.forms import ThreadForm, PostForm, SettingsForm, PlainErrorList, \
10 from boards.forms import ThreadForm, PostForm, SettingsForm, PlainErrorList, \
12 ThreadCaptchaForm, PostCaptchaForm, LoginForm
11 ThreadCaptchaForm, PostCaptchaForm, LoginForm
13
12
14 from boards.models import Post, Tag, Ban, User, RANK_USER, RANK_MODERATOR, NO_PARENT
13 from boards.models import Post, Tag, Ban, User, RANK_USER, NO_PARENT
15 from boards import authors
14 from boards import authors
16 import neboard
15 import neboard
17
16
@@ -155,7 +154,8 b' def login(request):'
155 context = _init_default_context(request)
154 context = _init_default_context(request)
156
155
157 if request.method == 'POST':
156 if request.method == 'POST':
158 form = LoginForm(request.POST, request.FILES, error_class=PlainErrorList)
157 form = LoginForm(request.POST, request.FILES,
158 error_class=PlainErrorList)
159 if form.is_valid():
159 if form.is_valid():
160 user = User.objects.get(user_id=form.cleaned_data['user_id'])
160 user = User.objects.get(user_id=form.cleaned_data['user_id'])
161 request.session['user_id'] = user.id
161 request.session['user_id'] = user.id
@@ -225,6 +225,7 b' def delete(request, post_id):'
225 post = get_object_or_404(Post, id=post_id)
225 post = get_object_or_404(Post, id=post_id)
226
226
227 if user.is_moderator():
227 if user.is_moderator():
228 # TODO Show confirmation page before deletion
228 Post.objects.delete_post(post)
229 Post.objects.delete_post(post)
229
230
230 if NO_PARENT == post.parent:
231 if NO_PARENT == post.parent:
@@ -263,10 +264,11 b' def tag_unsubscribe(request, tag_name):'
263 return redirect(all_tags)
264 return redirect(all_tags)
264
265
265
266
266 def _get_theme(request):
267 def _get_theme(request, user=None):
267 """Get user's CSS theme"""
268 """Get user's CSS theme"""
268
269
269 user = _get_user(request)
270 if not user:
271 user = _get_user(request)
270 theme = user.get_setting('theme')
272 theme = user.get_setting('theme')
271 if not theme:
273 if not theme:
272 theme = neboard.settings.DEFAULT_THEME
274 theme = neboard.settings.DEFAULT_THEME
@@ -287,9 +289,11 b' def _init_default_context(request):'
287 """Create context with default values that are used in most views"""
289 """Create context with default values that are used in most views"""
288
290
289 context = RequestContext(request)
291 context = RequestContext(request)
290 context['user'] = _get_user(request)
292
291 context['tags'] = _get_user(request).fav_tags.all()
293 user = _get_user(request)
292 context['theme'] = _get_theme(request)
294 context['user'] = user
295 context['tags'] = sorted(user.fav_tags.all(), key=lambda tag: tag.name)
296 context['theme'] = _get_theme(request, user)
293
297
294 return context
298 return context
295
299
@@ -313,8 +317,7 b' def _get_user(request):'
313 session['user_id'] = user.id
317 session['user_id'] = user.id
314 else:
318 else:
315 user = User.objects.get(id=session['user_id'])
319 user = User.objects.get(id=session['user_id'])
320 user.last_access_time = timezone.now()
316 user.save()
321 user.save()
317
322
318 user.last_access_time = timezone.now()
319
320 return user
323 return user
General Comments 0
You need to be logged in to leave comments. Login now