views.py 1.93 KB
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/")