Browse Source

refactor: Db model completely reviewed

develop
Mindiell 1 year ago
parent
commit
3faa1689e7
  1. 20
      app/model/address.py
  2. 4
      app/model/contact.py
  3. 6
      app/model/country.py
  4. 2
      app/model/decision.py
  5. 10
      app/model/entity.py
  6. 4
      app/model/matter.py
  7. 4
      app/model/membership.py
  8. 6
      app/model/model.py
  9. 8
      app/model/recommendation.py
  10. 6
      app/model/representative.py
  11. 6
      app/model/role.py
  12. 6
      app/model/type.py
  13. 7
      migrations/alembic.ini
  14. 11
      migrations/env.py
  15. 89
      migrations/versions/6ce21d3cdf0e_.py
  16. 28
      migrations/versions/8b260b23ba3a_.py
  17. 28
      migrations/versions/ac34a07322f6_.py
  18. 30
      migrations/versions/b83b29dc60b0_.py
  19. 2
      requirements.txt

20
app/model/address.py

@ -7,21 +7,21 @@ from app.model.model import Model, View
class AddressModel(db.Model, Model):
__tablename__ = "address"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(2000))
slug = db.Column(db.String(2000))
name = db.Column(db.String(200))
slug = db.Column(db.String(200))
country_id = db.Column(db.Integer, db.ForeignKey("country.id"))
country = db.relationship("CountryModel")
number = db.Column(db.String(2000))
number = db.Column(db.String(20))
street = db.Column(db.String(2000))
miscellaneous = db.Column(db.String(2000))
city = db.Column(db.String(2000))
zipcode = db.Column(db.String(2000))
building = db.Column(db.String(2000))
floor = db.Column(db.String(2000))
stair = db.Column(db.String(2000))
office = db.Column(db.String(2000))
latitude = db.Column(db.String(2000))
longitude = db.Column(db.String(2000))
zipcode = db.Column(db.String(20))
building = db.Column(db.String(20))
floor = db.Column(db.String(20))
stair = db.Column(db.String(20))
office = db.Column(db.String(20))
latitude = db.Column(db.String(20))
longitude = db.Column(db.String(20))
class AdminView(View):

4
app/model/contact.py

@ -15,8 +15,8 @@ class ContactModel(db.Model, Model):
address = db.relationship(
"AddressModel", backref=db.backref("contacts", lazy="dynamic")
)
name = db.Column(db.String(2000))
slug = db.Column(db.String(2000))
name = db.Column(db.String(200))
slug = db.Column(db.String(200))
value = db.Column(db.String(2000))
def __repr__(self):

6
app/model/country.py

@ -7,9 +7,9 @@ from app.model.model import Model, View
class CountryModel(db.Model, Model):
__tablename__ = "country"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(2000))
slug = db.Column(db.String(2000))
code = db.Column(db.String(2000))
name = db.Column(db.String(200))
slug = db.Column(db.String(200))
code = db.Column(db.String(20))
@property
def flag(self):

2
app/model/decision.py

@ -15,7 +15,7 @@ class DecisionModel(db.Model, Model):
recommendation = db.relationship(
"RecommendationModel", backref=db.backref("decisions", lazy="dynamic")
)
value = db.Column(db.String(2000))
value = db.Column(db.String(200))
def __repr__(self):
return self.value

10
app/model/entity.py

@ -9,14 +9,14 @@ from app.model.model import Model, View
class EntityModel(db.Model, Model):
__tablename__ = "entity"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(2000))
slug = db.Column(db.String(2000))
code = db.Column(db.String(2000))
name = db.Column(db.String(200))
slug = db.Column(db.String(200))
code = db.Column(db.String(20))
picture = db.Column(db.String(2000))
type_id = db.Column(db.Integer, db.ForeignKey("type.id"))
type = db.relationship("TypeModel")
start = db.Column(db.DateTime)
end = db.Column(db.DateTime)
start = db.Column(db.Date)
end = db.Column(db.Date)
country_id = db.Column(db.Integer, db.ForeignKey("country.id"))
country = db.relationship("CountryModel")
parent_id = db.Column(db.Integer, db.ForeignKey("entity.id"))

