Beispiel #1
0
 def clean_forms(self):
     required_forms = self.get_required_forms()
     forms = dict(self.forms)
     for key, form in forms.items():
         if not form.has_changed() and key not in required_forms:
             del forms[key]
     return forms
Beispiel #2
0
Datei: views.py Projekt: rerb/hub
    def post(self, request, *args, **kwargs):
        """
        Handle POST of multiple files.
        """
        forms = self.get_form()

        # Validate the form and formsets
        valid = True
        for key, form in forms.items():
            if not form.is_valid():
                valid = False

        # Validate conditionally_required
        meets_reqs = self.validate_conditional_requirements(forms)

        if valid and meets_reqs:
            instance = forms['document_form'].save(self.request)
            # Save formsets
            formset_keys = [
                'author_formset', 'file_formset', 'image_formset',
                'website_formset'
            ]
            for key in formset_keys:
                if key in forms.keys():
                    for form in forms[key]:
                        if form.has_changed():
                            form.save(instance=instance)

            instance.create_thumbnails()

            return HttpResponseRedirect(self.get_success_url())

        ctx = self.get_context_data(**forms)
        ctx.update({'errors': True})
        return self.render_to_response(ctx)
Beispiel #3
0
    def post(self, request, *args, **kwargs):
        """
        Handle POST of multiple files.
        """
        forms = self.get_form()

        # Validate the form and formsets
        valid = True
        for key, form in forms.items():
            if not form.is_valid():
                valid = False

        # Validate conditionally_required
        meets_reqs = self.validate_conditional_requirements(forms)

        if valid and meets_reqs:
            instance = forms['document_form'].save(self.request)
            # Save formsets
            formset_keys = [
                'author_formset', 'file_formset', 'image_formset',
                'website_formset'
            ]
            for key in formset_keys:
                if key in forms.keys():
                    for form in forms[key]:
                        if form.has_changed():
                            form.save(instance=instance)

            instance.create_thumbnails()

            return HttpResponseRedirect(self.get_success_url())

        ctx = self.get_context_data(**forms)
        ctx.update({'errors': True})
        return self.render_to_response(ctx)
Beispiel #4
0
 def get_form(with_data=False):
     # TODO: allow defaults from URL?
     if isinstance(form_cls, dict):
         assert "fh_form" in request.REQUEST
         form = form_cls[request.REQUEST["fh_form"]]
         forms = form_cls
         for k, f in forms.items():
             forms[k] = f(request) if pass_request else f()
     else:
         form = form_cls(request) if pass_request else form_cls()
         forms = { "form": form }
     if with_data:
         form.data = request.REQUEST
         form.files = request.FILES
         form.is_bound = True
     for f in forms.values():
         if hasattr(f, "init"):
             res = f.init(**kwargs)
             if res: return res
     return form, forms
Beispiel #5
0
    def post(self, request, *args, **kwargs):
        form_choices = request.session['form_choices']
        forms = {}
        valid = True
        for candidate, choices in form_choices.items():
            form = self.form_class(request.POST,
                                   companies=choices,
                                   prefix=candidate)
            if not form.is_valid():
                valid = False
            forms[candidate] = form
        if valid:
            new_names = []
            for name, form in forms.items():
                company_pk = form.cleaned_data.get('company_name')
                if company_pk == 'None':
                    company_pk = None
                position_pk = form.cleaned_data.get('position')
                position = Position.objects.get(pk=position_pk)
                if company_pk:
                    company = Company.objects.get(pk=company_pk)
                    position.company = company
                    position.save(update_fields=['company'])
                else:
                    new_names.append((position.id, name))

            if request.session.get('new_names') or new_names:
                request.session['new_names'] += new_names

            if request.session['new_names']:
                index = len(request.session['new_names']) - 1
                return redirect('company_create',
                                company=request.session['new_names'][index][1])
            else:
                return redirect('home')
        else:
            return render(request, self.template_name, {'forms': forms})