Skip to content

Instantly share code, notes, and snippets.

@mariocesar
Last active December 28, 2023 19:17
Show Gist options
  • Save mariocesar/8adc56de00104e90bac7 to your computer and use it in GitHub Desktop.
Save mariocesar/8adc56de00104e90bac7 to your computer and use it in GitHub Desktop.

Revisions

  1. mariocesar revised this gist Apr 30, 2022. 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
    @@ -4,6 +4,7 @@

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


  2. mariocesar revised this gist Apr 30, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion decorators.py
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ def wrapper(self, request, queryset):
    form = form_class()

    if 'confirm' in request.POST and request.POST:
    form = form_class(request.POST)
    form = form_class(request.POST, request.FILES)
    if form.is_valid():
    obj_count = func(self, request, queryset, form)
    self.message_user(request, '%s objects updated' % obj_count)
  3. mariocesar revised this gist Apr 30, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion form_action_confirmation.html
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,7 @@
    </ul>
    <hr>
    <br>
    <form action="" method="post">{% csrf_token %}
    <form action="" method="post" enctype="multipart/form-data">{% csrf_token %}
    <fieldset class="module aligned">
    {% for obj in queryset.all %}
    <input type="hidden" name="{{ action_checkbox_name }}" value="{{ obj.pk|unlocalize }}"/>
  4. mariocesar revised this gist Feb 18, 2016. No changes.
  5. 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())


  6. 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.
  7. 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)
  8. 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 %}
  9. 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