Skip to content

Instantly share code, notes, and snippets.

@eshaan7
Created August 11, 2020 15:48
Show Gist options
  • Save eshaan7/b111f887fa8b860c762aa38d99ec5482 to your computer and use it in GitHub Desktop.
Save eshaan7/b111f887fa8b860c762aa38d99ec5482 to your computer and use it in GitHub Desktop.
Helper script for users upgrading to v1.3.0 of Intel Owl from any prior version
"""
Helper script for users upgrading to v1.3.0 of Intel Owl from any prior version
"""
import django
if __name__ == "__main__":
django.setup()
from django.db import transaction
from django.contrib.auth.models import User, Group
from guardian.shortcuts import assign_perm
from api_app.models import Job, Tag
with transaction.atomic():
users = User.objects.all()
jobs = Job.objects.all()
tags = Tag.objects.all()
grp, created = Group.objects.get_or_create(name="DefaultGlobal")
# Add global permissions
if created:
# view/add permissions for Job model
assign_perm("api_app.view_job", grp)
assign_perm("api_app.add_job", grp)
# view/add/change permissions for Tag model
assign_perm("api_app.view_tag", grp)
assign_perm("api_app.add_tag", grp)
assign_perm("api_app.change_tag", grp)
assign_perm("api_app.delete_tag", grp)
grp.save()
# Add object level permissions for existing users, jobs and tags
for u in users:
u.groups.add(grp)
for job in jobs:
assign_perm("view_job", grp, job)
for tag in tags:
assign_perm("view_tag", grp, tag)
assign_perm("change_tag", grp, tag)
assign_perm("delete_tag", grp, tag)
print("Update successful!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment