diff --git a/boards/abstracts/settingsmanager.py b/boards/abstracts/settingsmanager.py
new file mode 100644
--- /dev/null
+++ b/boards/abstracts/settingsmanager.py
@@ -0,0 +1,106 @@
+from django.shortcuts import get_object_or_404
+from boards.models import Tag
+
+__author__ = 'neko259'
+
+SESSION_SETTING = 'setting'
+
+PERMISSION_MODERATE = 'moderator'
+
+SETTING_THEME = 'theme'
+SETTING_FAVORITE_TAGS = 'favorite_tags'
+SETTING_HIDDEN_TAGS = 'hidden_tags'
+SETTING_PERMISSIONS = 'permissions'
+
+DEFAULT_THEME = 'md'
+
+
+class SettingsManager:
+
+ def __init__(self, session):
+ self.session = session
+
+ def get_theme(self):
+ theme = self.get_setting(SETTING_THEME)
+ if not theme:
+ theme = DEFAULT_THEME
+ self.set_setting(SETTING_THEME, theme)
+
+ return theme
+
+ def set_theme(self, theme):
+ self.set_setting(SETTING_THEME, theme)
+
+ def has_permission(self, permission):
+ permissions = self.get_setting(SETTING_PERMISSIONS)
+ if permissions:
+ return permission in permissions
+ else:
+ return False
+
+ def get_setting(self, setting):
+ if setting in self.session:
+ return self.session[setting]
+ else:
+ return None
+
+ def set_setting(self, setting, value):
+ self.session[setting] = value
+
+ def add_permission(self, permission):
+ permissions = self.get_setting(SETTING_PERMISSIONS)
+ if not permissions:
+ permissions = [permission]
+ else:
+ permissions += permission
+ self.set_setting(SETTING_PERMISSIONS, permissions)
+
+ def get_fav_tags(self):
+ tag_names = self.get_setting(SETTING_FAVORITE_TAGS)
+ tags = []
+ if tag_names:
+ for tag_name in tag_names:
+ tag = get_object_or_404(Tag, name=tag_name)
+ tags.append(tag)
+
+ return tags
+
+ def add_fav_tag(self, tag):
+ tags = self.get_setting(SETTING_FAVORITE_TAGS)
+ if not tags:
+ tags = [tag.name]
+ else:
+ if not tag.name in tags:
+ tags.append(tag.name)
+ self.set_setting(SETTING_FAVORITE_TAGS, tags)
+
+ def del_fav_tag(self, tag):
+ tags = self.get_setting(SETTING_FAVORITE_TAGS)
+ if tag.name in tags:
+ tags.remove(tag.name)
+ self.set_setting(SETTING_FAVORITE_TAGS, tags)
+
+ def get_hidden_tags(self):
+ tag_names = self.get_setting(SETTING_HIDDEN_TAGS)
+ tags = []
+ if tag_names:
+ for tag_name in tag_names:
+ tag = get_object_or_404(Tag, name=tag_name)
+ tags.append(tag)
+
+ return tags
+
+ def add_hidden_tag(self, tag):
+ tags = self.get_setting(SETTING_HIDDEN_TAGS)
+ if not tags:
+ tags = [tag.name]
+ else:
+ if not tag.name in tags:
+ tags.append(tag.name)
+ self.set_setting(SETTING_HIDDEN_TAGS, tags)
+
+ def del_hidden_tag(self, tag):
+ tags = self.get_setting(SETTING_HIDDEN_TAGS)
+ if tag.name in tags:
+ tags.remove(tag.name)
+ self.set_setting(SETTING_HIDDEN_TAGS, tags)
diff --git a/boards/admin.py b/boards/admin.py
--- a/boards/admin.py
+++ b/boards/admin.py
@@ -1,5 +1,5 @@
from django.contrib import admin
-from boards.models import Post, Tag, User, Ban, Thread
+from boards.models import Post, Tag, Ban, Thread
class PostAdmin(admin.ModelAdmin):
@@ -15,12 +15,6 @@ class TagAdmin(admin.ModelAdmin):
list_filter = ('linked',)
-class UserAdmin(admin.ModelAdmin):
-
- list_display = ('user_id', 'rank')
- search_fields = ('user_id',)
-
-
class ThreadAdmin(admin.ModelAdmin):
def title(self, obj):
@@ -35,6 +29,5 @@ class ThreadAdmin(admin.ModelAdmin):
admin.site.register(Post, PostAdmin)
admin.site.register(Tag, TagAdmin)
-admin.site.register(User, UserAdmin)
admin.site.register(Ban)
admin.site.register(Thread, ThreadAdmin)
diff --git a/boards/context_processors.py b/boards/context_processors.py
--- a/boards/context_processors.py
+++ b/boards/context_processors.py
@@ -1,8 +1,10 @@
+from boards.abstracts.settingsmanager import SettingsManager, \
+ PERMISSION_MODERATE
+
__author__ = 'neko259'
-from boards import utils, settings
+from boards import settings
from boards.models import Post
-from boards.models.post import SETTING_MODERATE
CONTEXT_SITE_NAME = 'site_name'
CONTEXT_VERSION = 'version'
@@ -17,21 +19,17 @@ CONTEXT_USER = 'user'
def user_and_ui_processor(request):
context = {}
- user = utils.get_user(request)
- context[CONTEXT_USER] = user
- context[CONTEXT_TAGS] = user.fav_tags.all()
context[CONTEXT_PPD] = float(Post.objects.get_posts_per_day())
- theme = utils.get_theme(request, user)
+ settings_manager = SettingsManager(request.session)
+ context[CONTEXT_TAGS] = settings_manager.get_fav_tags()
+ theme = settings_manager.get_theme()
context[CONTEXT_THEME] = theme
context[CONTEXT_THEME_CSS] = 'css/' + theme + '/base_page.css'
# This shows the moderator panel
- moderate = user.get_setting(SETTING_MODERATE)
- if moderate == 'True':
- context[CONTEXT_MODERATOR] = user.is_moderator()
- else:
- context[CONTEXT_MODERATOR] = False
+ moderate = settings_manager.has_permission(PERMISSION_MODERATE)
+ context[CONTEXT_MODERATOR] = moderate
context[CONTEXT_VERSION] = settings.VERSION
context[CONTEXT_SITE_NAME] = settings.SITE_NAME
diff --git a/boards/forms.py b/boards/forms.py
--- a/boards/forms.py
+++ b/boards/forms.py
@@ -9,7 +9,7 @@ from django.utils.translation import uge
from boards.mdx_neboard import formatters
from boards.models.post import TITLE_MAX_LENGTH
-from boards.models import User, PostImage
+from boards.models import PostImage
from neboard import settings
from boards import utils
import boards.settings as board_settings
@@ -187,11 +187,7 @@ class PostForm(NeboardForm):
if not 'user_id' in self.session:
return
- user = User.objects.get(id=self.session['user_id'])
- if user.is_veteran():
- posting_delay = VETERAN_POSTING_DELAY
- else:
- posting_delay = settings.POSTING_DELAY
+ posting_delay = settings.POSTING_DELAY
if board_settings.LIMIT_POSTING_SPEED and LAST_POST_TIME in \
self.session:
@@ -288,51 +284,6 @@ class ModeratorSettingsForm(SettingsForm
'panel'))
-class LoginForm(NeboardForm):
-
- user_id = forms.CharField()
-
- session = None
-
- def clean_user_id(self):
- user_id = self.cleaned_data['user_id']
- if user_id:
- users = User.objects.filter(user_id=user_id)
- if len(users) == 0:
- raise forms.ValidationError(_('No such user found'))
-
- return user_id
-
- def _validate_login_speed(self):
- can_post = True
-
- if LAST_LOGIN_TIME in self.session:
- now = time.time()
- last_login_time = self.session[LAST_LOGIN_TIME]
-
- current_delay = int(now - last_login_time)
-
- if current_delay < board_settings.LOGIN_TIMEOUT:
- error_message = _('Wait %s minutes after last login') % str(
- (board_settings.LOGIN_TIMEOUT - current_delay) / 60)
- self._errors['user_id'] = self.error_class([error_message])
-
- can_post = False
-
- if can_post:
- self.session[LAST_LOGIN_TIME] = time.time()
-
- def clean(self):
- if not self.session:
- raise forms.ValidationError('Humans have sessions')
-
- self._validate_login_speed()
-
- cleaned_data = super(LoginForm, self).clean()
-
- return cleaned_data
-
-
class AddTagForm(NeboardForm):
tag = forms.CharField(max_length=TAG_MAX_LENGTH, label=LABEL_TAG)
diff --git a/boards/management/commands/cleanusers.py b/boards/management/commands/cleanusers.py
deleted file mode 100644
--- a/boards/management/commands/cleanusers.py
+++ /dev/null
@@ -1,29 +0,0 @@
-from datetime import datetime, timedelta
-from django.core.management import BaseCommand
-from django.db import transaction
-from django.db.models import Count
-from boards.models import User, Post
-
-__author__ = 'neko259'
-
-OLD_USER_AGE_DAYS = 90
-
-
-class Command(BaseCommand):
- help = 'Removes empty users (that don\'t have posts or tags'
-
- @transaction.atomic
- def handle(self, *args, **options):
- old_registration_date = datetime.now().date() - timedelta(
- OLD_USER_AGE_DAYS)
-
- old_users = User.objects.annotate(tags_count=Count('fav_tags')).filter(
- tags_count=0).filter(registration_time__lt=old_registration_date)
- deleted_users = 0
- for user in old_users:
- if not Post.objects.filter(user=user).exists():
- self.stdout.write('Deleting user %s' % user.user_id)
- user.delete()
- deleted_users += 1
-
- self.stdout.write('Deleted %d users' % deleted_users)
\ No newline at end of file
diff --git a/boards/management/commands/enforce_privacy.py b/boards/management/commands/enforce_privacy.py
--- a/boards/management/commands/enforce_privacy.py
+++ b/boards/management/commands/enforce_privacy.py
@@ -13,4 +13,4 @@ class Command(BaseCommand):
@transaction.atomic
def handle(self, *args, **options):
- Post.objects.all().update(poster_ip=NO_IP, user=None)
\ No newline at end of file
+ Post.objects.all().update(poster_ip=NO_IP)
\ No newline at end of file
diff --git a/boards/migrations/0029_auto__del_setting__del_user__del_field_post_user.py b/boards/migrations/0029_auto__del_setting__del_user__del_field_post_user.py
new file mode 100644
--- /dev/null
+++ b/boards/migrations/0029_auto__del_setting__del_user__del_field_post_user.py
@@ -0,0 +1,146 @@
+# -*- coding: utf-8 -*-
+from south.utils import datetime_utils as datetime
+from south.db import db
+from south.v2 import SchemaMigration
+from django.db import models
+
+
+class Migration(SchemaMigration):
+
+ def forwards(self, orm):
+ # Deleting model 'Setting'
+ db.delete_table(u'boards_setting')
+
+ # Deleting model 'User'
+ db.delete_table(u'boards_user')
+
+ # Removing M2M table for field hidden_threads on 'User'
+ db.delete_table(db.shorten_name(u'boards_user_hidden_threads'))
+
+ # Removing M2M table for field fav_threads on 'User'
+ db.delete_table(db.shorten_name(u'boards_user_fav_threads'))
+
+ # Removing M2M table for field fav_tags on 'User'
+ db.delete_table(db.shorten_name(u'boards_user_fav_tags'))
+
+ # Removing M2M table for field hidden_tags on 'User'
+ db.delete_table(db.shorten_name(u'boards_user_hidden_tags'))
+
+ # Deleting field 'Post.user'
+ db.delete_column(u'boards_post', 'user_id')
+
+
+ def backwards(self, orm):
+ # Adding model 'Setting'
+ db.create_table(u'boards_setting', (
+ ('user', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['boards.User'])),
+ (u'id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
+ ('value', self.gf('django.db.models.fields.CharField')(max_length=50)),
+ ('name', self.gf('django.db.models.fields.CharField')(max_length=50)),
+ ))
+ db.send_create_signal('boards', ['Setting'])
+
+ # Adding model 'User'
+ db.create_table(u'boards_user', (
+ ('registration_time', self.gf('django.db.models.fields.DateTimeField')()),
+ (u'id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
+ ('user_id', self.gf('django.db.models.fields.CharField')(max_length=50)),
+ ('rank', self.gf('django.db.models.fields.IntegerField')()),
+ ))
+ db.send_create_signal('boards', ['User'])
+
+ # Adding M2M table for field hidden_threads on 'User'
+ m2m_table_name = db.shorten_name(u'boards_user_hidden_threads')
+ db.create_table(m2m_table_name, (
+ ('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)),
+ ('user', models.ForeignKey(orm['boards.user'], null=False)),
+ ('post', models.ForeignKey(orm['boards.post'], null=False))
+ ))
+ db.create_unique(m2m_table_name, ['user_id', 'post_id'])
+
+ # Adding M2M table for field fav_threads on 'User'
+ m2m_table_name = db.shorten_name(u'boards_user_fav_threads')
+ db.create_table(m2m_table_name, (
+ ('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)),
+ ('user', models.ForeignKey(orm['boards.user'], null=False)),
+ ('post', models.ForeignKey(orm['boards.post'], null=False))
+ ))
+ db.create_unique(m2m_table_name, ['user_id', 'post_id'])
+
+ # Adding M2M table for field fav_tags on 'User'
+ m2m_table_name = db.shorten_name(u'boards_user_fav_tags')
+ db.create_table(m2m_table_name, (
+ ('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)),
+ ('user', models.ForeignKey(orm['boards.user'], null=False)),
+ ('tag', models.ForeignKey(orm['boards.tag'], null=False))
+ ))
+ db.create_unique(m2m_table_name, ['user_id', 'tag_id'])
+
+ # Adding M2M table for field hidden_tags on 'User'
+ m2m_table_name = db.shorten_name(u'boards_user_hidden_tags')
+ db.create_table(m2m_table_name, (
+ ('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)),
+ ('user', models.ForeignKey(orm['boards.user'], null=False)),
+ ('tag', models.ForeignKey(orm['boards.tag'], null=False))
+ ))
+ db.create_unique(m2m_table_name, ['user_id', 'tag_id'])
+
+ # Adding field 'Post.user'
+ db.add_column(u'boards_post', 'user',
+ self.gf('django.db.models.fields.related.ForeignKey')(default=None, to=orm['boards.User'], null=True),
+ keep_default=False)
+
+
+ models = {
+ 'boards.ban': {
+ 'Meta': {'object_name': 'Ban'},
+ 'can_read': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+ u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'ip': ('django.db.models.fields.GenericIPAddressField', [], {'max_length': '39'}),
+ 'reason': ('django.db.models.fields.CharField', [], {'default': "'Auto'", 'max_length': '200'})
+ },
+ 'boards.post': {
+ 'Meta': {'ordering': "('id',)", 'object_name': 'Post'},
+ '_text_rendered': ('django.db.models.fields.TextField', [], {}),
+ u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'images': ('django.db.models.fields.related.ManyToManyField', [], {'related_name': "'ip+'", 'to': "orm['boards.PostImage']", 'blank': 'True', 'symmetrical': 'False', 'null': 'True', 'db_index': 'True'}),
+ 'last_edit_time': ('django.db.models.fields.DateTimeField', [], {}),
+ 'poster_ip': ('django.db.models.fields.GenericIPAddressField', [], {'max_length': '39'}),
+ 'poster_user_agent': ('django.db.models.fields.TextField', [], {}),
+ 'pub_time': ('django.db.models.fields.DateTimeField', [], {}),
+ 'referenced_posts': ('django.db.models.fields.related.ManyToManyField', [], {'related_name': "'rfp+'", 'to': "orm['boards.Post']", 'blank': 'True', 'symmetrical': 'False', 'null': 'True', 'db_index': 'True'}),
+ 'refmap': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}),
+ 'text': ('markupfield.fields.MarkupField', [], {'rendered_field': 'True'}),
+ 'text_markup_type': ('django.db.models.fields.CharField', [], {'default': "'markdown'", 'max_length': '30'}),
+ 'thread_new': ('django.db.models.fields.related.ForeignKey', [], {'default': 'None', 'to': "orm['boards.Thread']", 'null': 'True'}),
+ 'title': ('django.db.models.fields.CharField', [], {'max_length': '200'})
+ },
+ 'boards.postimage': {
+ 'Meta': {'ordering': "('id',)", 'object_name': 'PostImage'},
+ 'hash': ('django.db.models.fields.CharField', [], {'max_length': '36'}),
+ 'height': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
+ u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'image': ('boards.thumbs.ImageWithThumbsField', [], {'max_length': '100', 'blank': 'True'}),
+ 'pre_height': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
+ 'pre_width': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
+ 'width': ('django.db.models.fields.IntegerField', [], {'default': '0'})
+ },
+ 'boards.tag': {
+ 'Meta': {'ordering': "('name',)", 'object_name': 'Tag'},
+ u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'linked': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['boards.Tag']", 'null': 'True', 'blank': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '100', 'db_index': 'True'}),
+ 'threads': ('django.db.models.fields.related.ManyToManyField', [], {'blank': 'True', 'related_name': "'tag+'", 'null': 'True', 'symmetrical': 'False', 'to': "orm['boards.Thread']"})
+ },
+ 'boards.thread': {
+ 'Meta': {'object_name': 'Thread'},
+ 'archived': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'bump_time': ('django.db.models.fields.DateTimeField', [], {}),
+ u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'last_edit_time': ('django.db.models.fields.DateTimeField', [], {}),
+ 'replies': ('django.db.models.fields.related.ManyToManyField', [], {'blank': 'True', 'related_name': "'tre+'", 'null': 'True', 'symmetrical': 'False', 'to': "orm['boards.Post']"}),
+ 'tags': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['boards.Tag']", 'symmetrical': 'False'})
+ }
+ }
+
+ complete_apps = ['boards']
\ No newline at end of file
diff --git a/boards/models/__init__.py b/boards/models/__init__.py
--- a/boards/models/__init__.py
+++ b/boards/models/__init__.py
@@ -5,5 +5,3 @@ from boards.models.thread import Thread
from boards.models.post import Post
from boards.models.tag import Tag
from boards.models.user import Ban
-from boards.models.user import Setting
-from boards.models.user import User
diff --git a/boards/models/post.py b/boards/models/post.py
--- a/boards/models/post.py
+++ b/boards/models/post.py
@@ -45,8 +45,8 @@ logger = logging.getLogger(__name__)
class PostManager(models.Manager):
- def create_post(self, title, text, image=None, thread=None,
- ip=NO_IP, tags=None, user=None):
+ def create_post(self, title, text, image=None, thread=None, ip=NO_IP,
+ tags=None):
"""
Creates new post
"""
@@ -69,8 +69,7 @@ class PostManager(models.Manager):
poster_ip=ip,
poster_user_agent=UNKNOWN_UA, # TODO Get UA at
# last!
- last_edit_time=posting_time,
- user=user)
+ last_edit_time=posting_time)
if image:
post_image = PostImage.objects.create(image=image)
@@ -196,7 +195,6 @@ class Post(models.Model, Viewable):
thread_new = models.ForeignKey('Thread', null=True, default=None,
db_index=True)
last_edit_time = models.DateTimeField()
- user = models.ForeignKey('User', null=True, default=None, db_index=True)
referenced_posts = models.ManyToManyField('Post', symmetrical=False,
null=True,
diff --git a/boards/models/user.py b/boards/models/user.py
--- a/boards/models/user.py
+++ b/boards/models/user.py
@@ -1,122 +1,10 @@
from django.db import models
-from django.db.models import Count
-from boards import settings
-from boards.models import Post
-from django.core.cache import cache
__author__ = 'neko259'
-RANK_ADMIN = 0
-RANK_MODERATOR = 10
-RANK_USER = 100
-
BAN_REASON_AUTO = 'Auto'
BAN_REASON_MAX_LENGTH = 200
-VETERAN_POSTS = 1000
-
-
-class User(models.Model):
-
- class Meta:
- app_label = 'boards'
-
- user_id = models.CharField(max_length=50)
- rank = models.IntegerField()
-
- registration_time = models.DateTimeField()
-
- fav_tags = models.ManyToManyField('Tag', null=True, blank=True)
- fav_threads = models.ManyToManyField(Post, related_name='+', null=True,
- blank=True)
-
- hidden_tags = models.ManyToManyField('Tag', null=True, blank=True,
- related_name='ht+')
- hidden_threads = models.ManyToManyField('Post', null=True, blank=True,
- related_name='hth+')
-
- def save_setting(self, name, value):
- setting, created = Setting.objects.get_or_create(name=name, user=self)
- setting.value = str(value)
- setting.save()
-
- return setting
-
- def get_setting(self, name):
- if Setting.objects.filter(name=name, user=self).exists():
- setting = Setting.objects.get(name=name, user=self)
- setting_value = setting.value
- else:
- setting_value = None
-
- return setting_value
-
- def is_moderator(self):
- return RANK_MODERATOR >= self.rank
-
- def get_sorted_fav_tags(self):
- cache_key = self._get_tag_cache_key()
- fav_tags = cache.get(cache_key)
- if fav_tags:
- return fav_tags
-
- tags = self.fav_tags.annotate(Count('threads')) \
- .filter(threads__count__gt=0).order_by('name')
-
- if tags:
- cache.set(cache_key, tags)
-
- return tags
-
- def get_post_count(self):
- return Post.objects.filter(user=self).count()
-
- def __unicode__(self):
- return self.user_id + '(' + str(self.rank) + ')'
-
- def get_last_access_time(self):
- """
- Gets user's last post time.
- """
-
- posts = Post.objects.filter(user=self)
- if posts.exists() > 0:
- return posts.latest('pub_time').pub_time
-
- def add_tag(self, tag):
- self.fav_tags.add(tag)
- cache.delete(self._get_tag_cache_key())
-
- def remove_tag(self, tag):
- self.fav_tags.remove(tag)
- cache.delete(self._get_tag_cache_key())
-
- def hide_tag(self, tag):
- self.hidden_tags.add(tag)
-
- def unhide_tag(self, tag):
- self.hidden_tags.remove(tag)
-
- def is_veteran(self):
- """
- Returns if a user is old (veteran).
- """
-
- return self.get_post_count() >= VETERAN_POSTS
-
- def _get_tag_cache_key(self):
- return self.user_id + '_tags'
-
-
-class Setting(models.Model):
-
- class Meta:
- app_label = 'boards'
-
- name = models.CharField(max_length=50)
- value = models.CharField(max_length=50)
- user = models.ForeignKey(User)
-
class Ban(models.Model):
diff --git a/boards/templates/boards/base.html b/boards/templates/boards/base.html
--- a/boards/templates/boards/base.html
+++ b/boards/templates/boards/base.html
@@ -46,7 +46,6 @@
{% block metapanel %}{% endblock %}
- [
{% trans 'Login' %}]
[
{% trans 'Search' %}]
{% with ppd=posts_per_day|floatformat:2 %}
{% blocktrans %}Speed: {{ ppd }} posts per day{% endblocktrans %}
diff --git a/boards/templates/boards/posting_general.html b/boards/templates/boards/posting_general.html
--- a/boards/templates/boards/posting_general.html
+++ b/boards/templates/boards/posting_general.html
@@ -44,14 +44,14 @@
{% if tag %}
- {% if tag in user.fav_tags.all %}
+ {% if tag in fav_tags %}
★
{% else %}
★
{% endif %}
- {% if tag in user.hidden_tags.all %}
+ {% if tag in hidden_tags %}
H
diff --git a/boards/templates/boards/settings.html b/boards/templates/boards/settings.html
--- a/boards/templates/boards/settings.html
+++ b/boards/templates/boards/settings.html
@@ -11,20 +11,11 @@
- {% trans 'User:' %} {{ user.user_id }}.
{% if user.is_moderator %}
{% trans 'You are moderator.' %}
{% endif %}
- {% if user.is_veteran %}
- {% trans 'You are veteran.' %}
- {% endif %}
-
{% trans 'Posts:' %} {{ user.get_post_count }}
-
{% trans 'First access:' %} {{ user.registration_time|naturaltime }}
- {% if user.get_last_access_time %}
-
{% trans 'Last access:' %} {{ user.get_last_access_time|naturaltime }}
- {% endif %}
- {% with hidden_tags=user.hidden_tags.all %}
+ {% with hidden_tags=hidden_tags %}
{% if hidden_tags %}
{% trans 'Hidden tags:' %}
{% for tag in hidden_tags %}
diff --git a/boards/tests.py b/boards/tests.py
--- a/boards/tests.py
+++ b/boards/tests.py
@@ -30,8 +30,7 @@ logger = logging.getLogger(__name__)
class PostTests(TestCase):
def _create_post(self):
- return Post.objects.create_post(title='title',
- text='text')
+ return Post.objects.create_post(title='title', text='text')
def test_post_add(self):
"""Test adding post"""
diff --git a/boards/urls.py b/boards/urls.py
--- a/boards/urls.py
+++ b/boards/urls.py
@@ -2,7 +2,7 @@ from django.conf.urls import patterns, u
from boards import views
from boards.rss import AllThreadsFeed, TagThreadsFeed, ThreadPostsFeed
from boards.views import api, tag_threads, all_threads, \
- login, settings, all_tags
+ settings, all_tags
from boards.views.authors import AuthorsView
from boards.views.delete_post import DeletePostView
from boards.views.ban import BanUserView
@@ -22,9 +22,6 @@ urlpatterns = patterns('',
url(r'^page/(?P\w+)/$', all_threads.AllThreadsView.as_view(),
name='index'),
- # login page
- url(r'^login/$', login.LoginView.as_view(), name='login'),
-
# /boards/tag/tag_name/
url(r'^tag/(?P\w+)/$', tag_threads.TagView.as_view(),
name='tag'),
diff --git a/boards/utils.py b/boards/utils.py
--- a/boards/utils.py
+++ b/boards/utils.py
@@ -5,11 +5,8 @@ import hashlib
import time
from django.utils import timezone
-import boards
from neboard import settings
-from boards.models import User
-from boards.models.user import RANK_USER
KEY_CAPTCHA_FAILS = 'key_captcha_fails'
@@ -82,48 +79,4 @@ def get_client_ip(request):
def datetime_to_epoch(datetime):
return int(time.mktime(timezone.localtime(
datetime,timezone.get_current_timezone()).timetuple())
- * 1000000 + datetime.microsecond)
-
-
-def get_user(request):
- """
- Get current user from the session. If the user does not exist, create
- a new one.
- """
-
- session = request.session
- if not 'user_id' in session:
- request.session.save()
-
- md5 = hashlib.md5()
- md5.update(session.session_key)
- new_id = md5.hexdigest()
-
- while User.objects.filter(user_id=new_id).exists():
- md5.update(str(timezone.now()))
- new_id = md5.hexdigest()
-
- time_now = timezone.now()
- user = User.objects.create(user_id=new_id, rank=RANK_USER,
- registration_time=time_now)
-
- session['user_id'] = user.id
- else:
- user = User.objects.select_related('fav_tags').get(
- id=session['user_id'])
-
- return user
-
-
-def get_theme(request, user=None):
- """
- Get user's CSS theme
- """
-
- if not user:
- user = get_user(request)
- theme = user.get_setting('theme')
- if not theme:
- theme = boards.settings.DEFAULT_THEME
-
- return theme
\ No newline at end of file
+ * 1000000 + datetime.microsecond)
\ No newline at end of file
diff --git a/boards/views/all_threads.py b/boards/views/all_threads.py
--- a/boards/views/all_threads.py
+++ b/boards/views/all_threads.py
@@ -5,6 +5,7 @@ from django.shortcuts import render, red
from boards import utils, settings
from boards.abstracts.paginator import get_paginator
+from boards.abstracts.settingsmanager import SettingsManager
from boards.forms import ThreadForm, PlainErrorList
from boards.models import Post, Thread, Ban, Tag
from boards.views.banned import BannedView
@@ -28,16 +29,17 @@ DEFAULT_PAGE = 1
class AllThreadsView(PostMixin, BaseBoardView):
- user = None
+ def __init__(self):
+ self.settings_manager = None
+ super(AllThreadsView, self).__init__()
def get(self, request, page=DEFAULT_PAGE, form=None):
context = self.get_context_data(request=request)
- self.user = utils.get_user(request)
-
if not form:
form = ThreadForm(error_class=PlainErrorList)
+ self.settings_manager = SettingsManager(request.session)
paginator = get_paginator(self.get_threads(),
settings.THREADS_PER_PAGE)
paginator.current_page = int(page)
@@ -122,9 +124,8 @@ class AllThreadsView(PostMixin, BaseBoar
tags = self.parse_tags_string(tag_strings)
- post = Post.objects.create_post(title=title, text=text, ip=ip,
- image=image, tags=tags,
- user=utils.get_user(request))
+ post = Post.objects.create_post(title=title, text=text, image=image,
+ ip=ip, tags=tags)
if html_response:
return redirect(post.get_url())
@@ -135,4 +136,4 @@ class AllThreadsView(PostMixin, BaseBoar
"""
return Thread.objects.all().order_by('-bump_time')\
- .exclude(tags__in=self.user.hidden_tags.all())
+ .exclude(tags__in=self.settings_manager.get_hidden_tags())
diff --git a/boards/views/ban.py b/boards/views/ban.py
--- a/boards/views/ban.py
+++ b/boards/views/ban.py
@@ -1,7 +1,8 @@
from django.db import transaction
from django.shortcuts import get_object_or_404
-from boards import utils
+from boards.abstracts.settingsmanager import SettingsManager, \
+ PERMISSION_MODERATE
from boards.views.base import BaseBoardView
from boards.models import Post, Ban
from boards.views.mixins import RedirectNextMixin
@@ -11,10 +12,11 @@ class BanUserView(BaseBoardView, Redirec
@transaction.atomic
def get(self, request, post_id):
- user = utils.get_user(request)
post = get_object_or_404(Post, id=post_id)
- if user.is_moderator():
+ settings_manager = SettingsManager(request.session)
+
+ if settings_manager.has_permission(PERMISSION_MODERATE):
# TODO Show confirmation page before ban
ban, created = Ban.objects.get_or_create(ip=post.poster_ip)
if created:
diff --git a/boards/views/delete_post.py b/boards/views/delete_post.py
--- a/boards/views/delete_post.py
+++ b/boards/views/delete_post.py
@@ -1,7 +1,8 @@
from django.shortcuts import redirect, get_object_or_404
from django.db import transaction
-from boards import utils
+from boards.abstracts.settingsmanager import SettingsManager, \
+ PERMISSION_MODERATE
from boards.views.base import BaseBoardView
from boards.views.mixins import RedirectNextMixin
from boards.models import Post
@@ -11,12 +12,12 @@ class DeletePostView(BaseBoardView, Redi
@transaction.atomic
def get(self, request, post_id):
- user = utils.get_user(request)
post = get_object_or_404(Post, id=post_id)
opening_post = post.is_opening()
- if user.is_moderator():
+ settings_manager = SettingsManager(request.session)
+ if settings_manager.has_permission(PERMISSION_MODERATE):
# TODO Show confirmation page before deletion
Post.objects.delete_post(post)
diff --git a/boards/views/login.py b/boards/views/login.py
deleted file mode 100644
--- a/boards/views/login.py
+++ /dev/null
@@ -1,30 +0,0 @@
-from django.shortcuts import render, redirect
-from boards.forms import LoginForm, PlainErrorList
-from boards.models import User
-from boards.views.base import BaseBoardView, CONTEXT_FORM
-
-__author__ = 'neko259'
-
-
-class LoginView(BaseBoardView):
-
- def get(self, request, form=None):
- context = self.get_context_data(request=request)
-
- if not form:
- form = LoginForm()
- context[CONTEXT_FORM] = form
-
- return render(request, 'boards/login.html', context)
-
- def post(self, request):
- form = LoginForm(request.POST, request.FILES,
- error_class=PlainErrorList)
- form.session = request.session
-
- if form.is_valid():
- user = User.objects.get(user_id=form.cleaned_data['user_id'])
- request.session['user_id'] = user.id
- return redirect('index')
- else:
- return self.get(request, form)
diff --git a/boards/views/post_admin.py b/boards/views/post_admin.py
--- a/boards/views/post_admin.py
+++ b/boards/views/post_admin.py
@@ -1,4 +1,6 @@
from django.shortcuts import render, get_object_or_404, redirect
+from boards.abstracts.settingsmanager import SettingsManager, \
+ PERMISSION_MODERATE
from boards.views.base import BaseBoardView
from boards.views.mixins import DispatcherMixin
@@ -6,11 +8,12 @@ from boards.models.post import Post
from boards.models.tag import Tag
from boards.forms import AddTagForm, PlainErrorList
+
class PostAdminView(BaseBoardView, DispatcherMixin):
def get(self, request, post_id, form=None):
- user = utils.get_user(request)
- if not user.is_moderator:
+ settings_manager = SettingsManager(request.session)
+ if not settings_manager.has_permission(PERMISSION_MODERATE):
redirect('index')
post = get_object_or_404(Post, id=post_id)
@@ -30,8 +33,8 @@ class PostAdminView(BaseBoardView, Dispa
return render(request, 'boards/post_admin.html', context)
def post(self, request, post_id):
- user = utils.get_user(request)
- if not user.is_moderator:
+ settings_manager = SettingsManager(request.session)
+ if not settings_manager.has_permission(PERMISSION_MODERATE):
redirect('index')
post = get_object_or_404(Post, id=post_id)
diff --git a/boards/views/settings.py b/boards/views/settings.py
--- a/boards/views/settings.py
+++ b/boards/views/settings.py
@@ -1,6 +1,8 @@
from django.db import transaction
from django.shortcuts import render, redirect
from boards import utils
+from boards.abstracts.settingsmanager import SettingsManager, \
+ PERMISSION_MODERATE
from boards.views.base import BaseBoardView, CONTEXT_FORM
from boards.forms import SettingsForm, ModeratorSettingsForm, PlainErrorList
@@ -11,16 +13,15 @@ class SettingsView(BaseBoardView):
def get(self, request):
context = self.get_context_data(request=request)
- user = utils.get_user(request)
- is_moderator = user.is_moderator()
+ settings_manager = SettingsManager(request.session)
+ is_moderator = settings_manager.has_permission(PERMISSION_MODERATE)
- selected_theme = utils.get_theme(request, user)
+ selected_theme = settings_manager.get_theme()
if is_moderator:
form = ModeratorSettingsForm(initial={
'theme': selected_theme,
- 'moderate': user.get_setting(SETTING_MODERATE) and \
- user.is_moderator()
+ 'moderate': settings_manager.has_permission(PERMISSION_MODERATE)
}, error_class=PlainErrorList)
else:
form = SettingsForm(initial={'theme': selected_theme},
@@ -31,8 +32,8 @@ class SettingsView(BaseBoardView):
return render(request, 'boards/settings.html', context)
def post(self, request):
- user = utils.get_user(request)
- is_moderator = user.is_moderator()
+ settings_manager = SettingsManager(request.session)
+ is_moderator = settings_manager.has_permission(PERMISSION_MODERATE)
with transaction.atomic():
if is_moderator:
@@ -44,10 +45,11 @@ class SettingsView(BaseBoardView):
if form.is_valid():
selected_theme = form.cleaned_data['theme']
- user.save_setting('theme', selected_theme)
+ settings_manager.set_theme(selected_theme)
if is_moderator:
moderate = form.cleaned_data['moderate']
- user.save_setting(SETTING_MODERATE, moderate)
+ if moderate == 'True':
+ settings_manager.add_permission(PERMISSION_MODERATE)
return redirect('settings')
diff --git a/boards/views/tag_threads.py b/boards/views/tag_threads.py
--- a/boards/views/tag_threads.py
+++ b/boards/views/tag_threads.py
@@ -1,5 +1,6 @@
from django.shortcuts import get_object_or_404
from boards import utils
+from boards.abstracts.settingsmanager import SettingsManager
from boards.models import Tag, Post
from boards.views.all_threads import AllThreadsView, DEFAULT_PAGE
from boards.views.mixins import DispatcherMixin, RedirectNextMixin
@@ -20,9 +21,14 @@ class TagView(AllThreadsView, Dispatcher
def get_context_data(self, **kwargs):
context = super(TagView, self).get_context_data(**kwargs)
+ settings_manager = SettingsManager(kwargs['request'].session)
+
tag = get_object_or_404(Tag, name=self.tag_name)
context['tag'] = tag
+ context['fav_tags'] = settings_manager.get_fav_tags()
+ context['hidden_tags'] = settings_manager.get_hidden_tags()
+
return context
def get(self, request, tag_name, page=DEFAULT_PAGE, form=None):
@@ -48,20 +54,18 @@ class TagView(AllThreadsView, Dispatcher
return self.get(request, tag_name, page, form)
def subscribe(self, request):
- user = utils.get_user(request)
tag = get_object_or_404(Tag, name=self.tag_name)
- if not tag in user.fav_tags.all():
- user.add_tag(tag)
+ settings_manager = SettingsManager(request.session)
+ settings_manager.add_fav_tag(tag)
return self.redirect_to_next(request)
def unsubscribe(self, request):
- user = utils.get_user(request)
tag = get_object_or_404(Tag, name=self.tag_name)
- if tag in user.fav_tags.all():
- user.remove_tag(tag)
+ settings_manager = SettingsManager(request.session)
+ settings_manager.del_fav_tag(tag)
return self.redirect_to_next(request)
@@ -71,17 +75,17 @@ class TagView(AllThreadsView, Dispatcher
shown.
"""
- user = utils.get_user(request)
tag = get_object_or_404(Tag, name=self.tag_name)
- user.hide_tag(tag)
+ settings_manager = SettingsManager(request.session)
+ settings_manager.add_hidden_tag(tag)
def unhide(self, request):
"""
Removed tag from user's hidden tags.
"""
- user = utils.get_user(request)
tag = get_object_or_404(Tag, name=self.tag_name)
- user.unhide_tag(tag)
+ settings_manager = SettingsManager(request.session)
+ settings_manager.del_hidden_tag(tag)
diff --git a/boards/views/thread.py b/boards/views/thread.py
--- a/boards/views/thread.py
+++ b/boards/views/thread.py
@@ -128,10 +128,8 @@ class ThreadView(BaseBoardView, PostMixi
post_thread = opening_post.get_thread()
- post = Post.objects.create_post(title=title, text=text, ip=ip,
- thread=post_thread, image=image,
- tags=tags,
- user=utils.get_user(request))
+ post = Post.objects.create_post(title=title, text=text, image=image,
+ thread=post_thread, ip=ip, tags=tags)
thread_to_show = (opening_post.id if opening_post else post.id)