def post(self, request, **kwargs): """POST request handler for Update operation""" operation = self.__class__.__name__ redirect_to_update = False instance = get_model_instance(**kwargs) app_title = get_request_params('app_name', **kwargs) form = get_form(**kwargs)(request.POST, instance=instance) formsets = get_formsets(operation, **kwargs) (result, context) = FormHandler(operation, request, form, formsets, instance).handle(**kwargs) if not result: return render(request, 'core/update.html', context) if 'save' in request.POST: redirect_to_update = True instance.update() elif 'finish' in request.POST: instance.finish() else: next_activity = request.POST['submit'] if not instance.validate_rule(next_activity): redirect_to_update = True else: instance.task.submit(app_title, self.request.user, next_activity) return HttpResponseRedirect( reverse('update', args=( app_title, instance.title, instance.id))) if redirect_to_update else HttpResponseRedirect( reverse('workflow-detail', args=[app_title]))
def handle(self, **kwargs): """Adds, validates and persist formsets""" instruction = next( iter(filter(lambda key: 'add-' in key, self.request.POST)), None) # Handle adding related instance if instruction: formsets = get_formsets(self.operation, **kwargs) return self.add(formsets, instruction) # Validate and save form/formsets formsets = [] for formset in self.formsets: formsets.append( formset( self.request.POST, instance=self.instance, # None for create operation prefix=formset.form.__name__)) if self.form.is_valid() and all(formset.is_valid() for formset in formsets): return self.save(formsets, **kwargs) else: return self.report(formsets)
def get(self, request, **kwargs): """GET request handler for Create operation""" form = get_form(**kwargs) formsets = [ formset(prefix=formset.form.__name__) for formset in get_formsets( self.__class__.__name__, extra=1, **kwargs) ] context = {'form': form, 'formsets': formsets} denied = self.check(request, **kwargs) return denied if denied else render(request, 'core/create.html', context)
def get(self, request, **kwargs): """GET request handler for Update operation""" instance = get_model_instance(**kwargs) form = get_form(**kwargs) formsets = get_formsets(self.__class__.__name__, extra=1, **kwargs) context = { 'form': form(instance=instance), 'formsets': [ formset(instance=instance, prefix=formset.form.__name__) for formset in formsets ], 'object': instance, 'next': instance.next_activity() } denied = self.check(request, **kwargs) return denied if denied else render(request, 'core/update.html', context)
def post(self, request, **kwargs): """POST request handler for Create operation""" operation = self.__class__.__name__ instance = None form = get_form(**kwargs)(request.POST) formsets = get_formsets(operation, **kwargs) app_title = get_request_params('app_name', **kwargs) (result, context) = FormHandler(operation, request, form, formsets).handle(**kwargs) if not result: return render(request, 'core/create.html', context) else: instance = context if instance.is_initial: instance.initiate_request(request.user, app_title) else: instance.assign_task(get_request_params('pk', **kwargs)) instance.task.initiate() return HttpResponseRedirect( reverse('update', args=(app_title, instance.title, instance.id)))