Added admin view stuffs

This commit is contained in:
2020-11-29 01:57:07 +01:00
parent d28cc70519
commit 116ef34a9a
5 changed files with 72 additions and 5 deletions

View File

@@ -7,12 +7,15 @@ from healthcheck import HealthCheck
from flask_cors import CORS
from flask_mail import Mail
from flask_admin import Admin
from utils import Config
from utils import health_database_status, init_security_real_good
from utils import health_database_status
from utils import init_security_real_good, user_datastore, AuthenticatedModelView, AuthenticatedAdminIndexView
from utils import storage
from views import ItemView, ProfileView, UploadView, IndexView, ContentView, PurchaseView
from models import db
from models import db, Comment, Item, Purchase, User, Role
"""
Main Flask entrypoint
@@ -51,7 +54,31 @@ for view in [ItemView, ProfileView, UploadView, IndexView, ContentView, Purchase
health.add_check(health_database_status)
app.add_url_rule("/healthz", "healthcheck", view_func=lambda: health.run())
admin = Admin(app, index_view=AuthenticatedAdminIndexView())
admin.add_view(AuthenticatedModelView(User, db.session))
admin.add_view(AuthenticatedModelView(Comment, db.session))
admin.add_view(AuthenticatedModelView(Item, db.session))
admin.add_view(AuthenticatedModelView(Purchase, db.session))
@app.before_first_request
def init_db():
db.create_all()
if Role.query.count() == 0:
user_datastore.create_role(name='administrator')
app.logger.info("Roles table is empty. Default roles created!")
default_admin_email = app.config.get('DEFAULT_ADMIN_EMAIL')
default_admin_password = app.config.get('DEFAULT_ADMIN_PASSWORD')
if default_admin_email and default_admin_password: # Create only if the default credentials are provided
if User.query.count() == 0: # Create default user, only if the user table is empty
default_admin_username = app.config.get('DEFAULT_ADMIN_USER')
user = user_datastore.create_user(email=default_admin_email, password=default_admin_password,
roles=['administrator'])
user.name = default_admin_username
db.session.add(user)
db.session.commit()