mirror of
https://github.com/cwinfo/powerdns-admin.git
synced 2025-01-07 19:05:39 +00:00
Merge pull request #18 from ivanfilippov/db_improvements
Fixes issue #11.
This commit is contained in:
commit
454dce2d71
15
README.md
15
README.md
@ -58,21 +58,6 @@ Create database after having proper configs
|
|||||||
(flask)% ./createdb.py
|
(flask)% ./createdb.py
|
||||||
```
|
```
|
||||||
|
|
||||||
Manually add some data into our `powerdnsadmin` database
|
|
||||||
```
|
|
||||||
$ mysql
|
|
||||||
|
|
||||||
MariaDB [(none)]> use powerdnsadmin;
|
|
||||||
|
|
||||||
MariaDB [powerdnsadmin]> INSERT INTO role(name, description) VALUES ('Administrator', 'Administrator');
|
|
||||||
Query OK, 1 row affected (0.00 sec)
|
|
||||||
|
|
||||||
MariaDB [powerdnsadmin]> INSERT INTO role(name, description) VALUES ('User', 'User');
|
|
||||||
Query OK, 1 row affected (0.01 sec)
|
|
||||||
|
|
||||||
MariaDB [powerdnsadmin]> INSERT INTO setting(name, value) VALUES('maintenance', 'False');
|
|
||||||
Query OK, 1 row affected (0.00 sec)
|
|
||||||
```
|
|
||||||
|
|
||||||
Run the application and enjoy!
|
Run the application and enjoy!
|
||||||
```
|
```
|
||||||
|
@ -187,9 +187,9 @@ class User(db.Model):
|
|||||||
|
|
||||||
# first register user will be in Administrator role
|
# first register user will be in Administrator role
|
||||||
if User.query.count() == 0:
|
if User.query.count() == 0:
|
||||||
self.role_id = 1
|
self.role_id = Role.query.filter_by(name='Administrator').first().id
|
||||||
else:
|
else:
|
||||||
self.role_id = 2
|
self.role_id = Role.query.filter_by(name='User').first().id
|
||||||
|
|
||||||
self.create_user()
|
self.create_user()
|
||||||
logging.info('Created user "%s" in the DB' % self.username)
|
logging.info('Created user "%s" in the DB' % self.username)
|
||||||
@ -230,9 +230,9 @@ class User(db.Model):
|
|||||||
try:
|
try:
|
||||||
# first register user will be in Administrator role
|
# first register user will be in Administrator role
|
||||||
if User.query.count() == 0:
|
if User.query.count() == 0:
|
||||||
self.role_id = 1
|
self.role_id = Role.query.filter_by(name='Administrator').first().id
|
||||||
else:
|
else:
|
||||||
self.role_id = 2
|
self.role_id = Role.query.filter_by(name='User').first().id
|
||||||
|
|
||||||
user = User(username=self.username, firstname=self.firstname, lastname=self.lastname, role_id=self.role_id, email=self.email, password=self.get_hashed_password(self.plain_text_password))
|
user = User(username=self.username, firstname=self.firstname, lastname=self.lastname, role_id=self.role_id, email=self.email, password=self.get_hashed_password(self.plain_text_password))
|
||||||
db.session.add(user)
|
db.session.add(user)
|
||||||
@ -340,10 +340,16 @@ class Role(db.Model):
|
|||||||
description = db.Column(db.String(128))
|
description = db.Column(db.String(128))
|
||||||
users = db.relationship('User', backref='role', lazy='dynamic')
|
users = db.relationship('User', backref='role', lazy='dynamic')
|
||||||
|
|
||||||
def __int__(self, id=None, name=None, description=None):
|
def __init__(self, id=None, name=None, description=None):
|
||||||
self.id = id
|
self.id = id
|
||||||
self.name = name
|
self.name = name
|
||||||
self.description = description
|
self.description = description
|
||||||
|
|
||||||
|
# allow database autoincrement to do its own ID assignments
|
||||||
|
def __init__(self, name=None, description=None):
|
||||||
|
self.id = None
|
||||||
|
self.name = name
|
||||||
|
self.description = description
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<Role %r>' % (self.name)
|
return '<Role %r>' % (self.name)
|
||||||
@ -910,6 +916,12 @@ class Setting(db.Model):
|
|||||||
self.id = id
|
self.id = id
|
||||||
self.name = name
|
self.name = name
|
||||||
self.value = value
|
self.value = value
|
||||||
|
|
||||||
|
# allow database autoincrement to do its own ID assignments
|
||||||
|
def __init__(self, name=None, value=None):
|
||||||
|
self.id = None
|
||||||
|
self.name = name
|
||||||
|
self.value = value
|
||||||
|
|
||||||
def set_mainteance(self, mode):
|
def set_mainteance(self, mode):
|
||||||
"""
|
"""
|
||||||
|
@ -3,8 +3,17 @@ from migrate.versioning import api
|
|||||||
from config import SQLALCHEMY_DATABASE_URI
|
from config import SQLALCHEMY_DATABASE_URI
|
||||||
from config import SQLALCHEMY_MIGRATE_REPO
|
from config import SQLALCHEMY_MIGRATE_REPO
|
||||||
from app import db
|
from app import db
|
||||||
|
from app.models import Role, Setting
|
||||||
import os.path
|
import os.path
|
||||||
db.create_all()
|
db.create_all()
|
||||||
|
# create initial user roles and turn off maintenance mode
|
||||||
|
admin_role = Role('Administrator', 'Administrator')
|
||||||
|
user_role = Role('User', 'User')
|
||||||
|
maintenance_setting = Setting('maintenance', 'False')
|
||||||
|
db.session.add(admin_role)
|
||||||
|
db.session.add(user_role)
|
||||||
|
db.session.add(maintenance_setting)
|
||||||
|
db.session.commit()
|
||||||
if not os.path.exists(SQLALCHEMY_MIGRATE_REPO):
|
if not os.path.exists(SQLALCHEMY_MIGRATE_REPO):
|
||||||
api.create(SQLALCHEMY_MIGRATE_REPO, 'database repository')
|
api.create(SQLALCHEMY_MIGRATE_REPO, 'database repository')
|
||||||
api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
|
api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
|
||||||
|
Loading…
Reference in New Issue
Block a user