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/resources/inventory/__init__.py

87 lines
2.6 KiB
Python
Raw Permalink Normal View History

2019-01-23 15:55:04 +00:00
import uuid
import boltons.urlutils
from flask import current_app
from ereuse_devicehub.db import db
from ereuse_devicehub.resources.inventory import schema
from ereuse_devicehub.resources.inventory.model import Inventory
2023-03-21 11:08:13 +00:00
from ereuse_devicehub.teal.db import ResourceNotFound
from ereuse_devicehub.teal.resource import Resource
2019-01-23 15:55:04 +00:00
class InventoryDef(Resource):
SCHEMA = schema.Inventory
VIEW = None
2023-03-21 11:08:13 +00:00
def __init__(
self,
app,
import_name=__name__.split('.')[0],
static_folder=None,
static_url_path=None,
template_folder=None,
url_prefix=None,
subdomain=None,
url_defaults=None,
root_path=None,
):
super().__init__(
app,
import_name,
static_folder,
static_url_path,
template_folder,
url_prefix,
subdomain,
url_defaults,
root_path,
)
2019-01-23 15:55:04 +00:00
@classmethod
2023-03-21 11:08:13 +00:00
def set_inventory_config(
cls,
name: str = None,
org_name: str = None,
org_id: str = None,
tag_url: boltons.urlutils.URL = None,
tag_token: uuid.UUID = None,
):
2019-01-23 15:55:04 +00:00
try:
inventory = Inventory.current
except ResourceNotFound: # No inventory defined in db yet
2023-03-21 11:08:13 +00:00
inventory = Inventory(
id=current_app.id, name=name, tag_provider=tag_url, tag_token=tag_token
)
2019-01-23 15:55:04 +00:00
db.session.add(inventory)
if org_name or org_id:
from ereuse_devicehub.resources.agent.models import Organization
2023-03-21 11:08:13 +00:00
2019-01-23 15:55:04 +00:00
try:
org = Organization.query.filter_by(tax_id=org_id, name=org_name).one()
except ResourceNotFound:
org = Organization(tax_id=org_id, name=org_name)
org.default_of = inventory
if tag_url:
inventory.tag_provider = tag_url
if tag_token:
inventory.tag_token = tag_token
@classmethod
def delete_inventory(cls):
"""Removes an inventory alongside with the users that have
only access to this inventory.
"""
from ereuse_devicehub.resources.user.models import User, UserInventory
2023-03-21 11:08:13 +00:00
inv = Inventory.query.filter_by(id=current_app.id).one()
db.session.delete(inv)
db.session.flush()
# Remove users that end-up without any inventory
# todo this should be done in a trigger / action
2023-03-21 11:08:13 +00:00
users = User.query.filter(
User.id.notin_(db.session.query(UserInventory.user_id).distinct())
)
for user in users:
db.session.delete(user)