本文整理汇总了Python中django.contrib.auth.models.Permission类的典型用法代码示例。如果您正苦于以下问题:Python Permission类的具体用法?Python Permission怎么用?Python Permission使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Permission类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: pre_register
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: test_user_permission_not_allowed
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)
示例3: test_usuarios_render_form_change_pas
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)
示例4: test_current_user_permission_not_allowed
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)
示例5: _get_permission
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
示例6: createProjectPermission
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
示例7: edit_all_permission
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
示例8: get_permission
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
示例9: test_user_is_checked_for_global_db_permission
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'))
示例10: permission
def permission(self, log=None):
ct = ContentType.objects.get_for_model(self)
try:
p = Permission.objects.get(content_type=ct,
codename=self.permission_name())
except Permission.DoesNotExist:
p = Permission(content_type=ct, codename=self.permission_name(),
name="upload data files for %s" % self.app)
p.save()
if log:
print >> log, "Created uploader permission for %s" % self.app
return p
示例11: view_permisions
def view_permisions(apps=None):
if not apps: apps = ['whs', 'lab', 'energy', 'it']
for app in apps:
for c in ContentType.objects.filter(app_label=app):
if issubclass(c.model_class(),UrlMixin):
p = Permission(codename='view_%s' % c.model, name=u'Можно просматривать %s' % c.name, content_type=c)
try:
p.validate_unique()
except ValidationError:
pass
else:
p.save()
示例12: employe
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))
示例13: test_render_configuracoes
def test_render_configuracoes(self):
# Faz chamada da pagina
contentItem = ContentType.objects.get(app_label='core',model='parametro')
permission = Permission(name='Can Access Configurations',content_type=contentItem,codename='core_configurations')
permission.save()
permissions = Permission.objects.all().filter(content_type=contentItem.id)
for permission in permissions:
self.user.user_permissions.add(permission)
response = self.client.get('/core/configuracoes/')
self.assertEquals(response.status_code, 200)
示例14: add_permissions
def add_permissions(self):
#busca tabela de models
contentItem = ContentType.objects.get(app_label='oportunidade',model='oportunidade')
#busca permissoes do model
if not contentItem:
self.assertTrue(False)
customPermission = Permission(name='Dashboard',content_type=contentItem,codename='index_dashboard')
customPermission.save()
permissions = Permission.objects.filter(content_type=contentItem.id)
for permission in permissions:
self.user.user_permissions.add(permission)
示例15: edit_all_permission
def edit_all_permission(self, log=None):
"""
Returns the actual :model:`auth.Permission` permission object required to manually edit
all data for this widget family, creating it if it does not already exist.
"""
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