本文整理汇总了Python中django.contrib.auth.models.Permission.save方法的典型用法代码示例。如果您正苦于以下问题:Python Permission.save方法的具体用法?Python Permission.save怎么用?Python Permission.save使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.contrib.auth.models.Permission
的用法示例。
在下文中一共展示了Permission.save方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: pre_register
# 需要导入模块: from django.contrib.auth.models import Permission [as 别名]
# 或者: from django.contrib.auth.models.Permission import save [as 别名]
def pre_register(self, model, **kwargs):
content_type = ContentType.objects.get_for_model(model)
codename = 'can_receive_notification_%s' % content_type.model
if not Permission.objects.filter(content_type=content_type, codename=codename):
permission = Permission(name = 'Can receive notification', content_type = content_type, codename = codename)
permission.save()
示例2: save_model
# 需要导入模块: from django.contrib.auth.models import Permission [as 别名]
# 或者: from django.contrib.auth.models.Permission import save [as 别名]
def save_model (self, request, obj, form, change):
""" Hey Hey Hey...let me see your id...or your boobs-your choice...i prefer the BOOBS unless you're OVER 18...EEEWWWWWWW """
if not request.user.is_superuser:
if not request.user.has_perm ("condor."+ Student.objects.get (pk = request.POST["student"]).class_room.grade.grade +"_"+ Student.objects.get (pk = request.POST["student"]).class_room.section +"_"+ Subject.objects.get (pk = request.POST ["subject"]).name):
messages.add_message (request, messages.ERROR, "Denied, the request you've made has content you are not authorized to add/edit. Your request has not been saved")
return None
elif request.user.has_perm ("condor."+ Student.objects.get (pk = request.POST["student"]).class_room.grade.grade +"_"+ Student.objects.get (pk = request.POST["student"]).class_room.section +"_"+ Subject.objects.get (pk = request.POST ["subject"]).name):
if not Permission.objects.filter (codename = "generate_report_card").exists():
NEW_P = Permission ()
NEW_P.codename = "generate_report_card"
NEW_P.name = "Can Generate Report Card"
NEW_P.content_type = ContentType.objects.get (app_label="condor", model="gradereport")
NEW_P.save()
obj.save()
elif request.user.is_superuser:
if not Permission.objects.filter (codename = "generate_report_card").exists():
NEW_P = Permission ()
NEW_P.codename = "generate_report_card"
NEW_P.name = "Can Generate Report Card"
NEW_P.content_type = ContentType.objects.get (app_label="condor", model="gradereport")
NEW_P.save()
""" we are going have to check whether or not the super user has made the right choices...i feel OLD...which takes like two lines """
if obj.subject not in obj.student.class_room.grade.subject.all():
messages.add_message (request, messages.ERROR, "Error: the subject you have selected is not given in the specified grade level, No changes have been made.")
return None
obj.save()
示例3: save_related
# 需要导入模块: from django.contrib.auth.models import Permission [as 别名]
# 或者: from django.contrib.auth.models.Permission import save [as 别名]
def save_related (self, request, form, formsets, change):
if change:
# If any grade change occurred; the save override will take care of the removal of previous permissions associated with the previous grade
# All we have to care aboot here is saving the related objects i.e. subject with the appropriate permissions
# codename: {Level.grade}_{ClassRoom.section}_{Subject.name}
# name: {Level.grade}-{ClassRoom.section}: {Subject.name}
LEVEL = Level.objects.get (pk = request.META['HTTP_REFERER'][request.META['HTTP_REFERER'].rfind('/', 0, -1) + 1: -1]) # level object: related fields UNSAVED untill form save
if LEVEL.classroom_set.count(): # we will have to create/edit permission if a level is associated with a class room
NEW_SUBJECT_LIST = []
for SID in request.POST.getlist ("subject"): # Looping through the subject id's
NEW_SUBJECT_LIST.append (Subject.objects.get(pk = SID).name)
for CR in LEVEL.classroom_set.all(): # After finishing this loop we would have deleted any permission that shouldn't exist anymore due to the change in the level object
for S in LEVEL.subject.all():
if S.name not in NEW_SUBJECT_LIST:
Permission.objects.filter (codename = LEVEL.grade +"_"+ CR.section +"_"+ S.name).delete()
for CR in LEVEL.classroom_set.all(): # After finishing this loop we would have created any new permission that should be created due to the change in the level object
for S in NEW_SUBJECT_LIST:
if not Permission.objects.filter (codename = LEVEL.grade +"_"+ CR.section +"_"+ S).exists():
# Permission Structure
# codename: {Level.grade}_{ClassRoom.section}_{Subject.name}
# name: {Level.grade}-{ClassRoom.section}: {Subject.name}
NEW_P = Permission ()
NEW_P.codename = LEVEL.grade +"_"+ CR.section +"_"+ S
NEW_P.name = LEVEL.grade +"-"+ CR.section +": "+ S
NEW_P.content_type = ContentType.objects.get (app_label="condor", model="classroom")
NEW_P.save()
form.save()
示例4: test_user_permission_not_allowed
# 需要导入模块: from django.contrib.auth.models import Permission [as 别名]
# 或者: from django.contrib.auth.models.Permission import save [as 别名]
def test_user_permission_not_allowed(self):
permission = Permission(content_type_id=1, name='Can something',
codename='can_something')
permission.save()
response, json = self.do_get(self.perm % (self.user.pk, 'auth.can_something'))
self.assertEquals(response.status_code, 200)
self.assertEquals(json['is-allowed'], False, json)
示例5: test_usuarios_render_form_change_pas
# 需要导入模块: from django.contrib.auth.models import Permission [as 别名]
# 或者: from django.contrib.auth.models.Permission import save [as 别名]
def test_usuarios_render_form_change_pas(self):
"""
Testa renderização do formulário de troca de senha
"""
contentItem = ContentType.objects.get(app_label='acesso',model='usuario')
permission = Permission(name='Can Change Pass',content_type=contentItem,codename='change_pass_usuario')
permission.save()
permissions = Permission.objects.all().filter(content_type=contentItem.id)
for permission in permissions:
self.user.user_permissions.add(permission)
# Faz chamada da pagina
response = self.client.get('/acesso/usuarios/password/')
self.assertEquals(response.status_code, 200)
self.assertContains(response, 'name="new_password1"', status_code=200)
"""
Testa renderização do formulário de troca de senha
"""
dataPost = {
'new_password1' : 'testpass2',
'new_password2' : 'testpass2',
}
response = self.client.post('/acesso/usuarios/password/', dataPost)
self.assertEquals(response.status_code, 200)
示例6: make_permission
# 需要导入模块: from django.contrib.auth.models import Permission [as 别名]
# 或者: from django.contrib.auth.models.Permission import save [as 别名]
def make_permission(modeladmin, request, queryset):
for project in queryset:
p = Permission(name=u'编辑%s锁定文本'%project.title_cn, \
content_type_id=10, codename='can_edit_lock_%s'%project.project_name)
p.save()
p = Permission(name=u'%s项目组员'%project.title_cn, \
content_type_id=11, codename='%s_member'%project.project_name)
p.save()
示例7: test_current_user_permission_not_allowed
# 需要导入模块: from django.contrib.auth.models import Permission [as 别名]
# 或者: from django.contrib.auth.models.Permission import save [as 别名]
def test_current_user_permission_not_allowed(self):
permission = Permission(content_type=self.content_type,
name='Can something', codename='can_something')
permission.save()
response, json = self.do_get(self.user_perm % 'auth.can_something',
username=self.user.username)
self.assertEquals(response.status_code, 200)
self.assertEquals(json['permissions']['auth.can_something'], False, json)
示例8: save
# 需要导入模块: from django.contrib.auth.models import Permission [as 别名]
# 或者: from django.contrib.auth.models.Permission import save [as 别名]
def save (self, *args, **kwargs):
if self.id == None: # object is bein created for the first time
super (ClassRoom, self).save(*args, **kwargs) # saving object
LEVEL = self.grade.grade
SECTION = self.section
for S in self.grade.subject.all():
if not Permission.objects.filter (codename = (u""+ LEVEL +"_"+ SECTION +"_"+ S.__unicode__())).exists(): # creating...
# Permission Structure
# codename: {Level.grade}_{ClassRoom.section}_{Subject.name}
# name: {Level.grade}-{ClassRoom.section}: {Subject.name}
NEW_P = Permission ()
NEW_P.name = u""+ LEVEL +"-"+ SECTION +": "+ S.__unicode__()
NEW_P.content_type = ContentType.objects.get (app_label="condor", model="classroom")
NEW_P.codename = u""+ LEVEL +"_"+ SECTION +"_"+ S.__unicode__()
NEW_P.save()
NEW_P = Permission ()
NEW_P.name = u"Head Teacher of "+ self.grade.grade +"-"+ self.section
NEW_P.content_type = ContentType.objects.get (app_label="condor", model="classroom")
NEW_P.codename = u"H_"+ self.grade.grade +"_"+ self.section
NEW_P.save()
else: # object is being edited...
PREVIOUS_CLASSROOM = ClassRoom.objects.get (pk = self.id) # we know this exists!
PREVIOUS_GRADE = PREVIOUS_CLASSROOM.grade
super (ClassRoom, self).save(*args, **kwargs)
if self.__unicode__() != PREVIOUS_CLASSROOM.__unicode__(): # There has been change in the object, Permissions update is necessary
"""
NOTE:
- On permission, when a class is changed say from 1A to 1B or 2A: we will assume (for permission sake) 1B is a different class!
- i.e. all permissions associated with 1A will be removed, and new Permissions for 1B will be created
"""
Permission.objects.filter (codename__istartswith = PREVIOUS_GRADE.grade +"_"+ PREVIOUS_CLASSROOM.section +"_").delete() # Deleting all associated permissions with the previous class room object
Permission.objects.filter (codename = u"H_" + PREVIOUS_GRADE.grade +"_"+ PREVIOUS_CLASSROOM.section).delete()
for S in self.grade.subject.all(): # Creating permissions for the NEW class room
NEW_P = Permission()
NEW_P.codename = self.grade.grade +"_"+ self.section +"_"+ S.name
NEW_P.name = self.grade.grade +"-"+ self.section +": "+ S.name
NEW_P.content_type = ContentType.objects.get (app_label="condor", model="classroom")
NEW_P.save()
NEW_P = Permission ()
NEW_P.name = u"Head Teacher of "+ self.grade.grade +"-"+ self.section
NEW_P.content_type = ContentType.objects.get (app_label="condor", model="classroom")
NEW_P.codename = u"H_"+ self.grade.grade +"_"+ self.section
NEW_P.save()
if not Permission.objects.filter (codename = "generate_report_card").exists():
NEW_P = Permission ()
NEW_P.codename = "generate_report_card"
NEW_P.name = "Can Generate Report Card"
NEW_P.content_type = ContentType.objects.get (app_label="condor", model="classroom")
NEW_P.save()
示例9: _get_permission
# 需要导入模块: from django.contrib.auth.models import Permission [as 别名]
# 或者: from django.contrib.auth.models.Permission import save [as 别名]
def _get_permission(self, obj, code, create = False):
ct = ContentType.objects.get_for_model(obj)
pe = Permission.objects.filter(codename = code, content_type = ct)
if pe:
pe = pe[0]
elif create:
pe = Permission(codename = code, content_type = ct, name = 'Can view %s' % force_str(obj._meta.verbose_name))
pe.save()
return pe
示例10: save
# 需要导入模块: from django.contrib.auth.models import Permission [as 别名]
# 或者: from django.contrib.auth.models.Permission import save [as 别名]
def save(self, *args, **kwargs):
if not self.id and hasattr(self, "instance_permissions"):
for perm in self.instance_permissions:
p = Permission()
p.codename = "%s%s" % (perm[0], self.slug)
p.name = "%s%s" % (perm[1], self.name)
p.content_type = ContentType.objects.get_for_model(self)
p.save()
return super(PermissionMixIn, self).save(*args, **kwargs)
示例11: createProjectPermission
# 需要导入模块: from django.contrib.auth.models import Permission [as 别名]
# 或者: from django.contrib.auth.models.Permission import save [as 别名]
def createProjectPermission(pDesc, pCodeName, groups):
projectContenType = ContentType.objects.get(app_label=APPLICATION_LABEL, model='project')
permission = Permission(name=pDesc, codename=pCodeName, content_type=projectContenType)
permission.save()
print 'Created permission=%s...' % permission.codename
for group in groups:
group.permissions.add(permission)
group.save()
print '...and associated to group=%s' % group.name
return permission
示例12: edit_all_permission
# 需要导入模块: from django.contrib.auth.models import Permission [as 别名]
# 或者: from django.contrib.auth.models.Permission import save [as 别名]
def edit_all_permission(self, log=None):
ct = ContentType.objects.get_for_model(self)
try:
p = Permission.objects.get(content_type=ct, codename=self.edit_all_permission_name())
except Permission.DoesNotExist:
p = Permission(content_type=ct, codename=self.edit_all_permission_name(),
name="Edit all data for widget %s" % self.name)
p.save()
if log is not None:
print >> log, "Created edit all permission for widget %s" % self.name
return p
示例13: get_permission
# 需要导入模块: from django.contrib.auth.models import Permission [as 别名]
# 或者: from django.contrib.auth.models.Permission import save [as 别名]
def get_permission(self, permission_prefix, create_permission=False):
permission_codename = u"%s_%s" % (permission_prefix, self.name)
permission_description = u"%s %s instances" % (permission_prefix, self.name)
try:
permission = Permission.objects.get(codename=permission_codename)
except Permission.DoesNotExist:
content_type = ContentType.objects.get(app_label=APP_LABEL, model='qproject')
permission = Permission(codename=permission_codename, name=permission_description, content_type=content_type)
if create_permission:
permission.save()
return permission
示例14: test_user_is_checked_for_global_db_permission
# 需要导入模块: from django.contrib.auth.models import Permission [as 别名]
# 或者: from django.contrib.auth.models.Permission import save [as 别名]
def test_user_is_checked_for_global_db_permission(self):
user = User(is_active=True)
user.save()
content_type = ContentType(app_label='test_app')
content_type.save()
permission = Permission(content_type=content_type,
codename='test_permission')
permission.save()
user.user_permissions.add(permission)
backend = PermissionBackend()
self.assertTrue(backend.has_perm(user, 'test_app.test_permission'))
示例15: employe
# 需要导入模块: from django.contrib.auth.models import Permission [as 别名]
# 或者: from django.contrib.auth.models.Permission import save [as 别名]
def employe(request):
logger.info('function_call employe(request) ->')
if request.method == 'POST':
employe = User.objects.get(id = int(request.POST.get('id_record')))
profile = UserProfile.objects.get(user = employe)
form = EmployeForm(initial={'username': employe.username, 'email': employe.email, 'firstname': employe.first_name, 'lastname': employe.last_name,
'vacation_days': profile.vacation_days, 'min_hours': profile.min_hours, 'max_hours': profile.max_hours,
'hourly_rate': profile.hourly_rate, 'management': employe.has_perm('auth.management'), 'api': employe.is_staff})
if request.POST.get('action'):
if request.POST.get('action') == "modifie":
form = EmployeForm(request.POST)
if form.is_valid():
employe.username = form.cleaned_data['username']
employe.email = form.cleaned_data['email']
employe.first_name = form.cleaned_data['firstname']
employe.last_name = form.cleaned_data['lastname']
employe.save()
profile.vacation_days = form.cleaned_data['vacation_days']
profile.min_hours = form.cleaned_data['min_hours']
profile.max_hours = form.cleaned_data['max_hours']
profile.hourly_rate = form.cleaned_data['hourly_rate']
profile.save()
try:
permission = Permission.objects.get(codename='management')
except Exception:
permissionNew = Permission(name = "management", content_type = ContentType.objects.get(model='permission'), codename = "management")
permissionNew.save()
permission = Permission.objects.get(codename='management')
if form.cleaned_data['management'] == True:
employe.user_permissions.add(permission)
else:
employe.user_permissions.remove(permission)
if form.cleaned_data['api'] == True:
employe.is_staff = 1
employe.save()
else:
employe.is_staff = 0
employe.save()
return HttpResponseRedirect(reverse('staff'))
c = {'user':request.user, 'employe':employe, 'form':form, 'SITENAME':settings.SITENAME, 'management':request.user.has_perm('auth.management')}
c.update(csrf(request))
return render_to_response('employe.html', c, context_instance=RequestContext(request))