def changelist_view(self, *args, **kwargs): """ Redirect to the add view if no records exist or the change view if the singlton instance exists. """ try: singleton = self.model.objects.get() except self.model.MultipleObjectsReturned: return super(SingletonAdmin, self).changelist_view(*args, **kwargs) except self.model.DoesNotExist: add_url = admin_url(model, "add") return HttpResponseRedirect(add_url) else: change_url = admin_url(self.model, "change", singleton.id) return HttpResponseRedirect(change_url)
def changelist_view(self, request, extra_context=None): """ Redirect to the ``Page`` changelist view for ``Page`` subclasses. """ if self.model is not Page: return HttpResponseRedirect(admin_url(Page, "changelist")) return super(PageAdmin, self).changelist_view(request, extra_context)
def add_view(self, request, **kwargs): """ For the ``Page`` model, redirect to the add view for the ``ContentPage`` model. """ if self.model is Page: add_url = admin_url(ContentPage, "add") return HttpResponseRedirect(add_url) return super(PageAdmin, self).add_view(request, **kwargs)
def add_view(self, *args, **kwargs): """ Redirect to the change view if the singlton instance exists. """ try: singleton = self.model.objects.get() except (self.model.DoesNotExist, self.model.MultipleObjectsReturned): return super(SingletonAdmin, self).add_view(*args, **kwargs) else: change_url = admin_url(self.model, "change", singleton.id) return HttpResponseRedirect(change_url)
def models_for_pages(*args): """ Create a select list containing each of the models that subclass the ``Page`` model. """ page_models = [] for model in get_models(): if model is not Page and issubclass(model, Page): setattr(model, "name", model._meta.verbose_name) setattr(model, "add_url", admin_url(model, "add")) page_models.append(model) return page_models
def change_view(self, request, object_id, extra_context=None): """ For the ``Page`` model, check ``page.get_content_model()`` for a subclass and redirect to its admin change view. """ if self.model is Page: page = get_object_or_404(Page, pk=object_id) content_model = page.get_content_model() if content_model is not None: change_url = admin_url(content_model.__class__, "change", content_model.id) return HttpResponseRedirect(change_url) return super(PageAdmin, self).change_view(request, object_id, extra_context=None)
def export_view(self, request, form_id): """ Output a CSV file to the browser containing the entries for the form. """ if request.POST.get("back"): change_url = admin_url(Form, "change", form_id) return HttpResponseRedirect(change_url) form = get_object_or_404(Form, id=form_id) export_form = ExportForm(form, request, request.POST or None) if export_form.is_valid(): response = HttpResponse(mimetype="text/csv") fname = "%s-%s.csv" % (form.slug, slugify(datetime.now().ctime())) response["Content-Disposition"] = "attachment; filename=%s" % fname csv = writer(response) csv.writerow(export_form.columns()) for rows in export_form.rows(): csv.writerow(rows) return response template = "admin/forms/export.html" context = {"title": _("Export Entries"), "export_form": export_form} return render_to_response(template, context, RequestContext(request))
def get_admin_url(self): return admin_url(self, "change", self.id)
def changelist_redirect(self): changelist_url = admin_url(Setting, "changelist") return HttpResponseRedirect(changelist_url)
def admin_app_list(request): """ Adopted from ``django.contrib.admin.sites.AdminSite.index``. Returns a list of lists of models grouped and ordered according to ``mezzanine.conf.ADMIN_MENU_ORDER``. Called from the ``admin_dropdown_menu`` template tag as well as the ``app_list`` dashboard widget. """ app_dict = {} menu_order = [(x[0], list(x[1])) for x in settings.ADMIN_MENU_ORDER] found_items = set() for (model, model_admin) in admin.site._registry.items(): opts = model._meta in_menu = not hasattr(model_admin, "in_menu") or model_admin.in_menu() if in_menu and request.user.has_module_perms(opts.app_label): perms = model_admin.get_model_perms(request) admin_url_name = "" if perms["change"]: admin_url_name = "changelist" elif perms["add"]: admin_url_name = "add" if admin_url_name: model_label = "%s.%s" % (opts.app_label, opts.object_name) for (name, items) in menu_order: try: index = list(items).index(model_label) except ValueError: pass else: found_items.add(model_label) app_title = name break else: index = None app_title = opts.app_label model_dict = { "index": index, "perms": model_admin.get_model_perms(request), "name": capfirst(model._meta.verbose_name_plural), "admin_url": admin_url(model, admin_url_name), } app_title = app_title.title() if app_title in app_dict: app_dict[app_title]["models"].append(model_dict) else: try: titles = [x[0] for x in settings.ADMIN_MENU_ORDER] index = titles.index(app_title) except ValueError: index = None app_dict[app_title] = { "index": index, "name": app_title, "models": [model_dict], } for (i, (name, items)) in enumerate(menu_order): name = unicode(name) for unfound_item in set(items) - found_items: if isinstance(unfound_item, (list, tuple)): item_name, item_url = unfound_item[0], try_url(unfound_item[1]) if item_url: if name not in app_dict: app_dict[name] = { "index": i, "name": name, "models": [], } app_dict[name]["models"].append({ "index": items.index(unfound_item), "perms": {"custom": True}, "name": item_name, "admin_url": item_url, }) app_list = app_dict.values() sort = lambda x: x["name"] if x["index"] is None else x["index"] for app in app_list: app["models"].sort(key=sort) app_list.sort(key=sort) return app_list