Exemplo n.º 1
0
    def test_seconds_to_expiry(self):
        "Test that it handles naive and tz-aware times"

        with self.settings(USE_TZ=False):
            at = AccessToken(expires_on=TOMORROW)
            expires_at = datetime.combine(at.expires_on, time.min)
            self.assertTrue(is_naive(expires_at))
            self.assertTrue(is_naive(now()))
            self.assertEqual(
                at.seconds_to_expiry,
                int((expires_at - now()).total_seconds())
            )

        with self.settings(USE_TZ=True):
            at = AccessToken(expires_on=TOMORROW)
            expires_at = make_aware(
                datetime.combine(at.expires_on, time.min),
                get_current_timezone()
            )
            self.assertTrue(is_aware(expires_at))
            self.assertTrue(is_aware(now()))
            self.assertEqual(
                at.seconds_to_expiry,
                int((expires_at - now()).total_seconds())
            )
Exemplo n.º 2
0
 def import_serialized_json_obj(self, json_obj):
     """ imports a serialized json object
         to add records to the database
     """
     all_ok = True
     try:
         with transaction.atomic():
             for obj in serializers.deserialize("json", json_obj):
                 # this just saves the object, so as to
                 # synch different instances of open contex
                 if hasattr(obj, 'object'):
                     # object_dict = obj.object.__dict__
                     # print(str(object_dict))
                     if hasattr(obj.object, 'data_date'):
                         if isinstance(obj.object.data_date, datetime):
                             if timezone.is_naive(obj.object.data_date):
                                 obj.object.data_date = pytz.utc.localize(obj.object.data_date)
                     if hasattr(obj.object, 'created'):
                         if isinstance(obj.object.created, datetime):
                             if timezone.is_naive(obj.object.created):
                                 obj.object.created = pytz.utc.localize(obj.object.created)
                     if hasattr(obj.object, 'updated'):
                         if isinstance(obj.object.updated, datetime):
                             if timezone.is_naive(obj.object.updated):
                                 obj.object.updated = pytz.utc.localize(obj.object.updated)
                 obj.save()
     except Exception as e:
         print('Problem: ' + str(e))
         all_ok = False
     return all_ok
Exemplo n.º 3
0
    def process_request(self, request):
        """
        Verify that the session should be considered active. We check
        the start time and the last activity time to determine if this
        is the case. We set the last activity time to now() if the session
        is still active.
        """
        if not hasattr(request, 'user'):
            raise ImproperlyConfigured(
                "The Login Required middleware "
                "requires authentication middleware to be installed."
            )

        path = request.path_info.lstrip('/')

        if any(m.match(path) for m in self.exempt_urls):
            return

        if (
            self.START_TIME_KEY not in request.session or
            self.LAST_ACTIVITY_KEY not in request.session or
            timezone.is_naive(self.get_start_time(request)) or
            timezone.is_naive(self.get_last_activity(request))
        ):
            response = self.process_new_session(request)
        else:
            response = self.process_existing_session(request)

        if response:
            return response
Exemplo n.º 4
0
 def _match_aware(cls, old, new):
     tz = timezone.get_default_timezone()
     if timezone.is_naive(old) and timezone.is_aware(new):
         return (old, timezone.make_naive(new, tz))
     elif timezone.is_aware(old) and timezone.is_naive(new):
         return (timezone.make_naive(old, tz), new)
     else:
         return (old, new)
Exemplo n.º 5
0
def test_make_aware_use_tz_false(settings):
    """Tests datetimes are left intact if `USE_TZ` is not in effect."""
    settings.USE_TZ = False

    datetime_object = datetime(2016, 1, 2, 21, 52, 25)
    assert timezone.is_naive(datetime_object)
    datetime_aware = make_aware(datetime_object)
    assert timezone.is_naive(datetime_aware)
Exemplo n.º 6
0
    def _checkrep(self):
        """Verify consistency of attributes and history"""

        #First, basic attributes.
        count = 0                
        if self.active:
            count += 1
        if self.completed:
            count += 1
        if self.withdrawn:
            count += 1
        if count != 1:
            msg="UC {0} _checkrep() detected errored state".format(self.pk)
            logger.error(msg)
            raise CheckRepError(msg)
        
        #Second, compare action history with attributes
        decoded_history = self.hist2list()
        last = decoded_history.pop()
        last2 = decoded_history.pop()
        
        if is_naive(last[0]):
            logger.warning("UC _checkrep() detected naive timezone")
            return False
        if is_naive(last2[0]):
            logger.warning("UC _checkrep() detected naive timezone")
            return False
            
        if self.active:
            if last[1] != 'ACTIVATION':
                logger.warning("UC _checkrep() detected errored state")
                return False
            if last2[1] != 'REGISTRATION' and last2[1] != 'REOPENING':
                logger.warning("UC _checkrep() detected errored state")
                return False
                
        if self.withdrawn:
            if last[1] != 'DEACTIVATION' or last2[1] != 'WITHDRAWAL':
                logger.warning("UC _checkrep() detected errored state")
                return False
                
        if self.completed:
            if last[1] != 'DEACTIVATION' or last2[1] != 'COMPLETION':
                logger.warning("UC _checkrep() detected errored state")
                return False       
       
        #Third, ensure that the user enrolled isn't organiser or instructor
        if self.course.organiser == self.user:
            logger.warning("UC _checkrep() detected error. Organiser can't "\
                "enrol.")
            return False
        if self.course.instructor == self.user:
            logger.warning("UC _checkrep() detected error. Instructor can't "\
                "enrol.")
            return False
    
        #We're good!
        return True
Exemplo n.º 7
0
def upgrade():

    #Convert all dates to timezone aware
    for d in DownloadItem.objects.all():
        if d.date and timezone.is_naive(d.date):
            d.date = convert_datetime(d.date)

        if d.dateadded and timezone.is_naive(d.dateadded):
            d.dateadded = convert_datetime(d.dateadded)

        if d.dlstart and timezone.is_naive(d.dlstart):
            d.dlstart = convert_datetime(d.dlstart)

        d.save()

    for t in TVShow.objects.all():
        if t.updated and timezone.is_naive(t.updated):
            t.updated = convert_datetime(t.updated)

        t.save()

    for m in Movie.objects.all():
        if m.updated and timezone.is_naive(m.updated):
            m.updated = convert_datetime(m.updated)

        m.save()

    #clear out logs
    DownloadLog.objects.all().delete()

    #delete cache


    #Delete all metaparser cache
    from lazy_common.models import MetaParserCache
    for p in MetaParserCache.objects.all():
        p.delete()

    #Update all download items
    count = DownloadItem.objects.all().count()
    i = 0
    for d in DownloadItem.objects.all():
        logger.debug("%s/%s Parsing %s" % (i, count, d.title))
        d.parse_title()
        d.save()
        i += 1

    #Update tvshow objects
    count = TVShow.objects.all().count()
    up_to = 0
    for tvshow in TVShow.objects.all():
        try:
            up_to += 1
            print "### %s / %s" % (up_to, count)
            tvshow.update_from_tvdb()
        except:
            pass
Exemplo n.º 8
0
 def forwards(self, orm):
     "Write your forwards methods here."
     # Note: Remember to use orm['appname.ModelName'] rather than "from appname.models..."
     t = timezone.get_current_timezone()
     for s in orm['sleep.Sleep'].objects.all():
         if timezone.is_naive(s.start_time):
             s.start_time = timezone.make_aware(s.start_time, t)
         if timezone.is_naive(s.end_time):
             s.end_time = timezone.make_aware(s.start_time, t)
         s.save()
