share lot structure

This commit is contained in:
Cayo Puigdefabregas 2023-04-26 18:25:03 +02:00
parent a47e99ce0b
commit e365c366f4
3 changed files with 94 additions and 0 deletions

View File

@ -0,0 +1,52 @@
"""share lot
Revision ID: 2f2ef041483a
Revises: ac476b60d952
Create Date: 2023-04-26 16:04:21.560888
"""
import sqlalchemy as sa
from alembic import context, op
from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic.
revision = '2f2ef041483a'
down_revision = 'ac476b60d952'
branch_labels = None
depends_on = None
def get_inv():
INV = context.get_x_argument(as_dictionary=True).get('inventory')
if not INV:
raise ValueError("Inventory value is not specified")
return INV
def upgrade():
op.create_table(
'share_lot',
sa.Column(
'created',
sa.TIMESTAMP(timezone=True),
server_default=sa.text('CURRENT_TIMESTAMP'),
nullable=False,
),
sa.Column(
'updated',
sa.TIMESTAMP(timezone=True),
server_default=sa.text('CURRENT_TIMESTAMP'),
nullable=False,
),
sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('user_to_id', postgresql.UUID(as_uuid=True), nullable=True),
sa.Column('lot_id', postgresql.UUID(as_uuid=True), nullable=False),
sa.ForeignKeyConstraint(['user_to_id'], ['common.user.id']),
sa.ForeignKeyConstraint(['lot_id'], [f'{get_inv()}.lot.id']),
sa.PrimaryKeyConstraint('id'),
schema=f'{get_inv()}',
)
def downgrade():
op.drop_table('share_lot', schema=f'{get_inv()}')

View File

@ -396,3 +396,15 @@ class LotParent(db.Model):
.select_from(Path)
.where(i > 0),
)
class ShareLot(Thing):
id = db.Column(UUID(as_uuid=True), primary_key=True)
lot_id = db.Column(db.UUID(as_uuid=True), db.ForeignKey(Lot.id), nullable=False)
lot = db.relationship(Lot, primaryjoin=lot_id == Lot.id)
user_to_id = db.Column(
UUID(as_uuid=True),
db.ForeignKey(User.id),
nullable=True,
)
user_to = db.relationship(User, primaryjoin=user_to_id == User.id)

30
scripts/sharelot.py Normal file
View File

@ -0,0 +1,30 @@
import sys
import uuid
from decouple import config
from ereuse_devicehub.db import db
from ereuse_devicehub.devicehub import Devicehub
from ereuse_devicehub.resources.lot.models import Lot, ShareLot
from ereuse_devicehub.resources.user.models import User
def main():
# import pdb; pdb.set_trace()
schema = config('DB_SCHEMA')
app = Devicehub(inventory=schema)
app.app_context().push()
email = sys.argv[1]
lot_id = sys.argv[2]
id = uuid.uuid4()
user = User.query.filter_by(email=email).first()
lot = Lot.query.filter_by(id=lot_id).first()
share_lot = ShareLot(id=id, lot=lot, user_to=user)
db.session.add(share_lot)
db.session.commit()
if __name__ == '__main__':
main()