This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
devicehub-teal/ereuse_devicehub/migrations/versions/e919fe0611ff_placeholder_monitors.py
2022-09-27 12:51:31 +02:00

87 lines
1.8 KiB
Python

"""placeholder-monitors
Revision ID: e919fe0611ff
Revises: bcfda54aaf2f
Create Date: 2022-09-27 10:55:00.859262
"""
from alembic import context
from ereuse_devicehub.db import db
from ereuse_devicehub.devicehub import Devicehub
from ereuse_devicehub.resources.device.models import (
Cellphone,
ComputerMonitor,
Display,
Mobile,
Monitor,
Placeholder,
Smartphone,
Tablet,
)
# revision identifiers, used by Alembic.
revision = 'e919fe0611ff'
down_revision = 'bcfda54aaf2f'
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 init_app():
app = Devicehub(inventory=f'{get_inv()}')
app.app_context().push()
def clone_monitors():
devices = [ComputerMonitor, Monitor, Display, Smartphone, Tablet, Cellphone, Mobile]
for dev in devices:
for d in dev.query.all():
if d.placeholder or d.binding:
continue
clone_device(d)
def clone_device(device):
if not device.owner_id:
return
placeholder = Placeholder(
device=device, is_abstract=False, owner_id=device.owner_id
)
db.session.add(placeholder)
def remove_placeholders():
devices = [ComputerMonitor, Monitor, Display, Smartphone, Tablet, Cellphone, Mobile]
for dev in devices:
for d in dev.query.all():
if d.placeholder and d.binding:
continue
remove_device(d)
def remove_device(device):
if not device.owner_id:
return
placeholder = device.placeholder
db.session.delete(placeholder)
def upgrade():
init_app()
clone_monitors()
db.session.commit()
def downgrade():
pass
# init_app()
# remove_placeholders()
# db.session.commit()