Exemplo n.º 9
0
def collect_eo_metadata(qs, insert=None, exclude=None, bbox=False):
    """ Helper function to collect EO metadata from all EOObjects in a queryset, 
    plus additionals from a list and exclude others from a different list. If 
    bbox is `True` then the returned polygon will only be a minimal bounding box
    of the collected footprints.
    """

    values = qs.exclude(
        pk__in=[eo_object.pk for eo_object in exclude or ()]
    ).aggregate(
        begin_time=Min("begin_time"), end_time=Max("end_time"),
        footprint=Union("footprint")
    )

    begin_time, end_time, footprint = (
        values["begin_time"], values["end_time"], values["footprint"]
    )

    # workaround for Django 1.4 bug: aggregate times are strings
    if isinstance(begin_time, basestring):
        begin_time = parse_datetime(begin_time)

    if isinstance(end_time, basestring):
        end_time = parse_datetime(end_time)

    if begin_time and is_naive(begin_time):
        begin_time = make_aware(begin_time, get_current_timezone())
    if end_time and is_naive(end_time):
        end_time = make_aware(end_time, get_current_timezone())

    for eo_object in insert or ():
        if begin_time is None:
            begin_time = eo_object.begin_time
        elif eo_object.begin_time is not None:
            begin_time = min(begin_time, eo_object.begin_time)

        if end_time is None:
            end_time = eo_object.end_time
        elif eo_object.end_time is not None:
            end_time = max(end_time, eo_object.end_time)

        if footprint is None:
            footprint = eo_object.footprint
        elif eo_object.footprint is not None:
            footprint = footprint.union(eo_object.footprint)

    if not isinstance(footprint, MultiPolygon) and footprint is not None:
        footprint = MultiPolygon(footprint)

    if bbox and footprint is not None:
        footprint = MultiPolygon(Polygon.from_bbox(footprint.extent))

    return begin_time, end_time, footprint
Exemplo n.º 10
0
def federation_charts(request, federation_slug=None):
    if federation_slug is None:
        federation = None
    else:
        federation = get_object_or_404(Federation, slug=federation_slug)

    if request.method == 'POST':
        form = ChartForm(request.POST, request.FILES, instance=federation)

        if form.is_valid():
            stats_config_dict = getattr(settings, "STATS")
            service_terms = stats_config_dict['statistics']['entity_by_type']['terms']
            protocol_terms = stats_config_dict['statistics']['entity_by_protocol']['terms']
            
            protocols = stats_config_dict['protocols']

            from_time = datetime.fromordinal(form.cleaned_data['fromDate'].toordinal())
            if timezone.is_naive(from_time):
                from_time = pytz.utc.localize(from_time)
            to_time = datetime.fromordinal(form.cleaned_data['toDate'].toordinal() + 1)
            if timezone.is_naive(to_time):
                to_time = pytz.utc.localize(to_time)

            service_stats = EntityStat.objects.filter(  federation=federation \
                                              , feature__in = service_terms \
                                              , time__gte = from_time \
                                              , time__lte = to_time).order_by("time")

            protocol_stats = EntityStat.objects.filter(  federation=federation \
                                              , feature__in = protocol_terms \
                                              , time__gte = from_time \
                                              , time__lte = to_time).order_by("time")

            s_chart = stats_chart(stats_config_dict, request, service_stats, 'entity_by_type')

            p_chart = stats_chart(stats_config_dict, request, protocol_stats, 'entity_by_protocol', protocols)

            return render_to_response('metadataparser/federation_chart.html',
                                      {'form': form,
                                       'statcharts': [s_chart, p_chart],
                                      },
                                      context_instance=RequestContext(request))

        else:
            messages.error(request, _('Please correct the errors indicated'
                                      ' below'))
    else:
        form = ChartForm(instance=federation)

    return render_to_response('metadataparser/federation_chart.html',
                              {'settings': settings, 'form': form},
                              context_instance=RequestContext(request))
Exemplo n.º 11
0
    def __init__(self, min_date=None, max_date=None, *args, **kwargs):
        if min_date is not None:
            self.min_date = min_date
        if is_naive(self.min_date):
            self.min_date = self.min_date.replace(tzinfo=utc)

        if max_date is not None:
            self.max_date = max_date
        if is_naive(self.max_date):
            self.max_date = self.max_date.replace(tzinfo=utc)

        assert self.min_date < self.max_date
        super(DateTimeGenerator, self).__init__(*args, **kwargs)
Exemplo n.º 12
0
def make_aware(value):
    """Force datatime to have timezone information."""
    if getattr(settings, 'USE_TZ', False):
        # naive datetimes are assumed to be in UTC.
        if timezone.is_naive(value):
            value = timezone.make_aware(value, timezone.utc)
        # then convert to the Django configured timezone.
        default_tz = timezone.get_default_timezone()
        value = timezone.localtime(value, default_tz)
    else:
        # naive datetimes are assumed to be in local timezone.
        if timezone.is_naive(value):
            value = timezone.make_aware(value, timezone.get_default_timezone())
    return value
Exemplo n.º 13
0
def total_usage(request):
    '''
    Show total network usage over the last week.
    '''
    transfer_logs = TransferLog.objects.filter(timestamp__gt=now() - timedelta(7)).order_by('-timestamp')
    for delta in [timedelta(0,i) for i in range(0,24*7)]:
        delta_end = delta + timedelta(0,1)
        window_start = now() - delta
        window_end = now() - delta_end
        _transfers = [log for log in transfer_logs
                      if log.timestamp > window_start and log.timestamp < window_end]
        print window_start, _transfers
        print is_naive(window_start), is_naive(window_end), is_naive(log.timestamp)
    return HttpResponse("ok")
Exemplo n.º 14
0
    def compute_new_stats(self):
        if not self._metadata: return ([], [])
        entities_from_xml = self._metadata.get_entities()

        entities = Entity.objects.filter(entityid__in=entities_from_xml)
        entities = entities.prefetch_related('types')
        Entity_Federations.objects.filter(federation=self)

        try:
            first_date = EntityStat.objects.filter(
                federation=self).aggregate(Max('time'))['time__max']
            if not first_date:
                raise Exception('Not able to find statistical data in the DB.')
        except Exception:
            first_date = datetime(2010, 1, 1)
            first_date = pytz.utc.localize(first_date)

        for curtimestamp in self._daterange(first_date, timezone.now() - timedelta(1)):
            computed = {}
            not_computed = []
            entity_stats = []
            for feature in stats['features'].keys():
                fun = getattr(self, 'get_%s' % feature, None)

                if callable(fun):
                    stat = EntityStat()
                    stat.feature = feature
                    stat.time = curtimestamp
                    stat.federation = self
                    stat.value = fun(
                        entities, stats['features'][feature], curtimestamp)
                    entity_stats.append(stat)
                    computed[feature] = stat.value
                else:
                    not_computed.append(feature)

            from_time = datetime.combine(curtimestamp, time.min)
            if timezone.is_naive(from_time):
                from_time = pytz.utc.localize(from_time)
            to_time = datetime.combine(curtimestamp, time.max)
            if timezone.is_naive(to_time):
                to_time = pytz.utc.localize(to_time)

            EntityStat.objects.filter(
                federation=self, time__gte=from_time, time__lte=to_time).delete()
            EntityStat.objects.bulk_create(entity_stats)

        return (computed, not_computed)
Exemplo n.º 15
0
    def test_make_dt_aware_without_pytz(self):
        with override_settings(USE_TZ=False):
            now = datetime.datetime.now()
            date = DateRangeFilter.make_dt_aware(now, None)

            self.assertEqual(date.tzinfo, None)
            self.assertTrue(timezone.is_naive(date))
Exemplo n.º 16
0
def get_pub_date(article):
    """ (newspaper.article.Article) -> str
    Searches and returns date of which the article was published
    Returns None otherwise

    Keyword arguments:
    article         -- 'Newspaper.Article' object of article
    """
    dates = []

    # For each metadata stored by newspaper's parsing ability,
    # check if any of the key contains 'date'
    for key, value in article.meta_data.iteritems():
        if re.search("date", key, re.IGNORECASE):
            # If the key contains 'date', try to parse the value as date
            try:
                dt = parser.parse(str(value))
                # If parsing succeeded, then append it to the list
                dates.append(dt)
            except (KeyboardInterrupt, SystemExit):
                raise
            except:
                pass
    # If one of more dates were found,
    # return the oldest date as new ones can be updated dates
    # instead of published date
    if dates:
        date = sorted(dates, key=lambda x: str(x)[0])[0]
        if timezone.is_naive(date):
            return \
                timezone.make_aware(date,
                                    timezone=timezone.get_default_timezone())
        else:
            return timezone.localtime(date)
    return None
