##// END OF EJS Templates
Cache user's tags
neko259 -
r323:a0b0f2ae default
parent child Browse files
Show More
@@ -0,0 +1,1 b''
1 CACHE_TIMEOUT = 600 # Timeout for caching, if cache is used No newline at end of file
@@ -2,12 +2,14 b' import os'
2 2 from random import random
3 3 import time
4 4 import math
5 from django.core.cache import cache
5 6
6 7 from django.db import models
7 8 from django.db.models import Count
8 9 from django.http import Http404
9 10 from django.utils import timezone
10 11 from markupfield.fields import MarkupField
12 from boards import settings as board_settings
11 13
12 14 from neboard import settings
13 15 import thumbs
@@ -350,9 +352,17 b' class User(models.Model):'
350 352 return RANK_MODERATOR >= self.rank
351 353
352 354 def get_sorted_fav_tags(self):
355 cache_key = self._get_tag_cache_key()
356 fav_tags = cache.get(cache_key)
357 if fav_tags:
358 return fav_tags
359
353 360 tags = self.fav_tags.annotate(Count('threads'))\
354 361 .filter(threads__count__gt=0).order_by('name')
355 362
363 if tags:
364 cache.set(cache_key, tags, board_settings.CACHE_TIMEOUT)
365
356 366 return tags
357 367
358 368 def get_post_count(self):
@@ -366,6 +376,17 b' class User(models.Model):'
366 376 if posts.count() > 0:
367 377 return posts.latest('pub_time').pub_time
368 378
379 def add_tag(self, tag):
380 self.fav_tags.add(tag)
381 cache.delete(self._get_tag_cache_key())
382
383 def remove_tag(self, tag):
384 self.fav_tags.remove(tag)
385 cache.delete(self._get_tag_cache_key())
386
387 def _get_tag_cache_key(self):
388 return self.user_id + '_tags'
389
369 390
370 391 class Setting(models.Model):
371 392
@@ -338,7 +338,7 b' def tag_subscribe(request, tag_name):'
338 338 tag = get_object_or_404(Tag, name=tag_name)
339 339
340 340 if not tag in user.fav_tags.all():
341 user.fav_tags.add(tag)
341 user.add_tag(tag)
342 342
343 343 return _redirect_to_next(request)
344 344
@@ -350,7 +350,7 b' def tag_unsubscribe(request, tag_name):'
350 350 tag = get_object_or_404(Tag, name=tag_name)
351 351
352 352 if tag in user.fav_tags.all():
353 user.fav_tags.remove(tag)
353 user.remove_tag(tag)
354 354
355 355 return _redirect_to_next(request)
356 356
General Comments 0
You need to be logged in to leave comments. Login now