# HG changeset patch # User neko259 # Date 2013-08-20 19:13:54 # Node ID 34858abe0445f82ddd7fc03aca7934e2ddeb2eaf # Parent 6241f2dc60848543ceac7dd43b6c56f4cfca6b46 Added users with unique (sort of) IDs. Moved theme setting to user instead of session. This refs #61 diff --git a/boards/models.py b/boards/models.py --- a/boards/models.py +++ b/boards/models.py @@ -287,3 +287,36 @@ class Admin(models.Model): def __unicode__(self): return self.name + '/' + '*' * len(self.password) + + +class Setting(models.Model): + + name = models.CharField(max_length=50) + value = models.CharField(max_length=50) + + +class User(models.Model): + + user_id = models.CharField(max_length=20) + settings = models.ManyToManyField(Setting) + + def save_setting(self, name, value): + setting, created = self.settings.get_or_create(name=name) + setting.value = value + setting.save() + + return setting + + def get_setting(self, name): + settings = self.settings.filter(name=name) + if len(settings) > 0: + setting = settings[0] + else: + setting = None + + if setting: + setting_value = setting.value + else: + setting_value = None + + return setting_value 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 @@ -8,6 +8,10 @@ {% block content %} +
+ {% trans 'User:' %} {{ request.session.user.user_id }} +
+
{% trans "Theme" %} diff --git a/boards/views.py b/boards/views.py --- a/boards/views.py +++ b/boards/views.py @@ -1,3 +1,4 @@ +import hashlib from django.core.urlresolvers import reverse from django.template import RequestContext from django.shortcuts import render, redirect, get_object_or_404 @@ -9,7 +10,7 @@ from boards import utils from boards.forms import ThreadForm, PostForm, SettingsForm, PlainErrorList, \ ThreadCaptchaForm, PostCaptchaForm -from boards.models import Post, Admin, Tag +from boards.models import Post, Admin, Tag, User from boards import authors import neboard @@ -174,21 +175,22 @@ def logout(request): def settings(request): """User's settings""" - context = RequestContext(request) + context = _init_default_context(request) if request.method == 'POST': form = SettingsForm(request.POST) if form.is_valid(): selected_theme = form.cleaned_data['theme'] - request.session['theme'] = selected_theme + + user = _get_user(request) + user.save_setting('theme', selected_theme) return redirect(settings) else: selected_theme = _get_theme(request) form = SettingsForm(initial={'theme': selected_theme}) context['form'] = form - context['tags'] = Tag.objects.get_popular_tags() - context['theme'] = _get_theme(request) + _get_user(request) return render(request, 'boards/settings.html', context) @@ -225,7 +227,12 @@ def authors(request): def _get_theme(request): """Get user's CSS theme""" - return request.session.get('theme', neboard.settings.DEFAULT_THEME) + user = _get_user(request) + theme = user.get_setting('theme') + if not theme: + theme = neboard.settings.DEFAULT_THEME + + return theme def _get_client_ip(request): @@ -245,3 +252,23 @@ def _init_default_context(request): context['theme'] = _get_theme(request) return context + + +def _get_user(request): + """Get current user from the session""" + + session = request.session + if not 'user' in session: + request.session.save() + + md5 = hashlib.md5() + md5.update(session.session_key) + new_id = md5.hexdigest() + + user = User.objects.create(user_id=new_id) + + session['user'] = user + else: + user = session['user'] + + return user \ No newline at end of file