當前位置: 首頁>>代碼示例>>Python>>正文


Python rest_framework.response方法代碼示例

本文整理匯總了Python中rest_framework.response方法的典型用法代碼示例。如果您正苦於以下問題:Python rest_framework.response方法的具體用法?Python rest_framework.response怎麽用?Python rest_framework.response使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在rest_framework的用法示例。


在下文中一共展示了rest_framework.response方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: create

# 需要導入模塊: import rest_framework [as 別名]
# 或者: from rest_framework import response [as 別名]
def create(self, request, *args, **kwargs):
        m = MboxMessage(request.data["mbox"])
        projects = [p for p in Project.objects.all() if p.recognizes(m)]
        grps = request.user.groups.all()
        grps_name = [grp.name for grp in grps]
        if "importers" not in grps_name:
            projects = (p for p in projects if p.maintained_by(self.request.user))
        results = []
        for project in projects:
            serializer = self.get_serializer(data=request.data)
            serializer.is_valid(raise_exception=True)
            serializer.save(project=project)
            results.append(serializer.data)
        # Fake paginator response.  Note that there is no Location header.
        return Response(
            OrderedDict([("count", len(results)), ("results", results)]),
            status=status.HTTP_201_CREATED,
        )


# Results 
開發者ID:patchew-project,項目名稱:patchew,代碼行數:23,代碼來源:rest.py

示例2: render

# 需要導入模塊: import rest_framework [as 別名]
# 或者: from rest_framework import response [as 別名]
def render(self, data, accepted_media_type=None, renderer_context=None):
        renderer_context = renderer_context or {}
        response = renderer_context.get("response")
        if response and response.exception:
            return "%d %s" % (response.status_code, response.status_text.title())
        else:
            return data


# patchew-specific permission classes 
開發者ID:patchew-project,項目名稱:patchew,代碼行數:12,代碼來源:rest.py

示例3: form_valid

# 需要導入模塊: import rest_framework [as 別名]
# 或者: from rest_framework import response [as 別名]
def form_valid(self, form):
        response = super().form_valid(form)
        if hasattr(form, 'multiple_objects_created') and form.multiple_objects_created > 1:
            msg = '%d "%s" were created/updated successfully' % (
                form.multiple_objects_created,
                cap_words(self.object._meta.verbose_name_plural))
        else:
            msg = base_success_msg % (
                cap_words(self.object._meta.verbose_name),
                self.object.__str__(), self.success_message)
        messages.success(self.request, msg)
        return response 
開發者ID:LexPredict,項目名稱:lexpredict-contraxsuite,代碼行數:14,代碼來源:mixins.py

示例4: response

# 需要導入模塊: import rest_framework [as 別名]
# 或者: from rest_framework import response [as 別名]
def response(self, request, *args, **kwargs):
        try:
            data = self.get_json_data(request, *args, **kwargs)
        except Exception as e:
            return JsonResponse(str(e), encoder=ImprovedDjangoJSONEncoder, safe=False, status=400)
        return JsonResponse(data, encoder=ImprovedDjangoJSONEncoder, safe=False) 
開發者ID:LexPredict,項目名稱:lexpredict-contraxsuite,代碼行數:8,代碼來源:mixins.py

示例5: get

# 需要導入模塊: import rest_framework [as 別名]
# 或者: from rest_framework import response [as 別名]
def get(self, request, *args, **kwargs):
        return self.response(request, *args, **kwargs) 
開發者ID:LexPredict,項目名稱:lexpredict-contraxsuite,代碼行數:4,代碼來源:mixins.py

示例6: post

# 需要導入模塊: import rest_framework [as 別名]
# 或者: from rest_framework import response [as 別名]
def post(self, request, *args, **kwargs):
        return self.response(request, *args, **kwargs) 
開發者ID:LexPredict,項目名稱:lexpredict-contraxsuite,代碼行數:4,代碼來源:mixins.py

示例7: put

# 需要導入模塊: import rest_framework [as 別名]
# 或者: from rest_framework import response [as 別名]
def put(self, request, *args, **kwargs):
        return self.response(request, *args, **kwargs) 
開發者ID:LexPredict,項目名稱:lexpredict-contraxsuite,代碼行數:4,代碼來源:mixins.py

示例8: finalize_response

# 需要導入模塊: import rest_framework [as 別名]
# 或者: from rest_framework import response [as 別名]
def finalize_response(self, request, response, *args, **kwargs):
        if self.response_unifier_serializer and \
                str(response.status_code)[0] not in ('4', '5') and \
                self.get_serializer_class() != self.response_unifier_serializer:
            try:
                if self.new_instance:
                    instance = self.new_instance
                else:
                    instance = self.get_object()
                response.data = self.response_unifier_serializer(instance, many=False).data
            except:
                pass
        return super().finalize_response(request, response, *args, **kwargs) 
