Changed object.id to object.pk cause on ietf models there are tables without id. See #535

- Legacy-Id: 2599
This commit is contained in:
Emilio A. Sánchez López 2010-11-02 16:11:59 +00:00
parent 3a5a5f0e47
commit 51bb64b7ee
3 changed files with 29 additions and 29 deletions

View file

@ -128,7 +128,7 @@ class Role(models.Model):
if content:
ctype = ContentType.objects.get_for_model(content)
prrs = PrincipalRoleRelation.objects.filter(role=self,
content_id__in = (None, content.id),
content_id__in = (None, content.pk),
content_type__in = (None, ctype)).exclude(group=None)
else:
prrs = PrincipalRoleRelation.objects.filter(role=self,
@ -143,7 +143,7 @@ class Role(models.Model):
if content:
ctype = ContentType.objects.get_for_model(content)
prrs = PrincipalRoleRelation.objects.filter(role=self,
content_id__in = (None, content.id),
content_id__in = (None, content.pk),
content_type__in = (None, ctype)).exclude(user=None)
else:
prrs = PrincipalRoleRelation.objects.filter(role=self,
@ -190,4 +190,4 @@ class PrincipalRoleRelation(models.Model):
else:
self.group = principal
principal = property(get_principal, set_principal)
principal = property(get_principal, set_principal)

View file

@ -62,13 +62,13 @@ def add_local_role(obj, principal, role):
ctype = ContentType.objects.get_for_model(obj)
if isinstance(principal, User):
try:
ppr = PrincipalRoleRelation.objects.get(user=principal, role=role, content_id=obj.id, content_type=ctype)
ppr = PrincipalRoleRelation.objects.get(user=principal, role=role, content_id=obj.pk, content_type=ctype)
except PrincipalRoleRelation.DoesNotExist:
PrincipalRoleRelation.objects.create(user=principal, role=role, content=obj)
return True
else:
try:
ppr = PrincipalRoleRelation.objects.get(group=principal, role=role, content_id=obj.id, content_type=ctype)
ppr = PrincipalRoleRelation.objects.get(group=principal, role=role, content_id=obj.pk, content_type=ctype)
except PrincipalRoleRelation.DoesNotExist:
PrincipalRoleRelation.objects.create(group=principal, role=role, content=obj)
return True
@ -120,10 +120,10 @@ def remove_local_role(obj, principal, role):
if isinstance(principal, User):
ppr = PrincipalRoleRelation.objects.get(
user=principal, role=role, content_id=obj.id, content_type=ctype)
user=principal, role=role, content_id=obj.pk, content_type=ctype)
else:
ppr = PrincipalRoleRelation.objects.get(
group=principal, role=role, content_id=obj.id, content_type=ctype)
group=principal, role=role, content_id=obj.pk, content_type=ctype)
except PrincipalRoleRelation.DoesNotExist:
return False
@ -169,10 +169,10 @@ def remove_local_roles(obj, principal):
if isinstance(principal, User):
ppr = PrincipalRoleRelation.objects.filter(
user=principal, content_id=obj.id, content_type=ctype)
user=principal, content_id=obj.pk, content_type=ctype)
else:
ppr = PrincipalRoleRelation.objects.filter(
group=principal, content_id=obj.id, content_type=ctype)
group=principal, content_id=obj.pk, content_type=ctype)
if ppr:
ppr.delete()
@ -222,7 +222,7 @@ def get_roles(user, obj=None):
FROM permissions_principalrolerelation
WHERE (user_id='%s' OR group_id IN (%s))
AND content_id='%s'
AND content_type_id='%s'""" % (user.id, groups_ids_str, obj.id, ctype.id))
AND content_type_id='%s'""" % (user.id, groups_ids_str, obj.pk, ctype.id))
for row in cursor.fetchall():
roles.append(row[0])
@ -253,10 +253,10 @@ def get_local_roles(obj, principal):
if isinstance(principal, User):
return [prr.role for prr in PrincipalRoleRelation.objects.filter(
user=principal, content_id=obj.id, content_type=ctype)]
user=principal, content_id=obj.pk, content_type=ctype)]
else:
return [prr.role for prr in PrincipalRoleRelation.objects.filter(
group=principal, content_id=obj.id, content_type=ctype)]
group=principal, content_id=obj.pk, content_type=ctype)]
# Permissions ################################################################
@ -306,7 +306,7 @@ def grant_permission(obj, role, permission):
ct = ContentType.objects.get_for_model(obj)
try:
ObjectPermission.objects.get(role=role, content_type = ct, content_id=obj.id, permission=permission)
ObjectPermission.objects.get(role=role, content_type = ct, content_id=obj.pk, permission=permission)
except ObjectPermission.DoesNotExist:
ObjectPermission.objects.create(role=role, content=obj, permission=permission)
@ -337,7 +337,7 @@ def remove_permission(obj, role, permission):
ct = ContentType.objects.get_for_model(obj)
try:
op = ObjectPermission.objects.get(role=role, content_type = ct, content_id=obj.id, permission = permission)
op = ObjectPermission.objects.get(role=role, content_type = ct, content_id=obj.pk, permission = permission)
except ObjectPermission.DoesNotExist:
return False
@ -362,7 +362,7 @@ def has_permission(obj, user, codename, roles=None):
If given these roles will be assigned to the user temporarily before
the permissions are checked.
"""
cache_key = "%s-%s-%s" % (obj.content_type, obj.id, codename)
cache_key = "%s-%s-%s" % (obj.content_type, obj.pk, codename)
result = _get_cached_permission(user, cache_key)
if result is not None:
return result
@ -381,7 +381,7 @@ def has_permission(obj, user, codename, roles=None):
result = False
while obj is not None:
p = ObjectPermission.objects.filter(
content_type=ct, content_id=obj.id, role__in=roles, permission__codename = codename).values("id")
content_type=ct, content_id=obj.pk, role__in=roles, permission__codename = codename).values("id")
if len(p) > 0:
result = True
@ -422,7 +422,7 @@ def add_inheritance_block(obj, permission):
ct = ContentType.objects.get_for_model(obj)
try:
ObjectPermissionInheritanceBlock.objects.get(content_type = ct, content_id=obj.id, permission=permission)
ObjectPermissionInheritanceBlock.objects.get(content_type = ct, content_id=obj.pk, permission=permission)
except ObjectPermissionInheritanceBlock.DoesNotExist:
try:
result = ObjectPermissionInheritanceBlock.objects.create(content=obj, permission=permission)
@ -451,7 +451,7 @@ def remove_inheritance_block(obj, permission):
ct = ContentType.objects.get_for_model(obj)
try:
opi = ObjectPermissionInheritanceBlock.objects.get(content_type = ct, content_id=obj.id, permission=permission)
opi = ObjectPermissionInheritanceBlock.objects.get(content_type = ct, content_id=obj.pk, permission=permission)
except ObjectPermissionInheritanceBlock.DoesNotExist:
return False
@ -473,7 +473,7 @@ def is_inherited(obj, codename):
ct = ContentType.objects.get_for_model(obj)
try:
ObjectPermissionInheritanceBlock.objects.get(
content_type=ct, content_id=obj.id, permission__codename = codename)
content_type=ct, content_id=obj.pk, permission__codename = codename)
except ObjectDoesNotExist:
return True
else:
@ -515,8 +515,8 @@ def reset(obj):
"""Resets all permissions and inheritance blocks of passed object.
"""
ctype = ContentType.objects.get_for_model(obj)
ObjectPermissionInheritanceBlock.objects.filter(content_id=obj.id, content_type=ctype).delete()
ObjectPermission.objects.filter(content_id=obj.id, content_type=ctype).delete()
ObjectPermissionInheritanceBlock.objects.filter(content_id=obj.pk, content_type=ctype).delete()
ObjectPermission.objects.filter(content_id=obj.pk, content_type=ctype).delete()
# Registering ################################################################
@ -662,4 +662,4 @@ def _get_cached_permission(user, cache_key):
"""
permissions = getattr(user, "permissions", None)
if permissions:
return user.permissions.get(cache_key, None)
return user.permissions.get(cache_key, None)

