Skip to content

Commit

Permalink
feat: Create Platform Registration (#177)
Browse files Browse the repository at this point in the history
* Platform Registration

* no message

* no message

* no message

* no message

* Test fixes

* Updated version
  • Loading branch information
kris-daxiom authored Oct 15, 2024
1 parent 5f7a160 commit 58f3edb
Show file tree
Hide file tree
Showing 22 changed files with 1,037 additions and 95 deletions.
2 changes: 0 additions & 2 deletions strr-api/migrations/versions/20240801_0544_64243bf62473_.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('certificates', schema=None) as batch_op:
batch_op.drop_constraint('certificates_registration_number_key', type_='unique')
batch_op.drop_column('registration_number')
with op.batch_alter_table('registrations', schema=None) as batch_op:
batch_op.add_column(sa.Column('registration_number', sa.String(), nullable=True))
Expand All @@ -35,5 +34,4 @@ def downgrade():

with op.batch_alter_table('certificates', schema=None) as batch_op:
batch_op.add_column(sa.Column('registration_number', sa.VARCHAR(), autoincrement=False, nullable=False))
batch_op.create_unique_constraint('certificates_registration_number_key', ['registration_number'])
# ### end Alembic commands ###
6 changes: 0 additions & 6 deletions strr-api/migrations/versions/20240927_1313_c8d5441975d9_.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ def upgrade():
sa.PrimaryKeyConstraint('id')
)
with op.batch_alter_table('documents', schema=None) as batch_op:
batch_op.drop_constraint('documents_eligibility_id_fkey', type_='foreignkey')
batch_op.drop_column('eligibility_id')

with op.batch_alter_table('registrations', schema=None) as batch_op:
Expand All @@ -76,7 +75,6 @@ def upgrade():
batch_op.drop_index('ix_registrations_submission_date')
batch_op.create_index(batch_op.f('ix_registrations_registration_type'), ['registration_type'], unique=False)
batch_op.create_index(batch_op.f('ix_registrations_sbc_account_id'), ['sbc_account_id'], unique=False)
batch_op.drop_constraint('registrations_rental_property_id_fkey', type_='foreignkey')
batch_op.drop_column('rental_property_id')
batch_op.drop_column('submission_date')

Expand All @@ -96,7 +94,6 @@ def upgrade():
type_=ownershiptype,
existing_nullable=False,
postgresql_using="ownership_type::ownershiptype")
batch_op.drop_constraint('rental_properties_property_manager_id_fkey', type_='foreignkey')
batch_op.create_foreign_key(None, 'registrations', ['registration_id'], ['id'])
batch_op.drop_column('property_manager_id')

