Python django.utils.timezone.utc() Examples

The following are 30 code examples for showing how to use django.utils.timezone.utc(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module django.utils.timezone , or try the search function .

Example 1
Project: figures   Author: appsembler   File: seed.py    License: MIT License 6 votes vote down vote up
def seed_course_overviews(data=None):

    if not data:
        data = cans.COURSE_OVERVIEW_DATA
        # append with randomly generated course overviews to test pagination
        new_courses = [generate_course_overview(i, org='FOO') for i in xrange(20)]
        data += new_courses

    for rec in data:
        course_id = rec['id']
        defaults = dict(
                display_name=rec['display_name'],
                org=rec['org'],
                display_org_with_default=rec['org'],
                number=rec['number'],
                created=as_datetime(rec['created']).replace(tzinfo=utc),
                enrollment_start=as_datetime(rec['enrollment_start']).replace(tzinfo=utc),
                enrollment_end=as_datetime(rec['enrollment_end']).replace(tzinfo=utc),
            )
        if RELEASE_LINE != 'ginkgo':
            defaults['version'] = CourseOverview.VERSION
        CourseOverview.objects.update_or_create(
            id=as_course_key(course_id),
            defaults=defaults,
        ) 
Example 2
Project: figures   Author: appsembler   File: seed.py    License: MIT License 6 votes vote down vote up
def seed_course_completions():
    """
    go over the dates
    """
    for co in CourseOverview.objects.all():
        # Note there is a performance hit for using '?'
        qs = CourseEnrollment.objects.filter(course_id=co.id)
        # we just want a few of the enrollments to have completed

        # first cut, have 25% of learners complete course
        sample = int(qs.count() * 0.25)
        for ce in qs.order_by('?')[:sample]:
            GeneratedCertificate.objects.create(
                user=ce.user,
                course_id=co.id,
                created_date=as_datetime(FAKE.date_between(
                    ce.created, LAST_DAY)).replace(tzinfo=utc),
            ) 
Example 3
Project: figures   Author: appsembler   File: test_learner_course_grades.py    License: MIT License 6 votes vote down vote up
def test_certificate_and_completion(self):
        '''
        We're testing both certificate completion because currently, the only
        way to tell if a user
        '''
        # We shouldn't have any certificates for this
        # course enrollment
        assert not self.lcg.certificates()
        assert not self.lcg.learner_completed()
        # Create a certificate record for this user
        expected_cert = GeneratedCertificateFactory(
            user=self.lcg.learner,
            course_id=self.lcg.course.id,
            created_date=datetime.datetime(2018, 6, 1, tzinfo=utc))
        assert expected_cert
        check_certs = self.lcg.certificates()
        assert check_certs.count() == 1
        assert check_certs[0] == expected_cert
        assert self.lcg.learner_completed() 
Example 4
Project: figures   Author: appsembler   File: test_enrollment_metrics.py    License: MIT License 6 votes vote down vote up
def setup(self, db):
        self.site = SiteFactory()
        self.date_1 = datetime(2020, 2, 2, tzinfo=utc)
        self.date_2 = self.date_1 + relativedelta(months=1)  # future of date_1
        self.course_enrollment = CourseEnrollmentFactory()
        self.student_modules = [
            StudentModuleFactory(student=self.course_enrollment.user,
                                 course_id=self.course_enrollment.course_id,
                                 modified=self.date_1),
            StudentModuleFactory(student=self.course_enrollment.user,
                                 course_id=self.course_enrollment.course_id,
                                 modified=self.date_2)]
        self.progress_data = dict(points_possible=100,
                                  points_earned=25,
                                  sections_worked=4,
                                  count=5) 
Example 5
Project: figures   Author: appsembler   File: test_site_monthly_metrics.py    License: MIT License 6 votes vote down vote up
def smm_test_data(db):
    """
    Minimal test data for very simple test case
    """
    site = SiteFactory()
    mock_today = datetime(year=2020, month=2, day=1, tzinfo=utc)
    last_month = mock_today - relativedelta(months=1)
    month_before = last_month - relativedelta(months=1)
    month_before_sm = [StudentModuleFactory(created=month_before,
                                            modified=month_before)]
    last_month_sm = [StudentModuleFactory(created=last_month,
                                          modified=last_month) for i in range(2)]
    return dict(site=site,
                mock_today=mock_today,
                last_month=last_month,
                month_before=month_before,
                last_month_sm=last_month_sm,
                month_before_sm=month_before_sm) 
Example 6
Project: figures   Author: appsembler   File: helpers.py    License: MIT License 6 votes vote down vote up
def as_datetime(val):
    '''
    TODO: Add arg flag to say if caller wants end of day, beginning of day
    or a particular time of day if the param is a datetime.date obj
    '''
    if isinstance(val, datetime.datetime):
        return val
    elif isinstance(val, datetime.date):
        # Return the end of the day, set timezone to be UTC
        return datetime.datetime(
            year=val.year,
            month=val.month,
            day=val.day,
            ).replace(tzinfo=utc)

    elif isinstance(val, basestring):  # noqa: F821
        return dateutil_parse(val).replace(tzinfo=utc)
    else:
        raise TypeError(
            'value of type "{}" cannot be converted to a datetime object'.format(
                type(val))) 
Example 7
Project: figures   Author: appsembler   File: backfill.py    License: MIT License 6 votes vote down vote up
def backfill_monthly_metrics_for_site(site, overwrite=False):
    """Backfill all historical site metrics for the specified site
    """
    site_sm = get_student_modules_for_site(site)
    if not site_sm:
        return None

    first_created = site_sm.order_by('created').first().created

    start_month = datetime(year=first_created.year,
                           month=first_created.month,
                           day=1,
                           tzinfo=utc)
    last_month = datetime.utcnow().replace(tzinfo=utc) - relativedelta(months=1)
    backfilled = []
    for dt in rrule(freq=MONTHLY, dtstart=start_month, until=last_month):
        obj, created = fill_month(site=site,
                                  month_for=dt,
                                  student_modules=site_sm,
                                  overwrite=overwrite)
        backfilled.append(dict(obj=obj, created=created, dt=dt))

    return backfilled 
Example 8
Project: python-mysql-pool   Author: LuciferJack   File: operations.py    License: MIT License 6 votes vote down vote up
def value_to_db_datetime(self, value):
        if value is None:
            return None
        # MySQL doesn't support tz-aware times
        if timezone.is_aware(value):
            if settings.USE_TZ:
                value = value.astimezone(timezone.utc).replace(tzinfo=None)
            else:
                raise ValueError(
                    "MySQL backend does not support timezone-aware times."
                )
        if not self.connection.features.supports_microsecond_precision:
            value = value.replace(microsecond=0)
        if not self.connection.use_pure:
            return datetime_to_mysql(value)
        return self.connection.converter.to_mysql(value) 
Example 9
Project: GTDWeb   Author: lanbing510   File: operations.py    License: GNU General Public License v2.0 6 votes vote down vote up
def value_to_db_datetime(self, value):
        """
        Transform a datetime value to an object compatible with what is expected
        by the backend driver for datetime columns.

        If naive datetime is passed assumes that is in UTC. Normally Django
        models.DateTimeField makes sure that if USE_TZ is True passed datetime
        is timezone aware.
        """

        if value is None:
            return None

        # cx_Oracle doesn't support tz-aware datetimes
        if timezone.is_aware(value):
            if settings.USE_TZ:
                value = value.astimezone(timezone.utc).replace(tzinfo=None)
            else:
                raise ValueError("Oracle backend does not support timezone-aware datetimes when USE_TZ is False.")

        return Oracle_datetime.from_datetime(value) 
Example 10
Project: GTDWeb   Author: lanbing510   File: base.py    License: GNU General Public License v2.0 6 votes vote down vote up
def adapt_datetime_with_timezone_support(value, conv):
    # Equivalent to DateTimeField.get_db_prep_value. Used only by raw SQL.
    if settings.USE_TZ:
        if timezone.is_naive(value):
            warnings.warn("MySQL 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)
        value = value.astimezone(timezone.utc).replace(tzinfo=None)
    return Thing2Literal(value.strftime("%Y-%m-%d %H:%M:%S.%f"), conv)

# MySQLdb-1.2.1 returns TIME columns as timedelta -- they are more like
# timedelta in terms of actual behavior as they are signed and include days --
# and Django expects time, so we still need to override that. We also need to
# add special handling for SafeText and SafeBytes as MySQLdb's type
# checking is too tight to catch those (see Django ticket #6052).
# Finally, MySQLdb always returns naive datetime objects. However, when
# timezone support is active, Django expects timezone-aware datetime objects. 
Example 11
Project: kobo-predict   Author: awemulya   File: sync_deleted_instances_fix.py    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
def handle(self, *args, **kwargs):
        # Reset all sql deletes to None
        Instance.objects.exclude(
            deleted_at=None, xform__downloadable=True).update(deleted_at=None)

        # Get all mongo deletes
        query = '{"$and": [{"_deleted_at": {"$exists": true}}, ' \
                '{"_deleted_at": {"$ne": null}}]}'
        query = json.loads(query)
        xform_instances = settings.MONGO_DB.instances
        cursor = xform_instances.find(query)
        for record in cursor:
            # update sql instance with deleted_at datetime from mongo
            try:
                i = Instance.objects.get(
                    uuid=record["_uuid"],  xform__downloadable=True)
            except Instance.DoesNotExist:
                continue
            else:
                deleted_at = parse_datetime(record["_deleted_at"])
                if not timezone.is_aware(deleted_at):
                    deleted_at = timezone.make_aware(
                        deleted_at, timezone.utc)
                i.set_deleted(deleted_at) 
Example 12
Project: jinja2-django-tags   Author: MoritzS   File: tests.py    License: MIT License 6 votes vote down vote up
def test_localize(self):
        env = Environment(extensions=[DjangoL10n])
        template = env.from_string("{{ foo }}")
        context1 = {'foo': 1.23}
        date = datetime.datetime(2000, 10, 1, 14, 10, 12, tzinfo=timezone.utc)
        context2 = {'foo': date}

        translation.activate('en')
        self.assertEqual('1.23', template.render(context1))

        translation.activate('de')
        self.assertEqual('1,23', template.render(context1))

        translation.activate('es')
        timezone.activate('America/Argentina/Buenos_Aires')
        self.assertEqual('1 de Octubre de 2000 a las 11:10', template.render(context2))

        timezone.activate('Europe/Berlin')
        self.assertEqual('1 de Octubre de 2000 a las 16:10', template.render(context2))

        translation.activate('de')
        self.assertEqual('1. Oktober 2000 16:10', template.render(context2))

        timezone.activate('America/Argentina/Buenos_Aires')
        self.assertEqual('1. Oktober 2000 11:10', template.render(context2)) 
Example 13
Project: bioforum   Author: reBiocoder   File: feedgenerator.py    License: MIT License 6 votes vote down vote up
def latest_post_date(self):
        """
        Return the latest item's pubdate or updateddate. If no items
        have either of these attributes this return the current UTC date/time.
        """
        latest_date = None
        date_keys = ('updateddate', 'pubdate')

        for item in self.items:
            for date_key in date_keys:
                item_date = item.get(date_key)
                if item_date:
                    if latest_date is None or item_date > latest_date:
                        latest_date = item_date

        # datetime.now(tz=utc) is slower, as documented in django.utils.timezone.now
        return latest_date or datetime.datetime.utcnow().replace(tzinfo=utc) 
Example 14
Project: plugin.video.netflix   Author: CastagnaIT   File: operations.py    License: MIT License 6 votes vote down vote up
def value_to_db_datetime(self, value):
        if value is None:
            return None
        # MySQL doesn't support tz-aware times
        if timezone.is_aware(value):
            if settings.USE_TZ:
                value = value.astimezone(timezone.utc).replace(tzinfo=None)
            else:
                raise ValueError(
                    "MySQL backend does not support timezone-aware times."
                )
        if not self.connection.features.supports_microsecond_precision:
            value = value.replace(microsecond=0)
        if not self.connection.use_pure:
            return datetime_to_mysql(value)
        return self.connection.converter.to_mysql(value) 
Example 15
Project: figures   Author: appsembler   File: seed.py    License: MIT License 5 votes vote down vote up
def seed_users(data=None):
    if not data:
        data = cans.USER_DATA

    first_date = days_from(LAST_DAY, DAYS_BACK * -1)
    created_users = []
    for rec in data:
        try:
            profile_rec = rec.get('profile', None)
            user = get_user_model().objects.create_user(
                username=rec['username'],
                password=rec['password'],
                email=rec['email'],
                )
            user.is_staff = rec.get('is_staff', False)
            user.is_superuser = rec.get('is_superuser', False)
            user.date_joined = as_datetime(
                FAKE.date_between(first_date, LAST_DAY)).replace(tzinfo=utc)
            user.save()
            created_users.append(user)
            if profile_rec:
                UserProfile.objects.create(
                    user=user,
                    name=profile_rec['fullname'],
                    gender=profile_rec.get('gender', None),
                    country=profile_rec.get('country', None),
                )
        except IntegrityError as e:
            print('skipping duplicate user email {}'.format(e))
    return created_users 
Example 16
Project: figures   Author: appsembler   File: seed.py    License: MIT License 5 votes vote down vote up
def seed_course_enrollments_for_course(course_id, users, max_days_back):

    def enroll_date(max_days_back):
        days_back = random.randint(1, abs(max_days_back))
        return days_from(LAST_DAY, days_back * -1)

    for user in users:
        if VERBOSE:
            print('seeding course enrollment for user {}'.format(user.username))

        CourseEnrollment.objects.update_or_create(
            course_id=course_id,
            user=user,
            created=as_datetime(enroll_date(max_days_back)).replace(tzinfo=utc),
            ) 
Example 17
Project: figures   Author: appsembler   File: test_course_monthly_metrics_viewset.py    License: MIT License 5 votes vote down vote up
def generate_date_series(months_back=6, count=20):
    """

    """
    start_date = '-{months_back}M'.format(months_back=months_back)
    date_series = [fake.date_time_between(start_date=start_date,
                                          end_date='now',
                                          tzinfo=utc) for i in range(count)]
    date_series.sort()
    return date_series 
Example 18
Project: figures   Author: appsembler   File: test_metrics.py    License: MIT License 5 votes vote down vote up
def setup(self, db):
        self.today = datetime.datetime(2018, 1, 6, tzinfo=utc)
        self.site_daily_metrics = None
        self.expected_keys = (
            'monthly_active_users',
            'total_site_users',
            'total_site_courses',
            'total_course_enrollments',
            'total_course_completions',) 
Example 19
Project: figures   Author: appsembler   File: test_enrollment_metrics.py    License: MIT License 5 votes vote down vote up
def setup(self, db):
        self.site = SiteFactory()
        self.course_enrollment = CourseEnrollmentFactory()
        self.date_for = datetime.utcnow().replace(tzinfo=utc).date() 
Example 20
Project: figures   Author: appsembler   File: test_serializers.py    License: MIT License 5 votes vote down vote up
def setup(self, db):
        self.site = Site.objects.first()
        self.certificate_date = datetime.datetime(2018, 4, 1, tzinfo=utc)
        self.course_enrollment = CourseEnrollmentFactory(
            )
        self.generated_certificate = GeneratedCertificateFactory(
            user=self.course_enrollment.user,
            course_id=self.course_enrollment.course_id,
            created_date=self.certificate_date,
            )
        self.serializer = LearnerCourseDetailsSerializer(
            instance=self.course_enrollment) 
Example 21
Project: figures   Author: appsembler   File: test_helpers.py    License: MIT License 5 votes vote down vote up
def test_get_now_from_str(self):
        format = '%Y-%m-%d %H:%M:%S'
        a_datetime_str = self.now.strftime(format)
        expected = dateutil_parse(a_datetime_str).replace(tzinfo=utc)
        assert isinstance(a_datetime_str, str)
        assert as_datetime(a_datetime_str) == expected 
Example 22
Project: figures   Author: appsembler   File: test_helpers.py    License: MIT License 5 votes vote down vote up
def test_get_now_from_unicode(self):
        format = '%Y-%m-%d %H:%M:%S'
        a_datetime_str = unicode(self.now.strftime(format))
        expected = dateutil_parse(a_datetime_str).replace(tzinfo=utc)
        assert isinstance(a_datetime_str, unicode)
        assert as_datetime(a_datetime_str) == expected 
Example 23
Project: figures   Author: appsembler   File: test_helpers.py    License: MIT License 5 votes vote down vote up
def test_get_now_from_date(self):
        '''
        Returns date at midnight
        '''
        a_date = self.now.date()
        expected = datetime.datetime(
            year=a_date.year,
            month=a_date.month,
            day=a_date.day,
            hour=0,
            minute=0,
            second=0,
            microsecond=0,
            ).replace(tzinfo=utc)
        assert as_datetime(a_date) == expected 
Example 24
Project: figures   Author: appsembler   File: course_daily_metrics.py    License: MIT License 5 votes vote down vote up
def load(self, date_for=None, force_update=False, **_kwargs):
        """
        TODO: clean up how we do this. We want to be able to call the loader
        with an existing data set (not having to call the extractor) but we
        need to make sure that the metrics row 'date_for' is the same as
        provided in the data. So before hacking something together, I want to
        think this over some more.

        If the record alrdady exists and force_update is False, then simply
        return the record with the 'created' flag to False. This saves us an
        unnecessary call to extract data

        Raises ValidationError if invalid data is attempted to be saved to the
        course daily metrics model instance
        """
        if not date_for:
            date_for = prev_day(
                datetime.datetime.utcnow().replace(tzinfo=utc).date())
        else:
            date_for = as_datetime(date_for).replace(tzinfo=utc)
        try:
            cdm = CourseDailyMetrics.objects.get(course_id=self.course_id,
                                                 date_for=date_for)
            # record found, only update if force update flag is True
            if not force_update:
                return (cdm, False,)
        except CourseDailyMetrics.DoesNotExist:
            # record not found, move on to creating
            pass

        data = self.get_data(date_for=date_for)
        return self.save_metrics(date_for=date_for, data=data) 
Example 25
Project: figures   Author: appsembler   File: site_daily_metrics.py    License: MIT License 5 votes vote down vote up
def extract(self, site, date_for=None, **kwargs):  # pylint: disable=unused-argument
        '''
        We get the count from the User model since there can be registered users
        who have not enrolled.

        TODO: Exclude non-students from the user count
        '''
        if not date_for:
            date_for = prev_day(
                datetime.datetime.utcnow().replace(tzinfo=utc).date()
            )

        data = dict()

        site_users = get_users_for_site(site)
        user_count = site_users.filter(
            date_joined__lt=as_datetime(next_day(date_for))).count()
        site_courses = get_courses_for_site(site)
        course_count = site_courses.filter(
            created__lt=as_datetime(next_day(date_for))).count()

        todays_active_users = get_site_active_users_for_date(site, date_for)
        todays_active_user_count = todays_active_users.count()
        data['todays_active_user_count'] = todays_active_user_count
        data['cumulative_active_user_count'] = get_previous_cumulative_active_user_count(
            site, date_for) + todays_active_user_count
        data['total_user_count'] = user_count
        data['course_count'] = course_count
        data['total_enrollment_count'] = get_total_enrollment_count(site, date_for)
        return data 
Example 26
Project: ideascube   Author: ideascube   File: test_views.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_content_are_ordered_by_last_published_by_default(app):
    content3 = ContentFactory(
        status=Content.PUBLISHED,
        published_at=datetime(2016, 6, 26, 16, 15, tzinfo=timezone.utc))
    content1 = ContentFactory(
        status=Content.PUBLISHED,
        published_at=datetime(2016, 6, 26, 16, 17, tzinfo=timezone.utc))
    content2 = ContentFactory(
        status=Content.PUBLISHED,
        published_at=datetime(2016, 6, 26, 16, 16, tzinfo=timezone.utc))
    response = app.get(reverse('blog:index'))
    titles = response.pyquery.find('.card.blog h3')
    assert titles[0].text == content1.title
    assert titles[1].text == content2.title
    assert titles[2].text == content3.title 
Example 27
Project: ideascube   Author: ideascube   File: test_views.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_should_take_sort_parameter_into_account(app):
    content3 = ContentFactory(
        status=Content.PUBLISHED,
        published_at=datetime(2016, 6, 26, 16, 15, tzinfo=timezone.utc))
    content1 = ContentFactory(
        status=Content.PUBLISHED,
        published_at=datetime(2016, 6, 26, 16, 17, tzinfo=timezone.utc))
    content2 = ContentFactory(
        status=Content.PUBLISHED,
        published_at=datetime(2016, 6, 26, 16, 16, tzinfo=timezone.utc))
    response = app.get(reverse('blog:index'), params={'sort': 'asc'})
    titles = response.pyquery.find('.card.blog h3')
    assert titles[0].text == content3.title
    assert titles[1].text == content2.title
    assert titles[2].text == content1.title 
Example 28
Project: ideascube   Author: ideascube   File: test_views.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_published_at_is_YMD_formatted_even_in_other_locale(staffapp,
                                                            published):
    published.published_at = datetime(2015, 1, 7, tzinfo=timezone.utc)
    published.save()
    translation.activate('fr')
    url = reverse('blog:content_update', kwargs={'pk': published.pk})
    form = staffapp.get(url).forms['model_form']
    assert form['published_at'].value == '2015-01-07'
    translation.deactivate() 
Example 29
Project: GTDWeb   Author: lanbing510   File: tz.py    License: GNU General Public License v2.0 5 votes vote down vote up
def utc(value):
    """
    Converts a datetime to UTC.
    """
    return do_timezone(value, timezone.utc) 
Example 30
Project: GTDWeb   Author: lanbing510   File: timesince.py    License: GNU General Public License v2.0 5 votes vote down vote up
def timesince(d, now=None, reversed=False):
    """
    Takes two datetime objects and returns the time between d and now
    as a nicely formatted string, e.g. "10 minutes".  If d occurs after now,
    then "0 minutes" is returned.

    Units used are years, months, weeks, days, hours, and minutes.
    Seconds and microseconds are ignored.  Up to two adjacent units will be
    displayed.  For example, "2 weeks, 3 days" and "1 year, 3 months" are
    possible outputs, but "2 weeks, 3 hours" and "1 year, 5 days" are not.

    Adapted from
    http://web.archive.org/web/20060617175230/http://blog.natbat.co.uk/archive/2003/Jun/14/time_since
    """
    # Convert datetime.date to datetime.datetime for comparison.
    if not isinstance(d, datetime.datetime):
        d = datetime.datetime(d.year, d.month, d.day)
    if now and not isinstance(now, datetime.datetime):
        now = datetime.datetime(now.year, now.month, now.day)

    if not now:
        now = datetime.datetime.now(utc if is_aware(d) else None)

    delta = (d - now) if reversed else (now - d)
    # ignore microseconds
    since = delta.days * 24 * 60 * 60 + delta.seconds
    if since <= 0:
        # d is in the future compared to now, stop processing.
        return avoid_wrapping(ugettext('0 minutes'))
    for i, (seconds, name) in enumerate(TIMESINCE_CHUNKS):
        count = since // seconds
        if count != 0:
            break
    result = avoid_wrapping(name % count)
    if i + 1 < len(TIMESINCE_CHUNKS):
        # Now get the second item
        seconds2, name2 = TIMESINCE_CHUNKS[i + 1]
        count2 = (since - (seconds * count)) // seconds2
        if count2 != 0:
            result += ugettext(', ') + avoid_wrapping(name2 % count2)
    return result