Python rest_framework.exceptions.ParseError() Examples
The following are 30
code examples of rest_framework.exceptions.ParseError().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
rest_framework.exceptions
, or try the search function
.
Example #1
Source File: api.py From linkedevents with MIT License | 6 votes |
def parse_duration_string(duration): """ Parse duration string expressed in format 86400 or 86400s (24 hours) 180m or 3h (3 hours) 3d (3 days) """ m = re.match(r'(\d+)\s*(d|h|m|s)?$', duration.strip().lower()) if not m: raise ParseError("Invalid duration supplied. Try '1d', '2h' or '180m'.") val, unit = m.groups() if not unit: unit = 's' if unit == 's': mul = 1 elif unit == 'm': mul = 60 elif unit == 'h': mul = 3600 elif unit == 'd': mul = 24 * 3600 return int(val) * mul
Example #2
Source File: off_topic_channel_name.py From site with MIT License | 6 votes |
def list(self, request: HttpRequest) -> Response: """ DRF method for listing OffTopicChannelName entries. Called by the Django Rest Framework in response to the corresponding HTTP request. """ if 'random_items' in request.query_params: param = request.query_params['random_items'] try: random_count = int(param) except ValueError: raise ParseError(detail={'random_items': ["Must be a valid integer."]}) if random_count <= 0: raise ParseError(detail={ 'random_items': ["Must be a positive integer."] }) queryset = self.get_queryset().order_by('?')[:random_count] serialized = self.serializer_class(queryset, many=True) return Response(serialized.data) queryset = self.get_queryset() serialized = self.serializer_class(queryset, many=True) return Response(serialized.data)
Example #3
Source File: entity.py From resolwe with Apache License 2.0 | 6 votes |
def duplicate(self, request, *args, **kwargs): """Duplicate (make copy of) ``Entity`` models.""" if not request.user.is_authenticated: raise exceptions.NotFound inherit_collection = request.data.get("inherit_collection", False) ids = self.get_ids(request.data) queryset = get_objects_for_user( request.user, "view_entity", Entity.objects.filter(id__in=ids) ) actual_ids = queryset.values_list("id", flat=True) missing_ids = list(set(ids) - set(actual_ids)) if missing_ids: raise exceptions.ParseError( "Entities with the following ids not found: {}".format( ", ".join(map(str, missing_ids)) ) ) duplicated = queryset.duplicate( contributor=request.user, inherit_collection=inherit_collection ) serializer = self.get_serializer(duplicated, many=True) return Response(serializer.data)
Example #4
Source File: collection.py From resolwe with Apache License 2.0 | 6 votes |
def duplicate(self, request, *args, **kwargs): """Duplicate (make copy of) ``Collection`` models.""" if not request.user.is_authenticated: raise exceptions.NotFound ids = self.get_ids(request.data) queryset = get_objects_for_user( request.user, "view_collection", Collection.objects.filter(id__in=ids) ) actual_ids = queryset.values_list("id", flat=True) missing_ids = list(set(ids) - set(actual_ids)) if missing_ids: raise exceptions.ParseError( "Collections with the following ids not found: {}".format( ", ".join(map(str, missing_ids)) ) ) duplicated = queryset.duplicate(contributor=request.user) serializer = self.get_serializer(duplicated, many=True) return Response(serializer.data)
Example #5
Source File: data.py From resolwe with Apache License 2.0 | 6 votes |
def duplicate(self, request, *args, **kwargs): """Duplicate (make copy of) ``Data`` objects.""" if not request.user.is_authenticated: raise exceptions.NotFound inherit_collection = request.data.get("inherit_collection", False) ids = self.get_ids(request.data) queryset = get_objects_for_user( request.user, "view_data", Data.objects.filter(id__in=ids) ) actual_ids = queryset.values_list("id", flat=True) missing_ids = list(set(ids) - set(actual_ids)) if missing_ids: raise exceptions.ParseError( "Data objects with the following ids not found: {}".format( ", ".join(map(str, missing_ids)) ) ) duplicated = queryset.duplicate( contributor=request.user, inherit_collection=inherit_collection, ) serializer = self.get_serializer(duplicated, many=True) return Response(serializer.data)
Example #6
Source File: mixins.py From resolwe with Apache License 2.0 | 6 votes |
def get_ids(self, request_data, parameter_name="ids"): """Extract a list of integers from request data.""" if parameter_name not in request_data: raise ParseError("`{}` parameter is required".format(parameter_name)) ids = request_data.get(parameter_name) if not isinstance(ids, list): raise ParseError("`{}` parameter not a list".format(parameter_name)) if not ids: raise ParseError("`{}` parameter is empty".format(parameter_name)) if any(map(lambda id: not isinstance(id, int), ids)): raise ParseError( "`{}` parameter contains non-integers".format(parameter_name) ) return ids
Example #7
Source File: views.py From cmdb with GNU Lesser General Public License v3.0 | 6 votes |
def send_verify_code(self, request, pk=None): serializer = self.get_serializer(data=request.data, context={"request", request}) serializer.is_valid(raise_exception=True) username = serializer.validated_data["username"] try: verify_code_inst = models.RestPWVerifyCode.objects.get(user__username=username) except models.RestPWVerifyCode.DoesNotExist: pass else: if datetime.datetime.now() - verify_code_inst.add_time < datetime.timedelta(seconds=60): raise exceptions.ParseError("Less than 60 seconds from last sent") verify_code_inst.delete() user = User.objects.get(username=username) if not user.email: raise exceptions.ParseError(f"{username} user does not have a email, please contact the administrator to" f" reset password") try: code = email_verify_code.send_verifycode(user.email) except Exception as exc: raise exceptions.ParseError("send failed, please try again later!") reset_pw_verify_code = models.RestPWVerifyCode(user=user, code=code) reset_pw_verify_code.save() return Response({"detail": "send successfully", "email": user.email})
Example #8
Source File: utils.py From resolwe with Apache License 2.0 | 6 votes |
def check_owner_permission(payload, allow_user_owner): """Raise ``PermissionDenied``if ``owner`` found in ``data``.""" for entity_type in ["users", "groups"]: for perm_type in ["add", "remove"]: for perms in payload.get(entity_type, {}).get(perm_type, {}).values(): if "owner" in perms: if entity_type == "users" and allow_user_owner: continue if entity_type == "groups": raise exceptions.ParseError( "Owner permission cannot be assigned to a group" ) raise exceptions.PermissionDenied( "Only owners can grant/revoke owner permission" )
Example #9
Source File: views.py From normandy with Mozilla Public License 2.0 | 6 votes |
def get_queryset(self): queryset = self.queryset if self.request.GET.get("status") == "enabled": queryset = queryset.only_enabled() elif self.request.GET.get("status") == "disabled": queryset = queryset.only_disabled() if "text" in self.request.GET: text = self.request.GET.get("text") if "\x00" in text: raise ParseError("Null bytes in text") queryset = queryset.filter( Q(latest_revision__name__contains=text) | Q(latest_revision__extra_filter_expression__contains=text) ) return queryset
Example #10
Source File: utils.py From resolwe with Apache License 2.0 | 6 votes |
def fetch_user(query): """Get user by ``pk``, ``username`` or ``email``. Raise error if user can not be determined. """ lookup = Q(username=query) | Q(email=query) if query.isdigit(): lookup = lookup | Q(pk=query) user_model = get_user_model() users = user_model.objects.filter(lookup) if not users: raise exceptions.ParseError("Unknown user: {}".format(query)) elif len(users) >= 2: raise exceptions.ParseError("Cannot uniquely determine user: {}".format(query)) return users[0]
Example #11
Source File: views.py From normandy with Mozilla Public License 2.0 | 6 votes |
def get_queryset(self): queryset = self.queryset if self.request.GET.get("status") == "enabled": queryset = queryset.only_enabled() elif self.request.GET.get("status") == "disabled": queryset = queryset.only_disabled() if "text" in self.request.GET: text = self.request.GET.get("text") if "\x00" in text: raise ParseError("Null bytes in text") tokens = set(re.split(r"[ /_-]", text)) query = Q() for token in tokens: query &= ( Q(latest_revision__name__icontains=token) | Q(latest_revision__extra_filter_expression__icontains=token) | Q(latest_revision__arguments_json__icontains=token) ) queryset = queryset.filter(query) return queryset
Example #12
Source File: viewsets.py From resolwe with Apache License 2.0 | 6 votes |
def order_search(self, search): """Order given search by the ordering parameter given in request. :param search: ElasticSearch query object """ ordering = self.get_query_param("ordering", self.ordering) if not ordering: return search sort_fields = [] for raw_ordering in ordering.split(","): ordering_field = raw_ordering.lstrip("-") if ordering_field not in self.ordering_fields: raise ParseError( "Ordering by `{}` is not supported.".format(ordering_field) ) ordering_field = self.ordering_map.get(ordering_field, ordering_field) direction = "-" if raw_ordering[0] == "-" else "" sort_fields.append("{}{}".format(direction, ordering_field)) return search.sort(*sort_fields)
Example #13
Source File: views.py From PrivacyScore with GNU General Public License v3.0 | 6 votes |
def delete_scan_list(request: Request, token: str) -> Response: """Update an existing list.""" # TODO: Access control (Or is token sufficient)? try: scan_list = ScanList.objects.get(token=token) # all related objects CASCADE automatically. scan_list.delete() return Response({ 'type': 'success', 'message': 'ok', }) except KeyError as e: raise ParseError except ScanList.DoesNotExist: raise NotFound # TODO: Why POST? # TODO: Add a filter option to get_lists and get rid of this search method
Example #14
Source File: off_topic_channel_name.py From site with MIT License | 6 votes |
def create(self, request: HttpRequest) -> Response: """ DRF method for creating a new OffTopicChannelName. Called by the Django Rest Framework in response to the corresponding HTTP request. """ if 'name' in request.query_params: create_data = {'name': request.query_params['name']} serializer = OffTopicChannelNameSerializer(data=create_data) serializer.is_valid(raise_exception=True) serializer.save() return Response(create_data, status=HTTP_201_CREATED) else: raise ParseError(detail={ 'name': ["This query parameter is required."] })
Example #15
Source File: filters.py From kobo-predict with BSD 2-Clause "Simplified" License | 6 votes |
def _xform_filter_queryset(self, request, queryset, view, keyword): """Use XForm permissions""" xform = request.query_params.get('xform') if xform: try: int(xform) except ValueError: raise ParseError( u"Invalid value for formid %s." % xform) xform = get_object_or_404(XForm, pk=xform) xform_qs = XForm.objects.filter(pk=xform.pk) else: xform_qs = XForm.objects.all() xforms = super(XFormPermissionFilterMixin, self).filter_queryset( request, xform_qs, view) kwarg = {"%s__in" % keyword: xforms} return queryset.filter(**kwarg)
Example #16
Source File: object_lookup_mixin.py From kobo-predict with BSD 2-Clause "Simplified" License | 6 votes |
def get_object(self): """ Incase the lookup is on an object that has been hyperlinked then update the queryset filter appropriately """ if self.kwargs.get(self.lookup_field, None) is None: raise ParseError( 'Expected URL keyword argument `%s`.' % self.lookup_field ) queryset = self.filter_queryset(self.get_queryset()) filter_kwargs = {} serializer = self.get_serializer() lookup_field = self.lookup_field if self.lookup_field in serializer.get_fields(): k = serializer.get_fields()[self.lookup_field] if isinstance(k, serializers.HyperlinkedRelatedField): lookup_field = '%s__%s' % (self.lookup_field, k.lookup_field) filter_kwargs[lookup_field] = self.kwargs[self.lookup_field] obj = get_object_or_404(queryset, **filter_kwargs) # May raise a permission denied self.check_object_permissions(self.request, obj) return obj
Example #17
Source File: uploaders.py From django-drf-filepond with BSD 3-Clause "New" or "Revised" License | 6 votes |
def _get_file_obj(cls, request): # By default the upload element name is expected to be "filepond" # As raised in issue #4, there are cases where there may be more # than one filepond instance on a page, or the developer has opted # not to use the name "filepond" for the filepond instance. # Using the example from #4, this provides support these cases. upload_field_name = 'filepond' if 'fp_upload_field' in request.data: upload_field_name = request.data['fp_upload_field'] if upload_field_name not in request.data: raise ParseError('Invalid request data has been provided.') file_obj = request.data[upload_field_name] return file_obj # The file ID and upload ID are generated by _get_file_id in # django_drf_filepond.views. The TemporaryUpload model should validate # that the values provided are within spec but in some cases, e.g. when # using SQLite, that doesn't happen. We therefore provide these two # methods for doing local validation of these values since they are # passed in as parameters to handle_upload.
Example #18
Source File: stats_serializer.py From kobo-predict with BSD 2-Clause "Simplified" License | 6 votes |
def to_representation(self, obj): if obj is None: return super(StatsInstanceSerializer, self).to_representation(obj) request = self.context.get('request') method = request.query_params.get('method', None) field = request.query_params.get('field', None) if field and field not in obj.data_dictionary().get_keys(): raise exceptions.ParseError(detail=_("Field not in XForm.")) stats_function = STATS_FUNCTIONS.get(method and method.lower(), get_all_stats) try: data = stats_function(obj, field) except ValueError as e: raise exceptions.ParseError(detail=e.message) return data
Example #19
Source File: data_serializer.py From kobo-predict with BSD 2-Clause "Simplified" License | 6 votes |
def to_representation(self, obj): request = self.context.get('request') if not isinstance(obj, XForm): return super(DataListSerializer, self).to_representation(obj) query_params = (request and request.query_params) or {} query = { ParsedInstance.USERFORM_ID: u'%s_%s' % (obj.user.username, obj.id_string) } try: query.update(json.loads(query_params.get('query', '{}'))) except ValueError: raise ParseError(_("Invalid query: %(query)s" % {'query': query_params.get('query')})) query_kwargs = { 'query': json.dumps(query), 'fields': query_params.get('fields'), 'sort': query_params.get('sort') } cursor = ParsedInstance.query_mongo_minimal(**query_kwargs) return list(cursor)
Example #20
Source File: search.py From course-discovery with GNU Affero General Public License v3.0 | 6 votes |
def filter_facet_queryset(self, queryset): queryset = super().filter_facet_queryset(queryset) q = self.request.query_params.get('q') if q: queryset = queryset.filter(SQ(text=AutoQuery(q)) | SQ(title=AutoQuery(q))) facet_serializer_cls = self.get_facet_serializer_class() field_queries = getattr(facet_serializer_cls.Meta, 'field_queries', {}) if self.ensure_published: # Ensure we only return published, non-hidden items queryset = queryset.filter(published=True).exclude(hidden=True) for facet in self.request.query_params.getlist('selected_query_facets'): query = field_queries.get(facet) if not query: raise ParseError('The selected query facet [{facet}] is not valid.'.format(facet=facet)) queryset = queryset.raw_search(query['query']) return queryset
Example #21
Source File: user_profile_viewset.py From kobo-predict with BSD 2-Clause "Simplified" License | 6 votes |
def get_object(self): """Lookup user profile by pk or username""" lookup = self.kwargs.get(self.lookup_field, None) if lookup is None: raise ParseError( 'Expected URL keyword argument `%s`.' % self.lookup_field ) queryset = self.filter_queryset(self.get_queryset()) try: pk = int(lookup) except (TypeError, ValueError): filter_kwargs = {'username': lookup} else: filter_kwargs = {'pk': pk} # Return a 404 if the user does not exist user = get_object_or_404(User, **filter_kwargs) # Since the user does exist, create a matching profile if necessary obj, created = queryset.get_or_create(user=user) # May raise a permission denied self.check_object_permissions(self.request, obj) return obj
Example #22
Source File: data_viewset.py From kobo-predict with BSD 2-Clause "Simplified" License | 6 votes |
def get_object(self): obj = super(DataViewSet, self).get_object() pk_lookup, dataid_lookup = self.lookup_fields pk = self.kwargs.get(pk_lookup) dataid = self.kwargs.get(dataid_lookup) if pk is not None and dataid is not None: try: int(dataid) except ValueError: raise ParseError(_(u"Invalid dataid %(dataid)s" % {'dataid': dataid})) obj = get_object_or_404(Instance, pk=dataid, xform__pk=pk) return obj
Example #23
Source File: data_viewset.py From kobo-predict with BSD 2-Clause "Simplified" License | 6 votes |
def filter_queryset(self, queryset, view=None): qs = super(DataViewSet, self).filter_queryset(queryset) pk = self.kwargs.get(self.lookup_field) tags = self.request.query_params.get('tags', None) if tags and isinstance(tags, six.string_types): tags = tags.split(',') qs = qs.filter(tags__name__in=tags).distinct() if pk: try: int(pk) except ValueError: if pk == self.public_data_endpoint: qs = self._get_public_forms_queryset() else: raise ParseError(_(u"Invalid pk %(pk)s" % {'pk': pk})) else: qs = self._filtered_or_shared_qs(qs, pk) return qs
Example #24
Source File: data_viewset.py From kobo-predict with BSD 2-Clause "Simplified" License | 6 votes |
def enketo(self, request, *args, **kwargs): self.object = self.get_object() data = {} if isinstance(self.object, XForm): raise ParseError(_(u"Data id not provided.")) elif(isinstance(self.object, Instance)): if request.user.has_perm("change_xform", self.object.xform): return_url = request.query_params.get('return_url') if not return_url: raise ParseError(_(u"return_url not provided.")) try: data["url"] = get_enketo_edit_url( request, self.object, return_url) except EnketoError as e: data['detail'] = "{}".format(e) else: raise PermissionDenied(_(u"You do not have edit permissions.")) return Response(data=data)
Example #25
Source File: viewsets.py From dynamic-rest with MIT License | 6 votes |
def get_request_patch_all(self): patch_all = self.get_request_feature(self.PATCH_ALL) if not patch_all: return None patch_all = patch_all.lower() if patch_all == 'query': pass elif is_truthy(patch_all): patch_all = True else: raise exceptions.ParseError( '"%s" is not valid for %s' % ( patch_all, self.PATCH_ALL ) ) return patch_all
Example #26
Source File: parsers.py From django-rest-framework-xml with BSD 3-Clause "New" or "Revised" License | 6 votes |
def parse(self, stream, media_type=None, parser_context=None): """ Parses the incoming bytestream as XML and returns the resulting data. """ assert etree, "XMLParser requires defusedxml to be installed" parser_context = parser_context or {} encoding = parser_context.get("encoding", settings.DEFAULT_CHARSET) parser = etree.DefusedXMLParser(encoding=encoding) try: tree = etree.parse(stream, parser=parser, forbid_dtd=True) except (etree.ParseError, ValueError) as exc: raise ParseError("XML parse error - %s" % str(exc)) data = self._xml_convert(tree.getroot()) return data
Example #27
Source File: test_parsers.py From django-rest-framework-json-api with BSD 2-Clause "Simplified" License | 6 votes |
def test_parse_invalid_data_key(self): parser = JSONParser() string = json.dumps({ 'data': [{ 'id': 123, 'type': 'Blog', 'attributes': { 'json-value': {'JsonKey': 'JsonValue'} }, }] }) stream = BytesIO(string.encode('utf-8')) with self.assertRaises(ParseError): parser.parse(stream, None, self.parser_context)
Example #28
Source File: api.py From linkedevents with MIT License | 6 votes |
def validate_hours(val, param): if len(val) > 2: raise ParseError(f'Only hours and minutes can be given in {param}. For example: 16:00.') try: hour = int(val[0]) except ValueError: raise ParseError(f'Hours should be passed as numbers in {param}. For example: 16:00.') if not (0 <= hour <= 23): raise ParseError(f'Hours should be between 0 and 23 in {param}, for example: 16:00. You have passed {hour}.') if len(val) == 2 and val[1]: try: minute = int(val[1]) except ValueError: raise ParseError(f'Minutes should be passed as numbers in {param}. For example: 16:20.') if not (0 <= minute <= 59): raise ParseError(f'Minutes should be between 0 and 59 in {param} as in 16:20. You passed {minute}.') return hour, minute return hour, 0
Example #29
Source File: decorators.py From GloboNetworkAPI with Apache License 2.0 | 6 votes |
def logs_method_apiview(func): @functools.wraps(func) def inner(self, request, *args, **kwargs): log = logging.getLogger(type(self).__name__) try: request.DATA except ParseError: pass log.info( 'View:%s, method:%s - Data send: %s - Url params: %s' % ( type(self).__name__, request.method, request.DATA, kwargs)) return func(self, request, *args, **kwargs) return inner
Example #30
Source File: permabots_hook.py From permabots with BSD 3-Clause "New" or "Revised" License | 5 votes |
def post(self, request, key): """ Process notitication hooks: 1. Obtain Hook 2. Check Auth 3. Delay processing to a task 4. Respond requester """ try: hook = Hook.objects.get(key=key, enabled=True) except Hook.DoesNotExist: msg = _("Key %s not associated to an enabled hook or bot") % key logger.warning(msg) return Response(msg, status=status.HTTP_404_NOT_FOUND) if hook.bot.owner != request.user: raise exceptions.AuthenticationFailed() try: parsed_data = request.data logger.debug("Hook %s attending request %s" % (hook, parsed_data)) handle_hook.delay(hook.id, parsed_data) except ParseError as e: return Response(str(e), status=status.HTTP_400_BAD_REQUEST) except: exc_info = sys.exc_info() traceback.print_exception(*exc_info) msg = _("Error processing %s for key %s") % (request.data, key) logger.error(msg) return Response(msg, status=status.HTTP_500_INTERNAL_SERVER_ERROR) else: return Response(status=status.HTTP_200_OK)