views.py 1.96 KB
import json
import logging
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 service.account.account_service import AccountService

my_user_model = get_user_model()

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('account/login.html', c)
    else:
        postdata = request.POST
        username = postdata.get('username','')
        password = postdata.get('password','')
        account_service = AccountService(my_user_model, request)
        next_url = account_service.get_login_next_url()
        if not (username and password):
            alert_msg = 1
            c = csrf(request)
            c.update({'alert_msg':alert_msg})
            return render_to_response('account/login.html', c, context_instance=RequestContext(request))
        else:
            user = account_service.user_auth_local(username, password)
            if user is not None and user.is_active:
                auth.login(request,user)
                t = HttpResponseRedirect(next_url)
                t.set_cookie('username', username, 864000)
                return t
            else:
                alert_msg = 2
                c = csrf(request)
                c.update({'alert_msg':alert_msg})
                t= render_to_response('account/login.html',c,context_instance=RequestContext(request) )
                t.set_cookie('username', username)
                return t


def mylogout(request):
    logout(request)
    return HttpResponseRedirect("/account/login")