開發者ID:LexPredict,項目名稱:lexpredict-contraxsuite,代碼行數:15,代碼來源:mixins.py

示例9: dispatch

# 需要導入模塊: import rest_framework [as 別名]
# 或者: from rest_framework import response [as 別名]
def dispatch(self, request, *args, **kwargs):
        request.needs_action_logging = getattr(request, 'needs_action_logging', (request.method != 'GET'))
        response = super().dispatch(request, *args, **kwargs)
        if not request.needs_action_logging:
            return response

        if not request.user or not request.user.is_authenticated:
            return response

        user_action_name = self.get_action_name() or self.user_action_methods.get(request.method)\
                           or 'unknown'

        user_action_object_pk = user_action_object = None
        if 'pk' in self.kwargs:
            # need this to get rid of extra sql in elif
            user_action_object_pk = self.kwargs['pk']
            user_action_object_pk = None \
                if str(user_action_object_pk).lower() == 'null' else user_action_object_pk
        elif (self.lookup_url_kwarg or self.lookup_field) in self.kwargs:
            user_action_object = self.get_object()
        else:
            if request.method == 'GET':
                user_action_name = 'list'
        model = self.queryset.model
        content_type = ContentType.objects.get_for_model(model)
        user_action = Action(
            user=request.user,
            name=user_action_name,
            # content_type=content_type
        )
        if user_action_object_pk:
            user_action.object_pk = user_action_object_pk
        else:
            user_action.object = user_action_object
        # this should be added after all otherwise it throws error object has no content type
        user_action.content_type = content_type
        user_action.save()
        self.user_action = user_action
        return response 
開發者ID:LexPredict,項目名稱:lexpredict-contraxsuite,代碼行數:41,代碼來源:mixins.py

示例10: should_log

# 需要導入模塊: import rest_framework [as 別名]
# 或者: from rest_framework import response [as 別名]
def should_log(self, request, response):
        """
        Log only if enable logging via AppVar
        """
        # do not log FileResponse and other possible large responses
        if isinstance(response, StreamingHttpResponse):
            return False
        from apps.common.app_vars import TRACK_API
        return TRACK_API.val 
開發者ID:LexPredict,項目名稱:lexpredict-contraxsuite,代碼行數:11,代碼來源:mixins.py

示例11: list

# 需要導入模塊: import rest_framework [as 別名]
# 或者: from rest_framework import response [as 別名]
def list(self, request, *args, **kwargs):
        # 1. get full queryset
        queryset = self.get_queryset()
        # 2. filter and sort
        queryset = self.filter_queryset(queryset)
        # 2.1 export in xlsx if needed
        if request.GET.get('export_to') in ['csv', 'xlsx', 'pdf']:
            serializer = self.get_serializer(queryset, many=True)
            data = serializer.data

            if 'columns' in request.GET:
                columns = request.GET['columns'].split(',')
                data = [{k: v for k, v in i.items() if k in columns} for i in data]
            return self.export(data,
                               source_name=self.get_export_file_name() or
                                           queryset.model.__name__.lower(),
                               fmt=request.GET.get('export_to'))
        # 3. count total records !before queryset paginated
        # try:
        #     total_records = queryset.count()
        # except:
        #     total_records = len(queryset)
        # 4. get extra data !before queryset paginated
        extra_data = self.get_extra_data(queryset)
        # 5. paginate
        queryset = self.paginate_queryset(queryset)
        # 6. serialize
        serializer = self.get_serializer(queryset, many=True)
        data = serializer.data
        if isinstance(data, GeneratorType):
            data = list(data)
        # 6.1 filter "columns"
        if 'columns' in request.GET:
            columns = request.GET['columns'].split(',')
            data = [{k: v for k, v in i.items() if k in columns} for i in data]
        # 7. compose returned data
        show_total_records = json.loads(self.request.GET.get('total_records', 'false'))
        if show_total_records:
            # first try to use paginator to get total records
            total_records = getattr(queryset, 'total_records', None)
            if total_records is None:
                total_records = len(data)
            ret = {'data': data,
                   'total_records': total_records}
            if extra_data:
                ret.update(extra_data)
            return rest_framework.response.Response(ret)
        return rest_framework.response.Response(data) 
開發者ID:LexPredict,項目名稱:lexpredict-contraxsuite,代碼行數:50,代碼來源:mixins.py


注:本文中的rest_framework.response方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。