def deviceserver_verify_view(request, pk): context = {} if 'breadcrumb_extra_ancestors' not in context: context['breadcrumb_extra_ancestors'] = [] context['breadcrumb_extra_ancestors'].append( (request.get_full_path(), 'Device Server verification')) device_server = dsc_models.DeviceServer.objects.get(pk=pk) if request.user.has_perm('dsc.admin_deviceserver'): if device_server.status == dsc_models.STATUS_NEW or device_server.status == dsc_models.STATUS_UPDATED: with transaction.atomic(): verify_activity = dsc_models.DeviceServerActivity( device_server=device_server, activity_type=dsc_models.DS_ACTIVITY_VERIFICATION, activity_info= 'Recent update of device classes in this device server has been verified by %s.' % request.user.get_full_name(), created_by=request.user) device_server.status = dsc_models.STATUS_VERIFIED verify_activity.save() device_server.save() if device_server.device_classes.all().count() > 1: context['message'] = 'Device classes are verified.' else: context['message'] = 'The device class is verified.' else: if device_server.status != dsc_models.STATUS_VERIFIED: context[ 'message'] = 'Only add or update operations can be verified.' if device_server.status == dsc_models.STATUS_VERIFIED: context[ 'message'] = 'This device server and classes it provides are already verified.' return HttpResponseRedirect( reverse('deviceserver_detail', kwargs={'pk': device_server.pk}))
def form_valid(self, form): """This method is called when form has been filed correctly. It creates model objects and save them.""" # just let IDE knows the type assert (isinstance(form, DeviceServerAddForm)) # make sure that database is consisten with transaction.atomic(): # remember data passed from the form add_device = form.save() assert (isinstance(add_device, dsc_models.DeviceServerAddModel)) add_device.created_by = self.request.user add_device.save() # mark activity activity = dsc_models.DeviceServerActivity( activity_type=dsc_models.DS_ACTIVITY_ADD, activity_info='The device class has been added to catalogue.', created_by=self.request.user) # it is related by other object, so it is good if it has primary key activity.save() # create device server self.device_server, old_ds = update_logic.create_or_update( add_device, activity) # just to make sure we save device server self.device_server.save() activity.device_server = self.device_server activity.save() # mark success add_device.activity = activity add_device.processed_ok = True add_device.save() github_backup.save_xmi_on_github( self.device_server, add_device.xmi_string(raise_exception=False), json.dumps(get_ds_structure(self.device_server, self.request), indent=4, sort_keys=True, default=str), self.request) return super(DeviceServerAddView, self).form_valid(form)
def get_context_data(self, **kwargs): """Detail view context will provide device server which has to be updated with verified state""" context = super(DeviceServerVerifyView, self).get_context_data(**kwargs) device_server = context['deviceserver'] assert isinstance(device_server, dsc_models.DeviceServer) if device_server.status == dsc_models.STATUS_NEW or device_server.status == dsc_models.STATUS_UPDATED: with transaction.atomic(): verify_activity = dsc_models.DeviceServerActivity( device_server=device_server, activity_type=dsc_models.DS_ACTIVITY_VERIFICATION, activity_info= 'Last update to device class(es) have been verified by %s.' % self.request.user.get_full_name(), created_by=self.request.user) device_server.status = dsc_models.STATUS_VERIFIED verify_activity.save() device_server.save() if device_server.device_classes.all().count() > 1: context[ 'message'] = 'Classes provided by device server are verified.' else: context[ 'message'] = 'Class provided by this device server is verified.' else: if device_server.status != dsc_models.STATUS_VERIFIED: context[ 'message'] = 'Only add or update operations can be verified.' if device_server.status == dsc_models.STATUS_VERIFIED: if device_server.device_classes.all().count() == 1: context[ 'message'] = 'This device class is already verified.' else: context[ 'message'] = 'These device classes are already verified.' return context
def deviceserver_delete_view(request, pk): context = {} if 'breadcrumb_extra_ancestors' not in context: context['breadcrumb_extra_ancestors'] = [] context['breadcrumb_extra_ancestors'].append( (request.get_full_path(), 'Device Server/Class delete')) device_server = dsc_models.DeviceServer.objects.get(pk=pk) device_class = request.GET.get('device_class') if device_class is not None: clqf = device_server.device_classes.filter(pk=device_class) if clqf.count() == 1: context['device_class'] = clqf.first() context['deviceserver'] = device_server if request.user != device_server.created_by and not request.user.has_perm( 'dsc.admin_deviceserver'): context['operation'] = 'delete' return render(request, 'dsc/deviceserver_notauthorized.html', context) if request.method == 'GET': return render(request, 'dsc/deviceserver_delete_question.html', context) else: with transaction.atomic(): if device_class is None: clq = device_server.device_classes.all() else: clq = clqf if clq.count() == 1: if device_server.device_classes.all().count() == 1: activity = dsc_models.DeviceServerActivity(activity_type=dsc_models.DS_ACTIVITY_DELETE, activity_info='The device class %s has been deleted by %s %s.' % \ (clq.first().name, request.user.first_name, request.user.last_name), device_server = device_server, device_server_backup = device_server, created_by = request.user) activity.save() device_server.invalidate_activity = activity device_server.status = dsc_models.STATUS_DELETED device_server.save() elif device_server.device_classes.all().count() > 1: activity = dsc_models.DeviceServerActivity(activity_type=dsc_models.DS_ACTIVITY_DELETE, activity_info='The device class %s has been deleted by %s %s.' % \ (clq.first().name, request.user.first_name, request.user.last_name), device_server=device_server, created_by=request.user) activity.save() backup_device_server = device_server.make_backup( activity=activity) backup_device_server.save() activity.device_server_backup = backup_device_server activity.save() cl = clq.first() cl.device_server = backup_device_server cl.invalidate_activity = activity cl.save() else: return render('dsc/deviceserver_delete_question.html', context) return render('dsc/deviceserver_delete_confirmed.html', context)
def form_valid(self, form): """This method is called when form has been filed correctly. It creates model objects and save them.""" # just let IDE knows the type assert (isinstance(form, DeviceServerAddForm)) # make sure that database is consisten with transaction.atomic(): # remember data passed from the form update_object = form.save() assert (isinstance(update_object, dsc_models.DeviceServerAddModel)) update_object.created_by = self.request.user update_object.valid_xmi_string = form.cleaned_data.get( 'xmi_string', None) update_object.save() if not update_object.only_github: if update_object.add_class: ainfo = 'A device class has been added.' else: ainfo = 'The device class has been updated.' # mark activity activity = dsc_models.DeviceServerActivity( activity_type=dsc_models.DS_ACTIVITY_EDIT, activity_info=ainfo, created_by=self.request.user) # it is related by other object, so it is good if it has primary key activity.save() # create device server self.device_server, old_ds = update_logic.create_or_update( update_object, activity, self.device_server, device_class=self.request.GET.get('device_class', None)) # just to make sure we save device server self.device_server.save() activity.device_server = self.device_server activity.device_server_backup = old_ds activity.save() # mark success update_object.activity = activity update_object.processed_ok = True update_object.save() if update_object.use_uploaded_xmi_file or update_object.use_url_xmi_file: github_backup.save_xmi_on_github( self.device_server, update_object.xmi_string(raise_exception=False), json.dumps(get_ds_structure(self.device_server, self.request), indent=4, sort_keys=True, default=str), self.request) return super(DeviceServerUpdateView, self).form_valid(form)