Exemplo n.º 17
0
def is_token_valid(token, margin=None):
    """Timezone-aware checking of the auth token's expiration timestamp.

    Returns ``True`` if the token has not yet expired, otherwise ``False``.

    :param token: The openstack_auth.user.Token instance to check

    :param margin:
       A time margin in seconds to subtract from the real token's validity.
       An example usage is that the token can be valid once the middleware
       passed, and invalid (timed-out) during a view rendering and this
       generates authorization errors during the view rendering.
       A default margin can be set by the TOKEN_TIMEOUT_MARGIN in the
       django settings.
    """
    expiration = token.expires
    # In case we get an unparseable expiration timestamp, return False
    # so you can't have a "forever" token just by breaking the expires param.
    if expiration is None:
        return False
    if margin is None:
        margin = getattr(settings, 'TOKEN_TIMEOUT_MARGIN', 0)
    expiration = expiration - datetime.timedelta(seconds=margin)
    if settings.USE_TZ and timezone.is_naive(expiration):
        # Presumes that the Keystone is using UTC.
        expiration = timezone.make_aware(expiration, timezone.utc)
    return expiration > timezone.now()
Exemplo n.º 18
0
    def save(self, *args, **kwargs):
        if self.name is None:
            self.name = ""
        self.name = self.name[:150]

        if self.description is None:
            self.description = ""
        self.description = self.description[:20000]

        # Validate the color_index field, correct if necessary
        if self.color_index is None or self.color_index == '':
            self.color_index = self.EVENT_COLORS_KEYS[0]
        try:
            assert int(self.color_index) in range(len(self.EVENT_COLORS_KEYS))
        except (ValueError, AssertionError):
            self.color_index = self.EVENT_COLORS_KEYS[0]

        made_aware = False
        if timezone.is_naive(self.start):
            made_aware = True

        self.start = ensure_timezone_awareness(self.start, self.timezone)
        self.end = ensure_timezone_awareness(self.end, self.timezone)

        super(GEvent, self).save(*args, **kwargs)

        if made_aware:
            print "Made datetime timezone aware for GEvent {} with id {}".format(self.name, self.id)
Exemplo n.º 19
0
    def last_reminder_tzaware(self):
        if self.last_reminder is not None and timezone.is_naive(self.last_reminder):
            logging.warning("Loaded a user_profile.last_reminder for user %s that's not tz-aware: %s"
                              % (self.email, self.last_reminder))
            return self.last_reminder.replace(tzinfo=timezone.utc)

        return self.last_reminder
Exemplo n.º 20
0
def cru_feedback(feedback_id, assembly, feedback_obj):
    try:
        feedback = Feedback.objects.get(appcivist_id=feedback_id)
        if feedback_obj:
            if feedback_obj["up"]:
                feedback.value = 1
            elif feedback_obj["down"]:
                feedback.value = -1
        feedback.sync = False
        feedback.save()
        return feedback
    except Feedback.DoesNotExist:
        # author_id = vote_obj.authorId if hasattr(vote_obj, 'authorId') else vote_obj.memberId
        author_id = feedback_obj["userId"]
        author = cru_author(author_id, assembly)
        #feedback_ac_dt = parse(feedback_obj["creation"])
        feedback_ac_dt = datetime.strptime(feedback_obj["creation"].replace(" GMT", ""), "%Y-%m-%d %H:%M %p")
        feedback_dt = _get_timezone_aware_datetime(feedback_ac_dt) if timezone.is_naive(feedback_ac_dt) else feedback_ac_dt
        
        if feedback_obj["up"]:
            feedback_value = 1
        elif feedback_obj["down"]:
            feedback_value = -1
        feedback_parent_type = feedback_obj["parentType"]
        feedback_parent_id = feedback_obj["contributionId"]
        feedback = Feedback(appcivist_id=feedback_obj["id"], value=feedback_value, datetime=feedback_dt, 
                            author=author, sync=False, parent_type=feedback_parent_type)
        if feedback_parent_type == 'idea':
            feedback.parent_idea = cru_idea(feedback_parent_id, assembly)
        else:
            feedback.parent_comment = cru_comment(feedback_parent_id, idea)
        feedback.save()
        return feedback
Exemplo n.º 21
0
 def set_param(self, parname, value, fullparname=None,
               example_value=None):
     try:
         param = self.get_param(parname)
     except ObjectDoesNotExist:
         param = self.blank_param()
         param.parameterset = self.parameterset
         param.name = self._get_create_parname(parname, fullparname,
             example_value=example_value)
         #param.string_value = value
         #param.save()
     if param.name.isNumeric():
         param.numerical_value = float(value)
     elif param.name.isDateTime():
         if settings.USE_TZ:
             if (is_naive(value)):
                 value = make_aware(value, LOCAL_TZ)
             param.datetime_value = value
         else:
             if (is_aware(value)):
                 value = make_naive(value, LOCAL_TZ)
             param.datetime_value = value
     else:
         param.string_value = unicode(value)
     param.save()
     return param.id
Exemplo n.º 22
0
    def _test_file_time_getter_tz_handling_off(self, getter):
        # Django's TZ (and hence the system TZ) is set to Africa/Algiers which
        # is UTC+1 and has no DST change. We can set the Django TZ to something
        # else so that UTC, Django's TIME_ZONE, and the system timezone are all
        # different.
        now_in_algiers = timezone.make_aware(datetime.now())

        with timezone.override(timezone.get_fixed_timezone(-300)):
            # At this point the system TZ is +1 and the Django TZ
            # is -5.
            self.assertFalse(self.storage.exists('test.file.tz.off'))

            f = ContentFile('custom contents')
            f_name = self.storage.save('test.file.tz.off', f)
            self.addCleanup(self.storage.delete, f_name)
            dt = getter(f_name)
            # dt should be naive, in system (+1) TZ
            self.assertTrue(timezone.is_naive(dt))

            # The three timezones are indeed distinct.
            naive_now = datetime.now()
            algiers_offset = now_in_algiers.tzinfo.utcoffset(naive_now)
            django_offset = timezone.get_current_timezone().utcoffset(naive_now)
            utc_offset = timezone.utc.utcoffset(naive_now)
            self.assertGreater(algiers_offset, utc_offset)
            self.assertLess(django_offset, utc_offset)

            # dt and naive_now should be the same effective time.
            self.assertLess(abs(dt - naive_now), timedelta(seconds=2))
            # If we convert dt to an aware object using the Algiers
            # timezone then it should be the same effective time to
            # now_in_algiers.
            _dt = timezone.make_aware(dt, now_in_algiers.tzinfo)
            self.assertLess(abs(_dt - now_in_algiers), timedelta(seconds=2))
Exemplo n.º 23
0
def convert_date(datestr, tz=False):
    """
    Takes datestr, parses it into a date if possible, then makes it timezone aware if lacking
    NB: it assumes the current timezone if a string that maps to a naive datetime is provided
    
    @param datestr: <str> or <datetime> - something which we can try resolve a date from
    @keyword tz: <timezone> or "local" or "utc" - the timezone to assume the date is in.
                 If already timezone aware, will convert it to that timezone
                 If none specified, will use LOCALTIME. 
    
    @return: <Django datetime> timezone-aware date, in the timezone you have specified
    """
    try:
        if isinstance(datestr,(datetime, datetime_dumb)): #First if the item is already a datetime object, don't try to resolve it!!
            datecon = datestr
        else: #Is a string, try to resolve it
            datecon = parse(datestr, fuzzy=True, default=None) #MJTB - 2017-04-12 - Bugfixed to ensure parser doesn't buff out
        if datecon:
            #Check if timezone needs to be made aware:
            if not tz or tz=="local": #DEFAULT TO LOCAL!!
                tz = get_current_timezone()
            elif str(tz).lower()=="utc":
                tz = utc
            if is_naive(datecon):
                #Need to make this timezone aware now!
                datecon = make_aware(datecon, tz)
            else: #Is already timezone aware, so need to convert to the specified timezone
                datecon = localtime(datecon, tz)
            return datecon
        else:
            return False
    except KeyError:
        return False
