Commits

jkoc...@bcc190cf-cafb-0310-a4f2-bffc1f526a37  committed 643e229

Made generic-auth backwards compatible with the trunk.

  • Participants
  • Parent commits 6ca9424
  • Branches generic-auth

Comments (0)

Files changed (4)

File django/contrib/admin/templatetags/adminapplist.py

                 model_list = []
                 for m in app_models:
                     if m._meta.admin:
-                        opts = m._meta
+                        # TODO: convert this to use Permission objects instead of strings
                         perms = {
-                            'add': has_permission(user, opts.get_add_permission()),
-                            'change': has_permission(user, opts.get_change_permission()),
-                            'delete': has_permission(user, opts.get_delete_permission()),
+                            'add': user.has_perm("%s.%s" % (app_label, m._meta.get_add_permission())),
+                            'change': user.has_perm("%s.%s" % (app_label, m._meta.get_change_permission())),
+                            'delete': user.has_perm("%s.%s" % (app_label, m._meta.get_delete_permission())),
                         }
 
                         # Check whether user has any perm for this module.

File django/contrib/admin/views/main.py

     extra_context = {
         'add': add,
         'change': change,
-        'has_delete_permission': context['perms'][app_label][opts.get_delete_permission().codename],
-        'has_change_permission': context['perms'][app_label][opts.get_change_permission().codename],
+        'has_delete_permission': context['perms'][app_label][opts.get_delete_permission()],
+        'has_change_permission': context['perms'][app_label][opts.get_change_permission()],
         'has_file_field': opts.has_field_type(models.FileField),
         'has_absolute_url': hasattr(model, 'get_absolute_url'),
         'auto_populated_fields': auto_populated_fields,
         raise Http404("App %r, model %r, not found" % (app_label, model_name))
     opts = model._meta
 
-    if not has_permission(request.user, opts.get_add_permission()):
+    if not has_permission(request.user, opts.add_permission):
         raise PermissionDenied
 
     manipulator = model.AddManipulator()
                 # We want to call has permission WITHOUT passing it the new
                 # object here. We're concerned with whether the user can edit
                 # ANY instances of this model, not just the one we created.
-                if has_permission(request.user, opts.get_change_permission()):
+                if has_permission(request.user, opts.change_permission):
                     # redirect to list view
                     post_url = '../'
                 else:
     except model.DoesNotExist:
         raise Http404('%s object with primary key %r does not exist' % (model_name, escape(object_id)))
 
-    if not has_permission(request.user, opts.get_change_permission(), manipulator.original_object):
+    if not has_permission(request.user, opts.change_permission, manipulator.original_object):
         raise PermissionDenied
 
     if request.POST:
                 pass
             else:
                 if related.opts.admin:
-                    if not has_permission(user, related.opts.get_delete_permission(), related):
+                    if not has_permission(user, related.opts.delete_permission, related):
                         perms_needed.add(related.opts.verbose_name)
                         # We don't care about populating deleted_objects now.
                         continue
             # If there were related objects, and the user doesn't have
             # permission to delete them, add the missing perm to perms_needed.
             if related.opts.admin and has_related_objs:
-                if not has_permission(user, related.opts.get_delete_permission(), related):
+                if not has_permission(user, related.opts.delete_permission, related):
                     perms_needed.add(related.opts.verbose_name)
     for related in opts.get_all_related_many_to_many_objects():
         if related.opts in opts_seen:
         # If there were related objects, and the user doesn't have
         # permission to change them, add the missing perm to perms_needed.
         if related.opts.admin and has_related_objs:
-            if not has_permission(user, related.opts.get_delete_permission(), related):
+            if not has_permission(user, related.opts.delete_permission, related):
                 perms_needed.add(related.opts.verbose_name)
 
 def delete_stage(request, app_label, model_name, object_id):
         raise Http404("App %r, model %r, not found" % (app_label, model_name))
     opts = model._meta
     obj = get_object_or_404(model, pk=object_id)
-    if not has_permission(request.user, opts.get_delete_permission(), obj):
+    if not has_permission(request.user, opts.delete_permission, obj):
         raise PermissionDenied
 
     # Populate deleted_objects, a data structure of all related objects that
     # There isn't a specific object to check here, so don't pass one to 
     # has_permission. There should be a has_permission implementation 
     # registered that knows when the obj arg is missing.
-    if not has_permission(request.user, model._meta.get_change_permission()):
+    if not has_permission(request.user, model._meta.change_permission):
         raise PermissionDenied
     try:
         cl = ChangeList(request, model)

File django/db/models/options.py

         return 'ORDER BY ' + orderlist2sql(self.ordering, self, pre)
 
     def get_add_permission(self):
+        return 'add_%s' % self.object_name.lower()
+
+    def get_change_permission(self):
+        return 'change_%s' % self.object_name.lower()
+
+    def get_delete_permission(self):
+        return 'delete_%s' % self.object_name.lower()
+
+    def _get_add_permission(self):
+        # import goes here because models aren't necessarily set up when this 
+        # module is imported
         from django.contrib.auth.models import Permission
         codename = 'add_%s' % self.object_name.lower()
         return Permission.objects.get(
             content_type__app_label__exact=self.app_label,
             codename=codename)
+    add_permission = property(_get_add_permission)
 
-    def get_change_permission(self):
+    def _get_change_permission(self):
+        # import goes here because models aren't necessarily set up when this 
+        # module is imported
         from django.contrib.auth.models import Permission
         codename = 'change_%s' % self.object_name.lower()
         return Permission.objects.get(
             content_type__app_label__exact=self.app_label,
             codename=codename)
+    change_permission = property(_get_change_permission)
 
-    def get_delete_permission(self):
+    def _get_delete_permission(self):
+        # import goes here because models aren't necessarily set up when this 
+        # module is imported
         from django.contrib.auth.models import Permission
         codename = 'delete_%s' % self.object_name.lower()
         return Permission.objects.get(
             content_type__app_label__exact=self.app_label,
             codename=codename)
+    delete_permission = property(_get_delete_permission)
 
     def get_all_related_objects(self):
         try: # Try the cache first.

File tests/regressiontests/generic_auth/tests.py

 Get permissions from the model
 
 >>> opts = Person._meta
->>> add_permission = opts.get_add_permission()
->>> change_permission = opts.get_change_permission()
->>> delete_permission = opts.get_delete_permission()
+>>> add_permission = opts.add_permission
+>>> change_permission = opts.change_permission
+>>> delete_permission = opts.delete_permission
 
 
 Give the user add and change permissions for Person models.
 
 Set up some convenient references to the various permission objects.
 
->>> add_permission = Article._meta.get_add_permission()
->>> change_permission = Article._meta.get_change_permission()
->>> delete_permission = Article._meta.get_delete_permission()
+>>> add_permission = Article._meta.add_permission
+>>> change_permission = Article._meta.change_permission
+>>> delete_permission = Article._meta.delete_permission
 
 Adding isn't tied to a particular object, and we haven't given the user
 permission to add Articles yet, so this should fail.