views.py
1.93 KB
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
56
57
58
59
60
61
import json
import logging
import requests
from django.contrib import auth
from django.contrib.auth import logout
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect
from django.contrib.auth import get_user_model
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.template.context_processors import csrf
from django.conf import settings
from decrators import weapp_login_required, weapp_logout
@weapp_login_required
def homepage(request):
c = csrf(request)
username = request.COOKIES.get('username', '')
return render_to_response('homepage/index.html', c, context_instance=RequestContext(request))
def mylogin(request):
alert_msg= 0
if request.method =='GET':
cookies_username = request.COOKIES.get('username','')
c = csrf(request)
c.update({'alert_msg':alert_msg,'cookies_username:':cookies_username})
return render_to_response('login.html', c)
else:
postdata = request.POST
username = postdata.get('username','')
password = postdata.get('password','')
post_params = {
'comefrom': 2,
'user_name': username,
'password': password,
}
resp = requests.post(settings.AUTH_DOMAIN, data=post_params, verify=False)
if resp.status_code == 200:
rst = resp.json()
if rst.get('status') == 1:
data = rst.get('data')
token = data.get('token')
suid = data.get('suid')
t = HttpResponseRedirect('/')
t.set_cookie('pu', token, 864000)
t.set_cookie('pt', suid, 864000)
t.set_cookie('username', username, 86400)
return t
t = HttpResponseRedirect('/')
return t
@weapp_login_required()
@weapp_logout()
def mylogout(request):
return HttpResponseRedirect("/login/")