Show More
@@ -287,3 +287,36 b' class Admin(models.Model):' | |||||
287 |
|
287 | |||
288 | def __unicode__(self): |
|
288 | def __unicode__(self): | |
289 | return self.name + '/' + '*' * len(self.password) |
|
289 | return self.name + '/' + '*' * len(self.password) | |
|
290 | ||||
|
291 | ||||
|
292 | class Setting(models.Model): | |||
|
293 | ||||
|
294 | name = models.CharField(max_length=50) | |||
|
295 | value = models.CharField(max_length=50) | |||
|
296 | ||||
|
297 | ||||
|
298 | class User(models.Model): | |||
|
299 | ||||
|
300 | user_id = models.CharField(max_length=20) | |||
|
301 | settings = models.ManyToManyField(Setting) | |||
|
302 | ||||
|
303 | def save_setting(self, name, value): | |||
|
304 | setting, created = self.settings.get_or_create(name=name) | |||
|
305 | setting.value = value | |||
|
306 | setting.save() | |||
|
307 | ||||
|
308 | return setting | |||
|
309 | ||||
|
310 | def get_setting(self, name): | |||
|
311 | settings = self.settings.filter(name=name) | |||
|
312 | if len(settings) > 0: | |||
|
313 | setting = settings[0] | |||
|
314 | else: | |||
|
315 | setting = None | |||
|
316 | ||||
|
317 | if setting: | |||
|
318 | setting_value = setting.value | |||
|
319 | else: | |||
|
320 | setting_value = None | |||
|
321 | ||||
|
322 | return setting_value |
@@ -8,6 +8,10 b'' | |||||
8 |
|
8 | |||
9 | {% block content %} |
|
9 | {% block content %} | |
10 |
|
10 | |||
|
11 | <div class="post"> | |||
|
12 | {% trans 'User:' %} <b>{{ request.session.user.user_id }}</b> | |||
|
13 | </div> | |||
|
14 | ||||
11 | <div class="post-form-w"> |
|
15 | <div class="post-form-w"> | |
12 | <div class="post-form"> |
|
16 | <div class="post-form"> | |
13 | <span class="form-title">{% trans "Theme" %}</span> |
|
17 | <span class="form-title">{% trans "Theme" %}</span> |
@@ -1,3 +1,4 b'' | |||||
|
1 | import hashlib | |||
1 | from django.core.urlresolvers import reverse |
|
2 | from django.core.urlresolvers import reverse | |
2 | from django.template import RequestContext |
|
3 | from django.template import RequestContext | |
3 | from django.shortcuts import render, redirect, get_object_or_404 |
|
4 | from django.shortcuts import render, redirect, get_object_or_404 | |
@@ -9,7 +10,7 b' from boards import utils' | |||||
9 | from boards.forms import ThreadForm, PostForm, SettingsForm, PlainErrorList, \ |
|
10 | from boards.forms import ThreadForm, PostForm, SettingsForm, PlainErrorList, \ | |
10 | ThreadCaptchaForm, PostCaptchaForm |
|
11 | ThreadCaptchaForm, PostCaptchaForm | |
11 |
|
12 | |||
12 | from boards.models import Post, Admin, Tag |
|
13 | from boards.models import Post, Admin, Tag, User | |
13 | from boards import authors |
|
14 | from boards import authors | |
14 | import neboard |
|
15 | import neboard | |
15 |
|
16 | |||
@@ -174,21 +175,22 b' def logout(request):' | |||||
174 | def settings(request): |
|
175 | def settings(request): | |
175 | """User's settings""" |
|
176 | """User's settings""" | |
176 |
|
177 | |||
177 |
context = |
|
178 | context = _init_default_context(request) | |
178 |
|
179 | |||
179 | if request.method == 'POST': |
|
180 | if request.method == 'POST': | |
180 | form = SettingsForm(request.POST) |
|
181 | form = SettingsForm(request.POST) | |
181 | if form.is_valid(): |
|
182 | if form.is_valid(): | |
182 | selected_theme = form.cleaned_data['theme'] |
|
183 | selected_theme = form.cleaned_data['theme'] | |
183 | request.session['theme'] = selected_theme |
|
184 | ||
|
185 | user = _get_user(request) | |||
|
186 | user.save_setting('theme', selected_theme) | |||
184 |
|
187 | |||
185 | return redirect(settings) |
|
188 | return redirect(settings) | |
186 | else: |
|
189 | else: | |
187 | selected_theme = _get_theme(request) |
|
190 | selected_theme = _get_theme(request) | |
188 | form = SettingsForm(initial={'theme': selected_theme}) |
|
191 | form = SettingsForm(initial={'theme': selected_theme}) | |
189 | context['form'] = form |
|
192 | context['form'] = form | |
190 | context['tags'] = Tag.objects.get_popular_tags() |
|
193 | _get_user(request) | |
191 | context['theme'] = _get_theme(request) |
|
|||
192 |
|
194 | |||
193 | return render(request, 'boards/settings.html', context) |
|
195 | return render(request, 'boards/settings.html', context) | |
194 |
|
196 | |||
@@ -225,7 +227,12 b' def authors(request):' | |||||
225 | def _get_theme(request): |
|
227 | def _get_theme(request): | |
226 | """Get user's CSS theme""" |
|
228 | """Get user's CSS theme""" | |
227 |
|
229 | |||
228 | return request.session.get('theme', neboard.settings.DEFAULT_THEME) |
|
230 | user = _get_user(request) | |
|
231 | theme = user.get_setting('theme') | |||
|
232 | if not theme: | |||
|
233 | theme = neboard.settings.DEFAULT_THEME | |||
|
234 | ||||
|
235 | return theme | |||
229 |
|
236 | |||
230 |
|
237 | |||
231 | def _get_client_ip(request): |
|
238 | def _get_client_ip(request): | |
@@ -245,3 +252,23 b' def _init_default_context(request):' | |||||
245 | context['theme'] = _get_theme(request) |
|
252 | context['theme'] = _get_theme(request) | |
246 |
|
253 | |||
247 | return context |
|
254 | return context | |
|
255 | ||||
|
256 | ||||
|
257 | def _get_user(request): | |||
|
258 | """Get current user from the session""" | |||
|
259 | ||||
|
260 | session = request.session | |||
|
261 | if not 'user' in session: | |||
|
262 | request.session.save() | |||
|
263 | ||||
|
264 | md5 = hashlib.md5() | |||
|
265 | md5.update(session.session_key) | |||
|
266 | new_id = md5.hexdigest() | |||
|
267 | ||||
|
268 | user = User.objects.create(user_id=new_id) | |||
|
269 | ||||
|
270 | session['user'] = user | |||
|
271 | else: | |||
|
272 | user = session['user'] | |||
|
273 | ||||
|
274 | return user No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now