我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用django.contrib.auth.decorators.permission_required()。
def rules(request): mimetype = 'application/json' data = {} return HttpResponse(json.dumps(data), mimetype) #@permission_required('is_staff', login_url=reverse_lazy('login')) #@login_required(login_url=reverse_lazy('login'))
def api_permission_required(perm): """Slight override on django.contrib.auth.decorators.permission_required that forces the `raise_exception` to be set to True. """ return permission_required(perm, raise_exception=True)
def access_required(view): return permission_required('audit.api')(permission_required('audit.search')(view))
def batch_add(request): return render(request, 'batch_add.html', locals()) # @login_required(login_url='/accounts/login/') # @permission_required('myapp.can_see_mysqladmin', login_url='/') # def test_tb(request): # dbtag = request.GET['dbtag'] # if dbtag!='all': # mydata = {'dupresult':get_dupreport(dbtag,request.GET['email'])} # # return render(request, 'batch_add.html', locals()) # return JsonResponse(mydata)
def change_multiple_items(request): """ For å endre flere itmes, tilgjengelig hvis man merker gjenstander i search og trykker 'endre'. """ # TODO søk på "arduino": hverken arduino eller OculusRift lar seg avmerke i boksen, det fungerer på resten # TODO endre her så man også kan lage nye tags # TODO endre så man kan endre zone og/eller shelf # TODO lag en form og gjør viewet lesbart. if request.method == "POST": try: """ Items marked in search-view for changing """ # items_for_changing: String of all item_id's to be changed, separated with '_' (also one at the end) items_for_changing = request.POST['items'] except KeyError: form = ItemForm(request.POST) # TODO is_valid() returnerer ikke true form.is_valid() items = form.cleaned_data['tags_chips'] try: new_tags = form.cleaned_data['name'] except KeyError: """ Deletes all marked items """ ItemForm.delete_all_items(items) messages.add_message(request, messages.SUCCESS, "Gjenstander ble slettet.") return HttpResponseRedirect(reverse('inventory:index')) else: """ Changes tag on all marked items """ ItemForm.change_tags(items, new_tags) messages.add_message(request, messages.SUCCESS, "Tagger ble oppdatert.") return HttpResponseRedirect(reverse('inventory:index')) else: items = [get_object_or_404(Item, pk=item_id) for item_id in items_for_changing.split('_')[:-1]] context = { 'autocomplete_dict': ItemForm.get_autocomplete_dict(), 'form': ItemForm(initial={'tags_chips': items_for_changing, 'name': 'skip'}), 'items': items, } return render(request, 'inventory/change_multiple_items.html', context) else: return render(request, 'inventory/search.html', show_all_items()) # TODO permission_required redirecter ikke til dit man kom fra når man må logge inn # TODO gjør så man kan sette sone og hylle for alle items med denne tagen.
def my_view(request): #PermissionRequiredMixin from django.contrib.auth.mixins import PermissionRequiredMixin class MyView(PermissionRequiredMixin, View): permission_required = 'polls.can_vote' # Or multiple of permissions: permission_required = ('polls.can_open', 'polls.can_edit') #AccessMixin