contentdb/utils/setup.py

49 lines
1.5 KiB
Python
Raw Normal View History

2020-07-12 17:34:25 +02:00
# ContentDB
2018-05-17 16:18:20 +02:00
# Copyright (C) 2018 rubenwardy
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
2019-11-12 23:36:30 +01:00
import os, sys, datetime, inspect
2018-03-18 18:43:30 +01:00
2018-03-24 03:49:30 +01:00
if not "FLASK_CONFIG" in os.environ:
os.environ["FLASK_CONFIG"] = "../config.cfg"
2019-01-09 22:58:11 +01:00
delete_db = len(sys.argv) >= 2 and sys.argv[1].strip() == "-d"
create_db = not (len(sys.argv) >= 2 and sys.argv[1].strip() == "-o")
test_data = len(sys.argv) >= 2 and sys.argv[1].strip() == "-t" or not create_db
2018-03-19 19:08:41 +01:00
2019-11-12 23:36:30 +01:00
# Allow finding the `app` module
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
2020-01-19 16:03:38 +01:00
sys.path.insert(0,parentdir)
2018-05-27 22:31:11 +02:00
2020-01-19 16:03:38 +01:00
from app.models import db, User, UserRank
from app.default_data import populate, populate_test_data
2018-05-27 22:31:11 +02:00
if delete_db and os.path.isfile("db.sqlite"):
os.remove("db.sqlite")
2019-01-09 22:58:11 +01:00
if create_db:
print("Creating database tables...")
db.create_all()
print("Filling database...")
2020-01-19 20:09:04 +01:00
2020-01-19 16:03:38 +01:00
populate(db.session)
if test_data:
2020-01-19 20:09:04 +01:00
populate_test_data(db.session)
db.session.commit()