4
app/model/matter.py

@ -9,8 +9,8 @@ from app.model.model import Model, View
class MatterModel(db.Model, Model):
__tablename__ = "matter"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(2000))
slug = db.Column(db.String(2000), unique=True)
name = db.Column(db.String(200))
slug = db.Column(db.String(200), unique=True)
description = db.Column(db.Text)
active = db.Column(db.Boolean, default=False)

4
app/model/membership.py

@ -15,8 +15,8 @@ class MembershipModel(db.Model, Model):
role = db.relationship(
"RoleModel", backref=db.backref("memberships", lazy="dynamic")
)
start = db.Column(db.DateTime)
end = db.Column(db.DateTime)
start = db.Column(db.Date)
end = db.Column(db.Date)
entity_id = db.Column(db.Integer, db.ForeignKey("entity.id"))
entity = db.relationship(
"EntityModel", backref=db.backref("memberships", lazy="dynamic")

6
app/model/model.py

@ -11,14 +11,14 @@ from app import db
class View(ModelView):
def is_accessible(self):
# TODO: develop mode
# TODO: develop mode, refer to current_app.config.debug
return True
if not current_user.is_authenticated:
return False
return current_user.is_authenticated and current_user.admin
def inaccessible_callback(self, name, **kwargs):
# TODO: develop mode
# TODO: develop mode, refer to current_app.config.debug
return redirect(url_for("core.login"))
return redirect(url_for("admin.login", next=request.url))
@ -55,4 +55,4 @@ class Model:
result_dict[key] = getattr(self, key).strftime("%Y-%m-%d")
else:
result_dict[key] = getattr(self, key)
return result_dict
return result_dict

8
app/model/recommendation.py

@ -17,12 +17,12 @@ class RecommendationModel(db.Model, Model):
entity = db.relationship(
"EntityModel", backref=db.backref("recommendations", lazy="dynamic")
)
name = db.Column(db.String(2000))
slug = db.Column(db.String(2000), unique=True)
code = db.Column(db.String(2000))
name = db.Column(db.String(200))
slug = db.Column(db.String(200))
code = db.Column(db.String(20))
date = db.Column(db.Date)
description = db.Column(db.Text)
value = db.Column(db.String(2000))
value = db.Column(db.String(200))
weight = db.Column(db.Integer, default=1)
def __repr__(self):

6
app/model/representative.py

@ -8,9 +8,9 @@ from app.model.country import CountryModel
class RepresentativeModel(db.Model, Model):
__tablename__ = "representative"
id = db.Column(db.Integer, primary_key=True)
code = db.Column(db.String(2000), unique=True)
name = db.Column(db.String(2000))
slug = db.Column(db.String(2000))
code = db.Column(db.String(20), unique=True)
name = db.Column(db.String(200))
slug = db.Column(db.String(200))
active = db.Column(db.Boolean, default=False)
picture = db.Column(db.String(2000))
nationality_id = db.Column(db.Integer, db.ForeignKey("country.id"))

6
app/model/role.py

@ -7,9 +7,9 @@ from app.model.model import Model, View
class RoleModel(db.Model, Model):
__tablename__ = "role"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(2000))
slug = db.Column(db.String(2000))
code = db.Column(db.String(2000))
name = db.Column(db.String(200))
slug = db.Column(db.String(200))
code = db.Column(db.String(20))
def __repr__(self):
return self.name

6
app/model/type.py

@ -9,9 +9,9 @@ from app.model.model import Model, View
class TypeModel(db.Model, Model):
__tablename__ = "type"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(2000))
slug = db.Column(db.String(2000))
code = db.Column(db.String(2000))
name = db.Column(db.String(200))
slug = db.Column(db.String(200))
code = db.Column(db.String(20))
def __repr__(self):
return self.name