Expand Down Expand Up @@ -148,7 +145,6 @@ def downgrade():
with op.batch_alter_table('rental_properties', schema=None) as batch_op:
batch_op.add_column(sa.Column('property_manager_id', sa.INTEGER(), autoincrement=False, nullable=False))
batch_op.drop_constraint(None, type_='foreignkey')
batch_op.create_foreign_key('rental_properties_property_manager_id_fkey', 'property_managers', ['property_manager_id'], ['id'])
batch_op.alter_column('ownership_type',
existing_type=ownershiptype,
type_=sa.VARCHAR(),
Expand All @@ -168,7 +164,6 @@ def downgrade():
with op.batch_alter_table('registrations', schema=None) as batch_op:
batch_op.add_column(sa.Column('submission_date', postgresql.TIMESTAMP(), autoincrement=False, nullable=False))
batch_op.add_column(sa.Column('rental_property_id', sa.INTEGER(), autoincrement=False, nullable=False))
batch_op.create_foreign_key('registrations_rental_property_id_fkey', 'rental_properties', ['rental_property_id'], ['id'])
batch_op.drop_index(batch_op.f('ix_registrations_sbc_account_id'))
batch_op.drop_index(batch_op.f('ix_registrations_registration_type'))
batch_op.create_index('ix_registrations_submission_date', ['submission_date'], unique=False)
Expand All @@ -191,7 +186,6 @@ def downgrade():

with op.batch_alter_table('documents', schema=None) as batch_op:
batch_op.add_column(sa.Column('eligibility_id', sa.INTEGER(), autoincrement=False, nullable=False))
batch_op.create_foreign_key('documents_eligibility_id_fkey', 'eligibilities', ['eligibility_id'], ['id'])

op.drop_table('property_listings')
op.drop_table('property_contacts')
Expand Down
602 changes: 602 additions & 0 deletions strr-api/migrations/versions/20241010_2151_a6abe20bd998_.py

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions strr-api/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions strr-api/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "strr-api"
version = "0.0.13"
version = "0.0.14"
description = ""
authors = ["thorwolpert <[email protected]>"]
license = "BSD 3-Clause"
Expand All @@ -14,7 +14,6 @@ launchdarkly-server-sdk = "^9.0.1"
python-dotenv = "^1.0.0"
flask-sqlalchemy = "^3.1.1"
flask-migrate = "^4.0.4"
sql-versioning = { git = "https://github.com/bcgov/lear.git", subdirectory = "python/common/sql-versioning", branch = "feature-legal-name" }
sentry-sdk = "^1.24.0"
flask-babel = "^4.0.0"
flask-cors = "^4.0.0"
Expand All @@ -33,6 +32,7 @@ google-cloud-storage = "2.14.0"
geoalchemy2 = "^0.15.1"
weasyprint = "^62.3"
sqlalchemy-utils = "^0.41.2"
sql-versioning = { git = "https://github.com/bcgov/sbc-connect-common.git", subdirectory = "python/sql-versioning", branch = "main" }

[tool.poetry.group.test.dependencies]
freezegun = "^1.2.2"
Expand Down
9 changes: 7 additions & 2 deletions strr-api/src/strr_api/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,22 @@
# POSSIBILITY OF SUCH DAMAGE.
"""This exports all of the models and schemas used by the application."""
from .account_roles import AccountRoles
from .address import Address
from .application import Application
from .auto_approval_record import AutoApprovalRecord
from .certificate import Certificate
from .db import db # noqa: I001
from .dss import DSSOrganization
from .events import Events
from .ltsa import LTSARecord
from .platforms import Platform
from .rental import Address, Document, PropertyContact, PropertyListing, Registration, RentalProperty
from .platforms import Platform, PlatformBrand, PlatformRegistration, PlatformRepresentative
from .rental import Document, PropertyContact, PropertyListing, Registration, RentalProperty
from .user import Contact, User

__all__ = (
"db",
"AccountRoles",
"Address",
"Application",
"User",
"RentalProperty",
Expand All @@ -62,4 +64,7 @@
"LTSARecord",
"Certificate",
"Platform",
"PlatformBrand",
"PlatformRegistration",
"PlatformRepresentative",
)
38 changes: 38 additions & 0 deletions strr-api/src/strr_api/models/address.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""Registration model.
"""

from __future__ import annotations

from sql_versioning import Versioned
from sqlalchemy.orm import relationship

from strr_api.models.base_model import BaseModel

from .db import db


class Address(Versioned, BaseModel):
"""Address"""

__tablename__ = "addresses"

id = db.Column(db.Integer, primary_key=True, autoincrement=True)
country = db.Column(db.String, nullable=False)
street_address = db.Column(db.String, nullable=False)
street_address_additional = db.Column(db.String, nullable=True)
city = db.Column(db.String, nullable=False)
province = db.Column(db.String, nullable=False)
postal_code = db.Column(db.String, nullable=False)

contact = relationship("Contact", back_populates="address", foreign_keys="Contact.address_id")
rental_properties_address = relationship(
"RentalProperty", back_populates="address", foreign_keys="RentalProperty.address_id"
)

def to_oneline_address(self):
"""Convert object to one line address."""
unit = ""
if self.street_address_additional:
unit = f"{self.street_address_additional} "
return f"{unit}{self.street_address}, {self.city}, {self.province}, {self.country}, {self.postal_code}"
4 changes: 2 additions & 2 deletions strr-api/src/strr_api/models/auto_approval_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.sql import text

from strr_api.models.base_model import BaseModel
from strr_api.models.base_model import SimpleBaseModel

from .db import db


class AutoApprovalRecord(BaseModel):
class AutoApprovalRecord(SimpleBaseModel):
"""AutoApprovalRecord Record"""

__tablename__ = "auto_approval_records"
Expand Down
43 changes: 42 additions & 1 deletion strr-api/src/strr_api/models/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,18 @@
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
"""Base Model."""
import datetime

from sqlalchemy import Column, DateTime, ForeignKey
from sqlalchemy.ext.declarative import declared_attr
from sqlalchemy.orm import relationship

from strr_api.models.user import User

from .db import db


class BaseModel(db.Model):
class SimpleBaseModel(db.Model):
"""Base class that defines all the common methods."""

__abstract__ = True
Expand Down Expand Up @@ -80,3 +87,37 @@ def reset(self):
if self:
db.session.delete(self)
db.session.commit()


class BaseModel(SimpleBaseModel):
"""Base class that defines all the common columns."""

__abstract__ = True

@declared_attr
def created_by_id(cls): # pylint:disable=no-self-argument, # noqa: N805
"""Return foreign key for created by."""
return Column(ForeignKey("users.id"), default=cls._get_current_user)

@declared_attr
def modified_by_id(cls): # pylint:disable=no-self-argument, # noqa: N805
"""Return foreign key for modified by."""
return Column(ForeignKey("users.id"), onupdate=cls._get_current_user)

@declared_attr
def created_by(cls): # pylint:disable=no-self-argument, # noqa: N805
"""Return relationship for created by."""
return relationship("User", foreign_keys=[cls.created_by_id], post_update=True, uselist=False)

@declared_attr
def modified_by(cls): # pylint:disable=no-self-argument, # noqa: N805
"""Return relationship for modified by."""
return relationship("User", foreign_keys=[cls.modified_by_id], post_update=True, uselist=False)

created = Column(DateTime, default=datetime.datetime.now)
modified = Column(DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now)

@staticmethod
def _get_current_user():
user = User.find_user_in_context()
return user.id if user else None
10 changes: 9 additions & 1 deletion strr-api/src/strr_api/models/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,16 @@
"""
from flask_sqlalchemy import SQLAlchemy
from sql_versioning import versioned_session
from sqlalchemy import MetaData

# by convention in the Flask community these are lower case,
# whereas pylint wants them upper case
db = SQLAlchemy()

metadata = MetaData(
naming_convention={
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
}
)

db = SQLAlchemy(metadata=metadata)
versioned_session(db.session)
4 changes: 2 additions & 2 deletions strr-api/src/strr_api/models/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
from sqlalchemy.sql import text

from strr_api.common.enum import BaseEnum, auto
from strr_api.models.base_model import BaseModel
from strr_api.models.base_model import SimpleBaseModel

from .db import db


class Events(BaseModel):
class Events(SimpleBaseModel):
"""Events related to application, registration etc."""

class EventType(BaseEnum):
Expand Down
4 changes: 2 additions & 2 deletions strr-api/src/strr_api/models/ltsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.sql import text

from strr_api.models.base_model import BaseModel
from strr_api.models.base_model import SimpleBaseModel

from .db import db


class LTSARecord(BaseModel):
class LTSARecord(SimpleBaseModel):
"""LTSA Record"""

__tablename__ = "ltsa"
Expand Down
83 changes: 78 additions & 5 deletions strr-api/src/strr_api/models/platforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,94 @@
"""
from __future__ import annotations

from sql_versioning import Versioned
from sqlalchemy.orm import relationship

from strr_api.common.enum import BaseEnum, auto
from strr_api.models.base_model import BaseModel

from .db import db


class Platform(BaseModel):
"""Rental Property"""
class Platform(Versioned, BaseModel):
"""Platform"""

class ListingSize(BaseEnum):
"""Enum of the registration types."""

GREATER_THAN_THOUSAND = auto() # pylint: disable=invalid-name
LESS_THAN_THOUSAND = auto() # pylint: disable=invalid-name

__tablename__ = "platforms"

id = db.Column(db.Integer, primary_key=True, autoincrement=True)
legal_name = db.Column(db.String, nullable=False)
business_number = db.Column(db.String, nullable=False)
legal_name = db.Column("legal_name", db.String(150), nullable=False, index=True)
home_jurisdiction = db.Column("home_jurisdiction", db.String(150), nullable=False)
business_number = db.Column("business_number", db.String(150), nullable=True)
cpbc_licence_number = db.Column("cpbc_licence_number", db.String(50), nullable=True)
primary_non_compliance_notice_email = db.Column(
"primary_non_compliance_notice_email", db.String(100), nullable=False
)
secondary_non_compliance_notice_email = db.Column(
"secondary_non_compliance_notice_email", db.String(100), nullable=True
)
primary_take_down_request_email = db.Column("primary_take_down_request_email", db.String(100), nullable=False)
secondary_take_down_request_email = db.Column("secondary_take_down_request_email", db.String(100), nullable=True)
attorney_name = db.Column("attorney_name", db.String(150), nullable=True)

attorney_name = db.Column("attorney_name", db.String(150), nullable=True)
listing_size = db.Column("listing_size", db.Enum(ListingSize))

mailing_address_id = db.Column(db.Integer, db.ForeignKey("addresses.id"), nullable=False)
registered_office_attorney_mailing_address_id = db.Column(db.Integer, db.ForeignKey("addresses.id"), nullable=True)

mailingAddress = relationship("Address", foreign_keys=[mailing_address_id])
registered_office_attorney_mailing_address = relationship(
"Address", foreign_keys=[registered_office_attorney_mailing_address_id]
)

representatives = relationship("PlatformRepresentative", back_populates="platform")
platform_registrations = relationship("PlatformRegistration", back_populates="platform")
brands = relationship("PlatformBrand", back_populates="platform")


class PlatformRegistration(Versioned, BaseModel):
"""Platform Registration mapping model"""

__tablename__ = "platform_registration"

id = db.Column(db.Integer, primary_key=True, autoincrement=True)

registration_id = db.Column(db.Integer, db.ForeignKey("registrations.id"), nullable=False)
registration = relationship("Registration", foreign_keys=[registration_id], back_populates="platform")
registration = relationship("Registration", foreign_keys=[registration_id], back_populates="platform_registration")

platform_id = db.Column(db.Integer, db.ForeignKey("platforms.id"), nullable=False)
platform = relationship("Platform", foreign_keys=[platform_id], back_populates="platform_registrations")


class PlatformRepresentative(Versioned, BaseModel):
"""Platform Representatives"""

__tablename__ = "platform_representatives"

id = db.Column(db.Integer, primary_key=True, autoincrement=True)

contact_id = db.Column(db.Integer, db.ForeignKey("contacts.id"), nullable=False)
platform_id = db.Column(db.Integer, db.ForeignKey("platforms.id"), nullable=False)

contact = relationship("Contact", foreign_keys=[contact_id])
platform = relationship("Platform", foreign_keys=[platform_id], back_populates="representatives")


class PlatformBrand(Versioned, BaseModel):
"""Platform Brands."""

__tablename__ = "platform_brands"

id = db.Column(db.Integer, primary_key=True, autoincrement=True)

name = db.Column("name", db.String(150), nullable=False, index=True)
website = db.Column("website", db.String(500), nullable=False)
platform_id = db.Column(db.Integer, db.ForeignKey("platforms.id"), nullable=False)

platform = relationship("Platform", back_populates="brands", foreign_keys=[platform_id])
Loading

0 comments on commit 58f3edb

Please sign in to comment.