Exemplo n.º 24
0
 def process_request(self, request):
     """
     Verify that the session should be considered active. We check
     the start time and the last activity time to determine if this
     is the case. We set the last activity time to now() if the session
     is still active.
     """
     if (
         self.START_TIME_KEY not in request.session or
         self.LAST_ACTIVITY_KEY not in request.session or
         timezone.is_naive(request.session[self.START_TIME_KEY]) or
         timezone.is_naive(request.session[self.LAST_ACTIVITY_KEY])
     ):
         self.process_new_session(request)
     else:
         self.process_existing_session(request)
Exemplo n.º 25
0
    def put_csv(self, content, unit, form_id, station_name):
        reader = csv.DictReader(content, delimiter=b';')
        print station_name
        try:
            station = MeasuringPoint.objects.get(form_id=form_id)
        except MeasuringPoint.DoesNotExist:
            self.stderr.write(u'MeasuringPoint "{0}"" not found'.format(station_name))
            return
        unit_key = unit.lower().replace('.', '')
        for row in reader:
            dateRow = row.get('Datum Zeit', '')
            if len(dateRow):
                try:
                    date = parser.parse(dateRow, dayfirst=True)
                    if timezone.is_naive(date):
                        try:
                            date = timezone.make_aware(date, self.tz)
                        except NonExistentTimeError as e:
                            self.stderr.write(str(e))
                except ValueError:
                    self.stderr.write('Failed to parse date "{0}"'.format(dateRow))
                    continue

                value = row[(' ' + station_name + ' ' + unit).encode('iso-8859-1')].strip()

                if value.find(',') > -1:
                    value = float(value.replace(',', '.'))
                is_float = isinstance(value, float)
                if is_float or (value.find('g/m') == -1 and value.find('n. def.') == -1):
                    IndicatedValue.objects.update_or_create(date_created=date,
                                measuring_point=station, defaults={unit_key: float(value)})
Exemplo n.º 26
0
    def _occurrences_after_generator(self, after=None):
        """
        returns a generator that produces unpresisted occurrences after the
        datetime ``after``. (Optionally) This generator will return up to
        ``max_occurences`` occurrences or has reached ``self.end_recurring_period``, whichever is smallest.
        """

        tzinfo = timezone.utc
        if after is None:
            after = timezone.now()
        elif not timezone.is_naive(after):
            tzinfo = after.tzinfo
        rule = self.get_rrule_object(tzinfo)
        if rule is None:
            if self.end > after:
                yield self._create_occurrence(self.start, self.end)
            return
        date_iter = iter(rule)
        difference = self.end - self.start
        loop_counter = 0
        for o_start in date_iter:
            o_start = tzinfo.localize(o_start)
            if self.end_recurring_period and self.end_recurring_period and o_start > self.end_recurring_period:
                break
            o_end = o_start + difference
            if o_end > after:
                yield self._create_occurrence(o_start, o_end)

            loop_counter += 1
Exemplo n.º 27
0
def grouped_totals(entries):
    select = {"day": {"date": """DATE_TRUNC('day', end_time)"""},
              "week": {"date": """DATE_TRUNC('week', end_time)"""}}
    weekly = entries.extra(select=select["week"]).values('date', 'billable')
    weekly = weekly.annotate(hours=Sum('hours')).order_by('date')
    daily = entries.extra(select=select["day"]).values('date', 'project__name',
                                                       'billable')
    daily = daily.annotate(hours=Sum('hours')).order_by('date',
                                                        'project__name')
    weeks = {}
    for week, week_entries in groupby(weekly, lambda x: x['date']):
        try:
            if timezone.is_naive(week):
                week = timezone.make_aware(week,
                    timezone.get_current_timezone())
        except AttributeError:
            week = datetime.datetime.combine(week,
                timezone.get_current_timezone())
        weeks[week] = get_hours(week_entries)
    days = []
    last_week = None
    for day, day_entries in groupby(daily, lambda x: x['date']):
        week = get_week_start(day)
        if last_week and week > last_week:
            yield last_week, weeks.get(last_week, {}), days
            days = []
        days.append((day, daily_summary(day_entries)))
        last_week = week
    yield week, weeks.get(week, {}), days
Exemplo n.º 28
0
 def fix(arg):
     if isinstance(arg, datetime.datetime):
         if is_naive(arg):
             warnings.warn("Received a naive datetime (%s) while timezone support is active." % arg, RuntimeWarning)
             arg = make_aware(arg, timezone.get_default_timezone())
         arg = arg.astimezone(utc).replace(tzinfo=None)
     return arg
Exemplo n.º 29
0
    def __init__(self, param, cursor, strings_only=False):
        # With raw SQL queries, datetimes can reach this function
        # without being converted by DateTimeField.get_db_prep_value.
        if settings.USE_TZ and isinstance(param, datetime.datetime):
            if timezone.is_naive(param):
                warnings.warn(u"Oracle received a naive datetime (%s)"
                              u" while time zone support is active." % param,
                              RuntimeWarning)
                default_timezone = timezone.get_default_timezone()
                param = timezone.make_aware(param, default_timezone)
            param = param.astimezone(timezone.utc).replace(tzinfo=None)

        if hasattr(param, 'bind_parameter'):
            self.smart_str = param.bind_parameter(cursor)
        else:
            self.smart_str = convert_unicode(param, cursor.charset,
                                             strings_only)
        if hasattr(param, 'input_size'):
            # If parameter has `input_size` attribute, use that.
            self.input_size = param.input_size
        elif isinstance(param, basestring) and len(param) > 4000:
            # Mark any string param greater than 4000 characters as a CLOB.
            self.input_size = Database.CLOB
        else:
            self.input_size = None
Exemplo n.º 30
0
    def __init__(self, param, cursor, strings_only=False):
        # With raw SQL queries, datetimes can reach this function
        # without being converted by DateTimeField.get_db_prep_value.
        if settings.USE_TZ and isinstance(param, datetime.datetime):
            if timezone.is_naive(param):
                warnings.warn("Oracle received a naive datetime (%s)"
                              " while time zone support is active." % param,
                              RuntimeWarning)
                default_timezone = timezone.get_default_timezone()
                param = timezone.make_aware(param, default_timezone)
            param = param.astimezone(timezone.utc).replace(tzinfo=None)

        # Oracle doesn't recognize True and False correctly in Python 3.
        # The conversion done below works both in 2 and 3.
        if param is True:
            param = "1"
        elif param is False:
            param = "0"
        if hasattr(param, 'bind_parameter'):
            self.force_bytes = param.bind_parameter(cursor)
        elif isinstance(param, six.memoryview):
            self.force_bytes = param
        else:
            self.force_bytes = convert_unicode(param, cursor.charset,
                                             strings_only)
        if hasattr(param, 'input_size'):
            # If parameter has `input_size` attribute, use that.
            self.input_size = param.input_size
        elif isinstance(param, six.string_types) and len(param) > 4000:
            # Mark any string param greater than 4000 characters as a CLOB.
            self.input_size = Database.CLOB
        else:
            self.input_size = None
Exemplo n.º 31
0
def get_field_value(field, model):
    if field.remote_field is None:
        value = field.pre_save(model, add=model.pk is None)

        # Make datetimes timezone aware
        # https://github.com/django/django/blob/master/django/db/models/fields/__init__.py#L1394-L1403
        if isinstance(value, datetime.datetime) and settings.USE_TZ:
            if timezone.is_naive(value):
                default_timezone = timezone.get_default_timezone()
                value = timezone.make_aware(
                    value, default_timezone).astimezone(timezone.utc)
            # convert to UTC
            value = timezone.localtime(value, timezone.utc)

        if is_protected_type(value):
            return value
        else:
            return field.value_to_string(model)
    else:
        return getattr(model, field.get_attname())
Exemplo n.º 32
0
    def save(self, commit=True):
        time = self.cleaned_data['time']

        inst = self.instance

        newjobs = []
        for date in self.get_dates(self.cleaned_data):
            dt = datetime.datetime.combine(date, time)
            if is_naive(dt):
                dt = gdtz().localize(dt)
            job = RecuringJob.objects.create(type=inst.type,
                                             slots=inst.slots,
                                             time=dt)
            newjobs.append(job)
            job.save()

        # create new objects
        # HACK: admin expects a saveable object to be returned when commit=False
        # return newjobs[-1]
        return inst
