Skip to content

Instantly share code, notes, and snippets.

@cooncesean
Created March 28, 2012 21:53
Show Gist options
  • Select an option

  • Save cooncesean/2230897 to your computer and use it in GitHub Desktop.

Select an option

Save cooncesean/2230897 to your computer and use it in GitHub Desktop.
User/Group Permission Management in Distillery.
from django.contrib.auth.models import User, Group, Permission
from django.contrib.contenttypes.models import ContentType
from ella.articles.models import Article
from ella.core.models import Listing, Publishable
from ella.photos.models import Photo, FormatedPhoto
from ella_galleries.models import Gallery, GalleryItem
from scout.podcasts.models import Podcast
from scout.videos.models import Video
# 1. Create new Groups for Editors and Freelancers ##############
editors = Group.objects.create(name='Editors')
freelancers = Group.objects.create(name='Freelancers')
# 2. Add admin permissions for Editors ##########################
# Can edit/add/delete Articles, Galleries, Podcasts, Videos
for p in Permission.objects.filter(
content_type__in=[
ContentType.objects.get_for_model(Article),
ContentType.objects.get_for_model(Gallery),
ContentType.objects.get_for_model(GalleryItem),
ContentType.objects.get_for_model(Podcast),
ContentType.objects.get_for_model(Video),
ContentType.objects.get_for_model(Photo),
ContentType.objects.get_for_model(FormatedPhoto),
ContentType.objects.get_for_model(Listing),
]):
print p
editors.permissions.add(p)
# 3. Create special Editor Permissions ##########################
# Create new Permission for Users App Panel Index
user_ct = ContentType.objects.get_for_model(User)
can_view_users_app_panel = Permission.objects.create(
name='Can View User App Panel',
codename='can_view_users_app_panel',
content_type=user_ct
)
editors.permissions.add(can_view_users_app_panel)
# Create new Permission for Topslot App Panel Index
can_view_topslot_app_panel = Permission.objects.create(
name='Can View Topslot App Panel',
codename='can_view_topslot_app_panel',
content_type=user_ct
)
editors.permissions.add(can_view_topslot_app_panel)
# Create new Permission for Promo App Panel Index
can_view_promos_app_panel = Permission.objects.create(
name='Can View Promos App Panel',
codename='can_view_promos_app_panel',
content_type=user_ct
)
editors.permissions.add(can_view_promos_app_panel)
# Create new Permission for Podcast App Panel Index
can_view_podcast_app_panel = Permission.objects.create(
name='Can View Podcast App Panel',
codename='can_view_podcast_app_panel',
content_type=ContentType.objects.get_for_model(Podcast)
)
editors.permissions.add(can_view_podcast_app_panel)
# Create new Permission for to allow editors to 'publish' things
can_publish_publishables = Permission.objects.create(
name='Can Publish Publishables',
codename='can_publish_publishables',
content_type=ContentType.objects.get_for_model(Publishable)
)
editors.permissions.add(can_publish_publishables)
# Create new Permission for to allow editors to 'save' already Published content
can_save_published_content = Permission.objects.create(
name='Can Save Published Content',
codename='can_save_published_content',
content_type=ContentType.objects.get_for_model(Publishable)
)
editors.permissions.add(can_save_published_content)
# Create new Permission for to allow editors to 'save' another user's content
can_edit_another_users_content = Permission.objects.create(
name='Can Edit Another Users Content',
codename='can_edit_another_users_content',
content_type=ContentType.objects.get_for_model(Publishable)
)
editors.permissions.add(can_edit_another_users_content)
editors.permissions.add(Permission.objects.get(codename='change_category'))
# 4. Add Freelancer Permissions #################################
# Freelancers can add/edit/delete the Following content_types
for p in Permission.objects.filter(
content_type__in=[
ContentType.objects.get_for_model(GalleryItem),
ContentType.objects.get_for_model(Photo),
ContentType.objects.get_for_model(FormatedPhoto),
ContentType.objects.get_for_model(Listing),
]):
print p
freelancers.permissions.add(p)
# 5. Extra Freelancer Permissions ###############################
for p in Permission.objects.filter(
content_type__in=[
ContentType.objects.get_for_model(Article),
ContentType.objects.get_for_model(Gallery),
ContentType.objects.get_for_model(Podcast),
ContentType.objects.get_for_model(Video),
]):
if p.codename.startswith('delete'):
continue
print p
freelancers.permissions.add(p)
-------------------------------
# 6. Remove superuser status for everyone except developers ####
valid_superuser_names = ['gpbmike', 'kvbik', 'coonce', 'honza', 'andy']
User.objects.filter(
is_superuser=True
).exclude(
username__in=valid_superuser_names
).update(is_superuser=False)
# 7. Add Users to the Editors Group ############################
editor_user_names = [
'wesleyfenlon',
'joeyf',
'norman',
'will',
'jamiehyneman',
'adamsavage',
'admin',
'tested_writer',
'loydcase',
'ryanw',
'bagels',
'danielcomfort',
'echelon11',
'wesleyfenlon'
]
for u in User.objects.filter(username__in=editor_user_names):
u.groups.add(editors)
# 8. Add Users to the Freelancers Group ########################
freelancer_user_names = [
# 'tested_writer',
# 'loydcase',
# 'ryanw',
# 'bagels',
# 'danielcomfort',
# 'echelon11',
# 'wesleyfenlon'
]
for u in User.objects.filter(username__in=freelancer_user_names):
u.groups.add(freelancers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment