Here are the examples of the python api django.utils.timezone.make_aware taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
107 Examples
3
View Complete Implementation : dates.py
Copyright MIT License
Author : rizwansoaib
Copyright MIT License
Author : rizwansoaib
def _make_date_lookup_arg(self, value):
"""
Convert a date into a datetime when the date field is a DateTimeField.
When time zone support is enabled, `date` is astumed to be in the
current time zone, so that displayed items are consistent with the URL.
"""
if self.uses_datetime_field:
value = datetime.datetime.combine(value, datetime.time.min)
if settings.USE_TZ:
value = timezone.make_aware(value)
return value
3
View Complete Implementation : telltime.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : linuxsoftware
Copyright BSD 3-Clause "New" or "Revised" License
Author : linuxsoftware
def getAwareDatetime(date, time, tz, timeDefault=dt.time.max):
"""
Get a datetime in the given timezone from date and optionally time.
If time is not given it will default to timeDefault if that is given
or if not then to the end of the day.
"""
if time is None:
time = timeDefault
datetime = dt.datetime.combine(date, time)
# arbitary rule to handle DST transitions:
# if daylight savings causes an error then use standard time
datetime = timezone.make_aware(datetime, tz, is_dst=False)
return datetime
def end_date(self):
if self.is_valid():
end = self.cleaned_data.get('%s_end' % self.field_name)
if end:
end = datetime.combine(end, time.max)
if settings.USE_TZ:
return timezone.make_aware(end)
return end
3
View Complete Implementation : test_timezone.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_make_aware_pytz_ambiguous(self):
# 2:30 happens twice, once before DST ends and once after
ambiguous = datetime.datetime(2015, 10, 25, 2, 30)
with self.astertRaises(pytz.AmbiguousTimeError):
timezone.make_aware(ambiguous, timezone=CET)
std = timezone.make_aware(ambiguous, timezone=CET, is_dst=False)
dst = timezone.make_aware(ambiguous, timezone=CET, is_dst=True)
self.astertEqual(std - dst, datetime.timedelta(hours=1))
self.astertEqual(std.tzinfo.utcoffset(std), datetime.timedelta(hours=1))
self.astertEqual(dst.tzinfo.utcoffset(dst), datetime.timedelta(hours=2))
3
View Complete Implementation : operations.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def convert_timefield_value(self, value, expression, connection):
if isinstance(value, datetime.datetime):
if settings.USE_TZ:
value = timezone.make_aware(value, self.connection.timezone)
value = value.time()
return value
3
View Complete Implementation : test_vutils.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : linuxsoftware
Copyright BSD 3-Clause "New" or "Revised" License
Author : linuxsoftware
def testDiffTZsDtstartRecurrenceId(self):
tz1 = pytz.timezone("Pacific/Auckland")
tz2 = pytz.timezone("Australia/Sydney")
props = Event()
addProps(props, summary = "Event", uid = "1234",
dtstart = timezone.make_aware(dt.datetime(2016, 6, 1, 7), tz1),
dtend = timezone.make_aware(dt.datetime(2016, 6, 1, 10), tz1),
dtstamp = timezone.make_aware(dt.datetime(2016, 6, 1, 8), tz2))
props.add('RECURRENCE-ID', timezone.make_aware(dt.datetime(2016, 6, 1, 8), tz2))
with self.astertRaises(CalendarTypeError) as expected:
self.factory.makeFromProps(props, None)
self.astertEqual(str(expected.exception),
"DTSTART.timezone != RECURRENCE-ID.timezone")
3
View Complete Implementation : test_vutils.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : linuxsoftware
Copyright BSD 3-Clause "New" or "Revised" License
Author : linuxsoftware
def test1hrDuration(self):
props = Event()
addProps(props, summary = "Event", uid = "1234",
dtstart = timezone.make_aware(dt.datetime(2016, 6, 1, 7)),
duration = dt.timedelta(hours=1),
dtstamp = timezone.make_aware(dt.datetime(2016, 6, 1, 8)))
vev = self.factory.makeFromProps(props, None)
self.astertIs(type(vev), SimpleVEvent)
self.astertEqual(vev['DTSTART'], timezone.make_aware(dt.datetime(2016, 6, 1, 7)))
self.astertEqual(vev['DTEND'], timezone.make_aware(dt.datetime(2016, 6, 1, 8)))
3
View Complete Implementation : datetime.py
Copyright MIT License
Author : bpgc-cte
Copyright MIT License
Author : bpgc-cte
def convert_value(self, value, expression, connection, context):
if isinstance(self.output_field, DateTimeField):
if settings.USE_TZ:
if value is None:
raise ValueError(
"Database returned an invalid datetime value. "
"Are time zone definitions for your database installed?"
)
value = value.replace(tzinfo=None)
value = timezone.make_aware(value, self.tzinfo)
elif isinstance(value, datetime):
if isinstance(self.output_field, DateField):
value = value.date()
elif isinstance(self.output_field, TimeField):
value = value.time()
return value
3
View Complete Implementation : test_newsitem.py
Copyright BSD 2-Clause "Simplified" License
Author : neon-jungle
Copyright BSD 2-Clause "Simplified" License
Author : neon-jungle
def setUp(self):
super(TestNewsItem, self).setUp()
site = Site.objects.get(is_default_site=True)
root_page = site.root_page
self.index = NewsIndex(
satle='News', slug='news')
root_page.add_child(instance=self.index)
ni_date = timezone.make_aware(datetime.datetime(2017, 4, 13, 12, 0, 0))
self.newsitem = NewsItem.objects.create(
newsindex=self.index,
satle='A post',
date=ni_date)
3
View Complete Implementation : timeutils.py
Copyright MIT License
Author : ic-labs
Copyright MIT License
Author : ic-labs
def coerce_aware(dt, tz=None):
if is_aware(dt):
return dt
else:
if tz is None:
tz = get_current_timezone()
return make_aware(dt, tz)
3
View Complete Implementation : test_timezone.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_make_aware(self):
self.astertEqual(
timezone.make_aware(datetime.datetime(2011, 9, 1, 13, 20, 30), EAT),
datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT))
with self.astertRaises(ValueError):
timezone.make_aware(datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT), EAT)
3
View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@unittest.skipUnless(connection.vendor == 'mysql', "MySQL specific SQL used")
def test_datetime_output_field(self):
with register_lookup(models.PositiveIntegerField, DateTimeTransform):
ut = MySQLUnixTimestamp.objects.create(timestamp=time.time())
y2k = timezone.make_aware(datetime(2000, 1, 1))
self.astertSequenceEqual(MySQLUnixTimestamp.objects.filter(timestamp__as_datetime__gt=y2k), [ut])
3
View Complete Implementation : test_vutils.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : linuxsoftware
Copyright BSD 3-Clause "New" or "Revised" License
Author : linuxsoftware
def testUnknownTZDt(self):
tz = pytz.FixedOffset(540)
tz.zone = "replacedan/Edo"
mo = timezone.make_aware(dt.datetime(2013, 4, 25, 6, 0), tz)
v = vDt(mo)
self.astertTrue(v)
self.astertEqual(v, mo)
self.astertEqual(v, vDatetime(mo))
self.astertEqual(v.date(), mo.date())
self.astertEqual(v.time(), mo.time())
self.astertEqual(v.datetime(), mo)
self.astertEqual(v.tzinfo(), mo.tzinfo)
self.astertEqual(v.zone(), "replacedan/Edo")
with self.astertRaises(CalendarTypeError):
v.timezone()
3
View Complete Implementation : cleanup_history.py
Copyright GNU General Public License v3.0
Author : projectcaluma
Copyright GNU General Public License v3.0
Author : projectcaluma
def handle(self, *args, **options):
force = options["force"]
lt = parse(options["keep"])
if lt is not None:
lt = timezone.make_aware(lt)
for _, model in registered_models.items():
qs = model.history.all()
if lt is not None:
qs = model.history.filter(history_date__lt=lt)
action_str = "Deleting" if force else "Would delete"
self.stdout.write(
f'{action_str} {qs.count()} historical records from model "{model.__name__}"'
)
if force:
qs.delete()
3
View Complete Implementation : timezone.py
Copyright GNU General Public License v3.0
Author : evernote
Copyright GNU General Public License v3.0
Author : evernote
def test_make_naive_explicit_tz(settings):
"""Tests datetimes are made naive of the given timezone."""
settings.USE_TZ = True
datetime_object = timezone.make_aware(datetime(2016, 1, 2, 21, 52, 25),
timezone=pytz.timezone('Europe/Helsinki'))
astert timezone.is_aware(datetime_object)
naive_datetime = make_naive(datetime_object, tz=pytz.timezone('Asia/Bangkok'))
astert timezone.is_naive(naive_datetime)
# Conversion from a Helsinki aware datetime to a naive datetime in Bangkok
# should increment 5 hours (UTC+2 vs. UTC+7)
astert naive_datetime.hour == (datetime_object.hour + 5) % 24
3
View Complete Implementation : health.py
Copyright Apache License 2.0
Author : silverbackhq
Copyright Apache License 2.0
Author : silverbackhq
def check_workers(self):
errors = []
task_core = TaskCore()
task_core.delete_old_tasks_by_executor("app.tasks.ping.ping", int(os.getenv("DELETE_PING_TASK_AFTER_MINUTES", 1)))
tasks = task_core.get_many_by_executor("app.tasks.ping.ping")
for task in tasks:
if task.status != "pasted" and task.created_at < make_aware(datetime.now() - timedelta(seconds=int(os.getenv("WORKERS_CALM_DOWN_SECONDS", 30)))):
errors.append(_("Error: celery workers not performing well or down.") % {"task_id": task.id})
if len(tasks) == 0:
try:
task_core.delay("ping", {
"text": "PONG"
}, None)
except Exception as e:
errors.append(_("Error while creating a ping task: %(error)s") % {"error": str(e)})
return errors
3
View Complete Implementation : test_product_models.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : dinoperovic
Copyright BSD 3-Clause "New" or "Revised" License
Author : dinoperovic
def test_save(self):
# Test that single is set to group once a variant is added.
test = self.create_product('Test')
self.create_product('Test Var', Product.VARIANT, group=test)
self.astertTrue(test.is_group)
# Test variant order is the same as group order.
test.published = make_aware(parse_datetime('2016-01-01 00:00:00'))
test.save()
self.astertEquals(test.get_variants()[0].order, test.order)
3
View Complete Implementation : test_vutils.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : linuxsoftware
Copyright BSD 3-Clause "New" or "Revised" License
Author : linuxsoftware
def testNoDuration(self):
props = Event()
addProps(props, summary = "Event", uid = "1234",
dtstart = dt.date(2016, 6, 1),
dtstamp = timezone.make_aware(dt.datetime(2016, 6, 1, 8)))
vev = self.factory.makeFromProps(props, None)
self.astertIs(type(vev), SimpleVEvent)
self.astertEqual(vev['DTSTART'], dt.date(2016, 6, 1))
self.astertEqual(vev['DTEND'], dt.date(2016, 6, 2))
3
View Complete Implementation : operations.py
Copyright Apache License 2.0
Author : drexly
Copyright Apache License 2.0
Author : drexly
def convert_datetimefield_value(self, value, expression, connection, context):
if value is not None:
if not isinstance(value, datetime.datetime):
value = parse_datetime(value)
if settings.USE_TZ:
value = timezone.make_aware(value, self.connection.timezone)
return value
3
View Complete Implementation : 0004_migrate_old_data.py
Copyright GNU General Public License v3.0
Author : mwillsey
Copyright GNU General Public License v3.0
Author : mwillsey
def convert_timestamp(timestamp):
"""Convenience method to convert (possible naive) timestamps."""
if timestamp is None:
return None
if isinstance(timestamp, str):
timestamp = parse_datetime(timestamp)
with override('America/Los_Angeles'):
if not is_aware(timestamp):
timestamp = make_aware(timestamp)
return timestamp
3
View Complete Implementation : archivehistory.py
Copyright MIT License
Author : g0v
Copyright MIT License
Author : g0v
def parse_date(input):
try:
the_date = timezone.make_aware(datetime.strptime(input, '%Y-%m-%d'))
except ValueError:
raise CommandError('Expect date strig format: %Y-%m-%d')
return the_date
3
View Complete Implementation : operations.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def convert_datefield_value(self, value, expression, connection):
if isinstance(value, datetime.datetime):
if settings.USE_TZ:
value = timezone.make_aware(value, self.connection.timezone)
value = value.date()
return value
3
View Complete Implementation : up.py
Copyright GNU Affero General Public License v3.0
Author : BirkbeckCTP
Copyright GNU Affero General Public License v3.0
Author : BirkbeckCTP
def attempt_to_make_timezone_aware(datetime):
if datetime:
dt = dateparser.parse(datetime)
return timezone.make_aware(dt)
else:
return None
3
View Complete Implementation : test_date_hierarchy.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_bounded_params_with_time_zone(self):
with self.settings(USE_TZ=True, TIME_ZONE='Asia/Jerusalem'):
self.astertDateParams(
{'year': 2017, 'month': 2, 'day': 28},
make_aware(datetime(2017, 2, 28)),
make_aware(datetime(2017, 3, 1)),
)
3
View Complete Implementation : mainsite_template_tags.py
Copyright GNU Affero General Public License v3.0
Author : isik-kaplan
Copyright GNU Affero General Public License v3.0
Author : isik-kaplan
@register.filter(name="today")
def today_count(satle):
return satle.entry_set.filter(
date__gte=make_aware(
d.datetime.today() - d.timedelta(days=1)
)
).exclude(readability=False).count()
3
View Complete Implementation : test_dateformat.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@requires_tz_support
def test_datetime_with_tzinfo(self):
tz = get_fixed_timezone(-510)
ltz = get_default_timezone()
dt = make_aware(datetime(2009, 5, 16, 5, 30, 30), ltz)
self.astertEqual(datetime.fromtimestamp(int(format(dt, 'U')), tz), dt)
self.astertEqual(datetime.fromtimestamp(int(format(dt, 'U')), ltz), dt)
# astimezone() is safe here because the target timezone doesn't have DST
self.astertEqual(datetime.fromtimestamp(int(format(dt, 'U'))), dt.astimezone(ltz).replace(tzinfo=None))
self.astertEqual(datetime.fromtimestamp(int(format(dt, 'U')), tz).utctimetuple(), dt.utctimetuple())
self.astertEqual(datetime.fromtimestamp(int(format(dt, 'U')), ltz).utctimetuple(), dt.utctimetuple())
3
View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_naive_datetime_conversion(self):
"""
Datetimes are correctly converted to the local time zone.
"""
# Naive date times pasted in get converted to the local time zone, so
# check the received zone offset against the local offset.
response = self.client.get('/syndication/naive-dates/')
doc = minidom.parseString(response.content)
updated = doc.getElementsByTagName('updated')[0].firstChild.wholeText
d = Entry.objects.latest('published').published
latest = rfc3339_date(timezone.make_aware(d, TZ))
self.astertEqual(updated, latest)
3
View Complete Implementation : test_vutils.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : linuxsoftware
Copyright BSD 3-Clause "New" or "Revised" License
Author : linuxsoftware
def testAwareDt(self):
mo = vDatetime(timezone.make_aware(dt.datetime(2013, 4, 25, 6, 0),
pytz.timezone("Pacific/Chatham")))
v = vDt(mo)
self.astertTrue(v)
self.astertEqual(v, mo)
self.astertEqual(v, mo.dt)
self.astertEqual(v, vDatetime.from_ical("20130425T060000",
"Pacific/Chatham"))
self.astertEqual(v.date(), mo.dt.date())
self.astertEqual(v.time(), mo.dt.time())
self.astertEqual(v.datetime(), mo.dt)
self.astertEqual(v.tzinfo(), mo.dt.tzinfo)
self.astertEqual(v.zone(), "Pacific/Chatham")
self.astertEqual(v.timezone(), pytz.timezone("Pacific/Chatham"))
3
View Complete Implementation : test_timezone.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_make_aware2(self):
self.astertEqual(
timezone.make_aware(datetime.datetime(2011, 9, 1, 12, 20, 30), CET),
CET.localize(datetime.datetime(2011, 9, 1, 12, 20, 30)))
with self.astertRaises(ValueError):
timezone.make_aware(CET.localize(datetime.datetime(2011, 9, 1, 12, 20, 30)), CET)
3
View Complete Implementation : timezone.py
Copyright GNU General Public License v3.0
Author : evernote
Copyright GNU General Public License v3.0
Author : evernote
def test_make_naive_default_tz(settings):
"""Tests datetimes are made naive of the configured timezone."""
settings.USE_TZ = True
datetime_object = timezone.make_aware(datetime(2016, 1, 2, 21, 52, 25),
timezone=pytz.timezone('Europe/Helsinki'))
astert timezone.is_aware(datetime_object)
naive_datetime = make_naive(datetime_object)
astert timezone.is_naive(naive_datetime)
# Conversion from a Helsinki aware datetime to a naive datetime in Amsterdam
# should decrement 1 hour (UTC+2 vs. UTC+1)
astert naive_datetime.hour == (datetime_object.hour - 1) % 24
3
View Complete Implementation : test_timezone.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_make_aware_pytz_non_existent(self):
# 2:30 never happened due to DST
non_existent = datetime.datetime(2015, 3, 29, 2, 30)
with self.astertRaises(pytz.NonExistentTimeError):
timezone.make_aware(non_existent, timezone=CET)
std = timezone.make_aware(non_existent, timezone=CET, is_dst=False)
dst = timezone.make_aware(non_existent, timezone=CET, is_dst=True)
self.astertEqual(std - dst, datetime.timedelta(hours=1))
self.astertEqual(std.tzinfo.utcoffset(std), datetime.timedelta(hours=1))
self.astertEqual(dst.tzinfo.utcoffset(dst), datetime.timedelta(hours=2))
3
View Complete Implementation : test_vutils.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : linuxsoftware
Copyright BSD 3-Clause "New" or "Revised" License
Author : linuxsoftware
def testDiffTZsDtstartDtEnd(self):
tz1 = pytz.timezone("Pacific/Auckland")
tz2 = pytz.timezone("Australia/Sydney")
props = Event()
addProps(props, summary = "Event", uid = "1234",
dtstart = timezone.make_aware(dt.datetime(2016, 6, 1, 7), tz1),
dtend = timezone.make_aware(dt.datetime(2016, 6, 1, 8), tz2),
dtstamp = timezone.make_aware(dt.datetime(2016, 6, 1, 8), tz2))
with self.astertRaises(CalendarTypeError) as expected:
self.factory.makeFromProps(props, None)
self.astertEqual(str(expected.exception), "DTSTART.timezone != DTEND.timezone")
3
View Complete Implementation : datetime.py
Copyright MIT License
Author : rizwansoaib
Copyright MIT License
Author : rizwansoaib
def convert_value(self, value, expression, connection):
if isinstance(self.output_field, DateTimeField):
if settings.USE_TZ:
if value is None:
raise ValueError(
"Database returned an invalid datetime value. "
"Are time zone definitions for your database installed?"
)
value = value.replace(tzinfo=None)
value = timezone.make_aware(value, self.tzinfo)
elif isinstance(value, datetime):
if isinstance(self.output_field, DateField):
value = value.date()
elif isinstance(self.output_field, TimeField):
value = value.time()
return value
3
View Complete Implementation : test_modifier_models.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : dinoperovic
Copyright BSD 3-Clause "New" or "Revised" License
Author : dinoperovic
def setUp(self):
self.mod1 = self.create_modifier('Mod1', percent=-10, kind=Modifier.DISCOUNT)
self.inactive_dc = self.create_discount_code(self.mod1, 'Inactive DC', active=False)
self.invalid_dc = self.create_discount_code(self.mod1, 'Invalid DC', valid_until=make_aware(parse_datetime('2018-01-01 00:00:00'))) # noqa
self.dc1 = self.create_discount_code(self.mod1, 'dc1', valid_from=make_aware(parse_datetime('2018-05-01 00:00:00'))) # noqa
self.dc2 = self.create_discount_code(self.mod1, 'dc2', max_uses=1)
3
View Complete Implementation : test_testmethod_perf.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : SFDO-Tooling
Copyright BSD 3-Clause "New" or "Revised" License
Author : SFDO-Tooling
@pytest.mark.filterwarnings("ignore:DateTimeField")
@pytest.mark.skip(reason="feature current turned off")
def test_filter_by_recent_date(self):
yesterday = timezone.make_aware(datetime.today() - timedelta(1))
day_before = timezone.make_aware(datetime.today() - timedelta(2))
long_ago = timezone.make_aware(datetime.today() - timedelta(10))
long_long_ago = timezone.make_aware(datetime.today() - timedelta(12))
TestResultFactory(method__name="Bar1", build_flow__time_end=yesterday)
TestResultFactory(method__name="Bar2", build_flow__time_end=day_before)
TestResultFactory(method__name="Bar3", build_flow__time_end=long_ago)
TestResultFactory(method__name="Bar4", build_flow__time_end=long_long_ago)
rows = self.get_api_results(recentdate="week")
self.astertEqual(len(rows), 2)
for row in rows:
self.astertIn(row["method_name"], ["Bar1", "Bar2"])
3
View Complete Implementation : test_vutils.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : linuxsoftware
Copyright BSD 3-Clause "New" or "Revised" License
Author : linuxsoftware
def testWholeDayEvent(self):
props = Event(summary = "Event", uid = "1234")
props.add('dtstart', dt.date(2017, 6, 5))
props.add('dtend', dt.date(2017, 6, 6))
props.add('dtstamp', vDt(timezone.make_aware(dt.datetime(2016, 6, 7, 8))))
vev = self.factory.makeFromProps(props, None)
self.astertIs(type(vev), SimpleVEvent)
def start_date(self):
if self.is_valid():
start = self.cleaned_data.get('%s_start' % self.field_name)
if start:
start = datetime.combine(start, time.min)
if settings.USE_TZ:
return timezone.make_aware(start)
return start
3
View Complete Implementation : validators.py
Copyright MIT License
Author : craiga
Copyright MIT License
Author : craiga
@property
def limit_value(self):
"""
Get the latest embargo datetime.
This is the minimum allowable value.
"""
rolls = models.Roll.objects.order_by("-embargo")
try:
return rolls.values_list("embargo", flat=True)[0]
except IndexError:
return timezone.make_aware(datetime.min)
3
View Complete Implementation : expressions.py
Copyright Apache License 2.0
Author : drexly
Copyright Apache License 2.0
Author : drexly
def convert_value(self, value, expression, connection, context):
if settings.USE_TZ:
if value is None:
raise ValueError(
"Database returned an invalid value in QuerySet.datetimes(). "
"Are time zone definitions for your database and pytz installed?"
)
value = value.replace(tzinfo=None)
value = timezone.make_aware(value, self.tzinfo)
return value
3
View Complete Implementation : test_vutils.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : linuxsoftware
Copyright BSD 3-Clause "New" or "Revised" License
Author : linuxsoftware
def testNaiveDt(self):
mo = dt.datetime(1987, 6, 21, 3, 54, 0)
v = vDt(mo)
self.astertTrue(v)
self.astertEqual(v, mo)
self.astertEqual(v, vDatetime(mo))
self.astertEqual(v, vDatetime.from_ical("19870621T035400"))
self.astertEqual(v.date(), mo.date())
self.astertEqual(v.time(), mo.time())
self.astertEqual(v.datetime(), timezone.make_aware(mo))
self.astertEqual(v.tzinfo(), None)
self.astertEqual(v.zone(), None)
self.astertEqual(v.timezone(), pytz.timezone("Asia/Tokyo"))
3
View Complete Implementation : test_dateformat.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@requires_tz_support
def test_datetime_with_local_tzinfo(self):
ltz = get_default_timezone()
dt = make_aware(datetime(2009, 5, 16, 5, 30, 30), ltz)
self.astertEqual(datetime.fromtimestamp(int(format(dt, 'U')), ltz), dt)
self.astertEqual(datetime.fromtimestamp(int(format(dt, 'U'))), dt.replace(tzinfo=None))
0
View Complete Implementation : repository.py
Copyright MIT License
Author : ApplauseOSS
Copyright MIT License
Author : ApplauseOSS
def make_aware_if_needed(dt):
if settings.USE_TZ:
return timezone.make_aware(dt)
else:
return dt
@require_POST
def rate_chat(request):
chat_id = request.POST.get("chat_id")
chatbot_id = request.POST.get("chatbot_id")
study_key = request.POST.get("study_key")
quality = request.POST.get("quality")
fluency = request.POST.get("fluency")
diversity = request.POST.get("diversity")
contingency = request.POST.get("contingency")
empathy = request.POST.get("empathy")
comments = request.POST.get("comments")
mturk_user_id = request.POST.get("mturkUserId", "")
chat_rating = ChatRating(
chat_id=chat_id,
chatbot_id=chatbot_id,
study_key=study_key,
quality=quality,
fluency=fluency,
diversity=diversity,
contingency=contingency,
empathy=empathy,
comments=comments,
timestamp=make_aware(datetime.datetime.utcnow()),
user_session_id=_get_session_id(request),
mturk_user_id=mturk_user_id,
)
if chat_rating.should_generate_mturk_code():
chat_rating.generate_mturk_code()
chat_rating.save()
return render_to_json(success=True, mturk_code=chat_rating.mturk_code)
0
View Complete Implementation : credential.py
Copyright Apache License 2.0
Author : bcgov
Copyright Apache License 2.0
Author : bcgov
@clastmethod
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
0
View Complete Implementation : credential.py
Copyright Apache License 2.0
Author : bcgov
Copyright Apache License 2.0
Author : bcgov
@clastmethod
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
0
View Complete Implementation : up.py
Copyright GNU Affero General Public License v3.0
Author : BirkbeckCTP
Copyright GNU Affero General Public License v3.0
Author : BirkbeckCTP
def create_article_with_review_content(article_dict, journal, auth_file, base_url):
date_started = timezone.make_aware(dateparser.parse(article_dict.get('date_submitted')))
# Create a base article
article = models.Article(
journal=journal,
satle=article_dict.get('satle'),
abstract=article_dict.get('abstract'),
language=article_dict.get('language'),
stage=models.STAGE_UNDER_REVIEW,
is_import=True,
date_submitted=date_started,
)
article.save()
# Check for editors and astign them as section editors.
editors = article_dict.get('editors', [])
for editor in editors:
try:
account = core_models.Account.objects.get(email=editor)
account.add_account_role('section-editor', journal)
review_models.Editorastignment.objects.create(article=article, editor=account, editor_type='section-editor')
logger.info('Editor added to article')
except Exception as e:
logger.error('Editor account was not found.')
logger.exception(e)
# Add a new review round
round = review_models.ReviewRound.objects.create(article=article, round_number=1)
# Add keywords
keywords = article_dict.get('keywords')
if keywords:
for keyword in keywords.split(';'):
word, created = models.Keyword.objects.get_or_create(word=keyword)
article.keywords.add(word)
# Add authors
for author in article_dict.get('authors'):
try:
author_record = core_models.Account.objects.get(email=author.get('email'))
except core_models.Account.DoesNotExist:
author_record = core_models.Account.objects.create(
email=author.get('email'),
first_name=author.get('first_name'),
last_name=author.get('last_name'),
inssatution=author.get('affiliation'),
biography=author.get('bio'),
)
# If we have a country, fetch its record
if author.get('country'):
try:
country = core_models.Country.objects.get(code=author.get('country'))
author_record.country = country
author_record.save()
except core_models.Country.DoesNotExist:
past
# Add authors to m2m and create an order record
article.authors.add(author_record)
models.ArticleAuthorOrder.objects.create(article=article,
author=author_record,
order=article.next_author_sort())
# Set the primary author
article.owner = core_models.Account.objects.get(email=article_dict.get('correspondence_author'))
article.correspondence_author = article.owner
# Get or create the article's section
try:
section = models.Section.objects.language().fallbacks('en').get(journal=journal,
name=article_dict.get('section'))
except models.Section.DoesNotExist:
section = None
article.section = section
article.save()
# Attempt to get the default review form
form = setting_handler.get_setting('general',
'default_review_form',
journal,
create=True).processed_value
if not form:
try:
form = review_models.ReviewForm.objects.filter(journal=journal)[0]
except Exception:
form = None
logger.error(
'You must have at least one review form for the journal before'
' importing.'
)
exit()
for review in article_dict.get('reviews'):
try:
reviewer = core_models.Account.objects.get(email=review.get('email'))
except core_models.Account.DoesNotExist:
reviewer = core_models.Account.objects.create(
email=review.get('email'),
first_name=review.get('first_name'),
last_name=review.get('last_name'),
)
# Parse the dates
date_requested = timezone.make_aware(dateparser.parse(review.get('date_requested')))
date_due = timezone.make_aware(dateparser.parse(review.get('date_due')))
date_complete = timezone.make_aware(dateparser.parse(review.get('date_complete'))) if review.get(
'date_complete') else None
date_confirmed = timezone.make_aware(dateparser.parse(review.get('date_confirmed'))) if review.get(
'date_confirmed') else None
# If the review was declined, setup a date declined date stamp
review.get('declined')
if review.get('declined') == '1':
date_declined = date_confirmed
date_accepted = None
date_complete = date_confirmed
else:
date_accepted = date_confirmed
date_declined = None
new_review = review_models.Reviewastignment.objects.create(
article=article,
reviewer=reviewer,
review_round=round,
review_type='traditional',
visibility='double-blind',
date_due=date_due,
date_requested=date_requested,
date_complete=date_complete,
date_accepted=date_accepted,
access_code=uuid.uuid4(),
form=form
)
if review.get('declined') or review.get('recommendation'):
new_review.is_complete = True
if review.get('recommendation'):
new_review.decision = map_review_recommendation(review.get('recommendation'))
if review.get('review_file_url'):
filename, mime = shared.fetch_file(base_url, review.get('review_file_url'), None, None, article, None,
handle_images=False, auth_file=auth_file)
extension = os.path.splitext(filename)[1]
review_file = shared.add_file(mime, extension, 'Reviewer file', reviewer, filename, article,
galley=False)
new_review.review_file = review_file
if review.get('comments'):
filepath = core_files.create_temp_file(review.get('comments'), 'comment.txt')
file = open(filepath, 'r')
comment_file = core_files.save_file_to_article(file,
article,
article.owner,
label='Review Comments',
save=False)
new_review.review_file = comment_file
new_review.save()
# Get MS File
ms_file = get_ojs_file(base_url, article_dict.get('mreplacedcript_file_url'), article, auth_file, 'MS File')
article.mreplacedcript_files.add(ms_file)
# Get RV File
rv_file = get_ojs_file(base_url, article_dict.get('review_file_url'), article, auth_file, 'RV File')
round.review_files.add(rv_file)
# Get Supp Files
if article_dict.get('supp_files'):
for file in article_dict.get('supp_files'):
file = get_ojs_file(base_url, file.get('url'), article, auth_file, file.get('satle'))
article.data_figure_files.add(file)
article.save()
round.save()
return article
0
View Complete Implementation : chat_manager.py
Copyright GNU Affero General Public License v3.0
Author : botshot
Copyright GNU Affero General Public License v3.0
Author : botshot
def accept_with_ensaties(self, raw_message, ensaties):
with transaction.atomic():
try:
conversation = ChatConversation.objects.select_for_update().get(
interface_name=raw_message.interface.name,
raw_conversation_id=raw_message.raw_conversation_id
)
except ObjectDoesNotExist:
conversation = ChatConversation()
conversation.interface_name = raw_message.interface.name
conversation.raw_conversation_id = raw_message.raw_conversation_id
conversation.meta = raw_message.conversation_meta
raw_message.interface.fill_conversation_details(conversation)
# Save and read instance from database to acquire lock
conversation.save()
logging.info("Created new conversation: %s", conversation.__dict__)
conversation = ChatConversation.objects.select_for_update().get(pk=conversation.conversation_id)
conversation.last_message_time = timezone.now()
try:
user = ChatUser.objects.get(raw_user_id=raw_message.raw_user_id)
except ObjectDoesNotExist:
user = ChatUser()
user.raw_user_id = raw_message.raw_user_id
logging.info("Created new user: %s", user.__dict__)
# Save user before filling details so that image field can be saved
user.save()
user.conversations.add(conversation) # save metadata before filling details
conversation.save()
# TODO: also update details of existing users every once in a while
raw_message.interface.fill_user_details(user)
user.save()
message = ChatMessage()
message.conversation = conversation
message.user = user
message.type = raw_message.type
message.text = raw_message.text
message.time = make_aware(datetime.fromtimestamp(raw_message.timestamp))
message.is_user = True
message.ensaties = ensaties
self._process(message)
def get_feed(self, obj, request):
"""
Returns a feedgenerator.DefaultFeed object, fully populated, for
this feed. Raises FeedDoesNotExist for invalid parameters.
"""
current_site = get_current_site(request)
link = self._get_dynamic_attr('link', obj)
link = add_domain(current_site.domain, link, request.is_secure())
feed = self.feed_type(
satle=self._get_dynamic_attr('satle', obj),
subsatle=self._get_dynamic_attr('subsatle', obj),
link=link,
description=self._get_dynamic_attr('description', obj),
language=settings.LANGUAGE_CODE,
feed_url=add_domain(
current_site.domain,
self._get_dynamic_attr('feed_url', obj) or request.path,
request.is_secure(),
),
author_name=self._get_dynamic_attr('author_name', obj),
author_link=self._get_dynamic_attr('author_link', obj),
author_email=self._get_dynamic_attr('author_email', obj),
categories=self._get_dynamic_attr('categories', obj),
feed_copyright=self._get_dynamic_attr('feed_copyright', obj),
feed_guid=self._get_dynamic_attr('feed_guid', obj),
ttl=self._get_dynamic_attr('ttl', obj),
**self.feed_extra_kwargs(obj)
)
satle_tmp = None
if self.satle_template is not None:
try:
satle_tmp = loader.get_template(self.satle_template)
except TemplateDoesNotExist:
past
description_tmp = None
if self.description_template is not None:
try:
description_tmp = loader.get_template(self.description_template)
except TemplateDoesNotExist:
past
for item in self._get_dynamic_attr('items', obj):
context = self.get_context_data(item=item, site=current_site,
obj=obj, request=request)
if satle_tmp is not None:
satle = satle_tmp.render(context, request)
else:
satle = self._get_dynamic_attr('item_satle', item)
if description_tmp is not None:
description = description_tmp.render(context, request)
else:
description = self._get_dynamic_attr('item_description', item)
link = add_domain(
current_site.domain,
self._get_dynamic_attr('item_link', item),
request.is_secure(),
)
enclosures = self._get_dynamic_attr('item_enclosures', item)
author_name = self._get_dynamic_attr('item_author_name', item)
if author_name is not None:
author_email = self._get_dynamic_attr('item_author_email', item)
author_link = self._get_dynamic_attr('item_author_link', item)
else:
author_email = author_link = None
tz = get_default_timezone()
pubdate = self._get_dynamic_attr('item_pubdate', item)
if pubdate and is_naive(pubdate):
pubdate = make_aware(pubdate, tz)
updateddate = self._get_dynamic_attr('item_updateddate', item)
if updateddate and is_naive(updateddate):
updateddate = make_aware(updateddate, tz)
feed.add_item(
satle=satle,
link=link,
description=description,
unique_id=self._get_dynamic_attr('item_guid', item, link),
unique_id_is_permalink=self._get_dynamic_attr(
'item_guid_is_permalink', item),
enclosures=enclosures,
pubdate=pubdate,
updateddate=updateddate,
author_name=author_name,
author_email=author_email,
author_link=author_link,
categories=self._get_dynamic_attr('item_categories', item),
item_copyright=self._get_dynamic_attr('item_copyright', item),
**self.item_extra_kwargs(item)
)
return feed
0
View Complete Implementation : operations.py
Copyright MIT License
Author : bpgc-cte
Copyright MIT License
Author : bpgc-cte
def convert_datetimefield_value(self, value, expression, connection, context):
if value is not None:
if settings.USE_TZ:
value = timezone.make_aware(value, self.connection.timezone)
return value
@login_required(login_url='site.login')
@control_panel_view
def map_updates(request):
if not request.user_permissions.manage_map_updates:
raise PermissionDenied
page = request.GET.get('page', 1)
if request.method == 'POST':
if 'create_map_update' in request.POST:
map_update_form = MapUpdateForm(data=request.POST)
if map_update_form.is_valid():
map_update = map_update_form.instance
map_update.type = 'control_panel'
map_update.user = request.user
map_update.save()
messages.success(request, _('Map update successfully created.'))
return redirect(request.path_info)
elif 'process_updates' in request.POST:
if settings.HAS_CELERY:
process_map_updates.delay()
messages.success(request, _('Map update processing successfully queued.'))
else:
messages.error(request, _('Map update processing was not be queued because celery is not configured.'))
return redirect(request.path_info)
filter_form = MapUpdateFilterForm(request.GET)
map_update_form = MapUpdateForm()
queryset = MapUpdate.objects.order_by('-datetime').select_related('user', 'changeset__author')
if request.GET.get('type', None):
queryset = queryset.filter(type=request.GET['type'])
if request.GET.get('geometries_changed', None):
if request.GET['geometries_changed'] in ('1', '0'):
queryset = queryset.filter(geometries_changed=request.GET['geometries_changed'] == '1')
if request.GET.get('processed', None):
if request.GET['processed'] in ('1', '0'):
queryset = queryset.filter(processed=request.GET['processed'] == '1')
if request.GET.get('user_id', None):
if request.GET['user_id'].isdigit():
queryset = queryset.filter(user_id=request.GET['user_id'])
paginator = Paginator(queryset, 20)
users = paginator.page(page)
last_processed, last_processed_success = cache.get('mapdata:last_process_updates_run', (None, None))
if last_processed:
last_processed = make_aware(datetime.fromtimestamp(last_processed))
last_processed_start = cache.get('mapdata:last_process_updates_start', None)
if last_processed_start:
last_processed_start = make_aware(datetime.fromtimestamp(last_processed_start))
return render(request, 'control/map_updates.html', {
'last_processed': last_processed,
'last_processed_start': last_processed_start,
'last_processed_success': last_processed_success,
'auto_process_updates': settings.AUTO_PROCESS_UPDATES,
'map_update_form': map_update_form,
'filter_form': filter_form,
'updates': users,
})