View file

@ -70,7 +70,7 @@ def remove_workflow_from_model(ctype):
for obj in get_objects_for_workflow(workflow):
try:
ctype = ContentType.objects.get_for_model(obj)
sor = StateObjectRelation.objects.get(content_id=obj.id, content_type=ctype)
sor = StateObjectRelation.objects.get(content_id=obj.pk, content_type=ctype)
except StateObjectRelation.DoesNotExist:
pass
else:
@ -201,7 +201,7 @@ def get_workflow_for_object(obj):
"""
try:
ctype = ContentType.objects.get_for_model(obj)
wor = WorkflowObjectRelation.objects.get(content_id=obj.id, content_type=ctype)
wor = WorkflowObjectRelation.objects.get(content_id=obj.pk, content_type=ctype)
except WorkflowObjectRelation.DoesNotExist:
return None
else:
@ -234,7 +234,7 @@ def get_state(obj):
"""
ctype = ContentType.objects.get_for_model(obj)
try:
sor = StateObjectRelation.objects.get(content_type=ctype, content_id=obj.id)
sor = StateObjectRelation.objects.get(content_type=ctype, content_id=obj.pk)
except StateObjectRelation.DoesNotExist:
return None
else:
@ -255,7 +255,7 @@ def set_state(obj, state):
"""
ctype = ContentType.objects.get_for_model(obj)
try:
sor = StateObjectRelation.objects.get(content_type=ctype, content_id=obj.id)
sor = StateObjectRelation.objects.get(content_type=ctype, content_id=obj.pk)
except StateObjectRelation.DoesNotExist:
sor = StateObjectRelation.objects.create(content=obj, state=state)
else:
@ -315,7 +315,7 @@ def update_permissions(obj):
ct = ContentType.objects.get_for_model(obj)
ps = [wpr.permission for wpr in WorkflowPermissionRelation.objects.filter(workflow=workflow)]
ObjectPermission.objects.filter(content_type = ct, content_id=obj.id, permission__in=ps).delete()
ObjectPermission.objects.filter(content_type = ct, content_id=obj.pk, permission__in=ps).delete()
# Grant permission for the state
for spr in StatePermissionRelation.objects.filter(state=state):
@ -323,8 +323,8 @@ def update_permissions(obj):
# Remove all inheritance blocks from the object
ObjectPermissionInheritanceBlock.objects.filter(
content_type = ct, content_id=obj.id, permission__in=ps).delete()
content_type = ct, content_id=obj.pk, permission__in=ps).delete()
# Add inheritance blocks of this state to the object
for sib in StateInheritanceBlock.objects.filter(state=state):
permissions.utils.add_inheritance_block(obj, sib.permission)
permissions.utils.add_inheritance_block(obj, sib.permission)