Skip to content

Commit 51bb64b

Browse files
committed
Changed object.id to object.pk cause on ietf models there are tables without id. See ietf-tools#535
- Legacy-Id: 2599
1 parent 3a5a5f0 commit 51bb64b

3 files changed

Lines changed: 29 additions & 29 deletions

File tree

permissions/models.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def get_groups(self, content=None):
128128
if content:
129129
ctype = ContentType.objects.get_for_model(content)
130130
prrs = PrincipalRoleRelation.objects.filter(role=self,
131-
content_id__in = (None, content.id),
131+
content_id__in = (None, content.pk),
132132
content_type__in = (None, ctype)).exclude(group=None)
133133
else:
134134
prrs = PrincipalRoleRelation.objects.filter(role=self,
@@ -143,7 +143,7 @@ def get_users(self, content=None):
143143
if content:
144144
ctype = ContentType.objects.get_for_model(content)
145145
prrs = PrincipalRoleRelation.objects.filter(role=self,
146-
content_id__in = (None, content.id),
146+
content_id__in = (None, content.pk),
147147
content_type__in = (None, ctype)).exclude(user=None)
148148
else:
149149
prrs = PrincipalRoleRelation.objects.filter(role=self,
@@ -190,4 +190,4 @@ def set_principal(self, principal):
190190
else:
191191
self.group = principal
192192

193-
principal = property(get_principal, set_principal)
193+
principal = property(get_principal, set_principal)

permissions/utils.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ def add_local_role(obj, principal, role):
6262
ctype = ContentType.objects.get_for_model(obj)
6363
if isinstance(principal, User):
6464
try:
65-
ppr = PrincipalRoleRelation.objects.get(user=principal, role=role, content_id=obj.id, content_type=ctype)
65+
ppr = PrincipalRoleRelation.objects.get(user=principal, role=role, content_id=obj.pk, content_type=ctype)
6666
except PrincipalRoleRelation.DoesNotExist:
6767
PrincipalRoleRelation.objects.create(user=principal, role=role, content=obj)
6868
return True
6969
else:
7070
try:
71-
ppr = PrincipalRoleRelation.objects.get(group=principal, role=role, content_id=obj.id, content_type=ctype)
71+
ppr = PrincipalRoleRelation.objects.get(group=principal, role=role, content_id=obj.pk, content_type=ctype)
7272
except PrincipalRoleRelation.DoesNotExist:
7373
PrincipalRoleRelation.objects.create(group=principal, role=role, content=obj)
7474
return True
@@ -120,10 +120,10 @@ def remove_local_role(obj, principal, role):
120120

121121
if isinstance(principal, User):
122122
ppr = PrincipalRoleRelation.objects.get(
123-
user=principal, role=role, content_id=obj.id, content_type=ctype)
123+
user=principal, role=role, content_id=obj.pk, content_type=ctype)
124124
else:
125125
ppr = PrincipalRoleRelation.objects.get(
126-
group=principal, role=role, content_id=obj.id, content_type=ctype)
126+
group=principal, role=role, content_id=obj.pk, content_type=ctype)
127127

128128
except PrincipalRoleRelation.DoesNotExist:
129129
return False
@@ -169,10 +169,10 @@ def remove_local_roles(obj, principal):
169169

170170
if isinstance(principal, User):
171171
ppr = PrincipalRoleRelation.objects.filter(
172-
user=principal, content_id=obj.id, content_type=ctype)
172+
user=principal, content_id=obj.pk, content_type=ctype)
173173
else:
174174
ppr = PrincipalRoleRelation.objects.filter(
175-
group=principal, content_id=obj.id, content_type=ctype)
175+
group=principal, content_id=obj.pk, content_type=ctype)
176176

177177
if ppr:
178178
ppr.delete()
@@ -222,7 +222,7 @@ def get_roles(user, obj=None):
222222
FROM permissions_principalrolerelation
223223
WHERE (user_id='%s' OR group_id IN (%s))
224224
AND content_id='%s'
225-
AND content_type_id='%s'""" % (user.id, groups_ids_str, obj.id, ctype.id))
225+
AND content_type_id='%s'""" % (user.id, groups_ids_str, obj.pk, ctype.id))
226226

