forked from NoGameNoLife00/mybolg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.py
133 lines (98 loc) · 4.3 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#encoding=utf-8
from flask import url_for, redirect, request
from flask.ext.login import current_user, login_user, logout_user
from flask.ext.admin import Admin, BaseView, expose, helpers
from flask.ext.admin.contrib.sqla import ModelView
from flask.ext.admin.base import AdminIndexView
from werkzeug.security import generate_password_hash
from blogapp import app, db
from data_model import Tag, User, Category, Entry, Friend_link
from forms import *
from config import REGISTRATION_CODE, ARTICLE_EDITOR
from widgets import SimpleMDEAreaField, CKTextAreaField
from flask.ext.admin.contrib.fileadmin import FileAdmin
import os.path as op
if ARTICLE_EDITOR == "ckeditor":
EDITOR_WIDGET = CKTextAreaField
else:
EDITOR_WIDGET = SimpleMDEAreaField
class EntryAdmin(ModelView):
create_template = 'admin/create.html'
edit_template = 'admin/edit.html'
form_overrides = dict(content=EDITOR_WIDGET)
# Visible columns in the list view
column_exclude_list = ['content', 'content_html', 'status', 'modified_time', 'author']
form_excluded_columns = ('content_html',)
# List of columns that can be sorted. For 'user' column, use User.username as
column_sortable_list = ('category', 'create_time')
# Rename 'title' columns to 'Post Title' in list view
column_labels = dict(title='Entry Title')
column_searchable_list = ('title', User.nickname)
column_filters = ('title', 'create_time', 'category')
def __init__(self, session):
super(EntryAdmin, self).__init__(Entry, db.session)
def is_accessible(self):
return current_user.is_authenticated()
class UserAdmin(ModelView):
column_exclude_list = ['password', 'Role']
def __init__(self, session):
super(UserAdmin, self).__init__(User, db.session)
def is_accessible(self):
return current_user.is_authenticated()
class MyAdminView(ModelView):
form_overrides = dict(content=EDITOR_WIDGET)
create_template = 'admin/create.html'
edit_template = 'admin/edit.html'
def is_accessible(self):
return current_user.is_authenticated()
class MyAdminIndexView(AdminIndexView):
@expose('/')
def index(self):
if not current_user.is_authenticated():
return redirect(url_for('.login_view'))
return super(MyAdminIndexView, self).index()
@expose('/login/', methods=('GET', 'POST'))
def login_view(self):
# handle user login
form = LoginForm(request.form)
if helpers.validate_form_on_submit(form):
user = form.get_user()
login_user(user)
if current_user.is_authenticated():
return redirect(url_for('.index'))
link = '<p>Don\'t have an account? <a href="' + url_for('.register_view') + '">Click here to register.</a></p>'
self._template_args['form'] = form
self._template_args['link'] = link
return super(MyAdminIndexView, self).index()
@expose('/register/', methods=('GET', 'POST'))
def register_view(self):
form = RegistrationForm(request.form)
if helpers.validate_form_on_submit(form):
user = User()
form.populate_obj(user)
# we hash the users password to avoid saving it as plaintext in the db,
# remove to use plain text:
# user.password = generate_password_hash(form.password.data)
db.session.add(user)
db.session.commit()
login_user(user)
return redirect(url_for('.index'))
link = '<p>Already have an account? <a href="' + url_for('.login_view') + '">Click here to log in.</a></p>'
self._template_args['form'] = form
self._template_args['link'] = link
return super(MyAdminIndexView, self).index()
@expose('/logout/')
def logout_view(self):
logout_user()
return redirect(url_for('.index'))
# Initialize flask-login
init_login()
admin = Admin(app, url='/blog/admin', index_view=MyAdminIndexView(), base_template='/admin/master.html')
admin.add_view(MyAdminView(Category, db.session))
admin.add_view(MyAdminView(Tag, db.session))
admin.add_view(EntryAdmin(db.session))
admin.add_view(UserAdmin(db.session))
admin.add_view(MyAdminView(Friend_link, db.session))
# file manage
# path = op.join(op.dirname(__file__), 'static')
# admin.add_view(FileAdmin(path, '/static/', name='Static Files'))