More work on admin

This commit is contained in:
Daniel Greenfeld 2013-05-31 17:38:34 +02:00
parent 0da6f453b7
commit 1195f38b84
4 changed files with 37 additions and 17 deletions

View file

@ -3,6 +3,7 @@ from django.core.exceptions import PermissionDenied
from django.template.response import TemplateResponse
from django.utils.encoding import force_text
from django.utils.text import capfirst
from django.utils.translation import ugettext_lazy, ugettext as _
from . import utils
@ -54,7 +55,7 @@ class BaseListAction(object):
class DeleteSelectedAction(BaseListAction):
description = "Delete selected items"
description = ugettext_lazy("Delete selected items")
def get_response(self):
if self.request.POST.get('confirmed'):
@ -62,8 +63,8 @@ class DeleteSelectedAction(BaseListAction):
if self.has_permission:
num_objects_deleted = len(self.queryset)
self.queryset.delete()
message = "Successfully deleted %d %s" % \
(num_objects_deleted, self.objects_name)
message = _("Successfully deleted %d %s" % \
(num_objects_deleted, self.objects_name))
messages.add_message(self.request, messages.INFO, message)
return None
else:
@ -89,7 +90,7 @@ class DeleteSelectedAction(BaseListAction):
}
return TemplateResponse(self.request, template, context)
else:
message = "Permission to delete %s denied" % self.objects_name
message = _("Permission to delete %s denied" % self.objects_name)
messages.add_message(self.request, messages.INFO, message)
return None

View file

@ -52,18 +52,6 @@ class ModelListView(AdminModel2Mixin, generic.ListView):
permissions.IsStaffPermission,
permissions.ModelViewPermission)
# def post(self, request):
# # This is where we handle actions
# action_name = request.POST['action']
# action_func = self.get_actions()[action_name]['func']
# selected_model_pks = request.POST.getlist('selected_model_pk')
# queryset = self.model.objects.filter(pk__in=selected_model_pks)
# response = action_func(request, queryset)
# if response is None:
# return HttpResponseRedirect(self.get_success_url())
# else:
# return response
def post(self, request):
action_name = request.POST['action']
action_class = self.get_actions()[action_name]['action_class']

View file

@ -15,6 +15,7 @@ The documentation works off a simple set of models, as listed below.
.. code-block:: python
# blog/models.py
from django.db import models
@ -33,6 +34,34 @@ The documentation works off a simple set of models, as listed below.
def __unicode__(self):
return self.body
Actions
=======
Actions are defined to work on a single view type. Currently, actions are only implemented against the ``ModelListView``. This view contains the default ``DeleteSelectedAction`` method, which in end functionality mirrors ``django.contrib.admin.delete_selected``.
However, under the hood, django-admin2's actions work very differently. Instead of functions with assigned attributes, they are full fledged objects. Which means you can more easily extend them to suit your needs.
Writing List Actions
-----------------------
Using our sample models, let's pretend we wrote a blog article about Django and our mother put in a whole bunch of embarressing comments. Rather than cherry-pick the comments, we want to delete the whole batch.
In our blog/admin.py module we write:
.. code-block:: python
import djadmin2
from .models import Post, Comment
class DeleteAllComments(BaseListAction):
description = ugettext_lazy("Delete selected items")
djadmin2.default.register(Post, PostAdmin)
djadmin2.default.register(Comment)
TODO - FINISH AFTER CHECKING THE MODEL CONTROL BUG!!!
Permissions
===========

View file

@ -1,14 +1,16 @@
# Import your custom models
from .models import Post, Comment
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.contrib.auth.models import Group, User
from rest_framework.relations import PrimaryKeyRelatedField
import djadmin2
from djadmin2.forms import floppify_form
from djadmin2.apiviews import Admin2APISerializer
from .models import Post, Comment
UserCreationForm = floppify_form(UserCreationForm)
UserChangeForm = floppify_form(UserChangeForm)