def create_notification(request, notification_type_pk=None): notification_type = get_object_or_404(NotificationType, pk=notification_type_pk) if request.method == 'GET' and request.docstash.value: form = request.docstash.get('form') else: form = notification_type.form_cls(request.POST or None) document_form = DocumentForm(request.POST or None, request.FILES or None, document_pks=[x.pk for x in request.docstash.get('documents', [])], prefix='document') if request.method == 'POST': submit = request.POST.get('submit', False) autosave = request.POST.get('autosave', False) request.docstash.update({ 'form': form, 'type_id': notification_type_pk, 'documents': list(Document.objects.filter(pk__in=map(int, request.POST.getlist('documents')))), 'submission_forms': getattr(form, 'cleaned_data', {}).get('submission_forms', []), }) request.docstash.name = "%s" % notification_type.name if autosave: return HttpResponse(_('autosave successful')) if document_form.is_valid(): documents = set(request.docstash['documents']) documents.add(document_form.save()) replaced_documents = [x.replaces_document for x in documents if x.replaces_document] for doc in replaced_documents: # remove replaced documents if doc in documents: documents.remove(doc) request.docstash['documents'] = list(documents) document_form = DocumentForm(document_pks=[x.pk for x in documents], prefix='document') if submit and form.is_valid(): notification = form.save(commit=False) notification.type = notification_type notification.save() submission_forms = form.cleaned_data['submission_forms'] notification.submission_forms = submission_forms notification.investigators.add(*Investigator.objects.filter(submission_form__in=submission_forms)) notification.documents = request.docstash['documents'] request.docstash.delete() return HttpResponseRedirect(reverse('ecs.notifications.views.view_notification', kwargs={'notification_pk': notification.pk})) return render(request, 'notifications/form.html', { 'notification_type': notification_type, 'form': form, 'tabs': NOTIFICATION_FORM_TABS[form.__class__], 'document_form': document_form, 'documents': request.docstash.get('documents', []), })
def create_submission_form(request): if request.method == 'GET' and request.docstash.value: form = request.docstash['form'] formsets = request.docstash['formsets'] else: form = SubmissionFormForm(request.POST or None) formsets = get_submission_formsets(request.POST or None) if request.method == 'GET': # neither docstash nor POST data: this is a completely new submission # => prepopulate submitter_* fields profile = request.user.get_profile() form.initial.update({ 'submitter_contact_first_name': request.user.first_name, 'submitter_contact_last_name': request.user.last_name, 'submitter_email': request.user.email, 'submitter_contact_gender': profile.gender, 'submitter_contact_title': profile.title, 'submitter_organisation': profile.organisation, 'submitter_jobtitle': profile.jobtitle, }) doc_post = 'document-file' in request.FILES document_form = DocumentForm(request.POST if doc_post else None, request.FILES if doc_post else None, document_pks=[x.pk for x in request.docstash.get('documents', [])], prefix='document' ) valid = False if request.method == 'POST': submit = request.POST.get('submit', False) autosave = request.POST.get('autosave', False) request.docstash.update({ 'form': form, 'formsets': formsets, 'documents': list(Document.objects.filter(pk__in=map(int, request.POST.getlist('documents')))), }) request.docstash.name = "%s" % request.POST.get('project_title', '') if autosave: return HttpResponse('autosave successfull') if document_form.is_valid(): documents = set(request.docstash['documents']) documents.add(document_form.save()) replaced_documents = [x.replaces_document for x in documents if x.replaces_document] for doc in replaced_documents: # remove replaced documents if doc in documents: documents.remove(doc) request.docstash['documents'] = list(documents) document_form = DocumentForm(document_pks=[x.pk for x in documents], prefix='document') valid = form.is_valid() and all(formset.is_valid() for formset in formsets.itervalues()) and not 'upload' in request.POST if submit and valid: if not request.user.get_profile().approved_by_office: messages.add_message(request, messages.INFO, _('You cannot submit studies yet. Please wait until the office has approved your account.')) else: submission_form = form.save(commit=False) submission = request.docstash.get('submission') or Submission.objects.create() submission_form.submission = submission submission_form.presenter = request.user submission_form.save() form.save_m2m() submission_form.documents = request.docstash['documents'] submission_form.save() for doc in request.docstash['documents']: doc.parent_object = submission_form doc.save() formsets = formsets.copy() investigators = formsets.pop('investigator').save(commit=False) for investigator in investigators: investigator.submission_form = submission_form investigator.save() for i, employee in enumerate(formsets.pop('investigatoremployee').save(commit=False)): employee.investigator = investigators[int(request.POST['investigatoremployee-%s-investigator_index' % i])] employee.save() for formset in formsets.itervalues(): for instance in formset.save(commit=False): instance.submission_form = submission_form instance.save() request.docstash.delete() return HttpResponseRedirect(reverse('ecs.core.views.readonly_submission_form', kwargs={'submission_form_pk': submission_form.pk})) context = { 'form': form, 'tabs': SUBMISSION_FORM_TABS, 'document_form': document_form, 'documents': request.docstash.get('documents', []), 'valid': valid, 'submission': request.docstash.get('submission', None) } for prefix, formset in formsets.iteritems(): context['%s_formset' % prefix] = formset return render(request, 'submissions/form.html', context)