-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.py
61 lines (43 loc) · 1.73 KB
/
admin.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
56
57
58
59
60
61
# -*- coding: utf-8 -*-
from flask.views import MethodView
from flask import url_for
from flask import redirect, render_template, request
from models.user import User
from models.forms import LoginForm, RegisterForm
from flask.ext.login import login_user, logout_user, login_required, current_user
from flask import flash
class AdminIndexView(MethodView):
def get(self):
if current_user.is_authenticated:
return render_template('admin/index.html')
else:
return '<script>window.location.href="/login?callback=/admin/"+window.location.hash.replace(/^#/,"%23")</script>';
class LoginView(MethodView):
def get(self):
loginform = LoginForm()
callback = request.args.get('callback','/')
return render_template('login.html', **locals())
def post(self):
loginform = LoginForm(request.form)
callback = request.form.get('callback','/')
if loginform.validate():
# add login session
print u'Successfully logged in as %s' % loginform.user.username
login_user(loginform.user, remember=loginform.remember.data)
return redirect(callback)
else:
# return error
return render_template('login.html', **locals())
class LogoutView(MethodView):
def get(self):
logout_user()
return redirect(url_for('main.home'))
class RegisterView(MethodView):
def get(self):
regform = RegisterForm()
return render_template('register.html', regform=regform)
def post(self):
regform = RegisterForm(request.form)
if regform.validate():
return redirect(url_for('main.login'))
return render_template('register.html', regform=regform)