Exemplo n.º 33
0
def get_modified_time(storage, name):
    """
    Get modified time from storage, ensuring the result is a timezone-aware
    datetime.
    """
    try:
        try:
            # Prefer Django 1.10 API and fall back to old one
            modified_time = storage.get_modified_time(name)
        except AttributeError:
            modified_time = storage.modified_time(name)
    except OSError:
        return 0
    except NotImplementedError:
        return None
    if modified_time and timezone.is_naive(modified_time):
        if getattr(settings, 'USE_TZ', False):
            default_timezone = timezone.get_default_timezone()
            return timezone.make_aware(modified_time, default_timezone)
    return modified_time
Exemplo n.º 34
0
def from_current_timezone(value):
    """
    When time zone support is enabled, convert naive datetimes
    entered in the current time zone to aware datetimes.
    """
    if settings.USE_TZ and value is not None and timezone.is_naive(value):
        current_timezone = timezone.get_current_timezone()
        try:
            return timezone.make_aware(value, current_timezone)
        except Exception as exc:
            raise ValidationError(
                _(
                    "%(datetime)s couldn't be interpreted "
                    "in time zone %(current_timezone)s; it "
                    "may be ambiguous or it may not exist."
                ),
                code="ambiguous_timezone",
                params={"datetime": value, "current_timezone": current_timezone},
            ) from exc
    return value
Exemplo n.º 35
0
def update_daily_stats(wc_id=1, dte=None):
    try:
        wc = Webcam.objects.get(pk=wc_id)
    except Webcam.DoesNotExist:
        logger.warning('no webcam found for: %d' % wc_id)
        return None
    if dte is None:
        dte = datetime.now()
    tz = timezone.get_current_timezone()
    if timezone.is_naive(dte):
        dte = timezone.make_aware(dte, tz)
    for_date = datetime(year=dte.year, month=dte.month, day=dte.day)
    logger.debug('lookup snapshot: %s %s' % (wc, for_date))
    ds = SnapshotDailyStat.lookup(webcam=wc, for_date=for_date)
    if ds is None:
        ds = SnapshotDailyStat.objects.create(webcam=wc, for_date=for_date)
    logger.debug('updating snapshot: %s' % ds)
    snaps = wc.snaps_for_day(for_date)
    cnt = snaps.count() if snaps else 0
    am = None
    pm = None
    chg = False
    noon = timezone.make_aware(
        datetime(for_date.year, for_date.month, for_date.day, 12, 0, 0), tz)
    if cnt > 0:
        am = snaps.filter(ts_create__lt=noon)
        pm = snaps.filter(ts_create__gt=noon)
    if am and am.count() > 0:
        chg = True
        ds.am_count = am.count()
        ds.am_start = am.earliest('ts_create').ts_create
        ds.am_end = am.latest('ts_create').ts_create
    if pm and pm.count() > 0:
        chg = True
        ds.pm_count = pm.count()
        ds.pm_start = pm.earliest('ts_create').ts_create
        ds.pm_end = pm.latest('ts_create').ts_create
    if chg:
        ds.save()
    logger.debug('snapshot update complete: %s' % ds)
    return ds
Exemplo n.º 36
0
    def testTimezone2(self):
        from dateutil import zoneinfo
        tz = zoneinfo.gettz(settings.TIME_ZONE)
        _USE_TZ = settings.USE_TZ
        settings.USE_TZ = False
        try:
            self.assertEqual(settings.USE_TZ, False)

            username = '******'
            password = '******'
            user = User.objects.create(
                username=username,
                email='*****@*****.**',
                is_active=True,
                is_staff=True,
                is_superuser=True,
            )
            user.set_password(password)
            user.save()

            client = Client()
            ret = client.login(username=username, password=password)
            self.assertTrue(ret)

            j = Job.objects.get(id=1)
            next_run = j.next_run
            print('next_run:', next_run)
            self.assertTrue(timezone.is_naive(next_run))
            try:
                #astimezone() cannot be applied to a naive datetime
                timezone.make_naive(j.next_run, timezone=tz)
                self.assertTrue(0)
            except ValueError:
                pass
            j.save()

            response = client.get('/admin/chroniker/job/')
            self.assertEqual(response.status_code, 200)

        finally:
            settings.USE_TZ = _USE_TZ
Exemplo n.º 37
0
def cru_idea(idea_id, initiative, idea_obj=None):
    try:
        idea = Idea.objects.get(ideascale_id=idea_id)
        if idea_obj:
            idea.title = idea_obj.title
            idea.text = idea_obj.text
            idea.positive_votes = idea_obj.upVoteCount if idea_obj.upVoteCount > 0 else 0
            idea.negative_votes = idea_obj.downVoteCount if idea_obj.downVoteCount > 0 else 0
            idea.comments = idea_obj.commentCount if idea_obj.commentCount > 0 else 0
            idea.campaign = cru_campaign(idea_obj.campaignId, initiative)
        idea.sync = False
        idea.save()
        return idea
    except Idea.DoesNotExist:
        if not idea_obj:
            idea_obj = get_ideascale_data(initiative, 'get_idea_details',
                                          {'ideaId': idea_id})
        author = cru_author(idea_obj.authorId, initiative, idea_obj.authorInfo)
        location = cru_location(idea_obj.locationInfo) if hasattr(
            idea_obj, 'locationInfo') else None
        campaign_idea = cru_campaign(idea_obj.campaignId, initiative)
        positive_votes = idea_obj.upVoteCount if idea_obj.upVoteCount > 0 else 0
        negative_votes = idea_obj.downVoteCount if idea_obj.downVoteCount > 0 else 0
        comments = idea_obj.commentCount if idea_obj.commentCount > 0 else 0
        idea_is_dt = parse(idea_obj.creationDateTime)
        idea_dt = _get_timezone_aware_datetime(
            idea_is_dt) if timezone.is_naive(idea_is_dt) else idea_is_dt
        idea = Idea(ideascale_id=idea_obj.id,
                    title=idea_obj.title,
                    text=idea_obj.text,
                    datetime=idea_dt,
                    positive_votes=positive_votes,
                    negative_votes=negative_votes,
                    comments=comments,
                    campaign=campaign_idea,
                    url=idea_obj.url,
                    user=author,
                    location=location,
                    sync=False)
        idea.save()
        return idea
Exemplo n.º 38
0
async def get_or_create_discord_user(member):
    """
    Searches in database for the given user or creates one if not found.

    Parameters
    ----------
    member: :class:`abc.User`
        Discord API abstract user.

    Returns
    -------
    user: :class:`models.DiscordUser`
        The user fetched.
    """

    if not hasattr(member, 'premium_since'):  # pragma: no cover
        premium_since = None
    else:
        premium_since = member.premium_since

    created_at = member.created_at
    if is_naive(created_at):
        created_at = make_aware(created_at)

    # Importing DiscordUser inside a function to avoid 'Apps not ready'
    DiscordUser = apps.get_model('bot.DiscordUser')

    defaults = {
        'nick': member.display_name,
        'code': member.discriminator,
        'avatar_url': member.avatar_url or None,
        'locale': settings.LANGUAGE_CODE or None,
        'premium': True if premium_since else False,
        'created_at': created_at
    }
    user, created = await async_get_or_create(DiscordUser, id=member.id, defaults=defaults)

    if created:
        logging.info('User %s created', user)

    return user
