Kai Diefenbach committed 50d56a0

Bugfix: DatabaseErrors with Postgres. Issue #5.

Comments (0)

Files changed (1)


 # 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
-    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
     return p
 def unregister_permission(codename):
         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.
         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
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.