我们从Python开源项目中,提取了以下41个代码示例,用于说明如何使用django.utils.dateparse.parse_duration()。
def create(self, request, *args, **kwargs): try: return super().create(request, *args, **kwargs) except UploadDuplicateError as e: dup = Image.objects.get(pk=e.instance.hash) # this usage of parse_duration is safe, because we already # validated the data (exception is thrown by the Model class, so # the validator passed). if 'expiration' in request.data: dup.expiration = parse_duration(request.data['expiration']) dup.save() serializer = self.get_serializer(dup) headers = self.get_success_headers(serializer.data) return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
def to_python(self, value): if value is None: return value if isinstance(value, datetime.timedelta): return value try: parsed = parse_duration(value) except ValueError: pass else: if parsed is not None: return parsed raise exceptions.ValidationError( self.error_messages['invalid'], code='invalid', params={'value': value}, )
def to_python(self, value): if value in self.empty_values: return None if isinstance(value, datetime.timedelta): return value value = parse_duration(value) if value is None: raise ValidationError(self.error_messages['invalid'], code='invalid') return value
def convert_durationfield_value(self, value, expression, connection, context): if value is not None: value = str(decimal.Decimal(value) / decimal.Decimal(1000000)) value = parse_duration(value) return value
def to_python(self, value): if value in self.empty_values: return None if isinstance(value, datetime.timedelta): return value value = parse_duration(force_text(value)) if value is None: raise ValidationError(self.error_messages['invalid'], code='invalid') return value
def _sqlite_format_dtdelta(conn, lhs, rhs): """ LHS and RHS can be either: - An integer number of microseconds - A string representing a timedelta object - A string representing a datetime """ try: if isinstance(lhs, six.integer_types): lhs = str(decimal.Decimal(lhs) / decimal.Decimal(1000000)) real_lhs = parse_duration(lhs) if real_lhs is None: real_lhs = backend_utils.typecast_timestamp(lhs) if isinstance(rhs, six.integer_types): rhs = str(decimal.Decimal(rhs) / decimal.Decimal(1000000)) real_rhs = parse_duration(rhs) if real_rhs is None: real_rhs = backend_utils.typecast_timestamp(rhs) if conn.strip() == '+': out = real_lhs + real_rhs else: out = real_lhs - real_rhs except (ValueError, TypeError): return None # typecast_timestamp returns a date or a datetime without timezone. # It will be formatted as "%Y-%m-%d" or "%Y-%m-%d %H:%M:%S[.%f]" return str(out)
def handle(self, *args, **options): now = timezone.now() expdate = now - parse_duration(options['exp_time']) self.stdout.write("Now: " + now.isoformat()) self.stdout.write("Exp: " + expdate.isoformat()) expired = Payment.objects.filter(created__lte=expdate, status='new', paid_amount=0) for p in expired: self.stdout.write("Payment #%d (%s): %s" % (p.id, p.user.username, p.created)) if not options['sim']: p.status = 'cancelled' p.save()