Serialize database updates with transactions.
@@ -9,6 +9,7 @@ from hs_core.models import BaseResource
from hs_core.hydroshare.utils import get_resource_by_shortkey
from hs_access_control.models.privilege import UserResourcePrivilege, PrivilegeCodes
from django_irods.icommands import SessionException
+from django.db import transaction
def set_quota_holder(resource, user):
@@ -61,7 +62,8 @@ class Command(BaseCommand):
prior = User.objects.get(username=options['owned_by'])
for res in BaseResource.objects.filter(r2urp__user=prior,
r2urp__privilege=PrivilegeCodes.OWNER):
- resource = get_resource_by_shortkey(res.short_id)
+ with transaction.atomic():
+ resource = res.get_content_model()
UserResourcePrivilege.share(user=user,
resource=resource,
privilege=PrivilegeCodes.OWNER,
@@ -75,7 +77,8 @@ class Command(BaseCommand):
if len(options['resource_ids']) > 0: # an array of resource short_id to check.
for rid in options['resource_ids']:
- resource = get_resource_by_shortkey(rid)
+ resource = get_resource_by_shortkey(rid, or_404=False)
+ with transaction.atomic():
UserResourcePrivilege.share(user=user,
resource=resource,
privilege=PrivilegeCodes.OWNER,
