Skip to content

Instantly share code, notes, and snippets.

@lvm
Forked from mariocesar/admin.py
Created February 11, 2020 22:04
Show Gist options
  • Save lvm/9a851d33f68b586ee0c9a14ad5d5207e to your computer and use it in GitHub Desktop.
Save lvm/9a851d33f68b586ee0c9a14ad5d5207e to your computer and use it in GitHub Desktop.

Revisions

  1. @mariocesar mariocesar revised this gist Feb 18, 2016. No changes.
  2. @mariocesar mariocesar revised this gist Feb 18, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions admin.py
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,7 @@


    class PostCategoryForm(forms.Form):
    title = 'Update category for the selected posts'
    category = forms.ModelChoiceField(queryset=Category.objects.all())


  3. @mariocesar mariocesar revised this gist Feb 18, 2016. 2 changed files with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion decorators.py
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,7 @@ def wrapper(self, request, queryset):
    queryset=queryset, form=form,
    action_checkbox_name=helpers.ACTION_CHECKBOX_NAME)

    return TemplateResponse(request, 'admin/action_form.html', context)
    return TemplateResponse(request, 'admin/form_action_confirmation.html', context)

    wrapper.short_description = form_class.title

    File renamed without changes.
  4. @mariocesar mariocesar revised this gist Feb 18, 2016. 1 changed file with 16 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions admin.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    from .models import Post, Category
    from .decorators import action_form


    class PostCategoryForm(forms.Form):
    category = forms.ModelChoiceField(queryset=Category.objects.all())


    @admin.register(Post)
    class PostAdmin(admin.ModelAdmin):
    actions = ['change_category']

    @action_form(PostCategoryForm)
    def change_category(self, request, queryset, form):
    category = form.cleaned_data['category']
    return queryset.update(category=category)
  5. @mariocesar mariocesar revised this gist Feb 18, 2016. 1 changed file with 42 additions and 0 deletions.
    42 changes: 42 additions & 0 deletions action_form.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    {% extends "admin/base_site.html" %}
    {% load i18n l10n admin_urls %}

    {% block bodyclass %}{{ block.super }} app-{{ opts.app_label }} model-{{ opts.model_name }} delete-confirmation
    delete-selected-confirmation{% endblock %}

    {% block breadcrumbs %}
    <div class="breadcrumbs">
    <a href="{% url 'admin:index' %}">{% trans 'Home' %}</a>
    &rsaquo; <a href="{% url 'admin:app_list' app_label=opts.app_label %}">{{ opts.app_config.verbose_name }}</a>
    &rsaquo; <a href="{% url opts|admin_urlname:'changelist' %}">{{ opts.verbose_name_plural|capfirst }}</a>
    &rsaquo; {{ title }}
    </div>
    {% endblock %}

    {% block content %}
    <ul style="padding: 0">
    {% for object in queryset.all %}
    <li style="list-style: none; float: left; margin: 5px">
    {{ object }}
    </li>
    {% endfor %}
    </ul>
    <hr>
    <br>
    <form action="" method="post">{% csrf_token %}
    <fieldset class="module aligned">
    {% for obj in queryset.all %}
    <input type="hidden" name="{{ action_checkbox_name }}" value="{{ obj.pk|unlocalize }}"/>
    {% endfor %}
    <div class="form-row">
    {{ form }}
    </div>
    </fieldset>
    <div class="submit-row">
    <input type="hidden" name="action" value="{{ action }}"/>
    <input type="submit" name="confirm" value="{% trans "Confirm" %}"/>
    <a href="#" onclick="window.history.back(); return false;"
    class="button cancel-link">{% trans "No, take me back" %}</a>
    </div>
    </form>
    {% endblock %}
  6. @mariocesar mariocesar created this gist Feb 18, 2016.
    34 changes: 34 additions & 0 deletions decorators.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    import functools

    from django.contrib.admin import helpers
    from django.template.response import TemplateResponse


    def action_form(form_class=None):
    def decorator(func):
    @functools.wraps(func)
    def wrapper(self, request, queryset):
    form = form_class()

    if 'confirm' in request.POST and request.POST:
    form = form_class(request.POST)
    if form.is_valid():
    obj_count = func(self, request, queryset, form)
    self.message_user(request, '%s objects updated' % obj_count)
    return None

    context = dict(
    self.admin_site.each_context(request),
    title=form_class.title,
    action=func.__name__,
    opts=self.model._meta,
    queryset=queryset, form=form,
    action_checkbox_name=helpers.ACTION_CHECKBOX_NAME)

    return TemplateResponse(request, 'admin/action_form.html', context)

    wrapper.short_description = form_class.title

    return wrapper

    return decorator