UserCookieMiddleWare   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 13
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A process_request() 0 6 2
1
import random
2
3
from chat import local
4
from chat.log_filters import id_generator
5
from django.conf import settings
6
from chat.utils import get_client_ip
7
8
9
def create_id(user_id, random=None):
10
	if user_id is None:
11
		user_id = 0
12
	if not random or len(random) != settings.WS_ID_CHAR_LENGTH:
13
		random = id_generator(settings.WS_ID_CHAR_LENGTH)
14
	return "{:04d}:{}".format(user_id, random), random
15
16
17
class UserCookieMiddleWare(object):
18
	"""
19
	Middleware to set user cookie
20
	If user is authenticated and there is no cookie, set the cookie,
21
	If the user is not authenticated and the cookie remains, delete it
22
	"""
23
24
	def process_request(self, request):
25
		try:
26
			local.random
27
		except AttributeError:
28
			local.random = create_id(getattr(request.user, 'id'))[0]
29
			local.client_ip = get_client_ip(request)
30