##// END OF EJS Templates
Split up user models
neko259 -
r386:309b8129 default
parent child Browse files
Show More
@@ -0,0 +1,108 b''
1 from django.db import models
2 from django.db.models import Count
3 from boards import settings
4 from boards.models import Post
5 from django.core.cache import cache
6
7 __author__ = 'neko259'
8
9 RANK_ADMIN = 0
10 RANK_MODERATOR = 10
11 RANK_USER = 100
12
13 BAN_REASON_AUTO = 'Auto'
14 BAN_REASON_MAX_LENGTH = 200
15
16
17 class User(models.Model):
18
19 class Meta:
20 app_label = 'boards'
21
22 user_id = models.CharField(max_length=50)
23 rank = models.IntegerField()
24
25 registration_time = models.DateTimeField()
26
27 fav_tags = models.ManyToManyField('Tag', null=True, blank=True)
28 fav_threads = models.ManyToManyField(Post, related_name='+', null=True,
29 blank=True)
30
31 def save_setting(self, name, value):
32 setting, created = Setting.objects.get_or_create(name=name, user=self)
33 setting.value = str(value)
34 setting.save()
35
36 return setting
37
38 def get_setting(self, name):
39 if Setting.objects.filter(name=name, user=self).exists():
40 setting = Setting.objects.get(name=name, user=self)
41 setting_value = setting.value
42 else:
43 setting_value = None
44
45 return setting_value
46
47 def is_moderator(self):
48 return RANK_MODERATOR >= self.rank
49
50 def get_sorted_fav_tags(self):
51 cache_key = self._get_tag_cache_key()
52 fav_tags = cache.get(cache_key)
53 if fav_tags:
54 return fav_tags
55
56 tags = self.fav_tags.annotate(Count('threads')) \
57 .filter(threads__count__gt=0).order_by('name')
58
59 if tags:
60 cache.set(cache_key, tags, settings.CACHE_TIMEOUT)
61
62 return tags
63
64 def get_post_count(self):
65 return Post.objects.filter(user=self).count()
66
67 def __unicode__(self):
68 return self.user_id + '(' + str(self.rank) + ')'
69
70 def get_last_access_time(self):
71 posts = Post.objects.filter(user=self)
72 if posts.count() > 0:
73 return posts.latest('pub_time').pub_time
74
75 def add_tag(self, tag):
76 self.fav_tags.add(tag)
77 cache.delete(self._get_tag_cache_key())
78
79 def remove_tag(self, tag):
80 self.fav_tags.remove(tag)
81 cache.delete(self._get_tag_cache_key())
82
83 def _get_tag_cache_key(self):
84 return self.user_id + '_tags'
85
86
87 class Setting(models.Model):
88
89 class Meta:
90 app_label = 'boards'
91
92 name = models.CharField(max_length=50)
93 value = models.CharField(max_length=50)
94 user = models.ForeignKey(User)
95
96
97 class Ban(models.Model):
98
99 class Meta:
100 app_label = 'boards'
101
102 ip = models.GenericIPAddressField()
103 reason = models.CharField(default=BAN_REASON_AUTO,
104 max_length=BAN_REASON_MAX_LENGTH)
105 can_read = models.BooleanField(default=True)
106
107 def __unicode__(self):
108 return self.ip
@@ -4,7 +4,8 b' from django import forms'
4 from django.forms.util import ErrorList
4 from django.forms.util import ErrorList
5 from django.utils.translation import ugettext_lazy as _
5 from django.utils.translation import ugettext_lazy as _
6 import time
6 import time
7 from boards.models.post import TITLE_MAX_LENGTH, User
7 from boards.models.post import TITLE_MAX_LENGTH
8 from boards.models import User
8 from neboard import settings
9 from neboard import settings
9 from boards import utils
10 from boards import utils
10 import boards.settings as board_settings
11 import boards.settings as board_settings
@@ -2,6 +2,6 b''
2
2
3 from boards.models.post import Post
3 from boards.models.post import Post
4 from boards.models.tag import Tag
4 from boards.models.tag import Tag
5 from boards.models.post import Ban
5 from boards.models.user import Ban
6 from boards.models.post import Setting
6 from boards.models.user import Setting
7 from boards.models.post import User
7 from boards.models.user import User
@@ -2,22 +2,16 b' import os'
2 from random import random
2 from random import random
3 import time
3 import time
4 import math
4 import math
5 from django.core.cache import cache
5 import re
6
6
7 from django.db import models
7 from django.db import models
8 from django.db.models import Count
9 from django.http import Http404
8 from django.http import Http404
10 from django.utils import timezone
9 from django.utils import timezone
11 from markupfield.fields import MarkupField
10 from markupfield.fields import MarkupField
12 from boards import settings as board_settings
13
11
14 from neboard import settings
12 from neboard import settings
15 from boards import thumbs
13 from boards import thumbs
16
14
17 import re
18
19 BAN_REASON_MAX_LENGTH = 200
20
21 BAN_REASON_AUTO = 'Auto'
15 BAN_REASON_AUTO = 'Auto'
22
16
23 IMAGE_THUMB_SIZE = (200, 150)
17 IMAGE_THUMB_SIZE = (200, 150)
@@ -33,10 +27,6 b' ALL_PAGES = -1'
33 IMAGES_DIRECTORY = 'images/'
27 IMAGES_DIRECTORY = 'images/'
34 FILE_EXTENSION_DELIMITER = '.'
28 FILE_EXTENSION_DELIMITER = '.'
35
29
36 RANK_ADMIN = 0
37 RANK_MODERATOR = 10
38 RANK_USER = 100
39
40 SETTING_MODERATE = "moderate"
30 SETTING_MODERATE = "moderate"
41
31
42 REGEX_REPLY = re.compile('>>(\d+)')
32 REGEX_REPLY = re.compile('>>(\d+)')
@@ -303,95 +293,3 b' class Post(models.Model):'
303 return self.referenced_posts.count() > 0
293 return self.referenced_posts.count() > 0
304
294
305
295
306 class User(models.Model):
307
308 class Meta:
309 app_label = 'boards'
310
311 user_id = models.CharField(max_length=50)
312 rank = models.IntegerField()
313
314 registration_time = models.DateTimeField()
315
316 fav_tags = models.ManyToManyField('Tag', null=True, blank=True)
317 fav_threads = models.ManyToManyField(Post, related_name='+', null=True,
318 blank=True)
319
320 def save_setting(self, name, value):
321 setting, created = Setting.objects.get_or_create(name=name, user=self)
322 setting.value = str(value)
323 setting.save()
324
325 return setting
326
327 def get_setting(self, name):
328 if Setting.objects.filter(name=name, user=self).exists():
329 setting = Setting.objects.get(name=name, user=self)
330 setting_value = setting.value
331 else:
332 setting_value = None
333
334 return setting_value
335
336 def is_moderator(self):
337 return RANK_MODERATOR >= self.rank
338
339 def get_sorted_fav_tags(self):
340 cache_key = self._get_tag_cache_key()
341 fav_tags = cache.get(cache_key)
342 if fav_tags:
343 return fav_tags
344
345 tags = self.fav_tags.annotate(Count('threads')) \
346 .filter(threads__count__gt=0).order_by('name')
347
348 if tags:
349 cache.set(cache_key, tags, board_settings.CACHE_TIMEOUT)
350
351 return tags
352
353 def get_post_count(self):
354 return Post.objects.filter(user=self).count()
355
356 def __unicode__(self):
357 return self.user_id + '(' + str(self.rank) + ')'
358
359 def get_last_access_time(self):
360 posts = Post.objects.filter(user=self)
361 if posts.count() > 0:
362 return posts.latest('pub_time').pub_time
363
364 def add_tag(self, tag):
365 self.fav_tags.add(tag)
366 cache.delete(self._get_tag_cache_key())
367
368 def remove_tag(self, tag):
369 self.fav_tags.remove(tag)
370 cache.delete(self._get_tag_cache_key())
371
372 def _get_tag_cache_key(self):
373 return self.user_id + '_tags'
374
375
376 class Setting(models.Model):
377
378 class Meta:
379 app_label = 'boards'
380
381 name = models.CharField(max_length=50)
382 value = models.CharField(max_length=50)
383 user = models.ForeignKey(User)
384
385
386 class Ban(models.Model):
387
388 class Meta:
389 app_label = 'boards'
390
391 ip = models.GenericIPAddressField()
392 reason = models.CharField(default=BAN_REASON_AUTO,
393 max_length=BAN_REASON_MAX_LENGTH)
394 can_read = models.BooleanField(default=True)
395
396 def __unicode__(self):
397 return self.ip
@@ -1,11 +1,9 b''
1 from boards.models import Post
1 from boards.models import Post
2 from django.db import models
3 from django.db.models import Count
2
4
3 __author__ = 'neko259'
5 __author__ = 'neko259'
4
6
5
6 from django.db import models
7 from django.db.models import Count
8
9 TAG_FONT_MULTIPLIER = 0.1
7 TAG_FONT_MULTIPLIER = 0.1
10 MAX_TAG_FONT = 10
8 MAX_TAG_FONT = 10
11 OPENING_POST_POPULARITY_WEIGHT = 2
9 OPENING_POST_POPULARITY_WEIGHT = 2
@@ -20,7 +20,8 b' from boards import utils'
20 from boards.forms import ThreadForm, PostForm, SettingsForm, PlainErrorList, \
20 from boards.forms import ThreadForm, PostForm, SettingsForm, PlainErrorList, \
21 ThreadCaptchaForm, PostCaptchaForm, LoginForm, ModeratorSettingsForm
21 ThreadCaptchaForm, PostCaptchaForm, LoginForm, ModeratorSettingsForm
22 from boards.models import Post, Tag, Ban, User
22 from boards.models import Post, Tag, Ban, User
23 from boards.models.post import RANK_USER, SETTING_MODERATE, REGEX_REPLY
23 from boards.models.post import SETTING_MODERATE, REGEX_REPLY
24 from boards.models.user import RANK_USER
24 from boards import authors
25 from boards import authors
25 from boards.utils import get_client_ip
26 from boards.utils import get_client_ip
26 import neboard
27 import neboard
General Comments 0
You need to be logged in to leave comments. Login now