<s> """<STR_LIT>""" <EOL> from django . contrib . auth import get_user_model <EOL> from django . contrib . auth . models import Group <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . db . models import Q <EOL> def get_users_with_model_perms ( model_cls , attach_perms = False , with_superusers = False , <EOL> with_group_users = True ) : <EOL> """<STR_LIT>""" <EOL> ctype = ContentType . objects . get_for_model ( model_cls ) <EOL> if not attach_perms : <EOL> user_filters = { <EOL> '<STR_LIT>' : ctype <EOL> } <EOL> qset = Q ( ** user_filters ) <EOL> if with_group_users : <EOL> group_filters = { <EOL> '<STR_LIT>' : ctype <EOL> } <EOL> qset = qset | Q ( ** group_filters ) <EOL> if with_superusers : <EOL> qset = qset | Q ( is_superuser = True ) <EOL> return get_user_model ( ) . objects . filter ( qset ) . distinct ( ) <EOL> else : <EOL> users = { } <EOL> for user in get_users_with_model_perms ( model_cls , with_group_users = with_group_users ) : <EOL> users [ user ] = sorted ( [ perm . codename for perm in <EOL> user . user_permissions . filter ( content_type = ctype ) ] ) <EOL> return users <EOL> def get_groups_with_model_perms ( model_cls , attach_perms = False ) : <EOL> """<STR_LIT>""" <EOL> ctype = ContentType . objects . get_for_model ( model_cls ) <EOL> if not attach_perms : <EOL> group_filters = { <EOL> '<STR_LIT>' : ctype <EOL> } <EOL> return Group . objects . filter ( ** group_filters ) . distinct ( ) <EOL> else : <EOL> groups = { } <EOL> for group in get_groups_with_model_perms ( model_cls ) : <EOL> if group not in groups : <EOL> groups [ group ] = sorted ( [ perm . codename for perm in <EOL> group . permissions . filter ( content_type = ctype ) ] ) <EOL> return groups </s>
