Exemple #1
0
    def add_view(self, request, form_url="", extra_context=None):
        model = self.model
        opts = model._meta

        if request.method == "POST":
            if "cancel" in request.POST:
                return redirect_to(request, reverse("admin:materials_library_changelist"))
            form = LibraryAddForm(request.POST, initial=dict(creator=request.user))
            authors_formset = AuthorsFormSet(request.POST, prefix="authors", queryset=Author.objects.none())
            if form.is_valid() and authors_formset.is_valid():
                obj = form.save()
                saved_authors = authors_formset.save()
                # TODO: reindex all related objects if author info were changed
                for author in saved_authors:
                    obj.authors.add(author)
                obj.save()
                return self.response_add(request, obj)
        else:
            form = LibraryAddForm(initial=dict(creator=request.user))
            authors_formset = AuthorsFormSet(prefix="authors", queryset=Author.objects.none())

        context = {
            "title": _("Add %s") % force_unicode(opts.verbose_name),
            "form": form,
            "authors_formset": authors_formset,
            "main_fields": MAIN_FIELDS_CHANGE,
            "is_popup": request.REQUEST.has_key("_popup"),
            "show_delete": False,
            "app_label": opts.app_label,
            "media": mark_safe(
                self.media + forms.Media(js=["%s%s" % (settings.ADMIN_MEDIA_PREFIX, "js/collapse.min.js")])
            ),
        }
        context.update(extra_context or {})
        return self.render_change_form(request, context, form_url=form_url, add=True)
    def add_view(self, request, form_url='', extra_context=None):
        model = self.model
        opts = model._meta

        if request.method == "POST":
            if "cancel" in request.POST:
                return redirect_to(request, reverse("admin:materials_communityitem_changelist"))
            form = CommunityItemAddForm(request.POST, initial=dict(creator=request.user))
            authors_formset = AuthorsFormSet(request.POST, prefix="authors", queryset=Author.objects.none())
            if form.is_valid() and authors_formset.is_valid():
                obj = form.save()
                saved_authors = authors_formset.save()
                # TODO: reindex all related objects if author info were changed
                for author in saved_authors:
                    obj.authors.add(author)
                obj.save()
                return self.response_add(request, obj)
        else:
            form = CommunityItemAddForm(initial=dict(creator=request.user))
            authors_formset = AuthorsFormSet(prefix="authors", queryset=Author.objects.none())

        context = {
            'title': _('Add %s') % force_unicode(opts.verbose_name),
            'form': form,
            'authors_formset': authors_formset,
            'main_fields': MAIN_FIELDS_CHANGE,
            'is_popup': request.REQUEST.has_key('_popup'),
            'show_delete': False,
            'app_label': opts.app_label,
            'media': mark_safe(self.media + forms.Media(js=['%s%s' % (settings.ADMIN_MEDIA_PREFIX, 'js/collapse.min.js')])),
        }
        context.update(extra_context or {})
        return self.render_change_form(request, context, form_url=form_url, add=True)
Exemple #3
0
    def change_view(self, request, object_id, extra_context=None):
        model = self.model
        opts = model._meta

        obj = self.get_object(request, unquote(object_id))

        if not self.has_change_permission(request, obj):
            raise PermissionDenied

        if obj is None:
            raise Http404(
                _("%(name)s object with primary key %(key)r does not exist.")
                % {"name": force_unicode(opts.verbose_name), "key": escape(object_id)}
            )

        if request.method == "POST":
            if "cancel" in request.POST:
                return redirect_to(request, reverse("admin:materials_library_changelist"))
            form = LibraryAddForm(request.POST, instance=obj)
            authors_formset = AuthorsFormSet(request.POST, prefix="authors", queryset=obj.authors.all())
            if form.is_valid() and authors_formset.is_valid():
                obj = form.save()
                saved_authors = authors_formset.save()
                # TODO: reindex all related objects if author info were changed
                existing_authors = list(obj.authors.all())
                for author in saved_authors:
                    if author not in existing_authors:
                        obj.authors.add(author)
                obj.save()
                return self.response_change(request, obj)
        else:
            form = LibraryAddForm(instance=obj)
            authors_formset = AuthorsFormSet(prefix="authors", queryset=obj.authors.all())

        tags = obj.tags.distinct().values_list("name", flat=True)
        alignment_tags = obj.alignment_tags.values_list("tag", flat=True).distinct()
        alignment_tags = AlignmentTag.objects.filter(id__in=alignment_tags)

        context = {
            "title": _("Change %s") % force_unicode(opts.verbose_name),
            "form": form,
            "authors_formset": authors_formset,
            "main_fields": MAIN_FIELDS_CHANGE,
            "tags": tags,
            "alignment_tags": alignment_tags,
            "object_id": object_id,
            "original": obj,
            "root_path": self.admin_site.root_path,
            "app_label": opts.app_label,
            "media": mark_safe(
                self.media + forms.Media(js=["%s%s" % (settings.ADMIN_MEDIA_PREFIX, "js/collapse.min.js")])
            ),
        }
        context.update(extra_context or {})
        return self.render_change_form(request, context, change=True, obj=obj)