227227
for row in cursor.fetchall():
228228
roles.append(row[0])
@@ -253,10 +253,10 @@ def get_local_roles(obj, principal):
253253

254254
if isinstance(principal, User):
255255
return [prr.role for prr in PrincipalRoleRelation.objects.filter(
256-
user=principal, content_id=obj.id, content_type=ctype)]
256+
user=principal, content_id=obj.pk, content_type=ctype)]
257257
else:
258258
return [prr.role for prr in PrincipalRoleRelation.objects.filter(
259-
group=principal, content_id=obj.id, content_type=ctype)]
259+
group=principal, content_id=obj.pk, content_type=ctype)]
260260

261261
# Permissions ################################################################
262262

@@ -306,7 +306,7 @@ def grant_permission(obj, role, permission):
306306

307307
ct = ContentType.objects.get_for_model(obj)
308308
try:
309-
ObjectPermission.objects.get(role=role, content_type = ct, content_id=obj.id, permission=permission)
309+
ObjectPermission.objects.get(role=role, content_type = ct, content_id=obj.pk, permission=permission)
310310
except ObjectPermission.DoesNotExist:
311311
ObjectPermission.objects.create(role=role, content=obj, permission=permission)
312312

@@ -337,7 +337,7 @@ def remove_permission(obj, role, permission):
337337
ct = ContentType.objects.get_for_model(obj)
338338

339339
try:
340-
op = ObjectPermission.objects.get(role=role, content_type = ct, content_id=obj.id, permission = permission)
340+
op = ObjectPermission.objects.get(role=role, content_type = ct, content_id=obj.pk, permission = permission)
341341
except ObjectPermission.DoesNotExist:
342342
return False
343343

@@ -362,7 +362,7 @@ def has_permission(obj, user, codename, roles=None):
362362
If given these roles will be assigned to the user temporarily before
363363
the permissions are checked.
364364
"""
365-
cache_key = "%s-%s-%s" % (obj.content_type, obj.id, codename)
365+
cache_key = "%s-%s-%s" % (obj.content_type, obj.pk, codename)
366366
result = _get_cached_permission(user, cache_key)
367367
if result is not None:
368368
return result
@@ -381,7 +381,7 @@ def has_permission(obj, user, codename, roles=None):
381381
result = False
382382
while obj is not None:
383383
p = ObjectPermission.objects.filter(
384-
content_type=ct, content_id=obj.id, role__in=roles, permission__codename = codename).values("id")
384+
content_type=ct, content_id=obj.pk, role__in=roles, permission__codename = codename).values("id")
385385

386386
if len(p) > 0:
387387
result = True
@@ -422,7 +422,7 @@ def add_inheritance_block(obj, permission):
422422

423423
ct = ContentType.objects.get_for_model(obj)
424424
try:
425-
ObjectPermissionInheritanceBlock.objects.get(content_type = ct, content_id=obj.id, permission=permission)
425+
ObjectPermissionInheritanceBlock.objects.get(content_type = ct, content_id=obj.pk, permission=permission)
426426
except ObjectPermissionInheritanceBlock.DoesNotExist:
427427
try:
428428
result = ObjectPermissionInheritanceBlock.objects.create(content=obj, permission=permission)
@@ -451,7 +451,7 @@ def remove_inheritance_block(obj, permission):
451451

452452
ct = ContentType.objects.get_for_model(obj)
453453
try:
454-
opi = ObjectPermissionInheritanceBlock.objects.get(content_type = ct, content_id=obj.id, permission=permission)
454+
opi = ObjectPermissionInheritanceBlock.objects.get(content_type = ct, content_id=obj.pk, permission=permission)
455455
except ObjectPermissionInheritanceBlock.DoesNotExist:
456456
return False
457457

@@ -473,7 +473,7 @@ def is_inherited(obj, codename):
473473
ct = ContentType.objects.get_for_model(obj)
474474
try:
475475
ObjectPermissionInheritanceBlock.objects.get(
476-
content_type=ct, content_id=obj.id, permission__codename = codename)
476+
content_type=ct, content_id=obj.pk, permission__codename = codename)
477477
except ObjectDoesNotExist:
478478
return True
479479
else:
@@ -515,8 +515,8 @@ def reset(obj):
515515
"""Resets all permissions and inheritance blocks of passed object.
516516
"""
517517
ctype = ContentType.objects.get_for_model(obj)
518-
ObjectPermissionInheritanceBlock.objects.filter(content_id=obj.id, content_type=ctype).delete()
519-
ObjectPermission.objects.filter(content_id=obj.id, content_type=ctype).delete()
518+
ObjectPermissionInheritanceBlock.objects.filter(content_id=obj.pk, content_type=ctype).delete()
519+
ObjectPermission.objects.filter(content_id=obj.pk, content_type=ctype).delete()
520520

521521
# Registering ################################################################
522522

@@ -662,4 +662,4 @@ def _get_cached_permission(user, cache_key):
662662
"""
663663
permissions = getattr(user, "permissions", None)
664664
if permissions:
665-
return user.permissions.get(cache_key, None)
665+
return user.permissions.get(cache_key, None)

