Skip to content

Commit 1be3dcb

Browse files
committed
create admin user before first request
1 parent c225b58 commit 1be3dcb

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ If you encounter any errors, please open an issue or contact us on slack in #oc-
121121
}'
122122
```
123123

124+
## Admin Panel Access
125+
126+
This project has an admin panel which can be used to view and manually edit categories and languages at a higher level than the API allows.
127+
In order to create admin user, set following environment variables before starting the application -
128+
`ADMIN_EMAIL` and `ADMIN_PASSWORD`
129+
124130
## Development Notes
125131

126132
If you make changes to the models.py or other schemas, you need to run a migration and upgrade again:

migrations/versions/824f1576e904_.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
# revision identifiers, used by Alembic.
1414
revision = '824f1576e904'
15-
down_revision = '205742d3b3f5'
15+
down_revision = 'fc34137ad3ba'
1616
branch_labels = None
1717
depends_on = None
1818

run.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from app import app, cli, admin
2+
import os
23
from app.models import Category, Language, Resource, db, User, Role
3-
from flask_security import Security, SQLAlchemyUserDatastore
4+
from flask_security import Security, SQLAlchemyUserDatastore, utils
45
from werkzeug.middleware.dispatcher import DispatcherMiddleware
56
from prometheus_client import make_wsgi_app
67

@@ -23,4 +24,35 @@
2324

2425
@app.shell_context_processor
2526
def make_shell_context():
26-
return {'db': db, 'Resource': Resource, 'Category': Category, 'Language': Language}
27+
return {'db': db, 'Resource': Resource, 'Category': Category,
28+
'Language': Language, 'User': User, 'Role': Role}
29+
30+
# Create Admin user and role.
31+
@app.before_first_request
32+
def before_first_request():
33+
# Create any database tables that don't exist yet.
34+
db.create_all()
35+
36+
# Create the Roles "admin" and "user" -- unless they already exist
37+
user_datastore.find_or_create_role(name='admin', description='Administrator')
38+
user_datastore.find_or_create_role(name='user', description='End user')
39+
40+
admin_email = os.environ.get('ADMIN_EMAIL', '[email protected]')
41+
admin_password = os.environ.get('ADMIN_PASSWORD', 'password')
42+
43+
# Create two Users for testing purposes -- unless they already exists.
44+
# In each case, use Flask-Security utility function to encrypt the password.
45+
encrypted_password = utils.encrypt_password(admin_password)
46+
if not user_datastore.get_user(admin_email):
47+
user_datastore.create_user(admin_email, password=encrypted_password)
48+
# Add more users.
49+
50+
# Commit any database changes; the User and Roles must exist before we
51+
# can add a Role to the User
52+
db.session.commit()
53+
54+
# Give one User has the "end-user" role, while the other has the "admin"
55+
# role. (This will have no effect if the
56+
# Users already have these Roles.) Again, commit any database changes.
57+
user_datastore.add_role_to_user(admin_email, 'admin')
58+
db.session.commit()

0 commit comments

Comments
 (0)