Completed
Push — master ( 6ca66a...a573ac )
by Andrew
01:29
created

UserCookieMiddleWare.process_request()   A

Complexity

Conditions 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 2
Metric Value
cc 2
c 4
b 0
f 2
dl 0
loc 7
rs 9.4285
1
import random
2
3
from chat import local
4
from chat.utils import get_client_ip
5
6
7
class UserCookieMiddleWare(object):
8
	"""
9
	Middleware to set user cookie
10
	If user is authenticated and there is no cookie, set the cookie,
11
	If the user is not authenticated and the cookie remains, delete it
12
	"""
13
14
	def process_request(self, request):
15
		try:
16
			local.random
17
		except AttributeError:
18
			local.random = str(random.randint(0, 10000)).rjust(4, '0')
19
			local.user = str(getattr(request.user, 'username', '')).rjust(8, ' ')
20
			local.client_ip = get_client_ip(request)
21