Exemplo n.º 39
0
def do_timezone(value, arg):
    """
    Convert a datetime to local time in a given time zone.

    The argument must be an instance of a tzinfo subclass or a time zone name.

    Naive datetimes are assumed to be in local time in the default time zone.
    """
    if not isinstance(value, datetime):
        return ''

    # Obtain a timezone-aware datetime
    try:
        if timezone.is_naive(value):
            default_timezone = timezone.get_default_timezone()
            value = timezone.make_aware(value, default_timezone)
    # Filters must never raise exceptions, and pytz' exceptions inherit
    # Exception directly, not a specific subclass. So catch everything.
    except Exception:
        return ''

    # Obtain a tzinfo instance
    if isinstance(arg, tzinfo):
        tz = arg
    elif isinstance(arg, str):
        try:
            tz = pytz.timezone(arg)
        except pytz.UnknownTimeZoneError:
            return ''
    else:
        return ''

    result = timezone.localtime(value, tz)

    # HACK: the convert_to_local_time flag will prevent
    #       automatic conversion of the value to local time.
    result = datetimeobject(result.year, result.month, result.day,
                            result.hour, result.minute, result.second,
                            result.microsecond, result.tzinfo)
    result.convert_to_local_time = False
    return result
Exemplo n.º 40
0
    def _checkrep(self):
        """Verify consistency of attributes and history"""

        #First, basic attributes.
        if not self.visited:
            msg = "UL {0} _checkrep() detected errored state".format(self.pk)
            logger.error(msg)
            raise CheckRepError(msg)

        #Second, compare action history with attributes
        decoded_history = self.hist2list()
        first = decoded_history[0]
        
        #If VISITING is the first history event then visited should be true
        #and vice-versa.
        if self.visited:
            if first[1] != 'VISITING':
                logger.warning("UL _checkrep() detected errored state")
                return False
        if first[1] == 'VISITING':
            if self.visited == False:
                logger.warning("UL _checkrep() detected errored state")
                return False
        #if the last history event is 'COMPLETING' then completed should be true
        last = decoded_history.pop()
        if last[1] == 'COMPLETING':
            if self.completed == False:
                logger.warning("UL _checkrep() detected errored state")
                return False
        
        #history events should be a member of the set 
        #[VISITING, COMPLETING, REOPENING]
        #Datetimes should be timezone aware.
        for event in decoded_history:
            if event[1] not in ULActions:
                logger.warning("UL _checkrep() detected errored state")
                return False
            if is_naive(event[0]):
                logger.warning("UL _checkrep() detected naive timezone)")
                return False   
        return True
Exemplo n.º 41
0
def naive_date(*args, **kwargs):
    u"""
    Returns ``date`` representing the same day as the date part of the given day and time.
    The day and time may be given as a naive ``datetime`` or a combination of ``date`` and
    ``time``. Both ``date`` and ``time`` may be given in their expanded forms.
    The time part of the given value is ignored.

    Usage:
        naive_date(naive_datetime)
        naive_date(date, time)
        naive_date(date, [hour], [minute], [second], [microsecond])
        naive_date(year, month, day, time)
        naive_date(year, month, day, [hour], [minute], [second], [microsecond])

    Where ``datetime``, ``date`` and ``time`` may be given formatted as strings or as the
    respective objects. Similarly all other values may be given as numbers or strings containing
    numbers.
    """
    dt = _datetime_factory(*args, **kwargs)
    assert timezone.is_naive(dt)
    return dt.date()
Exemplo n.º 42
0
    def test_get_created_time(self):
        naive_date = datetime(2017, 1, 2, 3, 4, 5, 678)
        aware_date = timezone.make_aware(naive_date, timezone.utc)

        self.storage._bucket = mock.MagicMock()
        blob = mock.MagicMock()
        blob.time_created = aware_date
        self.storage._bucket.get_blob.return_value = blob

        with self.settings(TIME_ZONE='America/Montreal', USE_TZ=False):
            mt = self.storage.get_created_time(self.filename)
            self.assertTrue(timezone.is_naive(mt))
            naive_date_montreal = timezone.make_naive(aware_date)
            self.assertEqual(mt, naive_date_montreal)
            self.storage._bucket.get_blob.assert_called_with(self.filename)

        with self.settings(TIME_ZONE='America/Montreal', USE_TZ=True):
            mt = self.storage.get_created_time(self.filename)
            self.assertTrue(timezone.is_aware(mt))
            self.assertEqual(mt, aware_date)
            self.storage._bucket.get_blob.assert_called_with(self.filename)
 def new_param(self, parname, value, fullparname=None):
     param = self.blank_param()
     param.parameterset = self.parameterset
     param.name = self._get_create_parname(parname, fullparname)
     param.string_value = value
     param.save()
     if param.name.isNumeric():
         param.numerical_value = float(value)
     elif param.name.isDateTime():
         if settings.USE_TZ:
             if (is_naive(value)):
                 value = make_aware(value, LOCAL_TZ)
             param.datetime_value = value
         else:
             if (is_aware(value)):
                 value = make_naive(value, LOCAL_TZ)
             param.datetime_value = value
     else:
         param.string_value = unicode(value)
     param.save()
     return param.id
Exemplo n.º 44
0
    def process_bind_param(self, value, dialect):
        if value is None:
            return None
        if isinstance(value, basestring):
            value = value.replace('T', ' ').replace('Z', '+00:00')
            p = re.compile('([0-9: -]+)\.?([0-9]*)([\+-][0-9]{2}:?[0-9]{2})?')
            dt, ms, tz = p.match(value).groups()
            value = datetime.datetime.strptime(dt, '%Y-%m-%d %H:%M:%S')
            if tz:
                pol, h, m = tz[0], int(tz[1:3]), int(tz[-2:])
                delta = datetime.timedelta(hours=h, minutes=m)
                if pol == '+':
                    value -= delta
                else:
                    value += delta

        if is_naive(value):
            value = make_aware(value, self.tz)
        else:
            value = value.replace(tzinfo=self.tz)
        return value.strftime('%Y-%m-%d %H:%M:%S')
    def map_value(self, old_value):
        old_value = super(EducateDateTimeMapping, self).map_value(old_value)

        # Ain't no localized None
        if old_value is None:
            return old_value

        assert isinstance(old_value, datetime)

        if timezone.is_naive(old_value):
            try:
                return self.tz.localize(old_value)
            except AmbiguousTimeError:
                logger.warning(
                    u"Ambiguous datetime '%s' encountered, assuming DST.",
                    old_value
                )

                return self.tz.localize(old_value, is_dst=True)

        return old_value
