-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add client table (#148) Add clientSQL in splmodels, with client_name Mapped relationships between Client and Site * Alembic migration : Add client table (#148) * Fix: make client_uuid nullable in Site table (#148) * Add read function get_sites_by_client_name and tests (#148) add function, tests, and edited sites fixture in conftest in order to have client informations in tests samples * update readme Database schema (#148) * Add create_client and edit_client functions (#148) added create_client and edit_client functions, and related tests. edited PVSiteEditMetadata pydantic model, so edit_site function can update the client_uuid. edited create_site function, so it can set the client_uuid. * Move create_client and edit_client to a new file (#148) Moved the client functions from user_and_site.py to client.py * Add get_client_by_name function (#148) Added get_client_by_name function, and related tests. This function can create a new client if it doesn't exist. * Add assign_site_to_client function (#148) * Adjust alembic migration (#148) --------- Co-authored-by: Peter Dudfield <[email protected]>
- Loading branch information
1 parent
f726172
commit 720720b
Showing
15 changed files
with
316 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
"""Add client table | ||
Revision ID: 31d501a0aa52 | ||
Revises: 2a6e6975cd72 | ||
Create Date: 2024-09-13 11:54:12.743980 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
from sqlalchemy.dialects import postgresql | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = '31d501a0aa52' | ||
down_revision = '2a6e6975cd72' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table('clients', | ||
sa.Column('client_uuid', sa.UUID(), nullable=False), | ||
sa.Column('client_name', sa.String(length=255), nullable=False), | ||
sa.Column('created_utc', sa.DateTime(), nullable=True), | ||
sa.PrimaryKeyConstraint('client_uuid') | ||
) | ||
op.create_index(op.f('ix_clients_client_name'), 'clients', ['client_name'], unique=True) | ||
op.add_column('sites', sa.Column('client_uuid', sa.UUID(), nullable=True, comment='The UUID of the client this site belongs to')) | ||
op.create_index(op.f('ix_sites_client_uuid'), 'sites', ['client_uuid'], unique=False) | ||
op.create_foreign_key(None, 'sites', 'clients', ['client_uuid'], ['client_uuid']) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_constraint(None, 'sites', type_='foreignkey') | ||
op.drop_index(op.f('ix_sites_client_uuid'), table_name='sites') | ||
op.drop_column('sites', 'client_uuid') | ||
op.drop_index(op.f('ix_clients_client_name'), table_name='clients') | ||
op.drop_table('clients') | ||
# ### end Alembic commands ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
""" Functions for reading user data from the database. """ | ||
|
||
import logging | ||
|
||
from sqlalchemy.orm import Session | ||
|
||
from pvsite_datamodel.sqlmodels import ClientSQL | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def get_client_by_name( | ||
session: Session, name: str, make_new_client_if_none: bool = True | ||
) -> ClientSQL: | ||
""" | ||
Get client by name. If client does not exist, make one. | ||
:param session: database session | ||
:param name: name of the client | ||
:param make_new_client_if_none: make client with name if doesn't exist | ||
:return: client object | ||
""" | ||
|
||
client = session.query(ClientSQL).filter(ClientSQL.client_name == name).first() | ||
|
||
if client is None: | ||
if make_new_client_if_none is True: | ||
logger.info(f"Client with name {name} not found, so making one") | ||
|
||
# make a new client | ||
client = ClientSQL(client_name=name) | ||
session.add(client) | ||
session.commit() | ||
else: | ||
raise Exception(f"Could not find client with name {name}") | ||
|
||
return client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
""" | ||
Tools for making clients in the database. | ||
""" | ||
|
||
import logging | ||
from uuid import UUID | ||
|
||
from sqlalchemy.orm.session import Session | ||
|
||
from pvsite_datamodel.sqlmodels import ClientSQL, SiteSQL | ||
|
||
_log = logging.getLogger(__name__) | ||
|
||
|
||
def create_client(session: Session, client_name: str) -> ClientSQL: | ||
"""Create a client. | ||
:param session: database session | ||
:param client_name: name of client being created | ||
""" | ||
client = ClientSQL(client_name=client_name) | ||
|
||
session.add(client) | ||
session.commit() | ||
|
||
return client | ||
|
||
|
||
def edit_client(session: Session, client_uuid: UUID, client_name: str) -> ClientSQL: | ||
"""Edit an existing client. | ||
:param session: database session | ||
:param client_uuid: the existing client uuid | ||
:param client_name: name of the client | ||
""" | ||
client = session.query(ClientSQL).filter(ClientSQL.client_uuid == client_uuid).first() | ||
|
||
client.client_name = client_name | ||
|
||
session.add(client) | ||
session.commit() | ||
|
||
return client | ||
|
||
|
||
def assign_site_to_client(session: Session, site_uuid: str, client_name: str) -> str: | ||
"""Assign site to client. | ||
:param session: database session | ||
:param site_uuid: uuid of site | ||
:param client_name: name of the client the site will be assigned to. | ||
""" | ||
|
||
client = session.query(ClientSQL).filter(ClientSQL.client_name == client_name).first() | ||
|
||
site = session.query(SiteSQL).filter(SiteSQL.site_uuid == site_uuid).first() | ||
|
||
site.client_uuid = client.client_uuid | ||
|
||
session.add(site) | ||
session.commit() | ||
|
||
message = f"Site with site uuid {site_uuid} successfully assigned to the client {client_name}" | ||
|
||
return message |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.