Completed
Push — master ( 8d3325...7753d5 )
by Andrew
01:03 queued 24s
created

create_id()   A

Complexity

Conditions 4

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
c 1
b 0
f 0
dl 0
loc 6
rs 9.2
1
import random
2
3
from chat import local
4
from chat.log_filters import id_generator
5
from chat.settings import WS_ID_CHAR_LENGTH
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) != WS_ID_CHAR_LENGTH:
13
		random = id_generator(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