-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
55 lines (47 loc) · 1.74 KB
/
common.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
50
51
52
53
54
55
from django.conf import settings
from django.http import HttpResponseRedirect, HttpResponse, JsonResponse
## helper functions common to all views
##
def __redirect(request, url):
if request.is_ajax():
return JsonResponse({'status':302, 'url': url})
return HttpResponseRedirect(url)
## decorator function check for request method, if not GET redirect to invalid reguest page.
##
def get_required(funct):
def _decorator(request, *args, **kwargs):
if request.method != 'GET':
return __redirect(request, settings.INVALID_REQUEST_URL)
#if 'application/json' in request.META.get('HTTP_ACCEPT'):
else:
return funct(request, *args, **kwargs)
return _decorator;
## decorator function check for request method, if not POST redirect to invalid reguest page.
##
def post_required(funct):
def _decorator(request, *args, **kwargs):
if request.method != 'POST':
return __redirect(request, settings.INVALID_REQUEST_URL)
#if 'application/json' in request.META.get('HTTP_ACCEPT'):
else:
return funct(request, *args, **kwargs)
return _decorator;
## Decorator function for chekcing the login status and redirect to login page
##
def login_required(funct):
#@warps(funct)
def _decorator(request, *args, **kwargs):
if request.user.is_loggedin() == False:
return __redirect(request, settings.USER_LOGIN_URL)
#if 'application/json' in request.META.get('HTTP_ACCEPT'):
else:
return funct(request, *args, **kwargs)
return _decorator;
## Decorator function for chekcing the login status and redirect to home page
##
def redirect_if_loggedin(funct):
def _decorator(request, *args, **kwargs):
if request.user.is_loggedin() == True:
return __redirect(request, settings.USER_PROFILE_URL)
return funct(request, *args, **kwargs)
return _decorator