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

UserCookieMiddleWare   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 14
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 2
Metric Value
c 4
b 0
f 2
dl 0
loc 14
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A process_request() 0 7 2
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