Skip to content

Instantly share code, notes, and snippets.

@hanleybrand
Created February 28, 2014 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hanleybrand/9281002 to your computer and use it in GitHub Desktop.
Save hanleybrand/9281002 to your computer and use it in GitHub Desktop.
add 'authenticated users' (i.e. anyone logged in) read privileges to presentations (in django shell_plus) - can be edited and run, or copy/pasted into the shell
from rooibos.presentation.models import Presentation
from rooibos.access.models import ExtendedGroup, AUTHENTICATED_GROUP, AccessControl
from django.contrib.contenttypes.models import ContentType
from rooibos.contrib.tagging.models import Tag, TaggedItem
# lets get the stuff we need
# user (owner) - edit that, of course
usr = User.objects.get(username='someuser')
# content-type, in this case presentation
ct = ContentType.objects.get(name='presentation')
# px = specific presentation objects - also edit specific ids
p1 = Presentation.objects.get(id=5457)
p2 = Presentation.objects.get(id=5451)
# g = the 'authenticated users' group
g = ExtendedGroup.objects.filter(type=AUTHENTICATED_GROUP)
g = g[0] if g else ExtendedGroup.objects.create(type=AUTHENTICATED_GROUP, name='Authenticated Users')
# add global read privileges for logged in users to p2
AccessControl.objects.get_or_create(object_id=p2.id, content_type=ct, usergroup=g, read=True)
### so going further, if you want to do a bunch at once
# get all the tags of a single user
existing_tags = Tag.objects.usage_for_model(OwnedWrapper,filters=dict(user=usr, content_type=OwnedWrapper.t(Presentation)))
# pick a tag (you can just type existing_tags to get a list) - this is how you choose
t1 = existing_tags[5]
# <Tag: Futurism>
# these next two commands grab all presentations tagged by a single users tag (futurism in this case)
qs = OwnedWrapper.objects.filter(content_type=OwnedWrapper.t(Presentation))
ids = [list(TaggedItem.objects.get_by_model(qs, '"%s"' % tag).values_list('object_id', flat=True)) for tag in [t1]]
# a little for-loopage and we're done.
for list in ids:
for id in list:
AccessControl.objects.get_or_create(object_id=id, content_type=ct, usergroup=g, read=True)
### here's what some of the variables from above look like printed out
In [89]: existing_tags
Out[89]:
[<Tag: 001. AWW Fall 13 Part III>,
<Tag: 01. Picasso and Modern Masters>,
<Tag: 1. Uneasy/Censorship>,
<Tag: 1960s>,
<Tag: AWW Fall 11 & 10 part III>,
<Tag: Futurism>,
<Tag: Futurism MDID2>,
<Tag: Italian Film/Neorealism>,
<Tag: Misc. shows>,
<Tag: Picasso and Modern Masters>,
<Tag: Picasso, Matisse, etc./Spring '06>,
<Tag: PicMatDuchBran>,
<Tag: Spain''>,
<Tag: Uneasy/Censorship>]
In [90]: p1
Out[90]: <Presentation: Presentation object>
In [91]: ids
Out[91]:
[[4995L,
5449L,
5450L,
5451L,
5452L,
5453L,
5454L,
5455L,
5456L,
5457L,
5458L,
5459L,
5460L,
5461L,
5462L,
5463L,
5474L]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment