Python django.utils.dateparse.parse_date() Examples
The following are 28
code examples of django.utils.dateparse.parse_date().
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
django.utils.dateparse
, or try the search function
.
Example #1
Source File: utils.py From django-csp-reports with MIT License | 6 votes |
def parse_date_input(value): """Return datetime based on the user's input. @param value: User's input @type value: str @raise ValueError: If the input is not valid. @return: Datetime of the beginning of the user's date. """ try: limit = parse_date(value) except ValueError: limit = None if limit is None: raise ValueError("'{}' is not a valid date.".format(value)) limit = datetime(limit.year, limit.month, limit.day) if settings.USE_TZ: limit = make_aware(limit) return limit
Example #2
Source File: __init__.py From luscan-devel with GNU General Public License v2.0 | 6 votes |
def to_python(self, value): if value is None: return value if isinstance(value, datetime.datetime): if settings.USE_TZ and timezone.is_aware(value): # Convert aware datetimes to the default time zone # before casting them to dates (#17742). default_timezone = timezone.get_default_timezone() value = timezone.make_naive(value, default_timezone) return value.date() if isinstance(value, datetime.date): return value try: parsed = parse_date(value) if parsed is not None: return parsed except ValueError: msg = self.error_messages['invalid_date'] % value raise exceptions.ValidationError(msg) msg = self.error_messages['invalid'] % value raise exceptions.ValidationError(msg)
Example #3
Source File: base.py From luscan-devel with GNU General Public License v2.0 | 6 votes |
def convert_values(self, value, field): """SQLite returns floats when it should be returning decimals, and gets dates and datetimes wrong. For consistency with other backends, coerce when required. """ internal_type = field.get_internal_type() if internal_type == 'DecimalField': return util.typecast_decimal(field.format_number(value)) elif internal_type and internal_type.endswith('IntegerField') or internal_type == 'AutoField': return int(value) elif internal_type == 'DateField': return parse_date(value) elif internal_type == 'DateTimeField': return parse_datetime_with_timezone_support(value) elif internal_type == 'TimeField': return parse_time(value) # No field, or the field isn't known to be a decimal or integer return value
Example #4
Source File: __init__.py From GTDWeb with GNU General Public License v2.0 | 5 votes |
def to_python(self, value): if value is None: return value if isinstance(value, datetime.datetime): if settings.USE_TZ and timezone.is_aware(value): # Convert aware datetimes to the default time zone # before casting them to dates (#17742). default_timezone = timezone.get_default_timezone() value = timezone.make_naive(value, default_timezone) return value.date() if isinstance(value, datetime.date): return value try: parsed = parse_date(value) if parsed is not None: return parsed except ValueError: raise exceptions.ValidationError( self.error_messages['invalid_date'], code='invalid_date', params={'value': value}, ) raise exceptions.ValidationError( self.error_messages['invalid'], code='invalid', params={'value': value}, )
Example #5
Source File: models.py From digihel with MIT License | 5 votes |
def events(self, future=False): try: events = self._event_methods[self.data_source](self) except (TimeoutError, ConnectionError, LookupError): # if the event source is unreachable or down or data is invalid events = [] if not future: return json.dumps(events) # the methods are assumed to return events latest first, reverse the order tz = pytz.timezone(settings.TIME_ZONE) for event in events: # for future filtering, make sure all events have end times not null try: end = event['end_time'] if not end: event['end_time'] = event['start_time'] except LookupError: event['end_time'] = event['start_time'] # check the datetimes first start = dateparse.parse_datetime(event['start_time']) end = dateparse.parse_datetime(event['end_time']) # linkedevents may not have exact times, parse_datetime may fail # we have to append time, assume server time zone and convert to utc for filtering if not start: start = tz.localize(datetime.combine(dateparse.parse_date(event['start_time']), time())) event['start_time'] = start.astimezone(pytz.utc).strftime('%Y-%m-%dT%H:%M:%SZ') if not end: end = tz.localize(datetime.combine(dateparse.parse_date(event['end_time']), time(23,59,59))) event['end_time'] = end.astimezone(pytz.utc).strftime('%Y-%m-%dT%H:%M:%SZ') # we want the next event first return json.dumps(list(reversed([event for event in events if dateparse.parse_datetime(event['end_time']) > datetime.now(tz)])))
Example #6
Source File: fields.py From esdc-ce with Apache License 2.0 | 5 votes |
def from_native(self, value): if value in validators.EMPTY_VALUES: return None if isinstance(value, datetime.datetime): if timezone and settings.USE_TZ and timezone.is_aware(value): # Convert aware datetimes to the default time zone # before casting them to dates (#17742). default_timezone = timezone.get_default_timezone() value = timezone.make_naive(value, default_timezone) return value.date() if isinstance(value, datetime.date): return value for fmt in self.input_formats: if fmt.lower() == ISO_8601: try: parsed = parse_date(value) except (ValueError, TypeError): pass else: if parsed is not None: return parsed else: try: parsed = datetime.datetime.strptime(value, fmt) except (ValueError, TypeError): pass else: return parsed.date() msg = self.error_messages['invalid'] % readable_date_formats(self.input_formats) raise ValidationError(msg)
Example #7
Source File: credential.py From TheOrgBook with Apache License 2.0 | 5 votes |
def process_config_date(cls, config, credential, field_name): date_value = cls.process_mapping( config.get(field_name), credential ) date_result = None if date_value: try: # could be seconds since epoch date_result = datetime.utcfromtimestamp( int(date_value) ) except ValueError: # Django method to parse a date string. Must be in ISO8601 format try: date_result = parse_datetime(date_value) if not date_result: date_result = parse_date(date_value) if not date_result: raise ValueError() date_result = datetime.combine(date_result, datetime.min.time()) date_result = timezone.make_aware(date_result) except re.error: raise CredentialException( "Error parsing {}: {}".format(field_name, date_value) ) except ValueError: raise CredentialException( "Credential {} is invalid: {}".format(field_name, date_value) ) if not date_result.tzinfo: # interpret as UTC date_result = date_result.replace(tzinfo=timezone.utc) else: # convert to UTC date_result = date_result.astimezone(timezone.utc) return date_result
Example #8
Source File: credential.py From aries-vcr with Apache License 2.0 | 5 votes |
def process_config_date(cls, config, credential, field_name): date_value = cls.process_mapping(config.get(field_name), credential) date_result = None if date_value: try: # could be seconds since epoch date_result = datetime.utcfromtimestamp(int(date_value)) except ValueError: # Django method to parse a date string. Must be in ISO8601 format try: date_result = parse_datetime(date_value) if not date_result: date_result = parse_date(date_value) if not date_result: raise ValueError() date_result = datetime.combine(date_result, datetime.min.time()) date_result = timezone.make_aware(date_result) except re.error: raise CredentialException( "Error parsing {}: {}".format(field_name, date_value) ) except ValueError: raise CredentialException( "Credential {} is invalid: {}".format(field_name, date_value) ) if not date_result.tzinfo: # interpret as UTC date_result = date_result.replace(tzinfo=timezone.utc) else: # convert to UTC date_result = date_result.astimezone(timezone.utc) return date_result
Example #9
Source File: operations.py From python2017 with MIT License | 5 votes |
def convert_datefield_value(self, value, expression, connection, context): if value is not None: if not isinstance(value, datetime.date): value = parse_date(value) return value
Example #10
Source File: operations.py From openhgsenti with Apache License 2.0 | 5 votes |
def convert_datefield_value(self, value, expression, connection, context): if value is not None: if not isinstance(value, datetime.date): value = parse_date(value) return value
Example #11
Source File: __init__.py From luscan-devel with GNU General Public License v2.0 | 5 votes |
def to_python(self, value): if value is None: return value if isinstance(value, datetime.datetime): return value if isinstance(value, datetime.date): value = datetime.datetime(value.year, value.month, value.day) if settings.USE_TZ: # For backwards compatibility, interpret naive datetimes in # local time. This won't work during DST change, but we can't # do much about it, so we let the exceptions percolate up the # call stack. warnings.warn("DateTimeField received a naive datetime (%s)" " while time zone support is active." % value, RuntimeWarning) default_timezone = timezone.get_default_timezone() value = timezone.make_aware(value, default_timezone) return value try: parsed = parse_datetime(value) if parsed is not None: return parsed except ValueError: msg = self.error_messages['invalid_datetime'] % value raise exceptions.ValidationError(msg) try: parsed = parse_date(value) if parsed is not None: return datetime.datetime(parsed.year, parsed.month, parsed.day) except ValueError: msg = self.error_messages['invalid_date'] % value raise exceptions.ValidationError(msg) msg = self.error_messages['invalid'] % value raise exceptions.ValidationError(msg)
Example #12
Source File: operations.py From python with Apache License 2.0 | 5 votes |
def convert_datefield_value(self, value, expression, connection, context): if value is not None: if not isinstance(value, datetime.date): value = parse_date(value) return value
Example #13
Source File: field_block.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def to_python(self, value): # Serialising to JSON uses DjangoJSONEncoder, which converts date/time objects to strings. # The reverse does not happen on decoding, because there's no way to know which strings # should be decoded; we have to convert strings back to dates here instead. if value is None or isinstance(value, datetime.date): return value else: return parse_date(value)
Example #14
Source File: test_revisions.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_scheduled_revision(self): self.last_christmas_revision.publish() self.this_christmas_revision.approved_go_live_at = local_datetime(2014, 12, 26) self.this_christmas_revision.save() this_christmas_unschedule_url = reverse( 'wagtailadmin_pages:revisions_unschedule', args=(self.christmas_event.id, self.this_christmas_revision.id) ) response = self.client.get( reverse('wagtailadmin_pages:revisions_index', args=(self.christmas_event.id, )) ) self.assertEqual(response.status_code, 200) self.assertContains(response, 'Scheduled for') self.assertContains(response, formats.localize(parse_date('2014-12-26'))) self.assertContains(response, this_christmas_unschedule_url)
Example #15
Source File: test_revisions.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_get_revisions_index(self): response = self.client.get( reverse('wagtailadmin_pages:revisions_index', args=(self.christmas_event.id, )) ) self.assertEqual(response.status_code, 200) self.assertContains(response, formats.localize(parse_date('2013-12-25'))) last_christmas_preview_url = reverse( 'wagtailadmin_pages:revisions_view', args=(self.christmas_event.id, self.last_christmas_revision.id) ) last_christmas_revert_url = reverse( 'wagtailadmin_pages:revisions_revert', args=(self.christmas_event.id, self.last_christmas_revision.id) ) self.assertContains(response, last_christmas_preview_url) self.assertContains(response, last_christmas_revert_url) self.assertContains(response, formats.localize(local_datetime(2014, 12, 25))) this_christmas_preview_url = reverse( 'wagtailadmin_pages:revisions_view', args=(self.christmas_event.id, self.this_christmas_revision.id) ) this_christmas_revert_url = reverse( 'wagtailadmin_pages:revisions_revert', args=(self.christmas_event.id, self.this_christmas_revision.id) ) self.assertContains(response, this_christmas_preview_url) self.assertContains(response, this_christmas_revert_url)
Example #16
Source File: operations.py From Hands-On-Application-Development-with-PyCharm with MIT License | 5 votes |
def convert_datefield_value(self, value, expression, connection): if value is not None: if not isinstance(value, datetime.date): value = parse_date(value) return value
Example #17
Source File: cache.py From drf-cached-instances with Mozilla Public License 2.0 | 5 votes |
def field_date_to_json(self, day): """Convert a date to a date triple.""" if isinstance(day, six.string_types): day = parse_date(day) return [day.year, day.month, day.day] if day else None
Example #18
Source File: processors.py From nefarious with GNU General Public License v3.0 | 5 votes |
def _is_match(self, parser): release_year = dateparse.parse_date(self.tmdb_media['release_date']).strftime('%Y') return parser.is_match( title=self.tmdb_media[self._get_tmdb_title_key()], year=release_year, )
Example #19
Source File: 0055_auto_20200425_2110.py From nefarious with GNU General Public License v3.0 | 5 votes |
def populate_release_date(apps, schema_editor): WatchMovie = apps.get_model('nefarious', 'WatchMovie') WatchTVEpisode = apps.get_model('nefarious', 'WatchTVEpisode') WatchTVSeason = apps.get_model('nefarious', 'WatchTVSeason') NefariousSettings = apps.get_model('nefarious', 'NefariousSettings') nefarious_settings = NefariousSettings.objects.all().first() if not nefarious_settings: # skip if no settings exist return tmdb_client = get_tmdb_client(nefarious_settings) logging.info('Adding release dates') for media in WatchMovie.objects.all(): try: movie_result = tmdb_client.Movies(media.tmdb_movie_id) data = movie_result.info() release_date = parse_date(data.get('release_date', '')) update_release_date(media, release_date) except Exception as e: logging.exception(e) for media in WatchTVSeason.objects.all(): try: season_result = tmdb_client.TV_Seasons(media.watch_tv_show.tmdb_show_id, media.season_number) data = season_result.info() release_date = parse_date(data.get('air_date', '')) update_release_date(media, release_date) except Exception as e: logging.exception(e) for media in WatchTVEpisode.objects.all(): try: episode_result = tmdb_client.TV_Episodes(media.watch_tv_show.tmdb_show_id, media.season_number, media.episode_number) data = episode_result.info() release_date = parse_date(data.get('air_date', '')) update_release_date(media, release_date) except Exception as e: logging.exception(e)
Example #20
Source File: operations.py From bioforum with MIT License | 5 votes |
def convert_datefield_value(self, value, expression, connection): if value is not None: if not isinstance(value, datetime.date): value = parse_date(value) return value
Example #21
Source File: validations.py From drf-url-filters with MIT License | 5 votes |
def DatetimeWithTZ(msg=None): ''' Checks whether a value is : - a valid castable datetime object with timezone. ''' def fn(value): try: date = parse_datetime(value) or parse_date(value) if date is not None: return date else: raise ValueError except ValueError: raise Invalid('<{0}> is not a valid datetime.'.format(value)) return fn
Example #22
Source File: operations.py From GTDWeb with GNU General Public License v2.0 | 5 votes |
def convert_datefield_value(self, value, expression, connection, context): if value is not None and not isinstance(value, datetime.date): value = parse_date(value) return value
Example #23
Source File: query_parsing.py From urbanfootprint with GNU General Public License v3.0 | 4 votes |
def parse_simple_token(token, manager, related_models, left=False, sibling_token=None): """ Parse the simple token dict. :param token: a dict with 'tokenType'=='PROPERTY'|'NUMBER'|'STRING' and 'tokenValue'==a value of the corresponding type. :param manager: The django model manager :param related_models: Related models joined to the manager model :param left: Default False. If set then the token is an assignee and shouldn't be wrapped in an F() expression if it's a property :param sibling_token: the left side token if we are evaluating a right side token (left=False) """ if token['tokenType'] == 'PROPERTY': # Resolve the field path in case it's relative to a joined related_model field_path = resolve_field_path_via_geographies('__'.join(token['tokenValue'].split('.')), manager, related_models) # Wrap in an F() expression if field is a right-side argument (the thing being assigned) return field_path if left else F(field_path) elif token['tokenType'] == 'NUMBER': return float(token['tokenValue']) elif token['tokenType'] in ['STRING', 'null', 'undefined']: if token['tokenType'] in ['null', 'undefined']: # Accept the 'null' or 'undefined' tokenType to mean None value = None else: value = token['tokenValue'] if sibling_token and sibling_token.get('tokenType', None) == 'PROPERTY': # If we are evaluating a right-side token, inspect the sibling_token (the left-side token) # to find out what type the property is. This only matters for Dates and Times # where we need to modify the resolved right-side value to be have a time zone # if one isn't specified # Resolve the possibly chained property field_path = resolve_field_path_via_geographies('__'.join(sibling_token['tokenValue'].split('.')), manager, related_models) field = resolve_field_of_path(manager, field_path) if not field: return parser_lookup = {DateTimeField: parse_datetime, DateField: parse_date, TimeField: parse_time} if isinstance(field, (DateTimeField, DateField, TimeField)): date_time = parser_lookup[field.__class__](value) if not date_time and isinstance(field, DateTimeField): # Accept dates without times for datetimes date_time = timezone.utc.localize(datetime.combine(parser_lookup[DateField](value), datetime.min.time())) if isinstance(field, (DateTimeField, TimeField)) and not date_time.tzinfo: # Default the timezone to UTC return date_time.replace(tzinfo=timezone.utc) return date_time return value # TODO handle booleans and other types return token['tokenType']
Example #24
Source File: tasks.py From nefarious with GNU General Public License v3.0 | 4 votes |
def auto_watch_new_seasons_task(): """ look for newly aired seasons that the user wants to automatically watch """ nefarious_settings = NefariousSettings.get() tmdb_client = get_tmdb_client(nefarious_settings) # cycle through every show that has auto-watch enabled for watch_show in WatchTVShow.objects.filter(auto_watch=True): tmdb_show = tmdb_client.TV(watch_show.tmdb_show_id) show_data = tmdb_show.info() added_season = False # find any season with a newer air date than the "auto watch" and queue it up for season in show_data['seasons']: air_date = parse_date(season['air_date'] or '') # air date is newer than our auto watch date if air_date and watch_show.auto_watch_date_updated and air_date >= watch_show.auto_watch_date_updated: # season & request params create_params = dict( watch_tv_show=watch_show, season_number=season['season_number'], defaults=dict( user=watch_show.user, release_date=air_date, ) ) # create a season request instance to keep up with slowly-released episodes WatchTVSeasonRequest.objects.get_or_create(**create_params) # also save a watch tv season instance to try and download the whole season immediately watch_tv_season, was_season_created = WatchTVSeason.objects.get_or_create(**create_params) # season was created if was_season_created: added_season = True logging.info('Automatically watching newly aired season {}'.format(watch_tv_season)) # send a websocket message for this new season media_type, data = websocket.get_media_type_and_serialized_watch_media(watch_tv_season) send_websocket_message_task.delay(websocket.ACTION_UPDATED, media_type, data) # create a task to download the whole season (fallback to individual episodes if it fails) watch_tv_show_season_task.delay(watch_tv_season.id) # new season added to show if added_season: # update auto watch date requested watch_show.auto_watch_date_updated = datetime.utcnow().date() watch_show.save()
Example #25
Source File: tasks.py From nefarious with GNU General Public License v3.0 | 4 votes |
def wanted_tv_season_task(): tasks = [] nefarious_settings = NefariousSettings.get() tmdb = get_tmdb_client(nefarious_settings) # # re-check for requested tv seasons that have had new episodes released from TMDB (which was stale previously) # for tv_season_request in WatchTVSeasonRequest.objects.filter(collected=False): season_request = tmdb.TV_Seasons(tv_season_request.watch_tv_show.tmdb_show_id, tv_season_request.season_number) season = season_request.info() now = datetime.utcnow() last_air_date = parse_date(season.get('air_date') or '') # season air date # otherwise add any new episodes to our watch list for episode in season['episodes']: episode_air_date = parse_date(episode.get('air_date') or '') # if episode air date exists, use as last air date if episode_air_date: last_air_date = episode_air_date if not last_air_date or episode_air_date > last_air_date else last_air_date watch_tv_episode, was_created = WatchTVEpisode.objects.get_or_create( watch_tv_show=tv_season_request.watch_tv_show, season_number=tv_season_request.season_number, episode_number=episode['episode_number'], defaults=dict( # add non-unique constraint fields for the default values tmdb_episode_id=episode['id'], user=tv_season_request.user, release_date=episode_air_date, )) if was_created: logging.info('adding newly found episode {} for {}'.format(episode['episode_number'], tv_season_request)) # add episode to task queue tasks.append(watch_tv_episode_task.si(watch_tv_episode.id)) # assume there's no new episodes for anything that's aired this long ago days_since_aired = (now.date() - last_air_date).days if last_air_date else 0 if days_since_aired > 30: logging.warning('completing old tv season request {}'.format(tv_season_request)) tv_season_request.collected = True tv_season_request.save() # execute tasks sequentially chain(*tasks)()
Example #26
Source File: tasks.py From nefarious with GNU General Public License v3.0 | 4 votes |
def watch_tv_show_season_task(watch_tv_season_id: int): processor = WatchTVSeasonProcessor(watch_media_id=watch_tv_season_id) success = processor.fetch() watch_tv_season = get_object_or_404(WatchTVSeason, pk=watch_tv_season_id) # success so update the season request instance as "collected" if success: season_request = WatchTVSeasonRequest.objects.filter( watch_tv_show=watch_tv_season.watch_tv_show, season_number=watch_tv_season.season_number) if season_request.exists(): season_request = season_request.first() season_request.collected = True season_request.save() # failed so delete season instance and fallback to trying individual episodes else: logging.info('Failed fetching entire season {} - falling back to individual episodes'.format(watch_tv_season)) nefarious_settings = NefariousSettings.get() tmdb = get_tmdb_client(nefarious_settings) season_request = tmdb.TV_Seasons(watch_tv_season.watch_tv_show.tmdb_show_id, watch_tv_season.season_number) season = season_request.info() # save individual episode watches watch_tv_episodes_tasks = [] for episode in season['episodes']: watch_tv_episode, was_created = WatchTVEpisode.objects.get_or_create( tmdb_episode_id=episode['id'], # add non-unique constraint fields for the default values defaults=dict( user=watch_tv_season.user, watch_tv_show=watch_tv_season.watch_tv_show, season_number=watch_tv_season.season_number, episode_number=episode['episode_number'], release_date=parse_date(episode.get('air_date') or ''), ) ) # build list of tasks to execute watch_tv_episodes_tasks.append(watch_tv_episode_task.si(watch_tv_episode.id)) # remove the "watch season" now that we've requested to fetch all individual episodes watch_tv_season.delete() # execute tasks sequentially chain(*watch_tv_episodes_tasks)()
Example #27
Source File: __init__.py From GTDWeb with GNU General Public License v2.0 | 4 votes |
def to_python(self, value): if value is None: return value if isinstance(value, datetime.datetime): return value if isinstance(value, datetime.date): value = datetime.datetime(value.year, value.month, value.day) if settings.USE_TZ: # For backwards compatibility, interpret naive datetimes in # local time. This won't work during DST change, but we can't # do much about it, so we let the exceptions percolate up the # call stack. warnings.warn("DateTimeField %s.%s received a naive datetime " "(%s) while time zone support is active." % (self.model.__name__, self.name, value), RuntimeWarning) default_timezone = timezone.get_default_timezone() value = timezone.make_aware(value, default_timezone) return value try: parsed = parse_datetime(value) if parsed is not None: return parsed except ValueError: raise exceptions.ValidationError( self.error_messages['invalid_datetime'], code='invalid_datetime', params={'value': value}, ) try: parsed = parse_date(value) if parsed is not None: return datetime.datetime(parsed.year, parsed.month, parsed.day) except ValueError: raise exceptions.ValidationError( self.error_messages['invalid_date'], code='invalid_date', params={'value': value}, ) raise exceptions.ValidationError( self.error_messages['invalid'], code='invalid', params={'value': value}, )
Example #28
Source File: utils.py From MetaCI with BSD 3-Clause "New" or "Revised" License | 4 votes |
def update_release_from_github(release, repo_api=None): if not repo_api: repo_api = release.repo.github_api if not release.git_tag: logger.info("Cannot update release, no git_tag specified") return ref = repo_api.ref("tags/{}".format(release.git_tag)) if not ref: logger.info( "Cannot update release, ref tags/{} not found in Github".format( release.git_tag ) ) return gh_release = repo_api.release_by_tag_name(release.git_tag) release.status = "published" release.version_name = gh_release.name release.version_number = gh_release.name release.github_release = gh_release.html_url release.release_creation_date = gh_release.created_at release.created_from_commit = ref.object.sha sandbox_date = re.findall( r"^Sandbox orgs: (20[\d][\d]-[\d][\d]-[\d][\d])", gh_release.body ) if sandbox_date: release.sandbox_push_date = parse_date(sandbox_date[0], "%Y-%m-%d") prod_date = re.findall( r"^Production orgs: (20[\d][\d]-[\d][\d]-[\d][\d])", gh_release.body ) if prod_date: release.production_push_date = parse_date(prod_date[0], "%Y-%m-%d") package_version_id = re.findall(r"(04t[\w]{15,18})", gh_release.body) if package_version_id: release.package_version_id = package_version_id[0] trialforce_id = re.findall(r"^(0TT[\w]{15,18})", gh_release.body) if trialforce_id: release.trialforce_id = trialforce_id[0] return release