##// END OF EJS Templates
Removed user and settings mode. Added settings manager to manage settings and keep them in the session (or any other backend like cookie in the future
neko259 -
r728:a5c2ce32 2.0-dev
parent child Browse files
Show More
@@ -0,0 +1,106 b''
1 from django.shortcuts import get_object_or_404
2 from boards.models import Tag
3
4 __author__ = 'neko259'
5
6 SESSION_SETTING = 'setting'
7
8 PERMISSION_MODERATE = 'moderator'
9
10 SETTING_THEME = 'theme'
11 SETTING_FAVORITE_TAGS = 'favorite_tags'
12 SETTING_HIDDEN_TAGS = 'hidden_tags'
13 SETTING_PERMISSIONS = 'permissions'
14
15 DEFAULT_THEME = 'md'
16
17
18 class SettingsManager:
19
20 def __init__(self, session):
21 self.session = session
22
23 def get_theme(self):
24 theme = self.get_setting(SETTING_THEME)
25 if not theme:
26 theme = DEFAULT_THEME
27 self.set_setting(SETTING_THEME, theme)
28
29 return theme
30
31 def set_theme(self, theme):
32 self.set_setting(SETTING_THEME, theme)
33
34 def has_permission(self, permission):
35 permissions = self.get_setting(SETTING_PERMISSIONS)
36 if permissions:
37 return permission in permissions
38 else:
39 return False
40
41 def get_setting(self, setting):
42 if setting in self.session:
43 return self.session[setting]
44 else:
45 return None
46
47 def set_setting(self, setting, value):
48 self.session[setting] = value
49
50 def add_permission(self, permission):
51 permissions = self.get_setting(SETTING_PERMISSIONS)
52 if not permissions:
53 permissions = [permission]
54 else:
55 permissions += permission
56 self.set_setting(SETTING_PERMISSIONS, permissions)
57
58 def get_fav_tags(self):
59 tag_names = self.get_setting(SETTING_FAVORITE_TAGS)
60 tags = []
61 if tag_names:
62 for tag_name in tag_names:
63 tag = get_object_or_404(Tag, name=tag_name)
64 tags.append(tag)
65
66 return tags
67
68 def add_fav_tag(self, tag):
69 tags = self.get_setting(SETTING_FAVORITE_TAGS)
70 if not tags:
71 tags = [tag.name]
72 else:
73 if not tag.name in tags:
74 tags.append(tag.name)
75 self.set_setting(SETTING_FAVORITE_TAGS, tags)
76
77 def del_fav_tag(self, tag):
78 tags = self.get_setting(SETTING_FAVORITE_TAGS)
79 if tag.name in tags:
80 tags.remove(tag.name)
81 self.set_setting(SETTING_FAVORITE_TAGS, tags)
82
83 def get_hidden_tags(self):
84 tag_names = self.get_setting(SETTING_HIDDEN_TAGS)
85 tags = []
86 if tag_names:
87 for tag_name in tag_names:
88 tag = get_object_or_404(Tag, name=tag_name)
89 tags.append(tag)
90
91 return tags
92
93 def add_hidden_tag(self, tag):
94 tags = self.get_setting(SETTING_HIDDEN_TAGS)
95 if not tags:
96 tags = [tag.name]
97 else:
98 if not tag.name in tags:
99 tags.append(tag.name)
100 self.set_setting(SETTING_HIDDEN_TAGS, tags)
101
102 def del_hidden_tag(self, tag):
103 tags = self.get_setting(SETTING_HIDDEN_TAGS)
104 if tag.name in tags:
105 tags.remove(tag.name)
106 self.set_setting(SETTING_HIDDEN_TAGS, tags)
@@ -0,0 +1,146 b''
1 # -*- coding: utf-8 -*-
2 from south.utils import datetime_utils as datetime
3 from south.db import db
4 from south.v2 import SchemaMigration
5 from django.db import models
6
7
8 class Migration(SchemaMigration):
9
10 def forwards(self, orm):
11 # Deleting model 'Setting'
12 db.delete_table(u'boards_setting')
13
14 # Deleting model 'User'
15 db.delete_table(u'boards_user')
16
17 # Removing M2M table for field hidden_threads on 'User'
18 db.delete_table(db.shorten_name(u'boards_user_hidden_threads'))
19
20 # Removing M2M table for field fav_threads on 'User'
21 db.delete_table(db.shorten_name(u'boards_user_fav_threads'))
22
23 # Removing M2M table for field fav_tags on 'User'
24 db.delete_table(db.shorten_name(u'boards_user_fav_tags'))
25
26 # Removing M2M table for field hidden_tags on 'User'
27 db.delete_table(db.shorten_name(u'boards_user_hidden_tags'))
28
29 # Deleting field 'Post.user'
30 db.delete_column(u'boards_post', 'user_id')
31
32
33 def backwards(self, orm):
34 # Adding model 'Setting'
35 db.create_table(u'boards_setting', (
36 ('user', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['boards.User'])),
37 (u'id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
38 ('value', self.gf('django.db.models.fields.CharField')(max_length=50)),
39 ('name', self.gf('django.db.models.fields.CharField')(max_length=50)),
40 ))
41 db.send_create_signal('boards', ['Setting'])
42
43 # Adding model 'User'
44 db.create_table(u'boards_user', (
45 ('registration_time', self.gf('django.db.models.fields.DateTimeField')()),
46 (u'id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
47 ('user_id', self.gf('django.db.models.fields.CharField')(max_length=50)),
48 ('rank', self.gf('django.db.models.fields.IntegerField')()),
49 ))
50 db.send_create_signal('boards', ['User'])
51
52 # Adding M2M table for field hidden_threads on 'User'
53 m2m_table_name = db.shorten_name(u'boards_user_hidden_threads')
54 db.create_table(m2m_table_name, (
55 ('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)),
56 ('user', models.ForeignKey(orm['boards.user'], null=False)),
57 ('post', models.ForeignKey(orm['boards.post'], null=False))
58 ))
59 db.create_unique(m2m_table_name, ['user_id', 'post_id'])
60
61 # Adding M2M table for field fav_threads on 'User'
62 m2m_table_name = db.shorten_name(u'boards_user_fav_threads')
63 db.create_table(m2m_table_name, (
64 ('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)),
65 ('user', models.ForeignKey(orm['boards.user'], null=False)),
66 ('post', models.ForeignKey(orm['boards.post'], null=False))
67 ))
68 db.create_unique(m2m_table_name, ['user_id', 'post_id'])
69
70 # Adding M2M table for field fav_tags on 'User'
71 m2m_table_name = db.shorten_name(u'boards_user_fav_tags')
72 db.create_table(m2m_table_name, (
73 ('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)),
74 ('user', models.ForeignKey(orm['boards.user'], null=False)),
75 ('tag', models.ForeignKey(orm['boards.tag'], null=False))
76 ))
77 db.create_unique(m2m_table_name, ['user_id', 'tag_id'])
78
79 # Adding M2M table for field hidden_tags on 'User'
80 m2m_table_name = db.shorten_name(u'boards_user_hidden_tags')
81 db.create_table(m2m_table_name, (
82 ('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)),
83 ('user', models.ForeignKey(orm['boards.user'], null=False)),
84 ('tag', models.ForeignKey(orm['boards.tag'], null=False))
85 ))
86 db.create_unique(m2m_table_name, ['user_id', 'tag_id'])
87
88 # Adding field 'Post.user'
89 db.add_column(u'boards_post', 'user',
90 self.gf('django.db.models.fields.related.ForeignKey')(default=None, to=orm['boards.User'], null=True),
91 keep_default=False)
92
93
94 models = {
95 'boards.ban': {
96 'Meta': {'object_name': 'Ban'},
97 'can_read': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
98 u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
99 'ip': ('django.db.models.fields.GenericIPAddressField', [], {'max_length': '39'}),
100 'reason': ('django.db.models.fields.CharField', [], {'default': "'Auto'", 'max_length': '200'})
101 },
102 'boards.post': {
103 'Meta': {'ordering': "('id',)", 'object_name': 'Post'},
104 '_text_rendered': ('django.db.models.fields.TextField', [], {}),
105 u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
106 'images': ('django.db.models.fields.related.ManyToManyField', [], {'related_name': "'ip+'", 'to': "orm['boards.PostImage']", 'blank': 'True', 'symmetrical': 'False', 'null': 'True', 'db_index': 'True'}),
107 'last_edit_time': ('django.db.models.fields.DateTimeField', [], {}),
108 'poster_ip': ('django.db.models.fields.GenericIPAddressField', [], {'max_length': '39'}),
109 'poster_user_agent': ('django.db.models.fields.TextField', [], {}),
110 'pub_time': ('django.db.models.fields.DateTimeField', [], {}),
111 'referenced_posts': ('django.db.models.fields.related.ManyToManyField', [], {'related_name': "'rfp+'", 'to': "orm['boards.Post']", 'blank': 'True', 'symmetrical': 'False', 'null': 'True', 'db_index': 'True'}),
112 'refmap': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}),
113 'text': ('markupfield.fields.MarkupField', [], {'rendered_field': 'True'}),
114 'text_markup_type': ('django.db.models.fields.CharField', [], {'default': "'markdown'", 'max_length': '30'}),
115 'thread_new': ('django.db.models.fields.related.ForeignKey', [], {'default': 'None', 'to': "orm['boards.Thread']", 'null': 'True'}),
116 'title': ('django.db.models.fields.CharField', [], {'max_length': '200'})
117 },
118 'boards.postimage': {
119 'Meta': {'ordering': "('id',)", 'object_name': 'PostImage'},
120 'hash': ('django.db.models.fields.CharField', [], {'max_length': '36'}),
121 'height': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
122 u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
123 'image': ('boards.thumbs.ImageWithThumbsField', [], {'max_length': '100', 'blank': 'True'}),
124 'pre_height': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
125 'pre_width': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
126 'width': ('django.db.models.fields.IntegerField', [], {'default': '0'})
127 },
128 'boards.tag': {
129 'Meta': {'ordering': "('name',)", 'object_name': 'Tag'},
130 u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
131 'linked': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['boards.Tag']", 'null': 'True', 'blank': 'True'}),
132 'name': ('django.db.models.fields.CharField', [], {'max_length': '100', 'db_index': 'True'}),
133 'threads': ('django.db.models.fields.related.ManyToManyField', [], {'blank': 'True', 'related_name': "'tag+'", 'null': 'True', 'symmetrical': 'False', 'to': "orm['boards.Thread']"})
134 },
135 'boards.thread': {
136 'Meta': {'object_name': 'Thread'},
137 'archived': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
138 'bump_time': ('django.db.models.fields.DateTimeField', [], {}),
139 u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
140 'last_edit_time': ('django.db.models.fields.DateTimeField', [], {}),
141 'replies': ('django.db.models.fields.related.ManyToManyField', [], {'blank': 'True', 'related_name': "'tre+'", 'null': 'True', 'symmetrical': 'False', 'to': "orm['boards.Post']"}),
142 'tags': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['boards.Tag']", 'symmetrical': 'False'})
143 }
144 }
145
146 complete_apps = ['boards'] No newline at end of file
@@ -1,5 +1,5 b''
1 1 from django.contrib import admin
2 from boards.models import Post, Tag, User, Ban, Thread
2 from boards.models import Post, Tag, Ban, Thread
3 3
4 4
5 5 class PostAdmin(admin.ModelAdmin):
@@ -15,12 +15,6 b' class TagAdmin(admin.ModelAdmin):'
15 15 list_filter = ('linked',)
16 16
17 17
18 class UserAdmin(admin.ModelAdmin):
19
20 list_display = ('user_id', 'rank')
21 search_fields = ('user_id',)
22
23
24 18 class ThreadAdmin(admin.ModelAdmin):
25 19
26 20 def title(self, obj):
@@ -35,6 +29,5 b' class ThreadAdmin(admin.ModelAdmin):'
35 29
36 30 admin.site.register(Post, PostAdmin)
37 31 admin.site.register(Tag, TagAdmin)
38 admin.site.register(User, UserAdmin)
39 32 admin.site.register(Ban)
40 33 admin.site.register(Thread, ThreadAdmin)
@@ -1,8 +1,10 b''
1 from boards.abstracts.settingsmanager import SettingsManager, \
2 PERMISSION_MODERATE
3
1 4 __author__ = 'neko259'
2 5
3 from boards import utils, settings
6 from boards import settings
4 7 from boards.models import Post
5 from boards.models.post import SETTING_MODERATE
6 8
7 9 CONTEXT_SITE_NAME = 'site_name'
8 10 CONTEXT_VERSION = 'version'
@@ -17,21 +19,17 b" CONTEXT_USER = 'user'"
17 19 def user_and_ui_processor(request):
18 20 context = {}
19 21
20 user = utils.get_user(request)
21 context[CONTEXT_USER] = user
22 context[CONTEXT_TAGS] = user.fav_tags.all()
23 22 context[CONTEXT_PPD] = float(Post.objects.get_posts_per_day())
24 23
25 theme = utils.get_theme(request, user)
24 settings_manager = SettingsManager(request.session)
25 context[CONTEXT_TAGS] = settings_manager.get_fav_tags()
26 theme = settings_manager.get_theme()
26 27 context[CONTEXT_THEME] = theme
27 28 context[CONTEXT_THEME_CSS] = 'css/' + theme + '/base_page.css'
28 29
29 30 # This shows the moderator panel
30 moderate = user.get_setting(SETTING_MODERATE)
31 if moderate == 'True':
32 context[CONTEXT_MODERATOR] = user.is_moderator()
33 else:
34 context[CONTEXT_MODERATOR] = False
31 moderate = settings_manager.has_permission(PERMISSION_MODERATE)
32 context[CONTEXT_MODERATOR] = moderate
35 33
36 34 context[CONTEXT_VERSION] = settings.VERSION
37 35 context[CONTEXT_SITE_NAME] = settings.SITE_NAME
@@ -9,7 +9,7 b' from django.utils.translation import uge'
9 9
10 10 from boards.mdx_neboard import formatters
11 11 from boards.models.post import TITLE_MAX_LENGTH
12 from boards.models import User, PostImage
12 from boards.models import PostImage
13 13 from neboard import settings
14 14 from boards import utils
15 15 import boards.settings as board_settings
@@ -187,10 +187,6 b' class PostForm(NeboardForm):'
187 187 if not 'user_id' in self.session:
188 188 return
189 189
190 user = User.objects.get(id=self.session['user_id'])
191 if user.is_veteran():
192 posting_delay = VETERAN_POSTING_DELAY
193 else:
194 190 posting_delay = settings.POSTING_DELAY
195 191
196 192 if board_settings.LIMIT_POSTING_SPEED and LAST_POST_TIME in \
@@ -288,51 +284,6 b' class ModeratorSettingsForm(SettingsForm'
288 284 'panel'))
289 285
290 286
291 class LoginForm(NeboardForm):
292
293 user_id = forms.CharField()
294
295 session = None
296
297 def clean_user_id(self):
298 user_id = self.cleaned_data['user_id']
299 if user_id:
300 users = User.objects.filter(user_id=user_id)
301 if len(users) == 0:
302 raise forms.ValidationError(_('No such user found'))
303
304 return user_id
305
306 def _validate_login_speed(self):
307 can_post = True
308
309 if LAST_LOGIN_TIME in self.session:
310 now = time.time()
311 last_login_time = self.session[LAST_LOGIN_TIME]
312
313 current_delay = int(now - last_login_time)
314
315 if current_delay < board_settings.LOGIN_TIMEOUT:
316 error_message = _('Wait %s minutes after last login') % str(
317 (board_settings.LOGIN_TIMEOUT - current_delay) / 60)
318 self._errors['user_id'] = self.error_class([error_message])
319
320 can_post = False
321
322 if can_post:
323 self.session[LAST_LOGIN_TIME] = time.time()
324
325 def clean(self):
326 if not self.session:
327 raise forms.ValidationError('Humans have sessions')
328
329 self._validate_login_speed()
330
331 cleaned_data = super(LoginForm, self).clean()
332
333 return cleaned_data
334
335
336 287 class AddTagForm(NeboardForm):
337 288
338 289 tag = forms.CharField(max_length=TAG_MAX_LENGTH, label=LABEL_TAG)
@@ -13,4 +13,4 b' class Command(BaseCommand):'
13 13
14 14 @transaction.atomic
15 15 def handle(self, *args, **options):
16 Post.objects.all().update(poster_ip=NO_IP, user=None) No newline at end of file
16 Post.objects.all().update(poster_ip=NO_IP) No newline at end of file
@@ -5,5 +5,3 b' from boards.models.thread import Thread'
5 5 from boards.models.post import Post
6 6 from boards.models.tag import Tag
7 7 from boards.models.user import Ban
8 from boards.models.user import Setting
9 from boards.models.user import User
@@ -45,8 +45,8 b' logger = logging.getLogger(__name__)'
45 45
46 46 class PostManager(models.Manager):
47 47
48 def create_post(self, title, text, image=None, thread=None,
49 ip=NO_IP, tags=None, user=None):
48 def create_post(self, title, text, image=None, thread=None, ip=NO_IP,
49 tags=None):
50 50 """
51 51 Creates new post
52 52 """
@@ -69,8 +69,7 b' class PostManager(models.Manager):'
69 69 poster_ip=ip,
70 70 poster_user_agent=UNKNOWN_UA, # TODO Get UA at
71 71 # last!
72 last_edit_time=posting_time,
73 user=user)
72 last_edit_time=posting_time)
74 73
75 74 if image:
76 75 post_image = PostImage.objects.create(image=image)
@@ -196,7 +195,6 b' class Post(models.Model, Viewable):'
196 195 thread_new = models.ForeignKey('Thread', null=True, default=None,
197 196 db_index=True)
198 197 last_edit_time = models.DateTimeField()
199 user = models.ForeignKey('User', null=True, default=None, db_index=True)
200 198
201 199 referenced_posts = models.ManyToManyField('Post', symmetrical=False,
202 200 null=True,
@@ -1,122 +1,10 b''
1 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 2
7 3 __author__ = 'neko259'
8 4
9 RANK_ADMIN = 0
10 RANK_MODERATOR = 10
11 RANK_USER = 100
12
13 5 BAN_REASON_AUTO = 'Auto'
14 6 BAN_REASON_MAX_LENGTH = 200
15 7
16 VETERAN_POSTS = 1000
17
18
19 class User(models.Model):
20
21 class Meta:
22 app_label = 'boards'
23
24 user_id = models.CharField(max_length=50)
25 rank = models.IntegerField()
26
27 registration_time = models.DateTimeField()
28
29 fav_tags = models.ManyToManyField('Tag', null=True, blank=True)
30 fav_threads = models.ManyToManyField(Post, related_name='+', null=True,
31 blank=True)
32
33 hidden_tags = models.ManyToManyField('Tag', null=True, blank=True,
34 related_name='ht+')
35 hidden_threads = models.ManyToManyField('Post', null=True, blank=True,
36 related_name='hth+')
37
38 def save_setting(self, name, value):
39 setting, created = Setting.objects.get_or_create(name=name, user=self)
40 setting.value = str(value)
41 setting.save()
42
43 return setting
44
45 def get_setting(self, name):
46 if Setting.objects.filter(name=name, user=self).exists():
47 setting = Setting.objects.get(name=name, user=self)
48 setting_value = setting.value
49 else:
50 setting_value = None
51
52 return setting_value
53
54 def is_moderator(self):
55 return RANK_MODERATOR >= self.rank
56
57 def get_sorted_fav_tags(self):
58 cache_key = self._get_tag_cache_key()
59 fav_tags = cache.get(cache_key)
60 if fav_tags:
61 return fav_tags
62
63 tags = self.fav_tags.annotate(Count('threads')) \
64 .filter(threads__count__gt=0).order_by('name')
65
66 if tags:
67 cache.set(cache_key, tags)
68
69 return tags
70
71 def get_post_count(self):
72 return Post.objects.filter(user=self).count()
73
74 def __unicode__(self):
75 return self.user_id + '(' + str(self.rank) + ')'
76
77 def get_last_access_time(self):
78 """
79 Gets user's last post time.
80 """
81
82 posts = Post.objects.filter(user=self)
83 if posts.exists() > 0:
84 return posts.latest('pub_time').pub_time
85
86 def add_tag(self, tag):
87 self.fav_tags.add(tag)
88 cache.delete(self._get_tag_cache_key())
89
90 def remove_tag(self, tag):
91 self.fav_tags.remove(tag)
92 cache.delete(self._get_tag_cache_key())
93
94 def hide_tag(self, tag):
95 self.hidden_tags.add(tag)
96
97 def unhide_tag(self, tag):
98 self.hidden_tags.remove(tag)
99
100 def is_veteran(self):
101 """
102 Returns if a user is old (veteran).
103 """
104
105 return self.get_post_count() >= VETERAN_POSTS
106
107 def _get_tag_cache_key(self):
108 return self.user_id + '_tags'
109
110
111 class Setting(models.Model):
112
113 class Meta:
114 app_label = 'boards'
115
116 name = models.CharField(max_length=50)
117 value = models.CharField(max_length=50)
118 user = models.ForeignKey(User)
119
120 8
121 9 class Ban(models.Model):
122 10
@@ -46,7 +46,6 b''
46 46
47 47 <div class="navigation_panel">
48 48 {% block metapanel %}{% endblock %}
49 [<a href="{% url "login" %}">{% trans 'Login' %}</a>]
50 49 [<a href="{% url "search" %}">{% trans 'Search' %}</a>]
51 50 {% with ppd=posts_per_day|floatformat:2 %}
52 51 {% blocktrans %}Speed: {{ ppd }} posts per day{% endblocktrans %}
@@ -44,14 +44,14 b''
44 44 {% if tag %}
45 45 <div class="tag_info">
46 46 <h2>
47 {% if tag in user.fav_tags.all %}
47 {% if tag in fav_tags %}
48 48 <a href="{% url 'tag' tag.name %}?method=unsubscribe&next={{ request.path }}"
49 49 class="fav"></a>
50 50 {% else %}
51 51 <a href="{% url 'tag' tag.name %}?method=subscribe&next={{ request.path }}"
52 52 class="not_fav"></a>
53 53 {% endif %}
54 {% if tag in user.hidden_tags.all %}
54 {% if tag in hidden_tags %}
55 55 <a href="{% url 'tag' tag.name %}?method=unhide&next={{ request.path }}"
56 56 title="{% trans 'Show tag' %}"
57 57 class="fav">H</a>
@@ -11,20 +11,11 b''
11 11
12 12 <div class="post">
13 13 <p>
14 {% trans 'User:' %} <b>{{ user.user_id }}</b>.
15 14 {% if user.is_moderator %}
16 15 {% trans 'You are moderator.' %}
17 16 {% endif %}
18 {% if user.is_veteran %}
19 {% trans 'You are veteran.' %}
20 {% endif %}
21 17 </p>
22 <p>{% trans 'Posts:' %} {{ user.get_post_count }}</p>
23 <p>{% trans 'First access:' %} {{ user.registration_time|naturaltime }}</p>
24 {% if user.get_last_access_time %}
25 <p>{% trans 'Last access:' %} {{ user.get_last_access_time|naturaltime }}</p>
26 {% endif %}
27 {% with hidden_tags=user.hidden_tags.all %}
18 {% with hidden_tags=hidden_tags %}
28 19 {% if hidden_tags %}
29 20 <p>{% trans 'Hidden tags:' %}
30 21 {% for tag in hidden_tags %}
@@ -30,8 +30,7 b' logger = logging.getLogger(__name__)'
30 30 class PostTests(TestCase):
31 31
32 32 def _create_post(self):
33 return Post.objects.create_post(title='title',
34 text='text')
33 return Post.objects.create_post(title='title', text='text')
35 34
36 35 def test_post_add(self):
37 36 """Test adding post"""
@@ -2,7 +2,7 b' from django.conf.urls import patterns, u'
2 2 from boards import views
3 3 from boards.rss import AllThreadsFeed, TagThreadsFeed, ThreadPostsFeed
4 4 from boards.views import api, tag_threads, all_threads, \
5 login, settings, all_tags
5 settings, all_tags
6 6 from boards.views.authors import AuthorsView
7 7 from boards.views.delete_post import DeletePostView
8 8 from boards.views.ban import BanUserView
@@ -22,9 +22,6 b" urlpatterns = patterns('',"
22 22 url(r'^page/(?P<page>\w+)/$', all_threads.AllThreadsView.as_view(),
23 23 name='index'),
24 24
25 # login page
26 url(r'^login/$', login.LoginView.as_view(), name='login'),
27
28 25 # /boards/tag/tag_name/
29 26 url(r'^tag/(?P<tag_name>\w+)/$', tag_threads.TagView.as_view(),
30 27 name='tag'),
@@ -5,11 +5,8 b' import hashlib'
5 5 import time
6 6
7 7 from django.utils import timezone
8 import boards
9 8
10 9 from neboard import settings
11 from boards.models import User
12 from boards.models.user import RANK_USER
13 10
14 11
15 12 KEY_CAPTCHA_FAILS = 'key_captcha_fails'
@@ -82,48 +79,4 b' def get_client_ip(request):'
82 79 def datetime_to_epoch(datetime):
83 80 return int(time.mktime(timezone.localtime(
84 81 datetime,timezone.get_current_timezone()).timetuple())
85 * 1000000 + datetime.microsecond)
86
87
88 def get_user(request):
89 """
90 Get current user from the session. If the user does not exist, create
91 a new one.
92 """
93
94 session = request.session
95 if not 'user_id' in session:
96 request.session.save()
97
98 md5 = hashlib.md5()
99 md5.update(session.session_key)
100 new_id = md5.hexdigest()
101
102 while User.objects.filter(user_id=new_id).exists():
103 md5.update(str(timezone.now()))
104 new_id = md5.hexdigest()
105
106 time_now = timezone.now()
107 user = User.objects.create(user_id=new_id, rank=RANK_USER,
108 registration_time=time_now)
109
110 session['user_id'] = user.id
111 else:
112 user = User.objects.select_related('fav_tags').get(
113 id=session['user_id'])
114
115 return user
116
117
118 def get_theme(request, user=None):
119 """
120 Get user's CSS theme
121 """
122
123 if not user:
124 user = get_user(request)
125 theme = user.get_setting('theme')
126 if not theme:
127 theme = boards.settings.DEFAULT_THEME
128
129 return theme No newline at end of file
82 * 1000000 + datetime.microsecond) No newline at end of file
@@ -5,6 +5,7 b' from django.shortcuts import render, red'
5 5
6 6 from boards import utils, settings
7 7 from boards.abstracts.paginator import get_paginator
8 from boards.abstracts.settingsmanager import SettingsManager
8 9 from boards.forms import ThreadForm, PlainErrorList
9 10 from boards.models import Post, Thread, Ban, Tag
10 11 from boards.views.banned import BannedView
@@ -28,16 +29,17 b' DEFAULT_PAGE = 1'
28 29
29 30 class AllThreadsView(PostMixin, BaseBoardView):
30 31
31 user = None
32 def __init__(self):
33 self.settings_manager = None
34 super(AllThreadsView, self).__init__()
32 35
33 36 def get(self, request, page=DEFAULT_PAGE, form=None):
34 37 context = self.get_context_data(request=request)
35 38
36 self.user = utils.get_user(request)
37
38 39 if not form:
39 40 form = ThreadForm(error_class=PlainErrorList)
40 41
42 self.settings_manager = SettingsManager(request.session)
41 43 paginator = get_paginator(self.get_threads(),
42 44 settings.THREADS_PER_PAGE)
43 45 paginator.current_page = int(page)
@@ -122,9 +124,8 b' class AllThreadsView(PostMixin, BaseBoar'
122 124
123 125 tags = self.parse_tags_string(tag_strings)
124 126
125 post = Post.objects.create_post(title=title, text=text, ip=ip,
126 image=image, tags=tags,
127 user=utils.get_user(request))
127 post = Post.objects.create_post(title=title, text=text, image=image,
128 ip=ip, tags=tags)
128 129
129 130 if html_response:
130 131 return redirect(post.get_url())
@@ -135,4 +136,4 b' class AllThreadsView(PostMixin, BaseBoar'
135 136 """
136 137
137 138 return Thread.objects.all().order_by('-bump_time')\
138 .exclude(tags__in=self.user.hidden_tags.all())
139 .exclude(tags__in=self.settings_manager.get_hidden_tags())
@@ -1,7 +1,8 b''
1 1 from django.db import transaction
2 2 from django.shortcuts import get_object_or_404
3 from boards import utils
4 3
4 from boards.abstracts.settingsmanager import SettingsManager, \
5 PERMISSION_MODERATE
5 6 from boards.views.base import BaseBoardView
6 7 from boards.models import Post, Ban
7 8 from boards.views.mixins import RedirectNextMixin
@@ -11,10 +12,11 b' class BanUserView(BaseBoardView, Redirec'
11 12
12 13 @transaction.atomic
13 14 def get(self, request, post_id):
14 user = utils.get_user(request)
15 15 post = get_object_or_404(Post, id=post_id)
16 16
17 if user.is_moderator():
17 settings_manager = SettingsManager(request.session)
18
19 if settings_manager.has_permission(PERMISSION_MODERATE):
18 20 # TODO Show confirmation page before ban
19 21 ban, created = Ban.objects.get_or_create(ip=post.poster_ip)
20 22 if created:
@@ -1,7 +1,8 b''
1 1 from django.shortcuts import redirect, get_object_or_404
2 2 from django.db import transaction
3 from boards import utils
4 3
4 from boards.abstracts.settingsmanager import SettingsManager, \
5 PERMISSION_MODERATE
5 6 from boards.views.base import BaseBoardView
6 7 from boards.views.mixins import RedirectNextMixin
7 8 from boards.models import Post
@@ -11,12 +12,12 b' class DeletePostView(BaseBoardView, Redi'
11 12
12 13 @transaction.atomic
13 14 def get(self, request, post_id):
14 user = utils.get_user(request)
15 15 post = get_object_or_404(Post, id=post_id)
16 16
17 17 opening_post = post.is_opening()
18 18
19 if user.is_moderator():
19 settings_manager = SettingsManager(request.session)
20 if settings_manager.has_permission(PERMISSION_MODERATE):
20 21 # TODO Show confirmation page before deletion
21 22 Post.objects.delete_post(post)
22 23
@@ -1,4 +1,6 b''
1 1 from django.shortcuts import render, get_object_or_404, redirect
2 from boards.abstracts.settingsmanager import SettingsManager, \
3 PERMISSION_MODERATE
2 4
3 5 from boards.views.base import BaseBoardView
4 6 from boards.views.mixins import DispatcherMixin
@@ -6,11 +8,12 b' from boards.models.post import Post'
6 8 from boards.models.tag import Tag
7 9 from boards.forms import AddTagForm, PlainErrorList
8 10
11
9 12 class PostAdminView(BaseBoardView, DispatcherMixin):
10 13
11 14 def get(self, request, post_id, form=None):
12 user = utils.get_user(request)
13 if not user.is_moderator:
15 settings_manager = SettingsManager(request.session)
16 if not settings_manager.has_permission(PERMISSION_MODERATE):
14 17 redirect('index')
15 18
16 19 post = get_object_or_404(Post, id=post_id)
@@ -30,8 +33,8 b' class PostAdminView(BaseBoardView, Dispa'
30 33 return render(request, 'boards/post_admin.html', context)
31 34
32 35 def post(self, request, post_id):
33 user = utils.get_user(request)
34 if not user.is_moderator:
36 settings_manager = SettingsManager(request.session)
37 if not settings_manager.has_permission(PERMISSION_MODERATE):
35 38 redirect('index')
36 39
37 40 post = get_object_or_404(Post, id=post_id)
@@ -1,6 +1,8 b''
1 1 from django.db import transaction
2 2 from django.shortcuts import render, redirect
3 3 from boards import utils
4 from boards.abstracts.settingsmanager import SettingsManager, \
5 PERMISSION_MODERATE
4 6
5 7 from boards.views.base import BaseBoardView, CONTEXT_FORM
6 8 from boards.forms import SettingsForm, ModeratorSettingsForm, PlainErrorList
@@ -11,16 +13,15 b' class SettingsView(BaseBoardView):'
11 13
12 14 def get(self, request):
13 15 context = self.get_context_data(request=request)
14 user = utils.get_user(request)
15 is_moderator = user.is_moderator()
16 settings_manager = SettingsManager(request.session)
17 is_moderator = settings_manager.has_permission(PERMISSION_MODERATE)
16 18
17 selected_theme = utils.get_theme(request, user)
19 selected_theme = settings_manager.get_theme()
18 20
19 21 if is_moderator:
20 22 form = ModeratorSettingsForm(initial={
21 23 'theme': selected_theme,
22 'moderate': user.get_setting(SETTING_MODERATE) and \
23 user.is_moderator()
24 'moderate': settings_manager.has_permission(PERMISSION_MODERATE)
24 25 }, error_class=PlainErrorList)
25 26 else:
26 27 form = SettingsForm(initial={'theme': selected_theme},
@@ -31,8 +32,8 b' class SettingsView(BaseBoardView):'
31 32 return render(request, 'boards/settings.html', context)
32 33
33 34 def post(self, request):
34 user = utils.get_user(request)
35 is_moderator = user.is_moderator()
35 settings_manager = SettingsManager(request.session)
36 is_moderator = settings_manager.has_permission(PERMISSION_MODERATE)
36 37
37 38 with transaction.atomic():
38 39 if is_moderator:
@@ -44,10 +45,11 b' class SettingsView(BaseBoardView):'
44 45 if form.is_valid():
45 46 selected_theme = form.cleaned_data['theme']
46 47
47 user.save_setting('theme', selected_theme)
48 settings_manager.set_theme(selected_theme)
48 49
49 50 if is_moderator:
50 51 moderate = form.cleaned_data['moderate']
51 user.save_setting(SETTING_MODERATE, moderate)
52 if moderate == 'True':
53 settings_manager.add_permission(PERMISSION_MODERATE)
52 54
53 55 return redirect('settings')
@@ -1,5 +1,6 b''
1 1 from django.shortcuts import get_object_or_404
2 2 from boards import utils
3 from boards.abstracts.settingsmanager import SettingsManager
3 4 from boards.models import Tag, Post
4 5 from boards.views.all_threads import AllThreadsView, DEFAULT_PAGE
5 6 from boards.views.mixins import DispatcherMixin, RedirectNextMixin
@@ -20,9 +21,14 b' class TagView(AllThreadsView, Dispatcher'
20 21 def get_context_data(self, **kwargs):
21 22 context = super(TagView, self).get_context_data(**kwargs)
22 23
24 settings_manager = SettingsManager(kwargs['request'].session)
25
23 26 tag = get_object_or_404(Tag, name=self.tag_name)
24 27 context['tag'] = tag
25 28
29 context['fav_tags'] = settings_manager.get_fav_tags()
30 context['hidden_tags'] = settings_manager.get_hidden_tags()
31
26 32 return context
27 33
28 34 def get(self, request, tag_name, page=DEFAULT_PAGE, form=None):
@@ -48,20 +54,18 b' class TagView(AllThreadsView, Dispatcher'
48 54 return self.get(request, tag_name, page, form)
49 55
50 56 def subscribe(self, request):
51 user = utils.get_user(request)
52 57 tag = get_object_or_404(Tag, name=self.tag_name)
53 58
54 if not tag in user.fav_tags.all():
55 user.add_tag(tag)
59 settings_manager = SettingsManager(request.session)
60 settings_manager.add_fav_tag(tag)
56 61
57 62 return self.redirect_to_next(request)
58 63
59 64 def unsubscribe(self, request):
60 user = utils.get_user(request)
61 65 tag = get_object_or_404(Tag, name=self.tag_name)
62 66
63 if tag in user.fav_tags.all():
64 user.remove_tag(tag)
67 settings_manager = SettingsManager(request.session)
68 settings_manager.del_fav_tag(tag)
65 69
66 70 return self.redirect_to_next(request)
67 71
@@ -71,17 +75,17 b' class TagView(AllThreadsView, Dispatcher'
71 75 shown.
72 76 """
73 77
74 user = utils.get_user(request)
75 78 tag = get_object_or_404(Tag, name=self.tag_name)
76 79
77 user.hide_tag(tag)
80 settings_manager = SettingsManager(request.session)
81 settings_manager.add_hidden_tag(tag)
78 82
79 83 def unhide(self, request):
80 84 """
81 85 Removed tag from user's hidden tags.
82 86 """
83 87
84 user = utils.get_user(request)
85 88 tag = get_object_or_404(Tag, name=self.tag_name)
86 89
87 user.unhide_tag(tag)
90 settings_manager = SettingsManager(request.session)
91 settings_manager.del_hidden_tag(tag)
@@ -128,10 +128,8 b' class ThreadView(BaseBoardView, PostMixi'
128 128
129 129 post_thread = opening_post.get_thread()
130 130
131 post = Post.objects.create_post(title=title, text=text, ip=ip,
132 thread=post_thread, image=image,
133 tags=tags,
134 user=utils.get_user(request))
131 post = Post.objects.create_post(title=title, text=text, image=image,
132 thread=post_thread, ip=ip, tags=tags)
135 133
136 134 thread_to_show = (opening_post.id if opening_post else post.id)
137 135
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now