Exemplo n.º 46
0
    def _get_occurrence_list(self, start, end):
        """
        returns a list of occurrences for this event from start to end.
        """
        difference = (self.end - self.start)
        if self.rule is not None:
            use_naive = timezone.is_naive(start)

            # Use the timezone from the start date
            tzinfo = timezone.utc
            if start.tzinfo:
                tzinfo = start.tzinfo

            occurrences = []
            if self.end_recurring_period and self.end_recurring_period < end:
                end = self.end_recurring_period

            rule = self.get_rrule_object(tzinfo)
            start = (start - difference).replace(tzinfo=None)
            end = (end - difference).replace(tzinfo=None)
            o_starts = []
            o_starts.append(rule.between(start, end, inc=True))
            o_starts.append(rule.between(start - (difference // 2), end - (difference // 2), inc=True))
            o_starts.append(rule.between(start - difference, end - difference, inc=True))
            for occ in o_starts:
                for o_start in occ:
                    o_start = tzinfo.localize(o_start)
                    if use_naive:
                        o_start = timezone.make_naive(o_start, tzinfo)
                    o_end = o_start + difference
                    occurrence = self._create_occurrence(o_start, o_end)
                    if occurrence not in occurrences:
                        occurrences.append(occurrence)
            return occurrences
        else:
            # check if event is in the period
            if self.start < end and self.end > start:
                return [self._create_occurrence(self.start)]
            else:
                return []
Exemplo n.º 47
0
    def __init__(self, param, cursor, strings_only=False):
        # With raw SQL queries, datetimes can reach this function
        # without being converted by DateTimeField.get_db_prep_value.
        if settings.USE_TZ and (isinstance(param, datetime.datetime) and
                                not isinstance(param, Oracle_datetime)):
            if timezone.is_naive(param):
                warnings.warn("Oracle received a naive datetime (%s)"
                              " while time zone support is active." % param,
                              RuntimeWarning)
                default_timezone = timezone.get_default_timezone()
                param = timezone.make_aware(param, default_timezone)
            param = Oracle_datetime.from_datetime(param.astimezone(timezone.utc))

        string_size = 0
        # Oracle doesn't recognize True and False correctly in Python 3.
        # The conversion done below works both in 2 and 3.
        if param is True:
            param = 1
        elif param is False:
            param = 0
        if hasattr(param, 'bind_parameter'):
            self.force_bytes = param.bind_parameter(cursor)
        elif isinstance(param, Database.Binary):
            self.force_bytes = param
        else:
            # To transmit to the database, we need Unicode if supported
            # To get size right, we must consider bytes.
            self.force_bytes = convert_unicode(param, cursor.charset,
                                             strings_only)
            if isinstance(self.force_bytes, six.string_types):
                # We could optimize by only converting up to 4000 bytes here
                string_size = len(force_bytes(param, cursor.charset, strings_only))
        if hasattr(param, 'input_size'):
            # If parameter has `input_size` attribute, use that.
            self.input_size = param.input_size
        elif string_size > 4000:
            # Mark any string param greater than 4000 characters as a CLOB.
            self.input_size = Database.CLOB
        else:
            self.input_size = None
Exemplo n.º 48
0
def cru_feedback(feedback_id, assembly, feedback_obj):
    try:
        feedback = Feedback.objects.get(appcivist_id=feedback_id)
        if feedback_obj:
            if feedback_obj["up"]:
                feedback.value = 1
            elif feedback_obj["down"]:
                feedback.value = -1
        feedback.sync = False
        feedback.save()
        return feedback
    except Feedback.DoesNotExist:
        # author_id = vote_obj.authorId if hasattr(vote_obj, 'authorId') else vote_obj.memberId
        author_id = feedback_obj["userId"]
        author = cru_author(author_id, assembly)
        #feedback_ac_dt = parse(feedback_obj["creation"])
        feedback_ac_dt = datetime.strptime(
            feedback_obj["creation"].replace(" GMT", ""), "%Y-%m-%d %H:%M %p")
        feedback_dt = _get_timezone_aware_datetime(
            feedback_ac_dt) if timezone.is_naive(
                feedback_ac_dt) else feedback_ac_dt

        if feedback_obj["up"]:
            feedback_value = 1
        elif feedback_obj["down"]:
            feedback_value = -1
        feedback_parent_type = feedback_obj["parentType"]
        feedback_parent_id = feedback_obj["contributionId"]
        feedback = Feedback(appcivist_id=feedback_obj["id"],
                            value=feedback_value,
                            datetime=feedback_dt,
                            author=author,
                            sync=False,
                            parent_type=feedback_parent_type)
        if feedback_parent_type == 'idea':
            feedback.parent_idea = cru_idea(feedback_parent_id, assembly)
        else:
            feedback.parent_comment = cru_comment(feedback_parent_id, idea)
        feedback.save()
        return feedback
Exemplo n.º 49
0
    def _value_from_db(self, value, field, field_kind, db_type):
        """
        Converts a database type to a type acceptable by the field.

        If you encoded a value for storage in the database, reverse the
        encoding here. This implementation only recursively deconverts
        elements of collection fields and handles embedded models.

        You may want to call this method after any back-end specific
        deconversions.

        :param value: A value to be passed to the database driver
        :param field: A field having the same properties as the field
                      the value comes from
        :param field_kind: Equal to field.get_internal_type()
        :param db_type: Same as creation.db_type(field)

        Note: lookup values never get deconverted.
        """

        # We did not convert Nones.
        if value is None:
            return None

        # Deconvert items or values of a collection field.
        if field_kind in ('ListField', 'SetField', 'DictField',):
            value = self._value_from_db_collection(value, field,
                                                   field_kind, db_type)

        # Reinstatiate a serialized model.
        elif field_kind == 'EmbeddedModelField':
            value = self._value_from_db_model(value, field,
                                              field_kind, db_type)

        elif field_kind in ('DateTimeField',):

            if value is not None and settings.USE_TZ and timezone.is_naive(value):
                value = value.replace(tzinfo=timezone.utc)

        return value
Exemplo n.º 50
0
    def is_valid(self, bundle, request=None):
        from django.utils.timezone import is_naive
        errors = {}

        for field in ['start', 'end']:
            data = bundle.data.get(field)
            if not data.get('date', None) and not data.get('datetime', None):
                errors['__all__'] = [
                    "One of 'date' and 'datetime' must be set."
                ]
            elif data.get('date', None) and data.get('datetime', None):
                errors['__all__'] = [
                    "Only one of 'date' and 'datetime' must be set. The 'date' field is used for all-day events."
                ]
            elif data.get('datetime', None) and is_naive(
                    parse(data.get('datetime'))) and not data.get(
                        'timezone', None):
                errors['datetime'] = [
                    "A timezone offset is required if not specified in the 'timezone' field"
                ]

        return errors
Exemplo n.º 51
0
 def coerce(self, value):
     combined = False
     if isinstance(value, datetime.datetime):
         value = value
     elif isinstance(value, datetime.date):
         combined = True
         value = datetime.datetime.combine(value, self.combine_datetime)
     elif isinstance(value, string_types) and self.strptime is not None:
         value = datetime.datetime.strptime(value, self.strptime)
     elif isinstance(value, string_types) and value.strip():
         try:
             value = parser.parse(value)
         except OverflowError as e:
             try:
                 value = datetime.datetime.strptime(value, '%Y%m%d%H%M%S')
             except ValueError:
                 raise e
     elif isinstance(value, (list, tuple)):
         value = datetime.datetime(*value)
     elif isinstance(value, (int, float)):
         value = datetime.datetime.fromtimestamp(value)
     else:
         raise TypeError('Cannot convert {0} to datetime instance'.format(
             type(value)))
     if settings.USE_TZ and timezone.is_naive(value):
         try:
             tz = timezone.get_current_timezone()
             if combined:
                 # If we combined the date with datetime.time.min we
                 # should adjust by dst to get the correct datetime
                 value += tz.dst(value)
             value = timezone.make_aware(value,
                                         timezone.get_current_timezone())
         except NonExistentTimeError:
             value = timezone.get_current_timezone().localize(value)
         value = timezone.utc.normalize(value)
     elif not settings.USE_TZ and timezone.is_aware(value):
         value = timezone.make_naive(value, timezone.get_current_timezone())
     return value
Exemplo n.º 52
0
def from_current_timezone(value):
    """
    When time zone support is enabled, convert naive datetimes
    entered in the current time zone to aware datetimes.
    """
    if settings.USE_TZ and value is not None and timezone.is_naive(value):
        current_timezone = timezone.get_current_timezone()
        try:
            return timezone.make_aware(value, current_timezone)
        except Exception:
            message = _(
                '%(datetime)s couldn\'t be interpreted '
                'in time zone %(current_timezone)s; it '
                'may be ambiguous or it may not exist.'
            )
            params = {'datetime': value, 'current_timezone': current_timezone}
            six.reraise(ValidationError, ValidationError(
                message,
                code='ambiguous_timezone',
                params=params,
            ), sys.exc_info()[2])
    return value
Exemplo n.º 53
0
    def is_meet_delete_time(self, bucket):
        """
        归档的桶是否满足删除时间要求,即是否可以清理

        :param bucket: Archive()
        :return:
            True    # 满足
            False   # 不满足
        """
        archive_time = bucket.archive_time.replace(tzinfo=None)

        if timezone.is_aware(self._clear_datetime):
            if not timezone.is_aware(archive_time):
                archive_time = timezone.make_aware(archive_time)
        else:
            if not timezone.is_naive(archive_time):
                archive_time = timezone.make_naive(archive_time)

        if archive_time < self._clear_datetime:
            return True

        return False
Exemplo n.º 54
0
def _parse(partial_dt):
    """
    parse a partial datetime object to a complete datetime object
    """
    dt = None
    try:
        if isinstance(partial_dt, datetime):
            dt = partial_dt
        elif isinstance(partial_dt, date):
            dt = _combine_date_time(partial_dt, time(0, 0, 0))
        elif isinstance(partial_dt, time):
            dt = _combine_date_time(date.today(), partial_dt)
        elif isinstance(partial_dt, (int, float)):
            dt = datetime.fromtimestamp(partial_dt)
        elif isinstance(partial_dt, (str, bytes)):
            dt = parser.parse(partial_dt, default=timezone.now())

        if dt is not None and timezone.is_naive(dt):
            dt = timezone.make_aware(dt)
        return dt
    except ValueError:
        return None
Exemplo n.º 55
0
def cru_vote(vote_id, initiative, vote_obj):
    try:
        vote = Vote.objects.get(ideascale_id=vote_id)
        if vote_obj:
            if hasattr(vote_obj, 'voteValue'):
                vote.value = vote_obj.voteValue
            elif hasattr(vote_obj, 'myVote'):
                vote.value = vote_obj.myVote
            if hasattr(vote_obj, 'ideaType'):
                vote.parent_type = vote_obj.ideaType
        vote.sync = False
        vote.save()
        return vote
    except Vote.DoesNotExist:
        author_id = vote_obj.authorId if hasattr(
            vote_obj, 'authorId') else vote_obj.memberId
        author = cru_author(author_id, initiative)
        vote_is_dt = parse(vote_obj.creationDateTime) if hasattr(
            vote_obj, 'creationDateTime') else parse(vote_obj.creationDate)
        vote_dt = _get_timezone_aware_datetime(
            vote_is_dt) if timezone.is_naive(vote_is_dt) else vote_is_dt
        vote_parent_type = vote_obj.ideaType if hasattr(vote_obj,
                                                        'ideaType') else 'idea'
        vote_value = vote_obj.voteValue if hasattr(
            vote_obj, 'voteValue') else vote_obj.myVote
        vote_parent_id = vote_obj.ideaId if hasattr(vote_obj,
                                                    'ideaId') else vote_obj.id
        vote = Vote(ideascale_id=vote_obj.id,
                    value=vote_value,
                    datetime=vote_dt,
                    author=author,
                    sync=False,
                    parent_type=vote_parent_type)
        if vote_parent_type == 'idea':
            vote.parent_idea = cru_idea(vote_parent_id, initiative)
        else:
            vote.parent_comment = cru_comment(vote_parent_id, initiative)
        vote.save()
        return vote
Exemplo n.º 56
0
async def get_or_create_discord_text_channel(channel, guild):
    """
    Searches in database for the given text channel or creates one if not found.

    Parameters
    ----------
    channel: :class:`discord.channel.TextChannel`
        Discord API TextChannel.
    guild: :class:`discord.Guild`
        Discord API Guild.

    Returns
    -------
    text_channel: :class:`models.DiscordTextChannel`
        The text channel fetched.
    """

    created_at = channel.created_at
    if is_naive(created_at):
        created_at = make_aware(created_at)

    # Importing DiscordTextChannel inside a function to avoid 'Apps not ready'
    DiscordTextChannel = apps.get_model('bot.DiscordTextChannel')

    defaults = {
        'name': channel.name,
        'position': channel.position,
        'nsfw': channel.is_nsfw(),
        'topic': channel.topic or None,
        'news': channel.is_news(),
        'created_at': created_at,
        'server_id': guild.id
    }
    text_channel, created = await async_get_or_create(DiscordTextChannel, id=channel.id, defaults=defaults)

    if created:
        logging.info('Text Channel %s created', text_channel)

    return text_channel
Exemplo n.º 57
0
    def aware_datetime(self, value):
        """Converts a date or datetime or timestamp to an aware datetime.

        Naive datetimes are assumed to be in Django's current_timezone.
        Dates are interpreted as midnight that date, in Django's current_timezone.
        Integers are interpreted as POSIX timestamps (which are inherently UTC).

        Anything else (e.g., str) is returned unchanged, which won't be portable.
        """
        if isinstance(value, datetime):
            dt = value
        else:
            if isinstance(value, date):
                dt = datetime(value.year, value.month, value.day)  # naive, midnight
            else:
                try:
                    dt = datetime.utcfromtimestamp(value).replace(tzinfo=utc)
                except (TypeError, ValueError):
                    return value
        if is_naive(dt):
            dt = make_aware(dt, get_current_timezone())
        return dt
Exemplo n.º 58
0
def get_user_tos_accepted_date(user):
    """ Gets the datetime the user accepted this portals ToS, or None if they have not accepted it yet. 
        @return: a Datetime object or None
    """
    from cosinnus.models.group import CosinnusPortal
    portal = CosinnusPortal.get_current()
    portal_dict_or_date = user.cosinnus_profile.settings.get(
        'tos_accepted_date', None)
    if portal_dict_or_date is None:
        if check_user_has_accepted_any_tos(user):
            # if user has accepted some ToS, but we don't know when, set it to in the past for this portal
            portal_dict_or_date = {
                str(portal.id): datetime.datetime(1999, 1, 1, 13, 37, 0,
                                                  pytz.utc)
            }
            user.cosinnus_profile.settings[
                'tos_accepted_date'] = portal_dict_or_date
            user.cosinnus_profile.save(update_fields=['settings'])
            portal_dict_or_date = user.cosinnus_profile.settings.get(
                'tos_accepted_date', None)
        else:
            return None
    if type(portal_dict_or_date) is not dict:
        # the old style setting for this only had a datetime saved, convert it to the modern
        # dict version of {<portal_id>: datetime, ...}
        portal_dict_or_date = {str(portal.id): portal_dict_or_date}
        user.cosinnus_profile.settings[
            'tos_accepted_date'] = portal_dict_or_date
        user.cosinnus_profile.save(update_fields=['settings'])

    datetime_or_none = portal_dict_or_date.get(str(portal.id), None)
    if datetime_or_none is not None:
        if not type(datetime_or_none) is datetime.datetime:
            datetime_or_none = parser.parse(datetime_or_none)
        # we had a phase where we had unaware default datetimes saved, so backport-make them aware
        if is_naive(datetime_or_none):
            datetime_or_none = pytz.utc.localize(datetime_or_none)
    return datetime_or_none
Exemplo n.º 59
0
    def _format_parameters(self,
                           parameters,
                           operation,
                           return_only_param=False):
        select_update = False
        if re.match(r'^(SELECT|UPDATE) ', operation):
            select_update = True

        new_parameters = []
        parameters = list(parameters)
        for index in range(len(parameters)):
            # With raw SQL queries, datetimes can reach this function
            # without being converted by DateTimeField.get_db_prep_value.
            if settings.USE_TZ and isinstance(parameters[index],
                                              datetime.datetime):
                param = parameters[index]
                if timezone.is_naive(param):
                    warnings.warn(
                        "Received a naive datetime (%s)"
                        " while time zone support is active." % param,
                        RuntimeWarning)
                    default_timezone = timezone.get_default_timezone()
                    param = timezone.make_aware(param, default_timezone)
                param = param.astimezone(timezone.utc).replace(tzinfo=None)
                parameters[index] = param

            need_quote = ''
            if (select_update and isinstance(parameters[index], Decimal)):
                operation = self._replacenth(operation, '%s',
                                             parameters[index],
                                             len(new_parameters), need_quote)
            else:
                new_parameters.append(parameters[index])

        if return_only_param:
            return tuple(new_parameters)

        return tuple(new_parameters), operation
Exemplo n.º 60
0
    def _convert_value(self, value, model_instance, add):
        """
        Converts the value to the appropriate timezone and time as declared by
        the `time_override` and `populate_from` attributes.
        """

        if not value:
            return value

        # Retrieve the default timezone as the default
        tz = get_default_timezone()

        # If populate_from exists, override the default timezone
        if self.populate_from is not None:
            tz = self._get_populate_from(model_instance)

        if is_naive(value):
            value = make_aware(value=value, timezone=tz)

        # Convert the value to a datetime object in the correct timezone. This
        #   insures that we will have the correct date if we are performing a
        #   time override below.
        value = value.astimezone(tz)

        # Do not convert the time to the time override if auto_now or
        #   auto_now_add is set
        if self.time_override is not None and not (self.auto_now or
                                                   (self.auto_now_add
                                                    and add)):
            # Retrieve the time override
            time_override = self._get_time_override()

            # Convert the value to the date/time with the appropriate timezone
            value = make_aware(value=datetime.combine(date=value.date(),
                                                      time=time_override),
                               timezone=tz)

        return value