diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index ac3c30ba..d29f239c 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -389,7 +389,7 @@ class NewActionForm(FlaskForm): Lot.owner_id == g.user.id).one() devices = set(self.devices.data.split(",")) - self._devices = set(Device.query.filter(Device.id.in_(devices)).filter( + self._devices = OrderedSet(Device.query.filter(Device.id.in_(devices)).filter( Device.owner_id == g.user.id).all()) if not self._devices: @@ -397,26 +397,34 @@ class NewActionForm(FlaskForm): return True - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - self.instance = Action() - self.populate_obj(self.instance) - def save(self): - self.instance.devices = self._devices - self.instance.severity = Severity[self.severity.data] + Model = db.Model._decl_class_registry.data[self.type.data]() + self.instance = Model() + self.devices.data = self._devices + self.severity.data = Severity[self.severity.data] + + self.populate_obj(self.instance) db.session.add(self.instance) db.session.commit() return self.instance class AllocateForm(NewActionForm): - def __init__(self, *args, **kwargs): - # import pdb; pdb.set_trace() - super().__init__(*args, **kwargs) - - start_time = DateField(u'Start time', validators=(validators.Optional(),)) - end_time = DateField(u'End time', validators=(validators.Optional(),)) + start_time = DateField(u'Start time') + end_time = DateField(u'End time') final_user_code = StringField(u'Final user code', [validators.length(max=50)]) transaction = StringField(u'Transaction', [validators.length(max=50)]) end_users = IntegerField(u'End users') + + def validate(self, extra_validators=None): + is_valid = super().validate(extra_validators) + + start_time = self.start_time.data + end_time = self.end_time.data + 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 is_valid diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index 4ec4eccd..f9386dc9 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -12,11 +12,11 @@ from ereuse_devicehub.inventory.forms import LotDeviceForm, LotForm, UploadSnaps devices = Blueprint('inventory.devices', __name__, url_prefix='/inventory') -class DeviceListView(View): +class DeviceListMix(View): decorators = [login_required] template_name = 'inventory/device_list.html' - def dispatch_request(self, lot_id=None): + def get_context(self, lot_id): # TODO @cayop adding filter # https://github.com/eReuse/devicehub-teal/blob/testing/ereuse_devicehub/resources/device/views.py#L56 filter_types = ['Desktop', 'Laptop', 'Server'] @@ -34,16 +34,22 @@ class DeviceListView(View): Device.updated.desc()) form_new_action = NewActionForm() - allocate = AllocateForm(start_time=datetime.datetime.now(), - end_time=datetime.datetime.now()+datetime.timedelta(1)) + form_new_allocate = AllocateForm() - context = {'devices': devices, + self.context = {'devices': devices, 'lots': lots, 'form_lot_device': LotDeviceForm(), 'form_new_action': form_new_action, - 'form_allocate': allocate, + 'form_new_allocate': form_new_allocate, 'lot': lot} - return flask.render_template(self.template_name, **context) + return self.context + + +class DeviceListView(DeviceListMix): + + def dispatch_request(self, lot_id=None): + self.get_context(lot_id) + return flask.render_template(self.template_name, **self.context) class DeviceDetailsView(View): @@ -167,20 +173,38 @@ class CreateDeviceView(View): class NewActionView(View): methods = ['POST'] decorators = [login_required] + _form = NewActionForm def dispatch_request(self): - form = NewActionForm() - # import pdb; pdb.set_trace() - next_url = url_for('inventory.devices.devicelist') - if form.validate_on_submit(): - # form.save() - if form.lot.data: - next_url = url_for('inventory.devices.lotdevicelist', id=form.lot.data) + self.form = self._form() + if self.form.validate_on_submit(): + self.form.save() + if self.form.lot.data: + next_url = url_for('inventory.devices.lotdevicelist', id=self.form.lot.data) + next_url = url_for('inventory.devices.devicelist') return flask.redirect(next_url) +class NewAllocateView(NewActionView, DeviceListMix): + methods = ['POST'] + decorators = [login_required] + template_name = 'inventory/device_list.html' + _form = AllocateForm + + def dispatch_request(self): + dispatch = super().dispatch_request() + if self.form.validate_on_submit(): + return dispatch + + lot_id = self.form.lot.data + self.get_context(lot_id) + self.context['form_new_allocate'] = self.form + return flask.render_template(self.template_name, **self.context) + + devices.add_url_rule('/action/add/', view_func=NewActionView.as_view('action_add')) +devices.add_url_rule('/action/allocate/add/', view_func=NewAllocateView.as_view('allocate_add')) devices.add_url_rule('/device/', view_func=DeviceListView.as_view('devicelist')) devices.add_url_rule('/device//', view_func=DeviceDetailsView.as_view('device_details')) devices.add_url_rule('/lot//device/', view_func=DeviceListView.as_view('lotdevicelist')) diff --git a/ereuse_devicehub/resources/action/models.py b/ereuse_devicehub/resources/action/models.py index a213ced5..02045be9 100644 --- a/ereuse_devicehub/resources/action/models.py +++ b/ereuse_devicehub/resources/action/models.py @@ -215,13 +215,13 @@ class Action(Thing): @validates('end_time') def validate_end_time(self, _, end_time: datetime): - if self.start_time and end_time <= self.start_time: + if self.start_time and end_time < self.start_time: raise ValidationError('The action cannot finish before it starts.') return end_time @validates('start_time') def validate_start_time(self, _, start_time: datetime): - if self.end_time and start_time >= self.end_time: + if self.end_time and start_time > self.end_time: raise ValidationError('The action cannot start after it finished.') return start_time diff --git a/ereuse_devicehub/resources/action/views/views.py b/ereuse_devicehub/resources/action/views/views.py index 88c95438..cc036a61 100644 --- a/ereuse_devicehub/resources/action/views/views.py +++ b/ereuse_devicehub/resources/action/views/views.py @@ -256,6 +256,7 @@ class ActionView(View): return erased.post() a = resource_def.schema.load(json) + import pdb; pdb.set_trace() Model = db.Model._decl_class_registry.data[json['type']]() action = Model(**a) db.session.add(action) diff --git a/ereuse_devicehub/static/js/main_inventory.js b/ereuse_devicehub/static/js/main_inventory.js index fcac3c96..a8373f80 100644 --- a/ereuse_devicehub/static/js/main_inventory.js +++ b/ereuse_devicehub/static/js/main_inventory.js @@ -1,6 +1,12 @@ $(document).ready(function() { - $(".deviceSelect").on("change", deviceSelect); - // $('#selectLot').selectpicker(); + var show_action_form = $("#allocateModal").data('show-action-form'); + if (show_action_form != "None") { + $("#allocateModal .btn-primary").show(); + newAllocate(show_action_form); + } else { + $(".deviceSelect").on("change", deviceSelect); + } + // $('#selectLot').selectpicker(); }) function deviceSelect() { @@ -17,19 +23,15 @@ function deviceSelect() { $("#actionModal .btn-primary").hide(); } else { $("#addingLotModal .text-danger").hide(); - $("#addingLotModal .btn-primary").removeClass('d-none'); $("#addingLotModal .btn-primary").show(); $("#removeLotModal .text-danger").hide(); - $("#removeLotModal .btn-primary").removeClass('d-none'); $("#removeLotModal .btn-primary").show(); $("#actionModal .text-danger").hide(); - $("#actionModal .btn-primary").removeClass('d-none'); $("#actionModal .btn-primary").show(); $("#allocateModal .text-danger").hide(); - $("#allocateModal .btn-primary").removeClass('d-none'); $("#allocateModal .btn-primary").show(); } $.map($(".devicesList"), function(x) { diff --git a/ereuse_devicehub/templates/inventory/actions.html b/ereuse_devicehub/templates/inventory/actions.html index 45776ef5..3eb6e4a8 100644 --- a/ereuse_devicehub/templates/inventory/actions.html +++ b/ereuse_devicehub/templates/inventory/actions.html @@ -41,7 +41,7 @@ diff --git a/ereuse_devicehub/templates/inventory/addDeviceslot.html b/ereuse_devicehub/templates/inventory/addDeviceslot.html index e05f665f..f489d28c 100644 --- a/ereuse_devicehub/templates/inventory/addDeviceslot.html +++ b/ereuse_devicehub/templates/inventory/addDeviceslot.html @@ -24,7 +24,7 @@ diff --git a/ereuse_devicehub/templates/inventory/allocate.html b/ereuse_devicehub/templates/inventory/allocate.html index 82095be1..4cc1d153 100644 --- a/ereuse_devicehub/templates/inventory/allocate.html +++ b/ereuse_devicehub/templates/inventory/allocate.html @@ -1,4 +1,5 @@ -