-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.py
49 lines (39 loc) · 1.46 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
42
43
44
45
46
47
48
49
import uuid
import logging
from django.conf import settings
if settings.HAS_XMPP:
from conversejs.models import XMPPAccount
else:
from .models import XMPPAccount
from django.contrib import messages
from .core import create_user
L = logging.getLogger(__name__)
def create_xmpp_account(request, user):
'''
Create a user's XMPP account if it doesn't already exist.
'''
if user.is_active and not user.xmpp.first():
username = user.username
password = str(uuid.uuid4())
jid = create_user(username, password, user.get_full_name(),
user.email)
if jid:
xmpp_account = XMPPAccount.objects.create(user=user, jid=jid,
password=password)
messages.add_message(request, messages.SUCCESS,
'Successfully created a XMPP account.')
else:
xmpp_account = None
messages.add_message(request, messages.WARNING,
'Failed to create a XMPP account.')
L.error('Could not create a XMPP account for %s.', user)
return xmpp_account
class UserXMPPMiddleware(object):
def process_request(self, request):
'''
On each page request, create a user's XMPP account if it doesn't already exist.
'''
if request.path.startswith('/admin'):
return
user = request.user
create_xmpp_account(request, user)