def log_delete_object(obj: Model, user: User): log_action( action=constants.ACTION_DELETE, user=user, instance=obj, prev_data=model_to_dict(obj), )
def get_object(self, queryset=None): try: global_settings = GlobalSettings.objects.get() self._prev_data = model_to_dict(global_settings) return global_settings except GlobalSettings.DoesNotExist: return GlobalSettings.objects\ .create(**GLOBAL_SETTINGS_DEFAULTS)
def log_create_object(instance: Model, user: User = None, fields=None, exclude=None): log_action(action=constants.ACTION_CREATE, user=user, instance=instance, next_data=model_to_dict(instance=instance, fields=fields, exclude=exclude))
def log_update_object(obj: Model, user: User, object_repr: str = None, prev_data: dict = None, fields=None, exclude=None): log_action(action=constants.ACTION_UPDATE, user=user, instance=obj, object_repr=object_repr, prev_data=prev_data, next_data=model_to_dict(instance=obj, fields=fields, exclude=exclude))
def post(self, request, **kwargs): context = self.get_context_data(**kwargs) component = context['component'] json_schema = context['json_schema'] schema_json = json_schema.schema_json prev_data = model_to_dict(json_schema) form = JSONSchemaForm(data=request.POST) form.fields['schema_json'].widget.attrs.update( {'rows': get_rows_length(schema_json)}) if form.is_valid(): json_schema.schema_json = form.cleaned_data['schema_json'] json_schema.save() log_update_object(obj=json_schema, object_repr=component.name, user=self.request.user, prev_data=prev_data) messages.success( request, _('%(component)s JSON schema ' 'was successfully changed.') % { 'component': component, }) context.update({ 'form': form, }) return self.render_to_response(context)
def post(self, request, **kwargs): context = self.get_context_data(**kwargs) config = context['config'] environment = context['environment'] self._prev_data = model_to_dict(config) form = ConfigForm( config=config, data=request.POST, ) form.fields['settings_json'].widget.attrs.update( {'rows': get_rows_length(request.POST.get('settings_json', ''))}) if form.is_valid(): config = update_config( config=config, settings_json=form.cleaned_data['settings_json'], commit=True) log_update_object(obj=config, user=self.request.user, prev_data=self._prev_data) messages.success( request, _('%(component)s %(environment)s settings ' 'were successfully changed.') % { 'component': config.component, 'environment': environment or _('Base'), }) context.update({'form': form}) return self.render_to_response(context)
def get_object(self, queryset=None): obj = super().get_object(queryset=queryset) self._prev_data = model_to_dict(obj, fields=self.fields) return obj