Commits

Kai Diefenbach  committed 50d56a0

Bugfix: DatabaseErrors with Postgres. Issue #5.

  • Participants
  • Parent commits d991640

Comments (0)

Files changed (1)

File permissions/utils.py

 # django imports
 from django.db import IntegrityError
+from django.db.models import Q
 from django.contrib.auth.models import User
 from django.contrib.auth.models import Group
 from django.contrib.contenttypes.models import ContentType
             used to display only reasonable permissions for an object. This
             must be a Django ContentType
     """
-    try:
-        p = Permission.objects.create(name=name, codename=codename)
+    # Permission with same codename and/or name must not exist.
+    if Permission.objects.filter(Q(name=name) | Q(codename=codename)):
+        return False
 
-        ctypes = [ContentType.objects.get_for_model(ctype) for ctype in ctypes]
-        if ctypes:
-            p.content_types = ctypes
-            p.save()
-    except IntegrityError:
-        return False
+    p = Permission.objects.create(name=name, codename=codename)
+
+    ctypes = [ContentType.objects.get_for_model(ctype) for ctype in ctypes]
+    if ctypes:
+        p.content_types = ctypes
+        p.save()
+
     return p
 
 def unregister_permission(codename):
 
     name
         The unique role name.
-    """
-    try:
-        role = Role.objects.create(name=name)
-    except IntegrityError:
+    """    
+    role, created = Role.objects.get_or_create(name=name)
+    if created:
+        return role
+    else:
         return False
-    return role
 
 def unregister_role(name):
     """Unregisters the role with passed name.
     name
         The unique group name.
     """
-    try:
-        group = Group.objects.create(name=name)
-    except IntegrityError:
+    group, created = Group.objects.get_or_create(name=name)
+    if created:
+        return group
+    else:
         return False
-    return group
 
 def unregister_group(name):
     """Unregisters the group with passed name. Returns True if the