我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用rest_framework.decorators.api_view()。
def deploy_general_remaining_sites(request, is_project, pk): fxf_id = request.data.get('id') fxf_status = request.data.get('is_deployed') try: if is_project == "1": with transaction.atomic(): fxf = FieldSightXF.objects.get(pk=fxf_id) if fxf_status: for site in fxf.project.sites.filter(is_active=True): child, created = FieldSightXF.objects.get_or_create(is_staged=False, is_scheduled=False, xf=fxf.xf, site=site, fsform_id=fxf_id) child.is_deployed = True child.save() if created: send_message_stages(site) else: return Response({'error':"Deploy Form First and deploy to remaining.."}, status=status.HTTP_400_BAD_REQUEST) return Response({'msg': 'ok'}, status=status.HTTP_200_OK) else: return Response({'error':"Site level Deploy to remaining Not permitted."}, status=status.HTTP_400_BAD_REQUEST) except Exception as e: return Response({'error':e.message}, status=status.HTTP_400_BAD_REQUEST) # @group_required("Project") # @api_view([]) # def deploy_general(request, fxf_id): # with transaction.atomic(): # fxf = FieldSightXF.objects.get(pk=fxf_id) # FieldSightXF.objects.filter(fsform=fxf, is_scheduled=False, is_staged=False).delete() # for site in fxf.project.sites.filter(is_active=True): # # cloning from parent # child = FieldSightXF(is_staged=False, is_scheduled=False, xf=fxf.xf, site=site, fsform_id=fxf_id, # is_deployed=True) # child.save() # messages.info(request, 'General Form {} Deployed to Sites'.format(fxf.xf.title)) # return HttpResponseRedirect(reverse("forms:project-general", kwargs={'project_id': fxf.project.pk}))
def generate_root(self, request): from rest_framework.reverse import reverse from rest_framework.response import Response from rest_framework.decorators import api_view index = OrderedDict(( ('Auth', OrderedDict(( ('????????? ?????? ?? ??????/??????', reverse('get_token', request=request)), ('?????????? ??????', reverse('refresh_token', request=request)), ('???????? ??????', reverse('check_token', request=request)), ))), )) project_apps = settings.PROJECT_APPS for app_name in project_apps: index[app_name.capitalize()] = {} models = dict(apps.all_models[app_name]) for name, model in models.items(): model_dict = {} from rest_framework.reverse import reverse_lazy model_dict[name] = reverse_lazy('api:{}-list'.format(name.lower()), request=request) index[app_name.capitalize()].update(model_dict) def root_view(request): return Response(index) root_view.__name__ = self.name root_view.__doc__ = self.description return api_view(['GET'])(root_view)(request)
def wrap_as_django_rest_framework_view(cls, *args, **kwargs): view = cls.as_view(*args, **kwargs) view = permission_classes((IsAuthenticated,))(view) view = authentication_classes(cls.get_view_authentication_classes())(view) view = api_view(['POST', 'GET'])(view) return view
def make_view(serializer_cls): @decorators.api_view(['POST']) def handler(request): if request.user.is_authenticated(): raise NotFound serializer = serializer_cls(data=request.data, context={'request': request}) if serializer.is_valid(raise_exception=True): user = serializer.save() auth_login(request, user) return Response(UserSerializer(user).data) return handler
def api_post_detail(request, id): try: post = Post.objects.get(id=id) except Post.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = PostSerializer(post, context={'request': request}) return Response(serializer.data) #@api_view(['POST', 'GET']) #def contact_mail_ws(request): #if request.method == 'POST': #contact = ContactMailSerializer(data=request.data) #if contact.is_valid(): #form_name = contact.data['name'] #form_phone = contact.data['phone'] #form_email = contact.data['email'] #form_message = contact.data['message'] + "email: " + form_email #send_mail("New contact form submission", #form_message, #form_email, #[config('DEFAULT_FROM_EMAIL')], #fail_silently=False #) #return Response(contact.data, status=status.HTTP_201_CREATED) #return Response({"success": False, 'error-code': 'invalid-data'})
def as_view(cls, *args, **kwargs): view = super(DRFAuthenticatedGraphQLView, cls).as_view(*args, **kwargs) view = permission_classes((IsAuthenticated,))(view) view = authentication_classes(api_settings.DEFAULT_AUTHENTICATION_CLASSES)(view) view = api_view(['GET', 'POST'])(view) return view