7
migrations/alembic.ini

@ -11,7 +11,7 @@
# Logging configuration
[loggers]
keys = root,sqlalchemy,alembic
keys = root,sqlalchemy,alembic,flask_migrate
[handlers]
keys = console
@ -34,6 +34,11 @@ level = INFO
handlers =
qualname = alembic
[logger_flask_migrate]
level = INFO
handlers =
qualname = flask_migrate
[handler_console]
class = StreamHandler
args = (sys.stderr,)

11
migrations/env.py

@ -3,8 +3,6 @@ from __future__ import with_statement
import logging
from logging.config import fileConfig
from sqlalchemy import engine_from_config
from sqlalchemy import pool
from flask import current_app
from alembic import context
@ -24,7 +22,8 @@ logger = logging.getLogger('alembic.env')
# target_metadata = mymodel.Base.metadata
config.set_main_option(
'sqlalchemy.url',
str(current_app.extensions['migrate'].db.engine.url).replace('%', '%%'))
str(current_app.extensions['migrate'].db.get_engine().url).replace(
'%', '%%'))
target_metadata = current_app.extensions['migrate'].db.metadata
# other values from the config, defined by the needs of env.py,
@ -72,11 +71,7 @@ def run_migrations_online():
directives[:] = []
logger.info('No changes in schema detected.')
connectable = engine_from_config(
config.get_section(config.config_ini_section),
prefix='sqlalchemy.',
poolclass=pool.NullPool,
)
connectable = current_app.extensions['migrate'].db.get_engine()
with connectable.connect() as connection:
context.configure(

89
migrations/versions/e0001237a466_.py → migrations/versions/6ce21d3cdf0e_.py

@ -1,8 +1,8 @@
"""empty message
Revision ID: e0001237a466
Revision ID: 6ce21d3cdf0e
Revises:
Create Date: 2021-07-16 19:43:05.321519
Create Date: 2021-07-24 09:44:44.681657
"""
from alembic import op
@ -10,7 +10,7 @@ import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'e0001237a466'
revision = '6ce21d3cdf0e'
down_revision = None
branch_labels = None
depends_on = None
@ -20,15 +20,15 @@ def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('country',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=2000), nullable=True),
sa.Column('slug', sa.String(length=2000), nullable=True),
sa.Column('code', sa.String(length=2000), nullable=True),
sa.Column('name', sa.String(length=200), nullable=True),
sa.Column('slug', sa.String(length=200), nullable=True),
sa.Column('code', sa.String(length=20), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_table('matter',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=2000), nullable=True),
sa.Column('slug', sa.String(length=2000), nullable=True),
sa.Column('name', sa.String(length=200), nullable=True),
sa.Column('slug', sa.String(length=200), nullable=True),
sa.Column('description', sa.Text(), nullable=True),
sa.Column('active', sa.Boolean(), nullable=True),
sa.PrimaryKeyConstraint('id'),
@ -36,46 +36,46 @@ def upgrade():
)
op.create_table('role',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=2000), nullable=True),
sa.Column('slug', sa.String(length=2000), nullable=True),
sa.Column('code', sa.String(length=2000), nullable=True),
sa.Column('name', sa.String(length=200), nullable=True),
sa.Column('slug', sa.String(length=200), nullable=True),
sa.Column('code', sa.String(length=20), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_table('type',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=2000), nullable=True),
sa.Column('slug', sa.String(length=2000), nullable=True),
sa.Column('code', sa.String(length=2000), nullable=True),
sa.Column('name', sa.String(length=200), nullable=True),
sa.Column('slug', sa.String(length=200), nullable=True),
sa.Column('code', sa.String(length=20), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_table('address',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=2000), nullable=True),
sa.Column('slug', sa.String(length=2000), nullable=True),
sa.Column('name', sa.String(length=200), nullable=True),
sa.Column('slug', sa.String(length=200), nullable=True),
sa.Column('country_id', sa.Integer(), nullable=True),
sa.Column('number', sa.String(length=2000), nullable=True),
sa.Column('number', sa.String(length=20), nullable=True),
sa.Column('street', sa.String(length=2000), nullable=True),
sa.Column('miscellaneous', sa.String(length=2000), nullable=True),
sa.Column('city', sa.String(length=2000), nullable=True),
sa.Column('zipcode', sa.String(length=2000), nullable=True),
sa.Column('building', sa.String(length=2000), nullable=True),
sa.Column('floor', sa.String(length=2000), nullable=True),
sa.Column('stair', sa.String(length=2000), nullable=True),
sa.Column('office', sa.String(length=2000), nullable=True),
sa.Column('latitude', sa.String(length=2000), nullable=True),
sa.Column('longitude', sa.String(length=2000), nullable=True),
sa.Column('zipcode', sa.String(length=20), nullable=True),
sa.Column('building', sa.String(length=20), nullable=True),
sa.Column('floor', sa.String(length=20), nullable=True),
sa.Column('stair', sa.String(length=20), nullable=True),
sa.Column('office', sa.String(length=20), nullable=True),
sa.Column('latitude', sa.String(length=20), nullable=True),
sa.Column('longitude', sa.String(length=20), nullable=True),
sa.ForeignKeyConstraint(['country_id'], ['country.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('entity',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=2000), nullable=True),
sa.Column('slug', sa.String(length=2000), nullable=True),
sa.Column('code', sa.String(length=2000), nullable=True),
sa.Column('name', sa.String(length=200), nullable=True),
sa.Column('slug', sa.String(length=200), nullable=True),
sa.Column('code', sa.String(length=20), nullable=True),
sa.Column('picture', sa.String(length=2000), nullable=True),
sa.Column('type_id', sa.Integer(), nullable=True),
sa.Column('start', sa.DateTime(), nullable=True),
sa.Column('end', sa.DateTime(), nullable=True),
sa.Column('start', sa.Date(), nullable=True),
sa.Column('end', sa.Date(), nullable=True),
sa.Column('country_id', sa.Integer(), nullable=True),
sa.Column('parent_id', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['country_id'], ['country.id'], ),
@ -85,8 +85,10 @@ def upgrade():
)
op.create_table('representative',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=2000), nullable=True),
sa.Column('slug', sa.String(length=2000), nullable=True),
sa.Column('code', sa.String(length=20), nullable=True),
sa.Column('name', sa.String(length=200), nullable=True),
sa.Column('slug', sa.String(length=200), nullable=True),
sa.Column('active', sa.Boolean(), nullable=True),
sa.Column('picture', sa.String(length=2000), nullable=True),
sa.Column('nationality_id', sa.Integer(), nullable=True),
sa.Column('sex', sa.String(length=1), nullable=True),
@ -95,14 +97,14 @@ def upgrade():
sa.Column('job', sa.String(length=2000), nullable=True),
sa.ForeignKeyConstraint(['nationality_id'], ['country.id'], ),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('slug')
sa.UniqueConstraint('code')
)
op.create_table('contact',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('representative_id', sa.Integer(), nullable=True),
sa.Column('address_id', sa.Integer(), nullable=True),
sa.Column('name', sa.String(length=2000), nullable=True),
sa.Column('slug', sa.String(length=2000), nullable=True),
sa.Column('name', sa.String(length=200), nullable=True),
sa.Column('slug', sa.String(length=200), nullable=True),
sa.Column('value', sa.String(length=2000), nullable=True),
sa.ForeignKeyConstraint(['address_id'], ['address.id'], ),
sa.ForeignKeyConstraint(['representative_id'], ['representative.id'], ),
@ -112,8 +114,8 @@ def upgrade():
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('representative_id', sa.Integer(), nullable=True),
sa.Column('role_id', sa.Integer(), nullable=True),
sa.Column('start', sa.DateTime(), nullable=True),
sa.Column('end', sa.DateTime(), nullable=True),
sa.Column('start', sa.Date(), nullable=True),
sa.Column('end', sa.Date(), nullable=True),
sa.Column('entity_id', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['entity_id'], ['entity.id'], ),
sa.ForeignKeyConstraint(['representative_id'], ['representative.id'], ),
@ -124,23 +126,22 @@ def upgrade():
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('matter_id', sa.Integer(), nullable=True),
sa.Column('entity_id', sa.Integer(), nullable=True),
sa.Column('name', sa.String(length=2000), nullable=True),
sa.Column('slug', sa.String(length=2000), nullable=True),
sa.Column('code', sa.String(length=2000), nullable=True),
sa.Column('name', sa.String(length=200), nullable=True),
sa.Column('slug', sa.String(length=200), nullable=True),
sa.Column('code', sa.String(length=20), nullable=True),
sa.Column('date', sa.Date(), nullable=True),
sa.Column('description', sa.Text(), nullable=True),
sa.Column('value', sa.String(length=2000), nullable=True),
sa.Column('value', sa.String(length=200), nullable=True),
sa.Column('weight', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['entity_id'], ['entity.id'], ),
sa.ForeignKeyConstraint(['matter_id'], ['matter.id'], ),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('slug')
sa.PrimaryKeyConstraint('id')
)
op.create_table('stance',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('representative_id', sa.Integer(), nullable=True),
sa.Column('matter_id', sa.Integer(), nullable=True),
sa.Column('date', sa.DateTime(), nullable=True),
sa.Column('date', sa.Date(), nullable=True),
sa.Column('subject', sa.String(length=2000), nullable=True),
sa.Column('extract', sa.Text(), nullable=True),
sa.Column('source_url', sa.String(length=2000), nullable=True),
@ -153,7 +154,7 @@ def upgrade():
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('representative_id', sa.Integer(), nullable=True),
sa.Column('recommendation_id', sa.Integer(), nullable=True),
sa.Column('value', sa.String(length=2000), nullable=True),
sa.Column('value', sa.String(length=200), nullable=True),
sa.ForeignKeyConstraint(['recommendation_id'], ['recommendation.id'], ),
sa.ForeignKeyConstraint(['representative_id'], ['representative.id'], ),
sa.PrimaryKeyConstraint('id')

28
migrations/versions/8b260b23ba3a_.py

@ -1,28 +0,0 @@
"""empty message
Revision ID: 8b260b23ba3a
Revises: e0001237a466
Create Date: 2021-07-21 14:03:53.023739
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '8b260b23ba3a'
down_revision = 'e0001237a466'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('representative', sa.Column('code', sa.String(length=2000), nullable=True))
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('representative', 'code')
# ### end Alembic commands ###

28
migrations/versions/ac34a07322f6_.py

@ -1,28 +0,0 @@
"""empty message
Revision ID: ac34a07322f6
Revises: b83b29dc60b0
Create Date: 2021-07-21 15:36:01.454538
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'ac34a07322f6'
down_revision = 'b83b29dc60b0'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('representative', sa.Column('active', sa.Boolean(), nullable=True))
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('representative', 'active')
# ### end Alembic commands ###

30
migrations/versions/b83b29dc60b0_.py

@ -1,30 +0,0 @@
"""empty message
Revision ID: b83b29dc60b0
Revises: 8b260b23ba3a
Create Date: 2021-07-21 14:04:54.107335
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'b83b29dc60b0'
down_revision = '8b260b23ba3a'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint('representative_slug_key', 'representative', type_='unique')
op.create_unique_constraint(None, 'representative', ['code'])
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint(None, 'representative', type_='unique')
op.create_unique_constraint('representative_slug_key', 'representative', ['slug'])
# ### end Alembic commands ###

2
requirements.txt

@ -9,3 +9,5 @@ flask_sqlalchemy
flask_wtf
python-slugify
requests
mysqlclient

Loading…
Cancel
Save