diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index 4d809f80..cb04f425 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -7,6 +7,7 @@ from typing import Dict, List, Set from boltons import urlutils from citext import CIText +from flask import g from ereuse_utils.naming import HID_CONVERSION_DOC, Naming from more_itertools import unique_everseen from sqlalchemy import BigInteger, Boolean, Column, Enum as DBEnum, Float, ForeignKey, Integer, \ @@ -16,6 +17,7 @@ from sqlalchemy.ext.hybrid import hybrid_property from sqlalchemy.orm import ColumnProperty, backref, relationship, validates from sqlalchemy.util import OrderedSet from sqlalchemy_utils import ColorType +from sqlalchemy.dialects.postgresql import UUID from stdnum import imei, meid from teal.db import CASCADE_DEL, POLYMORPHIC_ID, POLYMORPHIC_ON, ResourceNotFound, URL, \ check_lower, check_range @@ -27,6 +29,7 @@ from ereuse_devicehub.db import db from ereuse_devicehub.resources.enums import BatteryTechnology, CameraFacing, ComputerChassis, \ DataStorageInterface, DisplayTech, PrinterTechnology, RamFormat, RamInterface, Severity from ereuse_devicehub.resources.models import STR_SM_SIZE, Thing +from ereuse_devicehub.resources.user.models import User class Device(Thing): @@ -379,6 +382,11 @@ class Computer(Device): It is a subset of the Linux definition of DMI / DMI decode. """ deposit = Column(Integer, check_range('deposit',min=0,max=100), default=0) + author_id = db.Column(UUID(as_uuid=True), + db.ForeignKey(User.id), + nullable=False, + default=lambda: g.user.id) + author = db.relationship(User, primaryjoin=author_id == User.id) def __init__(self, chassis, **kwargs) -> None: chassis = ComputerChassis(chassis) diff --git a/ereuse_devicehub/resources/device/models.pyi b/ereuse_devicehub/resources/device/models.pyi index 5e75b933..77740a76 100644 --- a/ereuse_devicehub/resources/device/models.pyi +++ b/ereuse_devicehub/resources/device/models.pyi @@ -142,13 +142,15 @@ class Computer(DisplayMixin, Device): components = ... # type: Column chassis = ... # type: Column deposit = ... # type: Column + author_id = ... # type: Column def __init__(self, **kwargs) -> None: super().__init__(**kwargs) self.components = ... # type: Set[Component] self.actions_parent = ... # type: Set[e.Action] self.chassis = ... # type: ComputerChassis - + self.author_id = ... + @property def actions(self) -> List: pass diff --git a/ereuse_devicehub/resources/device/schemas.py b/ereuse_devicehub/resources/device/schemas.py index 14c34fef..ec9b85a2 100644 --- a/ereuse_devicehub/resources/device/schemas.py +++ b/ereuse_devicehub/resources/device/schemas.py @@ -1,7 +1,7 @@ import datetime from marshmallow import post_load, pre_load -from marshmallow.fields import Boolean, Date, DateTime, Float, Integer, List, Str, String +from marshmallow.fields import Boolean, Date, DateTime, Float, Integer, List, Str, String, UUID from marshmallow.validate import Length, OneOf, Range from sqlalchemy.util import OrderedSet from stdnum import imei, meid @@ -14,6 +14,7 @@ from ereuse_devicehub.resources import enums from ereuse_devicehub.resources.device import models as m, states from ereuse_devicehub.resources.models import STR_BIG_SIZE, STR_SIZE from ereuse_devicehub.resources.schemas import Thing, UnitCodes +from ereuse_devicehub.resources.user import schemas as s_user class Device(Thing): @@ -124,6 +125,9 @@ class Computer(Device): deposit = Integer(dump_only=True, data_key='deposit', description=m.Computer.deposit.__doc__) + # author_id = NestedOn(s_user.User,only_query='author_id') + author_id = UUID(dump_only=True, + data_key='author_id') class Desktop(Computer): __doc__ = m.Desktop.__doc__