forked from hovel/pybbm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.py
41 lines (33 loc) · 1.54 KB
/
middleware.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import django
from django.utils import translation
from django.db.models import ObjectDoesNotExist
from pybb import util
if django.VERSION < (1, 10): # pragma: no cover
MiddlewareParentClass = object
else: # pragma: no cover
from django.utils.deprecation import MiddlewareMixin
MiddlewareParentClass = MiddlewareMixin
class PybbMiddleware(MiddlewareParentClass):
def process_request(self, request):
if request.user.is_authenticated():
try:
# Here we try to load profile, but can get error
# if user created during syncdb but profile model
# under south control. (Like pybb.Profile).
profile = util.get_pybb_profile(request.user)
except ObjectDoesNotExist:
# Ok, we should create new profile for this user
# and grant permissions for add posts
# It should be caused rarely, so we move import signal here
# to prevent circular import
from pybb.signals import user_saved
user_saved(request.user, created=True)
profile = util.get_pybb_profile(request.user)
if not profile.language:
profile.language = translation.get_language_from_request(request)
profile.save()
request.session['django_language'] = profile.language
translation.activate(profile.language)
request.LANGUAGE_CODE = translation.get_language()