##// END OF EJS Templates
Code cleanup. Update only edited fields while performing thread archiving or post editing. Remove image when post is removed
neko259 -
r715:056b308f default
parent child Browse files
Show More
@@ -1,9 +1,7 b''
1 1 # -*- coding: utf-8 -*-
2 import datetime
3 2 from south.db import db
4 3 from south.v2 import SchemaMigration
5 4 from django.db import models
6 from boards.models.post import Post, NO_PARENT
7 5
8 6
9 7 class Migration(SchemaMigration):
@@ -36,23 +36,12 b' TITLE_MAX_LENGTH = 200'
36 36
37 37 DEFAULT_MARKUP_TYPE = 'markdown'
38 38
39 # TODO This should be removed when no code relies on it because thread id field
40 # was removed a long time ago
41 NO_PARENT = -1
42
43 39 # TODO This should be removed
44 40 NO_IP = '0.0.0.0'
45 41
46 42 # TODO Real user agent should be saved instead of this
47 43 UNKNOWN_UA = ''
48 44
49 # TODO This should be checked for usage and removed because a nativa
50 # paginator is used now
51 ALL_PAGES = -1
52
53 IMAGES_DIRECTORY = 'images/'
54 FILE_EXTENSION_DELIMITER = '.'
55
56 45 SETTING_MODERATE = "moderate"
57 46
58 47 REGEX_REPLY = re.compile('>>(\d+)')
@@ -107,7 +96,7 b' class PostManager(models.Manager):'
107 96 map(thread.add_tag, tags)
108 97
109 98 if new_thread:
110 self._delete_old_threads()
99 Thread.objects.archive_oldest_threads()
111 100 self.connect_replies(post)
112 101
113 102 logger.info('Created post #%d' % post.id)
@@ -141,30 +130,6 b' class PostManager(models.Manager):'
141 130 posts = self.filter(poster_ip=ip)
142 131 map(self.delete_post, posts)
143 132
144 # TODO Move this method to thread manager
145 # TODO Rename it, because the threads are archived instead of plain
146 # removal. Split the delete and archive methods and make a setting to
147 # enable or disable archiving.
148 def _delete_old_threads(self):
149 """
150 Preserves maximum thread count. If there are too many threads,
151 archive the old ones.
152 """
153
154 threads = Thread.objects.filter(archived=False).order_by('-bump_time')
155 thread_count = threads.count()
156
157 if thread_count > settings.MAX_THREAD_COUNT:
158 num_threads_to_delete = thread_count - settings.MAX_THREAD_COUNT
159 old_threads = threads[thread_count - num_threads_to_delete:]
160
161 for thread in old_threads:
162 thread.archived = True
163 thread.last_edit_time = timezone.now()
164 thread.save()
165
166 logger.info('Archived %d old threads' % num_threads_to_delete)
167
168 133 def connect_replies(self, post):
169 134 """
170 135 Connects replies to a post to show them as a reflink map
@@ -178,11 +143,11 b' class PostManager(models.Manager):'
178 143 referenced_post.referenced_posts.add(post)
179 144 referenced_post.last_edit_time = post.pub_time
180 145 referenced_post.build_refmap()
181 referenced_post.save()
146 referenced_post.save(update_fields=['refmap', 'last_edit_time'])
182 147
183 148 referenced_thread = referenced_post.get_thread()
184 149 referenced_thread.last_edit_time = post.pub_time
185 referenced_thread.save()
150 referenced_thread.save(update_fields=['last_edit_time'])
186 151
187 152 def get_posts_per_day(self):
188 153 """
@@ -235,7 +200,7 b' class Post(models.Model, Viewable):'
235 200 poster_user_agent = models.TextField()
236 201
237 202 thread_new = models.ForeignKey('Thread', null=True, default=None,
238 db_index=True)
203 db_index=True)
239 204 last_edit_time = models.DateTimeField()
240 205 user = models.ForeignKey('User', null=True, default=None, db_index=True)
241 206
@@ -379,3 +344,11 b' class Post(models.Model, Viewable):'
379 344 def get_first_image(self):
380 345 return self.images.earliest('id')
381 346
347 def delete(self, using=None):
348 """
349 Delete all post images and the post itself.
350 """
351
352 self.images.all().delete()
353
354 super(Post, self).delete(using) No newline at end of file
@@ -7,14 +7,6 b' from boards.models.base import Viewable'
7 7
8 8 __author__ = 'neko259'
9 9
10 # TODO Tag popularity ratings are not used any more, remove all of this
11 MAX_TAG_FONT = 1
12 MIN_TAG_FONT = 0.2
13
14 TAG_POPULARITY_MULTIPLIER = 20
15
16 ARCHIVE_POPULARITY_MODIFIER = 0.5
17
18 10
19 11 class TagManager(models.Manager):
20 12
@@ -59,25 +51,6 b' class Tag(models.Model, Viewable):'
59 51 def get_thread_count(self):
60 52 return self.threads.count()
61 53
62 # TODO Remove, not used any more
63 def get_popularity(self):
64 """
65 Gets tag's popularity value as a percentage of overall board post
66 count.
67 """
68
69 all_post_count = Post.objects.count()
70
71 tag_reply_count = 0.0
72
73 tag_reply_count += self.get_post_count()
74 tag_reply_count +=\
75 self.get_post_count(archived=True) * ARCHIVE_POPULARITY_MODIFIER
76
77 popularity = tag_reply_count / all_post_count
78
79 return popularity
80
81 54 def get_linked_tags(self):
82 55 """
83 56 Gets tags linked to the current one.
@@ -101,20 +74,6 b' class Tag(models.Model, Viewable):'
101 74
102 75 linked_tag.get_linked_tags_list(tag_list)
103 76
104 # TODO Remove
105 def get_font_value(self):
106 """
107 Gets tag font value to differ most popular tags in the list
108 """
109
110 popularity = self.get_popularity()
111
112 font_value = popularity * Tag.objects.get_not_empty_tags().count()
113 font_value = max(font_value, MIN_TAG_FONT)
114 font_value = min(font_value, MAX_TAG_FONT)
115
116 return str(font_value)
117
118 77 def get_post_count(self, archived=False):
119 78 """
120 79 Gets posts count for the tag's threads.
@@ -124,8 +83,8 b' class Tag(models.Model, Viewable):'
124 83
125 84 threads = self.threads.filter(archived=archived)
126 85 if threads.exists():
127 posts_count = threads.annotate(posts_count=Count('replies')).aggregate(
128 posts_sum=Sum('posts_count'))['posts_sum']
86 posts_count = threads.annotate(posts_count=Count('replies')) \
87 .aggregate(posts_sum=Sum('posts_count'))['posts_sum']
129 88
130 89 if not posts_count:
131 90 posts_count = 0
@@ -14,7 +14,30 b' logger = logging.getLogger(__name__)'
14 14 CACHE_KEY_OPENING_POST = 'opening_post_id'
15 15
16 16
17 class ThreadManager(models.Manager):
18 def archive_oldest_threads(self):
19 """
20 Preserves maximum thread count. If there are too many threads,
21 archive the old ones.
22 """
23
24 threads = Thread.objects.filter(archived=False).order_by('-bump_time')
25 thread_count = threads.count()
26
27 if thread_count > settings.MAX_THREAD_COUNT:
28 num_threads_to_delete = thread_count - settings.MAX_THREAD_COUNT
29 old_threads = threads[thread_count - num_threads_to_delete:]
30
31 for thread in old_threads:
32 thread.archived = True
33 thread.last_edit_time = timezone.now()
34 thread.save(update_fields=['archived', 'last_edit_time'])
35
36 logger.info('Archived %d old threads' % num_threads_to_delete)
37
38
17 39 class Thread(models.Model):
40 objects = ThreadManager()
18 41
19 42 class Meta:
20 43 app_label = 'boards'
@@ -75,8 +75,6 b" urlpatterns = patterns('',"
75 75 name='get_thread'),
76 76 url(r'^api/add_post/(?P<opening_post_id>\w+)/$', api.api_add_post,
77 77 name='add_post'),
78 url(r'^api/get_tag_popularity/(?P<tag_name>\w+)$', api.get_tag_popularity,
79 name='get_tag_popularity'),
80 78
81 79 # Search
82 80 url(r'^search/', include('haystack.urls')),
@@ -215,15 +215,6 b' def api_get_post(request, post_id):'
215 215 return HttpResponse(content=json)
216 216
217 217
218 def get_tag_popularity(request, tag_name):
219 tag = get_object_or_404(Tag, name=tag_name)
220
221 json_data = []
222 json_data['popularity'] = tag.get_popularity()
223
224 return HttpResponse(content=json.dumps(json_data))
225
226
227 218 # TODO Add pub time and replies
228 219 def _get_post_data(post_id, format_type=DIFF_TYPE_JSON, request=None,
229 220 include_last_update=False):
General Comments 0
You need to be logged in to leave comments. Login now