Permalink
Browse files

get_obj_perms_model should not fail for models with FileField. Fixes

Thanks to Piotr Kilczuk for finding the bug and a hotfix
  • Loading branch information...
1 parent 84289d8 commit 6952722dae6514b04183e9c102b4ef2f97b24d58 @lukaszb lukaszb committed Feb 26, 2013
Showing with 31 additions and 1 deletion.
  1. +1 −0 AUTHORS
  2. +25 −0 guardian/tests/utils_test.py
  3. +5 −1 guardian/utils.py
View
@@ -16,3 +16,4 @@ Authors ordered by first contribution
- Greg Hinch <greg@greghinch.com>
- Aram Dulyan <aram@dulyan.com>
- Florian Hahn <flo@fhahn.com>
+- Piotr Kilczuk <piotr@tymaszweb.pl>
@@ -2,18 +2,21 @@
from django.test import TestCase
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import Group, AnonymousUser
+from django.db import models
from guardian.compat import get_user_model
from guardian.tests.core_test import ObjectPermissionTestCase
from guardian.tests.testapp.models import Project
from guardian.tests.testapp.models import ProjectUserObjectPermission
from guardian.tests.testapp.models import ProjectGroupObjectPermission
from guardian.models import UserObjectPermission
+from guardian.models import UserObjectPermissionBase
from guardian.models import GroupObjectPermission
from guardian.utils import get_anonymous_user
from guardian.utils import get_identity
from guardian.utils import get_user_obj_perms_model
from guardian.utils import get_group_obj_perms_model
+from guardian.utils import get_obj_perms_model
from guardian.exceptions import NotUserNorGroup
User = get_user_model()
@@ -91,3 +94,25 @@ def test_group_model(self):
self.assertEqual(get_group_obj_perms_model(Group),
GroupObjectPermission)
+class GetObjPermsModelTest(TestCase):
+
+ def test_image_field(self):
+
+ class SomeModel(models.Model):
+ image = models.ImageField(upload_to='images/')
+
+ obj = SomeModel()
+ perm_model = get_obj_perms_model(obj, UserObjectPermissionBase,
+ UserObjectPermission)
+ self.assertEqual(perm_model, UserObjectPermission)
+
+ def test_file_field(self):
+
+ class SomeModel(models.Model):
+ file = models.FileField(upload_to='images/')
+
+ obj = SomeModel()
+ perm_model = get_obj_perms_model(obj, UserObjectPermissionBase,
+ UserObjectPermission)
+ self.assertEqual(perm_model, UserObjectPermission)
+
View
@@ -148,7 +148,11 @@ def get_obj_perms_model(obj, base_cls, generic_cls):
obj = obj.__class__
ctype = ContentType.objects.get_for_model(obj)
for name in dir(obj):
- attr = getattr(obj, name)
+ try:
+ attr = getattr(obj, name)
+ except AttributeError:
+ # this might be thrown if field is a FileField
+ continue
if hasattr(attr, 'related'):
related = attr.related
model = getattr(related, 'model', None)

0 comments on commit 6952722

Please sign in to comment.