1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
from django.contrib import admin
from django.contrib.admin import AdminSite
from django.db.models import F
from django.http import HttpResponseRedirect
from django.urls import reverse
from django_object_actions import (
DjangoObjectActions,
action,
takes_instance_or_queryset,
)
from .models import Choice, Comment, Poll, RelatedData
class ChoiceAdmin(DjangoObjectActions, admin.ModelAdmin):
list_display = ("poll", "choice_text", "votes")
# Actions
#########
@action(
description="+1",
label="vote++",
attrs={
"test": '"foo&bar"',
"Robert": '"); DROP TABLE Students; ', # 327
"class": "addlink",
},
)
@takes_instance_or_queryset
def increment_vote(self, request, queryset):
queryset.update(votes=F("votes") + 1)
actions = ["increment_vote"]
# Object actions
################
@action(description="-1")
def decrement_vote(self, request, obj):
obj.votes -= 1
obj.save()
def delete_all(self, request, queryset):
self.message_user(request, "just kidding!")
@action(description="0", methods=("POST",), button_type="form")
def reset_vote(self, request, obj):
obj.votes = 0
obj.save()
def edit_poll(self, request, obj):
url = reverse("admin:polls_poll_change", args=(obj.poll.pk,))
return HttpResponseRedirect(url)
def raise_key_error(self, request, obj):
raise KeyError
change_actions = (
"increment_vote",
"decrement_vote",
"reset_vote",
"edit_poll",
"raise_key_error",
)
changelist_actions = ("delete_all",)
admin.site.register(Choice, ChoiceAdmin)
class ChoiceInline(admin.StackedInline):
model = Choice
extra = 3
class PollAdmin(DjangoObjectActions, admin.ModelAdmin):
# List
######
list_display = ("question", "pub_date", "was_published_recently")
list_filter = ["pub_date"]
search_fields = ["question"]
date_hierarchy = "pub_date"
def changelist_view(self, request, extra_context=None):
extra_context = {"foo": "changelist_view"}
return super().changelist_view(request, extra_context)
# Detail
########
fieldsets = [
(None, {"fields": ["question"]}),
("Date information", {"fields": ["pub_date"], "classes": ["collapse"]}),
]
inlines = [ChoiceInline]
def change_view(self, request, object_id, form_url="", extra_context=None):
extra = {"foo": "change_view"}
return super().change_view(request, object_id, form_url, extra)
# Object actions
################
@action(label="Delete All Choices")
def delete_all_choices(self, request, obj):
from django.shortcuts import render
if request.method == "POST":
obj.choice_set.all().delete()
return None
self.message_user(request, "All choices deleted")
return render(request, "clear_choices.html", {"object": obj})
def question_mark(self, request, obj):
"""Add a question mark."""
obj.question = obj.question + "?"
obj.save()
change_actions = ("delete_all_choices", "question_mark")
def get_change_actions(self, request, object_id, form_url):
actions = super().get_change_actions(request, object_id, form_url)
actions = list(actions)
if not request.user.is_superuser:
return []
obj = self.model.objects.get(pk=object_id)
if obj.question.endswith("?"):
actions.remove("question_mark")
return actions
admin.site.register(Poll, PollAdmin)
class CommentAdmin(DjangoObjectActions, admin.ModelAdmin):
# Object actions
################
def hodor(self, request, obj):
if not obj.comment:
# bail because we need a comment
return
obj.comment = " ".join(["hodor" for x in obj.comment.split()])
obj.save()
change_actions = ("hodor",)
admin.site.register(Comment, CommentAdmin)
class RelatedDataAdmin(DjangoObjectActions, admin.ModelAdmin):
# Object actions
################
def fill_up(self, request, obj):
if not obj.extra_data:
# bail because we need a comment
obj.extra_data = "hodor"
else:
obj.extra_data = ""
obj.save()
change_actions = ("fill_up",)
admin.site.register(RelatedData, RelatedDataAdmin)
support_admin = AdminSite(name="support")
support_admin.register(Poll, PollAdmin)
|