def fields(request, field_id=None): if field_id: field = get_object_or_404(models.Field, pk=field_id, journal=request.journal) else: field = None fields = models.Field.objects.filter(journal=request.journal) form = forms.FieldForm(instance=field) if request.POST: if 'save' in request.POST: form = forms.FieldForm(request.POST, instance=field) if form.is_valid(): new_field = form.save(commit=False) new_field.journal = request.journal new_field.save() messages.add_message(request, messages.SUCCESS, 'Field saved.') return redirect(reverse('submission_fields')) elif 'delete' in request.POST: delete_id = request.POST.get('delete') field_to_delete = get_object_or_404(models.Field, pk=delete_id, journal=request.journal) field_to_delete.delete() messages.add_message( request, messages.SUCCESS, 'Field deleted. Existing answers will remain intact.') return redirect(reverse('submission_fields')) elif 'order[]' in request.POST: ids = [int(_id) for _id in request.POST.getlist('order[]')] for field in fields: order = ids.index(field.pk) field.order = order field.save() return HttpResponse('Thanks') template = 'admin/submission/manager/fields.html' context = { 'field': field, 'fields': fields, 'form': form, } return render(request, template, context)
def fields(request, field_id=None): """ Allows the editor to create, edit and delete new submission fields. :param request: HttpRequest object :param field_id: Field object PK, optional :return: HttpResponse or HttpRedirect """ field = logic.get_current_field(request, field_id) fields = logic.get_submission_fields(request) form = forms.FieldForm(instance=field) if request.POST: if 'save' in request.POST: form = forms.FieldForm(request.POST, instance=field) if form.is_valid(): logic.save_field(request, form) return redirect(reverse('submission_fields')) elif 'delete' in request.POST: logic.delete_field(request) return redirect(reverse('submission_fields')) elif 'order[]' in request.POST: logic.order_fields(request, fields) return HttpResponse('Thanks') template = 'admin/submission/manager/fields.html' context = { 'field': field, 'fields': fields, 'form': form, } return render(request, template, context)