diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index 76d18af4..f02c2d01 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -564,30 +564,68 @@ class NewActionForm(ActionFormMix): class AllocateForm(ActionFormMix): start_time = DateField('Start time') - end_time = DateField('End time') - final_user_code = StringField('Final user code', [validators.length(max=50)]) - transaction = StringField('Transaction', [validators.length(max=50)]) - end_users = IntegerField('End users') + end_time = DateField('End time', [validators.Optional()]) + final_user_code = StringField( + 'Final user code', [validators.Optional(), validators.length(max=50)] + ) + transaction = StringField( + 'Transaction', [validators.Optional(), validators.length(max=50)] + ) + end_users = IntegerField('End users', [validators.Optional()]) def validate(self, extra_validators=None): - is_valid = super().validate(extra_validators) + if not super().validate(extra_validators): + return False if self.type.data not in ['Allocate', 'Deallocate']: return False + if not self.validate_dates(): + return False + + if not self.check_devices(): + return False + + return True + + def validate_dates(self): start_time = self.start_time.data end_time = self.end_time.data + + if not start_time: + self.start_time.errors = ['Not a valid date value.!'] + return False + if start_time and end_time and end_time < start_time: error = ['The action cannot finish before it starts.'] - self.start_time.errors = error self.end_time.errors = error - is_valid = False + return False - if not self.end_users.data: - self.end_users.errors = ["You need to specify a number of users"] - is_valid = False + if not end_time: + self.end_time.data = self.start_time.data - return is_valid + return True + + def check_devices(self): + if self.type.data == 'Allocate': + txt = "You need deallocate before allocate this device again" + for device in self._devices: + if device.allocated: + self.devices.errors = [txt] + return False + + device.allocated = True + + if self.type.data == 'Deallocate': + txt = "Sorry some of this devices are actually deallocate" + for device in self._devices: + if not device.allocated: + self.devices.errors = [txt] + return False + + device.allocated = False + + return True class DataWipeDocumentForm(Form): diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index dcd16b68..1bd33cf6 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -10,7 +10,7 @@ from typing import Dict, List, Set from boltons import urlutils from citext import CIText from ereuse_utils.naming import HID_CONVERSION_DOC, Naming -from flask import g +from flask import g, request from flask_sqlalchemy import event from more_itertools import unique_everseen from sqlalchemy import BigInteger, Boolean, Column @@ -297,6 +297,11 @@ class Device(Thing): actions.reverse() return actions + @property + def public_link(self) -> str: + host_url = request.host_url.strip('/') + return "{}{}".format(host_url, self.url.to_text()) + @property def url(self) -> urlutils.URL: """The URL where to GET this device.""" diff --git a/ereuse_devicehub/templates/inventory/device_detail.html b/ereuse_devicehub/templates/inventory/device_detail.html index 7a96c4a9..f18c1e0e 100644 --- a/ereuse_devicehub/templates/inventory/device_detail.html +++ b/ereuse_devicehub/templates/inventory/device_detail.html @@ -26,6 +26,14 @@ +