From e2137e7bb26889cdecb58fd14dabc245c965078c Mon Sep 17 00:00:00 2001 From: Aaron Kimbrell Date: Thu, 28 Mar 2019 17:49:48 -0500 Subject: [PATCH] Setup Script Database initialization `python manage.py init_db` --- app/commands/__init__.py | 4 ++ app/commands/init_db.py | 90 ++++++++++++++++++++++++++++++++++++++++ app/main.py | 5 +-- manage.py | 21 ++++++++++ 4 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 app/commands/__init__.py create mode 100644 app/commands/init_db.py create mode 100644 manage.py diff --git a/app/commands/__init__.py b/app/commands/__init__.py new file mode 100644 index 0000000..1768b05 --- /dev/null +++ b/app/commands/__init__.py @@ -0,0 +1,4 @@ +# __init__.py is a special Python file that allows a directory to become +# a Python package so it can be accessed using the 'import' statement. + +from .init_db import InitDbCommand diff --git a/app/commands/init_db.py b/app/commands/init_db.py new file mode 100644 index 0000000..05e2b8a --- /dev/null +++ b/app/commands/init_db.py @@ -0,0 +1,90 @@ +import datetime +from flask import current_app +from flask_script import Command +import pycountry + +from app import db +from app.models import User, Role + + +def init_db(): + """ Initialize the database.""" + print('Dropping all.') + db.drop_all() + print('Creating all.') + db.create_all() + print('Creating Admin User.') + create_users() + + +class InitDbCommand(Command): + """ Initialize the database.""" + + def run(self): + print('Initializeing Database.') + init_db() + print('Database has been initialized.') + + +def create_users(): + """ Create users """ + + # Create all tables + db.create_all() + + # Adding roles + admin_role = find_or_create_role('admin', u'Admin') + user_role = find_or_create_role('user', u'User') + reviewer = find_or_create_role('reviewer', u'Reviewer') + viewer = find_or_create_role('viewer', u'Viewer') + helper = find_or_create_role('helper', u'Helper') + + + # Add users + admin_user = find_or_create_user(u'Admin', u'Admin', u'Admin', u'admin@library.msstate.edu', 'Password1', u'CSE', u'net001', u'000-000-000', 1970, 1, 1, u'16623257668', u'US', u'Mississippi', u'Mississippi State', u'39762', u'395 Hardy Rd', None, admin_role) + + # Save to DB + db.session.commit() + + +def find_or_create_role(name, label): + """ Find existing role or create new role """ + role = Role.query.filter(Role.name == name).first() + if not role: + role = Role(name=name, label=label) + db.session.add(role) + return role + + +def find_or_create_user(first_name, middle_name, last_name, email, password, department, net_id, msu_id, b_year, b_month, b_day, prim_phone, country=u'US', administrative_area=u'Mississippi', locality=u'Mississippi State', postal_code=u'39762', thoroughfare=u'395 Hardy Rd', premise=None, role=None): + """ Find existing user or create new user """ + user = User.query.filter(User.email == email).first() + if not user: + b_time = datetime.datetime(b_year, b_month, b_day) + user = User(email=email, + first_name=first_name, + middle_name=middle_name, + last_name=last_name, + department=department, + net_id=net_id, + msu_id=msu_id, + birth_date=b_time.strftime('%B %d, %Y'), + prim_phone=prim_phone, + country=country, + administrative_area=administrative_area, + locality=locality, + postal_code=postal_code, + thoroughfare=thoroughfare, + premise=premise, + password=current_app.user_manager.password_manager.hash_password(password), + active=True, + email_confirmed_at=datetime.datetime.utcnow()) + if role: + user.roles.append(role) + db.session.add(user) + return user + + +if __name__ == '__main__': + init_db() + diff --git a/app/main.py b/app/main.py index 2ef8372..4f42c83 100644 --- a/app/main.py +++ b/app/main.py @@ -1,5 +1,5 @@ from flask import render_template, Blueprint, url_for, redirect, session -from flask_user import current_user +from flask_user import current_user, login_required import pycountry main_blueprint = Blueprint('main', __name__) @@ -29,7 +29,7 @@ def profile(): """Profile Page""" if current_user.pref_name == "": - full_name=current_user.first_name + ' ' + current_user.middle_name + ' ' + current_user.last_name + full_name = current_user.first_name + ' ' + current_user.middle_name + ' ' + current_user.last_name else: full_name = current_user.first_name + ' "' + current_user.pref_name + '" ' + current_user.middle_name + ' ' + current_user.last_name @@ -49,7 +49,6 @@ def profile(): postal_code=current_user.postal_code, thoroughfare=current_user.thoroughfare, premise=current_user.premise) - print(info) return render_template('main/profile.jinja2', data=info) diff --git a/manage.py b/manage.py new file mode 100644 index 0000000..fd61c35 --- /dev/null +++ b/manage.py @@ -0,0 +1,21 @@ +"""This file sets up a command line manager. +Use "python manage.py" for a list of available commands. +Use "python manage.py runserver" to start the development web server on localhost:5000. +Use "python manage.py runserver --help" for a list of runserver options. +""" + +from flask_migrate import MigrateCommand +from flask_script import Manager + +from app import create_app +from app.commands import InitDbCommand + +# Setup Flask-Script with command line commands +manager = Manager(create_app) +manager.add_command('db', MigrateCommand) +manager.add_command('init_db', InitDbCommand) + +if __name__ == "__main__": + # python manage.py # shows available commands + # python manage.py runserver --help # shows available runserver options + manager.run()