workflows/utils.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def remove_workflow_from_model(ctype):
7070
for obj in get_objects_for_workflow(workflow):
7171
try:
7272
ctype = ContentType.objects.get_for_model(obj)
73-
sor = StateObjectRelation.objects.get(content_id=obj.id, content_type=ctype)
73+
sor = StateObjectRelation.objects.get(content_id=obj.pk, content_type=ctype)
7474
except StateObjectRelation.DoesNotExist:
7575
pass
7676
else:
@@ -201,7 +201,7 @@ def get_workflow_for_object(obj):
201201
"""
202202
try:
203203
ctype = ContentType.objects.get_for_model(obj)
204-
wor = WorkflowObjectRelation.objects.get(content_id=obj.id, content_type=ctype)
204+
wor = WorkflowObjectRelation.objects.get(content_id=obj.pk, content_type=ctype)
205205
except WorkflowObjectRelation.DoesNotExist:
206206
return None
207207
else:
@@ -234,7 +234,7 @@ def get_state(obj):
234234
"""
235235
ctype = ContentType.objects.get_for_model(obj)
236236
try:
237-
sor = StateObjectRelation.objects.get(content_type=ctype, content_id=obj.id)
237+
sor = StateObjectRelation.objects.get(content_type=ctype, content_id=obj.pk)
238238
except StateObjectRelation.DoesNotExist:
239239
return None
240240
else:
@@ -255,7 +255,7 @@ def set_state(obj, state):
255255
"""
256256
ctype = ContentType.objects.get_for_model(obj)
257257
try:
258-
sor = StateObjectRelation.objects.get(content_type=ctype, content_id=obj.id)
258+
sor = StateObjectRelation.objects.get(content_type=ctype, content_id=obj.pk)
259259
except StateObjectRelation.DoesNotExist:
260260
sor = StateObjectRelation.objects.create(content=obj, state=state)
261261
else:
@@ -315,16 +315,16 @@ def update_permissions(obj):
315315
ct = ContentType.objects.get_for_model(obj)
316316
ps = [wpr.permission for wpr in WorkflowPermissionRelation.objects.filter(workflow=workflow)]
317317

318-
ObjectPermission.objects.filter(content_type = ct, content_id=obj.id, permission__in=ps).delete()
318+
ObjectPermission.objects.filter(content_type = ct, content_id=obj.pk, permission__in=ps).delete()
319319

320320
# Grant permission for the state
321321
for spr in StatePermissionRelation.objects.filter(state=state):
322322
permissions.utils.grant_permission(obj, spr.role, spr.permission)
323323

324324
# Remove all inheritance blocks from the object
325325
ObjectPermissionInheritanceBlock.objects.filter(
326-
content_type = ct, content_id=obj.id, permission__in=ps).delete()
326+
content_type = ct, content_id=obj.pk, permission__in=ps).delete()
327327

328328
# Add inheritance blocks of this state to the object
329329
for sib in StateInheritanceBlock.objects.filter(state=state):
330-
permissions.utils.add_inheritance_block(obj, sib.permission)
330+
permissions.utils.add_inheritance_block(obj, sib.permission)

0 commit comments

Comments
 (0)