From 31357276ae50b32ff2912dbb3fc76630b9f87c93 Mon Sep 17 00:00:00 2001 From: emmdim Date: Wed, 11 Dec 2019 02:19:13 +0100 Subject: [PATCH] Adds author_id to Computer resource --- ereuse_devicehub/resources/device/models.py | 8 ++++++++ ereuse_devicehub/resources/device/models.pyi | 4 +++- ereuse_devicehub/resources/device/schemas.py | 6 +++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index 40512a2c..b36c00dc 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): @@ -380,6 +383,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 2de7bb93..453f93d0 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 97c229b7..ec9aa72c 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__