diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index f9b10aa2..5235c78e 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -41,7 +41,7 @@ from ereuse_devicehub.inventory.models import ( from ereuse_devicehub.parser.models import PlaceholdersLog, SnapshotsLog from ereuse_devicehub.parser.parser import ParseSnapshotLsHw from ereuse_devicehub.parser.schemas import Snapshot_lite -from ereuse_devicehub.resources.action.models import Snapshot, Trade +from ereuse_devicehub.resources.action.models import Snapshot, Trade, VisualTest from ereuse_devicehub.resources.action.schemas import Snapshot as SnapshotSchema from ereuse_devicehub.resources.action.views.snapshot import ( SnapshotMixin, @@ -68,6 +68,7 @@ from ereuse_devicehub.resources.device.models import ( Projector, Server, Smartphone, + SolarPanel, SolidStateDrive, Tablet, TelevisionSet, @@ -113,7 +114,7 @@ DEVICES = { "SAI", "Keyboard", ], - "Other Devices": ["Other"], + "Other Devices": ["SolarPanel", "Other"], } TYPES_DOCUMENTS = [ @@ -131,7 +132,7 @@ MONITORS = ["ComputerMonitor", "Monitor", "TelevisionSet", "Projector"] MOBILE = ["Mobile", "Tablet", "Smartphone", "Cellphone"] STORAGE = ["HardDrive", "SolidStateDrive"] ACCESSORIES = ["Mouse", "MemoryCardReader", "SAI", "Keyboard"] -OTHERS = ["Other"] +OTHERS = ["Other", "SolarPanel"] DATASTORAGE = ['HardDrive', 'SolidStateDrive'] @@ -433,6 +434,7 @@ class NewDeviceForm(FlaskForm): "Keyboard": Keyboard, "SAI": SAI, "MemoryCardReader": MemoryCardReader, + "SolarPanel": SolarPanel, "Other": Other, } @@ -480,6 +482,10 @@ class NewDeviceForm(FlaskForm): if self._obj.type in ['HardDrive', 'SolidStateDrive']: if self._obj.size: self.data_storage_size.data = self._obj.size / 1000 + if self._obj.appearance(): + self.appearance.data = self._obj.appearance().name + if self._obj.functionality(): + self.functionality.data = self._obj.functionality().name if self._obj.placeholder.is_abstract: self.type.render_kw = disabled @@ -698,17 +704,7 @@ class NewDeviceForm(FlaskForm): if self.data_storage_size.data: self._obj.size = self.data_storage_size.data * 1000 - if ( - self.appearance.data - and self.appearance.data != self._obj.appearance().name - ): - self._obj.set_appearance(self.appearance.data) - - if ( - self.functionality.data - and self.functionality.data != self._obj.functionality().name - ): - self._obj.set_functionality(self.functionality.data) + self.edit_visual_test(self._obj) else: self._obj.placeholder.id_device_supplier = ( @@ -718,11 +714,33 @@ class NewDeviceForm(FlaskForm): self.id_device_internal.data or None ) self._obj.placeholder.pallet = self.pallet.data or None + + pl_dev = self._obj.placeholder.device + self.edit_visual_test(pl_dev) + placeholder_log = PlaceholdersLog( type="Update", source='Web form', placeholder=self._obj.placeholder ) db.session.add(placeholder_log) + def edit_visual_test(self, dev): + if not dev.appearance() or not dev.functionality(): + visual_test = VisualTest( + appearance_range=self.appearance.data, + functionality_range=self.functionality.data, + device=dev, + ) + db.session.add(visual_test) + else: + if self.appearance.data and self.appearance.data != dev.appearance().name: + dev.set_appearance(self.appearance.data) + + if ( + self.functionality.data + and self.functionality.data != dev.functionality().name + ): + dev.set_functionality(self.functionality.data) + class TagDeviceForm(FlaskForm): tag = SelectField( @@ -1211,7 +1229,6 @@ class TradeForm(ActionFormMixin): or email_to == email_from or g.user.email not in [email_from, email_to] ): - errors = ["If you want confirm, you need a correct email"] self.user_to.errors = errors self.user_from.errors = errors @@ -1917,7 +1934,6 @@ class UploadPlaceholderForm(FlaskForm): return True def save(self, commit=True): - for device, placeholder_log in self.placeholders: db.session.add(device) db.session.add(placeholder_log) @@ -1946,7 +1962,6 @@ class EditPlaceholderForm(FlaskForm): return True def save(self, commit=True): - for device in self.placeholders: db.session.add(device) diff --git a/ereuse_devicehub/migrations/versions/8ccba3cb37c2_add_solar_panel.py b/ereuse_devicehub/migrations/versions/8ccba3cb37c2_add_solar_panel.py new file mode 100644 index 00000000..f5dc87df --- /dev/null +++ b/ereuse_devicehub/migrations/versions/8ccba3cb37c2_add_solar_panel.py @@ -0,0 +1,41 @@ +"""add solar panel + +Revision ID: 8ccba3cb37c2 +Revises: 5169765e2653 +Create Date: 2023-07-26 09:23:21.326465 + +""" +import sqlalchemy as sa +from alembic import context, op + +# revision identifiers, used by Alembic. +revision = '8ccba3cb37c2' +down_revision = '5169765e2653' +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(): + # creating Solar panel device. + + op.create_table( + 'solar_panel', + sa.Column('id', sa.BigInteger(), nullable=False), + sa.ForeignKeyConstraint( + ['id'], + [f'{get_inv()}.device.id'], + ), + sa.PrimaryKeyConstraint('id'), + schema=f'{get_inv()}', + ) + + +def downgrade(): + op.drop_table('solar_panel', schema=f'{get_inv()}') diff --git a/ereuse_devicehub/resources/device/definitions.py b/ereuse_devicehub/resources/device/definitions.py index febacf38..c9ac1142 100644 --- a/ereuse_devicehub/resources/device/definitions.py +++ b/ereuse_devicehub/resources/device/definitions.py @@ -726,3 +726,34 @@ class OtherDef(DeviceDef): root_path, cli_commands, ) + + +class SolarPanelDef(DeviceDef): + VIEW = None + SCHEMA = schemas.SolarPanel + + def __init__( + self, + app, + import_name=__name__, + static_folder=None, + static_url_path=None, + template_folder=None, + url_prefix=None, + subdomain=None, + url_defaults=None, + root_path=None, + cli_commands: Iterable[Tuple[Callable, str or None]] = tuple(), + ): + super().__init__( + app, + import_name, + static_folder, + static_url_path, + template_folder, + url_prefix, + subdomain, + url_defaults, + root_path, + cli_commands, + ) diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index c7950d1d..1d9c1e79 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -476,7 +476,8 @@ class Device(Thing): """The trading state, or None if no Trade action has ever been performed to this device. This extract the posibilities for to do. This method is performed for show in the web. - If you need to do one simple and generic response you can put simple=True for that.""" + If you need to do one simple and generic response you can put simple=True for that. + """ if not hasattr(lot, 'trade'): return @@ -1986,3 +1987,11 @@ class Other(Device): """ id = Column(BigInteger, ForeignKey(Device.id), primary_key=True) + + +class SolarPanel(Device): + """ + Used solar panels devices. + """ + + id = Column(BigInteger, ForeignKey(Device.id), primary_key=True) diff --git a/ereuse_devicehub/resources/device/schemas.py b/ereuse_devicehub/resources/device/schemas.py index 59f6afd5..7d168a03 100644 --- a/ereuse_devicehub/resources/device/schemas.py +++ b/ereuse_devicehub/resources/device/schemas.py @@ -590,5 +590,9 @@ class Racket(Recreation): pass +class SolarPanel(Device): + pass + + class Other(Device): pass diff --git a/ereuse_devicehub/resources/device/views.py b/ereuse_devicehub/resources/device/views.py index a8d0e88d..6e0f3f86 100644 --- a/ereuse_devicehub/resources/device/views.py +++ b/ereuse_devicehub/resources/device/views.py @@ -176,7 +176,7 @@ class DeviceView(View): """Gets many devices.""" # Compute query query = self.query(args) - devices = query.paginate(page=args['page'], per_page=30) # type: Pagination + devices = query.paginate(page=args['page'], per_page=100) # type: Pagination return things_response( self.schema.dump(devices.items, many=True, nested=1), devices.page, diff --git a/ereuse_devicehub/templates/inventory/device_create.html b/ereuse_devicehub/templates/inventory/device_create.html index 3de677c9..82f0d7f2 100644 --- a/ereuse_devicehub/templates/inventory/device_create.html +++ b/ereuse_devicehub/templates/inventory/device_create.html @@ -79,6 +79,8 @@ {% if form.type.data == 'Keyboard' %} selected="selected"{% endif %}>Keyboard + diff --git a/ereuse_devicehub/templates/inventory/upload_placeholder.html b/ereuse_devicehub/templates/inventory/upload_placeholder.html index 7763cd43..141345ac 100644 --- a/ereuse_devicehub/templates/inventory/upload_placeholder.html +++ b/ereuse_devicehub/templates/inventory/upload_placeholder.html @@ -72,6 +72,12 @@ + + + + Type of devices {% if form.type.errors %} diff --git a/tests/test_basic.py b/tests/test_basic.py index 3922e959..847521bc 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -125,4 +125,4 @@ def test_api_docs(client: Client): 'scheme': 'basic', 'name': 'Authorization', } - assert len(docs['definitions']) == 135 + assert len(docs['definitions']) == 136