Beispiel #1
0
def get_local_day_min(dt=None, year=None, month=None, day=None, utc=True):
    """讲时间转换成 本地时0点 并转换成UTC
    :param dt:
    :param navie:
    :return:
    """
    if dt is None:
        tm = timezone.now()
    elif timezone.is_aware(dt):
        tm = timezone.make_naive(dt)
    else:
        tm = dt
    if year is not None:
        tm = tm.replace(year=year)
    if month is not None:
        tm = tm.replace(month=month)
    if day is not None:
        tm = tm.replace(day=day)
    tm = datetime.datetime.combine(tm, datetime.time.min)
    # 计算后的tm没有tzinfo
    tm = timezone.make_aware(tm)
    if utc:
        tm = timezone.make_naive(tm, timezone.utc)
        return tm.replace(tzinfo=timezone.utc)
    else:
        return tm
Beispiel #2
0
  def clean(self):
    """Clean form."""
    cleaned_data = super(EventForm, self).clean()

    start_date = cleaned_data.get('start_date')
    start_time = cleaned_data.get('start_time')
    end_date = cleaned_data.get('end_date')
    end_time = cleaned_data.get('end_time')
    space = cleaned_data.get('space')

    if space is not None and space.timezone is not None:
      tz = timezone(space.timezone)
    else:
      tz = timezone(settings.TIME_ZONE)

    # We need to handle a special case here, because midnight is incorrectly
    # considered as False in Python < 3.5. This test allows users to set a "00:00"
    # value for start_time and end_time fields.
    if start_date and (start_time or start_time == time(0,0,0)):
      start = make_aware(datetime.combine(start_date, start_time), tz)
      cleaned_data['start'] = make_naive(start, timezone(settings.TIME_ZONE))

    if end_date and (end_time or end_time == time(0,0,0)):
      end = make_aware(datetime.combine(end_date, end_time), tz)
      cleaned_data['end'] = make_naive(end, timezone(settings.TIME_ZONE))

    return cleaned_data
Beispiel #3
0
    def clean(self):
        """Clean form."""
        super(EventForm, self).clean()

        cdata = self.cleaned_data

        cdata["budget_bug"] = cdata.get("budget_bug_form", None)
        cdata["swag_bug"] = cdata.get("swag_bug_form", None)
        if self.editable_owner:
            cdata["owner"] = cdata.get("owner_form", None)
        else:
            cdata["owner"] = self.instance.owner

        # Set timezone
        t = timezone(cdata["timezone"])
        if "start_form" in cdata:
            start = make_naive(cdata["start_form"], timezone(settings.TIME_ZONE))
            cdata["start"] = t.localize(start)

        if "end_form" in cdata:
            end = make_naive(cdata["end_form"], timezone(settings.TIME_ZONE))
            cdata["end"] = t.localize(end)

        # Directly write to self.errors as
        # ValidationError({'start_form': ['Error message']}) doesn't
        # seem to work.
        if cdata["start"] >= cdata["end"]:
            self.errors["start_form"] = u"Start date should come " "before end date."
            raise ValidationError({"start_form": ["Error"]})

        return cdata
Beispiel #4
0
    def get_calendar(self, start, end, target_date, end_date):
        calendar = self.get_raw_calendar()
        search_start = start.replace(':', '')
        search_end = end.replace(':', '')
        events = self.event_set.filter(room=self, start_ts__gte=target_date, end_ts__lte=end_date)
        print

        for event in events:
            start_wtz = timezone.make_naive(event.start_ts, timezone.get_current_timezone())
            end_wtz = timezone.make_naive(event.end_ts, timezone.get_current_timezone())
            starts = start_wtz.strftime('%H%M')
            ends = end_wtz.strftime('%H%M')
            for block in calendar:
                id = block['mil_hour'] + block['minutes']
                if int(starts) <= int(id) and int(id) <= int(ends):
                    block['status'] = 'reserved'
                else:
                    block['status'] = 'available'

        for block in calendar:
            id = block['mil_hour'] + block['minutes']
            if int(search_start) <= int(id) and int(id) <= int(search_end):
                block['status'] = 'searched'

        return calendar
Beispiel #5
0
    def save(self):
        if not self._id:
            query = ('INSERT INTO `issues_issues` ('
                     '`title`, `description`, `create_time`, `update_time`, '
                     '`assigned_to_user_id`, `created_by_user_id`, `priority_id`, `status_id`) '
                     'VALUES (%s, %s, %s, %s, %s, %s, %s, %s)')
            args = (
                self._title,
                self._description,
                timezone.make_naive(self._create_time, timezone.utc),
                timezone.make_naive(self._update_time, timezone.utc) if self._update_time else None,
                self._assigned_to.id if self._assigned_to else None,
                self._created_by.id,
                self._priority.id,
                self._status.id
            )
        elif not self._changes:
            return
        else:
            query = 'UPDATE `issues_issues` SET `{}` = %s WHERE `id` = %s'.format(
                '` = %s, `'.join(self._changes.keys())
            )
            args = tuple(self._changes.values()) + (self.id,)
            self._changes.clear()

        c = db.cursor()
        c.execute(query, args)
        c.close()
        if not self._id:
            self._id = db.insert_id()
        db.commit()
Beispiel #6
0
def ajax_send_cur_time(request):

    def timedelta_to_seconds(td):
        return td.seconds + td.days*24*60*60

    if request.is_ajax() and 't_id' in request.GET and request.GET['t_id']:
        D = {}
        # Текущее время для текущего часового пояса (make_naive)
        tmn = timezone.make_naive(timezone.now())
        D['ctm'] = '%02i:%02i:%02i' % (tmn.hour, tmn.minute, tmn.second)
        # Достать время начала теста и определить, сколько осталось
        lm = LogMaster.objects.get(pk=request.GET['t_id'])
        # Ожидаемое время завершения (время начала + заданный интервал из Settings)
        endtime = timezone.make_naive(lm.dt) + lm.tst_id.max_time
        # Оставшееся время
        if endtime >= tmn:
            elapsed = endtime - tmn
            if timedelta_to_seconds(elapsed) > 0:
                D['etm'] = str(elapsed).split('.')[0]
            else:
                D['etm'] = 0
        else:
            D['etm'] = 0
        return JsonResponse(D)
    else:
        return JsonResponse({}, status=400)
Beispiel #7
0
    def clean(self):
        """Clean form."""
        super(EventForm, self).clean()

        cdata = self.cleaned_data

        cdata['budget_bug'] = cdata.get('budget_bug_form', None)
        cdata['swag_bug'] = cdata.get('swag_bug_form', None)
        if self.editable_owner:
            cdata['owner'] = cdata.get('owner_form', None)
        else:
            cdata['owner'] = self.instance.owner

        # Check if keys exists in cleaned data.
        if not all(k in cdata for k in ('start_form', 'end_form')):
            raise ValidationError('Please correct the form errors.')
        # Set timezone
        t = timezone(cdata['timezone'])
        start = make_naive(cdata['start_form'],
                           timezone(settings.TIME_ZONE))
        cdata['start'] = t.localize(start)
        end = make_naive(cdata['end_form'],
                         timezone(settings.TIME_ZONE))
        cdata['end'] = t.localize(end)

        if cdata['start'] >= cdata['end']:
            msg = 'Start date should come before end date.'
            self._errors['start_form'] = self.error_class([msg])

        return cdata
Beispiel #8
0
 def get_filtered(min_date=None, max_date=None, reporter_is_blocked=None, keywords=None):
     c = db.cursor()
     query = Issue._select_query
     filter = []
     filter_args = []
     if min_date:
         filter.append('`issues`.`create_time` >= TIMESTAMP(%s)')
         filter_args.append(timezone.make_naive(min_date, timezone.utc))
     if max_date:
         filter.append('`issues`.`create_time` <= TIMESTAMP(%s)')
         filter_args.append(timezone.make_naive(max_date, timezone.utc))
     if reporter_is_blocked is not None:
         filter.append('`c_u`.`is_blocked` = %s')
         filter_args.append(int(reporter_is_blocked))
     if keywords:
         filter.append('MATCH (`title`, `description`) AGAINST (%s IN BOOLEAN MODE)')
         filter_args.append(keywords)
     if filter:
         query += ' WHERE ' + ' AND '.join(filter)
     c.execute(query, filter_args)
     issues = []
     while True:
         e = c.fetchone()
         if not e:
             break
         i = Issue.from_query_result(e)
         issues.append(i)
     return issues
Beispiel #9
0
def ack_task(request, task_id):
    """Acknowledge a task, from a reminder.

    This is the page that reminder emails link to. It allows the user to set
    when the task was done, and when it is due next.
    """
    try:
        task = Task.objects.get(id=task_id)
    except ObjectDoesNotExist:
        task = None
    if not task or task.user.id != request.cym_user.id:
        return HttpResponseNotFound(_("Couldn't find this task!"))

    if task and request.method == 'POST':
        task_done = request.POST.get('done', '')
        task_due = request.POST.get('due', '')

        valid = True

        if task_done:
            try:
                task_done = dateutil.parser.parse(task_done).date()
            except ValueError:
                task_done = None
        if not task_done:
            messages.add_message(request, messages.ERROR,
                                 _("Please enter the date you performed the "
                                   "task"))
            task_done = timezone.make_naive(timezone.now()).date()
            valid = False

        if task_due:
            try:
                task_due = dateutil.parser.parse(task_due).date()
            except ValueError:
                task_due = None
        if not task_due:
            messages.add_message(request, messages.ERROR,
                                 _("Please enter the date this task is due "
                                   "next"))
            task_due = task_done + datetime.timedelta(days=task.interval_days)
            valid = False

        if valid:
            done = TaskDone(task=task, done=task_done)
            done.save()

            task.due = task_due
            task.save()

            return redirect('profile')
    else:
        task_done = timezone.make_naive(timezone.now()).date()
        task_due = task_done + datetime.timedelta(days=task.interval_days)

    return render(request, 'call_your_mom/ack_task.html',
                  {'task': task,
                   'task_done': task_done,
                   'task_due': task_due,
                   'task_is_due': task.is_due(request.cym_user.timezone)})
    def _generate_date_clause(self,start,end=None):
        """
        Helper function to generate time constraint on influx query
        to handle if using epoch time or datetime
        """
        query_str =  "time  >=  {start}s"
        if end:
            query_str = query_str + " AND time <= {end}s"

        try:
            start = int(start)
        except:
            start = start
            pass
        if isinstance(start,datetime):
            start = timezone.make_naive(start)
            if end:
                end = timezone.make_naive(end)

        if isinstance(start,date) or isinstance(start,datetime):
            query_str =  query_str.replace("{start}s","\'{start}\'")
            if end:
                query_str = query_str.replace("{end}s","\'{end}\'")



        query_str_formatted =  query_str.format(start=start, end=end)
        return query_str_formatted
Beispiel #11
0
    def __init__(self, *args, **kwargs):
        """Initialize form.

        Dynamically set choices for country field.
        """
        if 'editable_owner' in kwargs:
            self.editable_owner = kwargs['editable_owner']
            del(kwargs['editable_owner'])

        super(EventForm, self).__init__(*args, **kwargs)

        # Dynamic countries field.
        countries = product_details.get_regions('en').values()
        countries.sort()
        country_choices = ([('', "Country")] +
                           [(country, country) for country in countries])
        self.fields['country'].choices = country_choices

        # Dynamic owner field.
        if self.editable_owner:
            self.fields['owner_form'] = forms.ModelChoiceField(
                queryset=User.objects.filter(
                    userprofile__registration_complete=True,
                    groups__name='Rep'),
                empty_label='Owner', initial=self.instance.owner.pk)
        else:
            self.fields['owner_form'] = forms.CharField(
                required=False, initial=get_full_name(self.instance.owner),
                widget=forms.TextInput(attrs={'readonly': 'readonly',
                                              'class': 'input-text big'}))

        instance = self.instance
        # Dynamically set the year portion of the datetime widget
        now = datetime.now()
        start_year = getattr(self.instance.start, 'year', now.year)
        end_year = getattr(self.instance.end, 'year', now.year)
        self.fields['start_form'] = forms.DateTimeField(
            widget=SplitSelectDateTimeWidget(
                years=range(start_year, now.year + 10), minute_step=5),
            validators=[validate_datetime])
        self.fields['end_form'] = forms.DateTimeField(
            widget=SplitSelectDateTimeWidget(
                years=range(end_year, now.year + 10), minute_step=5),
            validators=[validate_datetime])
        # Make times local to venue
        if self.instance.start:
            start = make_naive(instance.local_start,
                               timezone(instance.timezone))
            self.fields['start_form'].initial = start

        if self.instance.end:
            end = make_naive(instance.local_end, timezone(instance.timezone))
            self.fields['end_form'].initial = end

        # Use of intermediate fields to translate between bug.id and
        # bug.bug_id
        if instance.budget_bug:
            self.fields['budget_bug_form'].initial = instance.budget_bug.bug_id
        if instance.swag_bug:
            self.fields['swag_bug_form'].initial = instance.swag_bug.bug_id
Beispiel #12
0
    def summarize(self, start, end):
        if not api.nova.extension_supported('SimpleTenantUsage', self.request):
            return

        if start <= end and start <= self.today:
            # The API can't handle timezone aware datetime, so convert back
            # to naive UTC just for this last step.
            start = timezone.make_naive(start, timezone.utc)
            end = timezone.make_naive(end, timezone.utc)
            try:
                self.usage_list = self.get_usage_list(start, end)
            except Exception:
                exceptions.handle(self.request,
                                  _('Unable to retrieve usage information.'))
        elif end < start:
            messages.error(self.request,
                           _("Invalid time period. The end date should be "
                             "more recent than the start date."))
        elif start > self.today:
            messages.error(self.request,
                           _("Invalid time period. You are requesting "
                             "data from the future which may not exist."))

        for project_usage in self.usage_list:
            project_summary = project_usage.get_summary()
            for key, value in project_summary.items():
                self.summary.setdefault(key, 0)
                self.summary[key] += value
Beispiel #13
0
    def clean(self):
        """Clean form."""
        super(EventForm, self).clean()

        cdata = self.cleaned_data

        cdata['budget_bug'] = cdata.get('budget_bug_form', None)
        cdata['swag_bug'] = cdata.get('swag_bug_form', None)
        if self.editable_owner:
            cdata['owner'] = cdata.get('owner_form', None)
        else:
            cdata['owner'] = self.instance.owner

        # Set timezone
        t = timezone(cdata['timezone'])
        if 'start_form' in cdata:
            start = make_naive(cdata['start_form'],
                               timezone(settings.TIME_ZONE))
            cdata['start'] = t.localize(start)

        if 'end_form' in cdata:
            end = make_naive(cdata['end_form'],
                             timezone(settings.TIME_ZONE))
            cdata['end'] = t.localize(end)

        # Directly write to self.errors as
        # ValidationError({'start_form': ['Error message']}) doesn't
        # seem to work.
        if cdata['start'] >= cdata['end']:
            self.errors['start_form'] = (u'Start date should come '
                                         'before end date.')
            raise ValidationError({'start_form': ['Error']})

        return cdata
 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)
Beispiel #15
0
 def backwards(self, orm):
     "Write your backwards methods here."
     for s in orm['sleep.Sleep'].objects.all():
         if timezone.is_aware(s.start_time):
             s.start_time = timezone.make_naive(s.start_time, t)
         if timezone.is_aware(s.end_time):
             s.end_time = timezone.make_naive(s.start_time, t)
         s.save()
Beispiel #16
0
def strdate(dt):
    tz = timezone.get_current_timezone()
    if type(dt) == datetime:
        return timezone.make_naive(dt, tz).strftime("%d/%m/%Y %H:%M")
    elif type(dt) == date:
        return timezone.make_naive(dt, tz).strftime("%d/%m/%Y")
    else:
        return dt
Beispiel #17
0
 def test_make_naive(self):
     self.assertEqual(
         timezone.make_naive(datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT), EAT),
         datetime.datetime(2011, 9, 1, 13, 20, 30))
     self.assertEqual(
         timezone.make_naive(datetime.datetime(2011, 9, 1, 17, 20, 30, tzinfo=ICT), EAT),
         datetime.datetime(2011, 9, 1, 13, 20, 30))
     with self.assertRaises(ValueError):
         timezone.make_naive(datetime.datetime(2011, 9, 1, 13, 20, 30), EAT)
Beispiel #18
0
 def test_make_aware_pytz(self):
     self.assertEqual(
         timezone.make_naive(CET.localize(datetime.datetime(2011, 9, 1, 12, 20, 30)), CET),
         datetime.datetime(2011, 9, 1, 12, 20, 30))
     self.assertEqual(
         timezone.make_naive(pytz.timezone("Asia/Bangkok").localize(datetime.datetime(2011, 9, 1, 17, 20, 30)), CET),
         datetime.datetime(2011, 9, 1, 12, 20, 30))
     with self.assertRaises(ValueError):
         timezone.make_naive(datetime.datetime(2011, 9, 1, 12, 20, 30), CET)
Beispiel #19
0
def _get_column_period_args(begin_time, end_time):
    begin_naive = make_naive(begin_time.value)
    end_naive = make_naive(end_time.value)
    return (
        begin_naive.hour,
        begin_naive.strftime('%M'),
        end_naive.hour,
        end_naive.strftime('%M'),
    )
Beispiel #20
0
def render_attached_period(begin, end):
    begin = make_naive(begin.value)
    end = make_naive(end.value)
    return format_html(
        '<div class="attached time-table__time">'
        '{begin_h}:{begin_m} &ndash; {end_h}:{end_m}</div>',
        begin_h=begin.hour, begin_m=begin.strftime(r'%M'),
        end_h=end.hour, end_m=end.strftime(r'%M'),
    )
Beispiel #21
0
    def test_make_naive(self):
        self.assertEqual(
            timezone.make_naive(datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT), EAT),
            datetime.datetime(2011, 9, 1, 13, 20, 30))
        self.assertEqual(
            timezone.make_naive(datetime.datetime(2011, 9, 1, 17, 20, 30, tzinfo=ICT), EAT),
            datetime.datetime(2011, 9, 1, 13, 20, 30))

        with self.assertRaisesMessage(ValueError, 'make_naive() cannot be applied to a naive datetime'):
            timezone.make_naive(datetime.datetime(2011, 9, 1, 13, 20, 30), EAT)
Beispiel #22
0
 def test_make_aware_pytz(self):
     self.assertEqual(
         timezone.make_naive(CET.localize(datetime.datetime(2011, 9, 1, 12, 20, 30)), CET),
         datetime.datetime(2011, 9, 1, 12, 20, 30))
     self.assertEqual(
         timezone.make_naive(
             pytz.timezone("Asia/Bangkok").localize(datetime.datetime(2011, 9, 1, 17, 20, 30)), CET
         ),
         datetime.datetime(2011, 9, 1, 12, 20, 30))
     with self.assertRaisesMessage(ValueError, 'make_naive() cannot be applied to a naive datetime'):
         timezone.make_naive(datetime.datetime(2011, 9, 1, 12, 20, 30), CET)
def between(self, after, before, **kwargs):
    if timezone.is_aware(after):
        after = timezone.make_naive(after)

    if timezone.is_aware(before):
        before = timezone.make_naive(before)

    for dt in self._between(after, before, **kwargs):
        try:
            yield timezone.make_aware(dt)
        except NonExistentTimeError:
            pass
Beispiel #24
0
 def save_extra_sessions(self):
     data = []
     for session in self.type.sessions.all():
         for extra in session.orig_sessions.all():
             prev = self.get_extra_session(extra.extra.id)
             data.append({
                 'session': extra.extra.id,
                 'start': timezone.make_naive(extra.start).strftime(settings.DATETIME_FORMAT),
                 'end': timezone.make_naive(extra.end).strftime(settings.DATETIME_FORMAT),
                 'used': prev['used'] if prev else extra.used
             })
     self.set_extra_data('extra_sessions', data)
Beispiel #25
0
def fetch_from_source(pk):#self, pk):
    #self.update_state(state='PROGRESS')

    source = OaiSource.objects.get(pk=pk)
    baseStatus = 'records'

    # Set up the OAI fetcher
    format, created = OaiFormat.objects.get_or_create(name=source.format)
    client = source.getClient()
    client._metadata_registry.registerReader('oai_dc', oai_dc_reader)
    client._metadata_registry.registerReader('base_dc', base_dc_reader)

    # Limit queries to records in a time range of 7 days (by default)
    time_chunk = QUERY_TIME_RANGE

    start_date = make_naive(source.last_update, UTC())
    current_date = make_naive(now(), UTC())
    until_date = start_date + time_chunk

    # Restrict to a set ?
    restrict_set = source.restrict_set
    if not restrict_set:
        restrict_set = None

    while start_date <= current_date:
        source.status = baseStatus+' between '+str(start_date)+' and '+str(until_date)
        source.save()

        real_until_date = until_date
        if source.day_granularity and until_date < (current_date - timedelta(days=1)):
            real_until_date -= timedelta(days=1)
        try:
            if restrict_set:
                listRecords = client.listRecords(
                        metadataPrefix=format.name,
                        from_=start_date,
                        until=real_until_date,
                        set=restrict_set)
            else:
                listRecords = client.listRecords(
                        metadataPrefix=format.name,
                        from_=start_date,
                        until=real_until_date)
        except NoRecordsMatchError:
            listRecords = []

        saveRecordList(source, format, listRecords)

        source.last_update = make_aware(min(until_date, current_date), UTC())
        source.save()
        until_date += time_chunk
        start_date += time_chunk
Beispiel #26
0
def resume_request(context, request, queryType, key):
    token = ResumptionToken.objects.get(queryType=queryType, key=key)
    if not token:
        return OaiRequestError("badResumptionToken", "This resumption token is invalid.")
    parameters = dict()
    parameters["format"] = token.metadataPrefix
    if token.set:
        parameters["sets"] = token.set
    if token.fro:
        parameters["timestamp__gte"] = make_naive(token.fro, UTC())
    if token.until:
        parameters["timestamp__lte"] = make_naive(token.until, UTC())
    return handle_list_query(request, context, queryType, parameters, token.offset)
Beispiel #27
0
 def save_extra_sessions(self):
     data = []
     for extra in self.session.orig_sessions.all():
         prev = self.get_extra_session(extra.extra.id)
         data.append(
             {
                 "session": extra.extra.id,
                 "start": timezone.make_naive(extra.start).strftime(settings.DATETIME_FORMAT),
                 "end": timezone.make_naive(extra.end).strftime(settings.DATETIME_FORMAT),
                 "used": prev["used"] if prev else extra.used,
             }
         )
     self.set_extra_data("extra_sessions", data)
Beispiel #28
0
    def _test_datetimes_equality(self, inst, response_dict):
        '''
        Ensures dtstart/dtend objects stored in inst match the serialized
        versions in response_dict.

        NOTE: Tastypie's out-of-the-box behavior returns naive ISO-
              formatted datetimes relative to settings.DATE_TIME. Thus, we
              have to convert our instance's datetimes first.
        '''
        iso_dtstart = make_naive(inst.dtstart, get_current_timezone()).isoformat()
        iso_dtend = make_naive(inst.dtend, get_current_timezone()).isoformat()
        self.assertEquals(iso_dtstart, response_dict.get('dtstart'))
        self.assertEquals(iso_dtend, response_dict.get('dtend'))
Beispiel #29
0
    def value_to_db_time(self, value):
        if value is None:
            return None

        if djangoVersion[:2] <= (1, 3):
            # SQL Anywhere doesn't support tz-aware datetimes
            if value.tzinfo is not None:
                raise ValueError("SQL Anywhere backend does not support timezone-aware datetimes.")
        else:
            if is_aware(value):
                make_naive(value, get_default_timezone())

        return str(value)
Beispiel #30
0
 def test_make_naive(self):
     self.assertEqual(
         timezone.make_naive(datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT), EAT),
         datetime.datetime(2011, 9, 1, 13, 20, 30)
     )
     self.assertEqual(
         timezone.make_naive(datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT), UTC),
         datetime.datetime(2011, 9, 1, 10, 20, 30)
     )
     self.assertEqual(
         timezone.make_naive(datetime.datetime(2011, 9, 1, 10, 20, 30, tzinfo=UTC), UTC),
         datetime.datetime(2011, 9, 1, 10, 20, 30)
     )
Beispiel #31
0
def get_other_stats(request):
    if not request.is_ajax():
        return JsonResponse({'error': 'Unauthorized access'}, status=401)

    tracks = get_tracks_from_request(request)

    if not tracks.exists():
        return JsonResponse({})

    hours = {}
    for i in range(24):
        hours[str(i)] = 0

    weekdays = {
        0: 'Lundi',
        1: 'Mardi',
        2: 'Mercredi',
        3: 'Jeudi',
        4: 'Vendredi',
        5: 'Samedi',
        6: 'Dimanche'
    }
    days = {}
    for weekday in weekdays.values():
        days[weekday] = 0

    deltas = []
    prev = minimum = maximum = None
    min_1 = min_2 = max_1 = max_2 = None
    for track in tracks:
        dt = make_naive(track.datetime)
        hours[str(dt.hour)] += 1
        days[weekdays[dt.weekday()]] += 1
        if prev:
            # Store the diff between the previous track and this one
            delta = prev.datetime - track.datetime
            deltas.append(delta)
            # Keep min and max with their respective datetime
            if not minimum or delta < minimum:
                minimum = delta
                min_1 = track.datetime
                min_2 = prev.datetime
            if not maximum or delta > maximum:
                maximum = delta
                max_1 = track.datetime
                max_2 = prev.datetime

        prev = track

    delta_stats = None
    if deltas:
        format = '%d/%m/%y %H:%M'
        delta_stats = {
            'deltaMin':
            format_html('<b>{}</b> <br><small>Entre le {} et le {}</small>',
                        minimum, min_1.strftime(format),
                        min_2.strftime(format)),
            'deltaAvg':
            str(sum(deltas, timedelta(0)) / len(deltas)),
            'deltaMax':
            format_html('<b>{}</b> <br><small>Entre le {} et le {}</small>',
                        maximum, max_1.strftime(format),
                        max_2.strftime(format))
        }

    return JsonResponse({
        'trackByHourChart': {
            'labels': list(x + 'h' for x in hours.keys()),
            'values': list(hours.values())
        },
        'trackByDayChart': {
            'labels': list(days.keys()),
            'values': list(days.values())
        },
        'deltaStats': delta_stats
    })
Beispiel #32
0
def import_registrations(f):
    """Import registration data from Google Forms csv.
    
    Usage
        f = open('my_data.csv', newline='')
        import_registrations(f)
    
    The date of the last registration is cached in the SIGNUP_IMPORT_LAST_TSTAMP 
    setting to make it safe to repeatedly import the same data without creating
    duplicate registrations.
    
    Input stream should be opened with newline='' or muti-line entries will not be parsed correctly.
    
    .. todo:: Pay attention to timezones when parsing dates. See https://docs.djangoproject.com/en/1.11/topics/i18n/timezones/
    """

    import csv
    from collections import namedtuple
    import datetime

    R = namedtuple('CsvMap', field_names='name')
    csv_map = {
        'tstamp': R('Timestamp', ),
        'first_name': R('First Name', ),
        'last_name': R('Last Name', ),
        'gender': R('Gender', ),  # Male, Female
        'age': R('Age', ),
        'rank': R('Rank', ),
        'instructor': R('Instructor', ),
        'phone_number': R("Phone number", ),
        'email': R(['Email Address', 'Username'], ),
        'parent': R('Name of parent or guardian (competitors under 18 years)'),
        'events': R('Events', ),
        'teammates': R('Teammates for Team Kata'),
        'reg_date': R('Timestamp', ),
        #paid = models.BooleanField(default=False)
        #paidDate = models.DateField('Date paid', blank=True, null=True)
        'notes': R('Notes', ),
    }
    #'City': 'Cambridge'
    #'Name of parent or guardian (competitors under 18 years)': 'Ann Pratt'
    #'Address': '', 'Postal Code': 'N1R 5J8'
    #'Province': 'Ontario', 'Email': '', 'Rank': 'Purple (4th kyu)'

    c = csv.DictReader(f)

    t_min = config.SIGNUP_IMPORT_LAST_TSTAMP
    if timezone.is_aware(t_min):
        t_min = timezone.make_naive(t_min)
    t_max = t_min
    added = 0
    skipped = 0
    line = 1  # Since first row is header
    for row in c:

        line += 1
        err_str = " while parsing line {}:\n{}".format(line, row)

        # Parse datestamp.
        tstamp = dateutil.parser.parse(row[csv_map['tstamp'].name])
        if timezone.is_aware(tstamp):
            tstamp = timezone.make_naive(tstamp)

        if tstamp <= t_min:
            skipped += 1
            continue
        t_max = max(t_max, tstamp)
        p = Person()
        events = []
        for (field, field_info) in csv_map.items():

            found = False
            field_names = field_info.name
            if isinstance(field_names, str):
                field_names = [field_names]
            for fn in field_names:
                if fn in row:
                    v = row[fn]
                    found = True
                    break
            if not found:
                if field in ('teammates', ):
                    continue
                else:
                    raise ValueError('Unable to find ' + field_info.name +
                                     err_str)

            if field == 'rank':
                v = Rank.parse(v)
            elif field == 'events':
                # Delimiter used to be ", ", now ":"
                if ';' in v:
                    v = v.split(';')
                else:
                    v = v.split(", ")
                events = [Event.objects.get(name=e) for e in v]
                continue
            elif field == 'reg_date':
                v = tstamp
            elif field == 'gender':
                if v == 'Male':
                    v = 'M'
                elif v == 'Female':
                    v = 'F'
                else:
                    raise ValueError("Unexpected gender {}{}".format(
                        v, err_str))
            else:
                pass
            setattr(p, field, v)
        try:
            p.full_clean()
        except ValidationError as e:
            raise ValueError("Validation error{}:\n{}".format(err_str, e))
        p.save()
        for e in events:
            el = EventLink(person=p, event=e)
            el.update_division()
            el.save()
        added += 1
    config.SIGNUP_IMPORT_LAST_TSTAMP = t_max

    return {"added": added, "skipped": skipped}
Beispiel #33
0
 def event_date(self, obj):
     event_date = timezone.make_naive(obj.start)
     return _date(event_date, "l d b Y, G:i")
Beispiel #34
0
def _fix_value(value):
    if isinstance(value, datetime.datetime):
        if timezone.is_aware(value):
            value = timezone.make_naive(value, timezone.utc)
    return value
Beispiel #35
0
 def get_minute(self, instance):
     return make_naive(instance.value).strftime(r'%M')
Beispiel #36
0
 def get_hour(self, instance):
     return make_naive(instance.value).strftime(r'%H')
Beispiel #37
0
 def get_day(self, instance):
     return make_naive(instance.value).strftime(r'%d')
Beispiel #38
0
 def get_month(self, instance):
     return make_naive(instance.value).strftime(r'%m')
Beispiel #39
0
 def modified_time(self, name):
     return make_naive(self.meta(name)["LastModified"], utc)
Beispiel #40
0
 def get_modified_time(self, name):
     timestamp = self.meta(name)["LastModified"]
     return timestamp if settings.USE_TZ else make_naive(timestamp)
Beispiel #41
0
 def __str__(self):
     return str(make_naive(self.value))
Beispiel #42
0
 def modified_time(self, name):
     """Returns a naive datetime object containing the last modified time."""
     # If USE_TZ=False then get_modified_time will return a naive datetime
     # so we just return that, else we have to localize and strip the tz
     mtime = self.get_modified_time(name)
     return mtime if is_naive(mtime) else make_naive(mtime)
Beispiel #43
0
 def modified_time(self, name):
     name = self._normalize_name(clean_name(name))
     blob = self._get_blob(self._encode_name(name))
     return timezone.make_naive(blob.updated)
Beispiel #44
0
    def set_value(self, value):
        """
        Sets the parameter value, converting into the appropriate data type.
        Deals with date/time strings that are timezone naive or aware, based
        on the USE_TZ setting.

        :param value: a string (or string-like) representing the value
        :return:
        """
        if self.name.isNumeric():
            self.numerical_value = float(value)
        elif self.name.isDateTime():
            # We convert the value string into datetime object.
            # dateutil.parser detects and converts many date formats and is
            # quite permissive in what it accepts (form validation and API
            # input validation happens elsewhere and may be less permissive)
            datevalue = dateutil.parser.parse(value)
            if settings.USE_TZ and is_naive(datevalue):
                datevalue = make_aware(datevalue, LOCAL_TZ)
            elif not settings.USE_TZ and is_aware(datevalue):
                datevalue = make_naive(datevalue, LOCAL_TZ)
            self.datetime_value = datevalue
        elif self.name.isLink():
            # Always store the raw value as a string, even if setting
            # the GenericForeignKey via link_id/link_ct
            if str(value) == '' or value is None:
                return
            self.string_value = unicode(value)

            try:
                # We detect experiment or dataset view URLs
                # (eg, /experiment/view/12345/ or /api/v1/dataset/456)
                # and extract values to populate link_ct and link_id. This
                # covers two common cases, allowing LINK Parameters to be
                # properly created via the REST API.

                match = resolve(value)
                if match.view_name == u'api_dispatch_detail':
                    model_name = match.kwargs.get(u'resource_name', None)
                    if model_name not in ('experiment', 'dataset'):
                        model_name, pk = None, None
                    else:
                        pk = match.kwargs.get('pk', None)
                elif match.view_name.endswith('view_experiment'):
                    model_name = 'experiment'
                    pk = match.kwargs.get('experiment_id')
                elif match.view_name.endswith('view_dataset'):
                    model_name = 'dataset'
                    pk = match.kwargs.get('dataset_id')
                else:
                    model_name, pk = None, None

                if pk is not None and model_name is not None:
                    self.link_id = pk
                    self.link_ct = ContentType.objects.get(
                        app_label='tardis_portal', model=model_name.lower())
            except (ValueError, IndexError, Resolver404):
                # If we were unable to successfully match the url to model
                # instance, return an error. For any URL the URL Parameter
                # type should be used.
                raise SuspiciousOperation('Link parameter could not be set '
                                          'from string: %s' % str(value))
        else:
            self.string_value = unicode(value)
Beispiel #45
0
 def localize_datetime(self, val):
     if val is not None:
         event_tz = pytz.timezone(self.tz)
         return timezone.make_naive(val.astimezone(event_tz), event_tz)
     else:
         return None
def value_clean(value, options={}):
    "Takes a value and cleans it up (so e.g. it has timezone working right)"
    # Lazy-eval functions get eval'd.
    if isinstance(value, Promise):
        value = text_type(value)
    # Callables get called.
    if not options.get('is_django_function', False) and callable(value) and not isinstance(value, ModelBase):
        # Datetime.datetime.now is special, as we can access it from the eval
        # context (and because it changes all the time; people will file bugs otherwise).
        if value == datetime.datetime.now:
            return "datetime.datetime.now"
        elif value == datetime.datetime.utcnow:
            return "datetime.datetime.utcnow"
        elif value == datetime.date.today:
            return "datetime.date.today"
        # In case we use Django's own now function, revert to datetime's
        # original one since we'll deal with timezones on our own.
        elif timezone and value == timezone.now:
            return "datetime.datetime.now"
        # All other callables get called.
        value = value()
    # Models get their own special repr()
    if isinstance(value, ModelBase):
        # If it's a proxy model, follow it back to its non-proxy parent
        if getattr(value._meta, "proxy", False):
            value = value._meta.proxy_for_model
        return "orm['%s.%s']" % (value._meta.app_label, value._meta.object_name)
    # As do model instances
    if isinstance(value, Model):
        if options.get("ignore_dynamics", False):
            raise IsDefault
        return "orm['%s.%s'].objects.get(pk=%r)" % (value.__class__._meta.app_label, value.__class__._meta.object_name, value.pk)
    # Make sure Decimal is converted down into a string
    if isinstance(value, decimal.Decimal):
        value = str(value)
    # in case the value is timezone aware
    datetime_types = (
        datetime.datetime,
        datetime.time,
        datetime_safe.datetime,
    )
    if (timezone and isinstance(value, datetime_types) and
            getattr(settings, 'USE_TZ', False) and
            value is not None and timezone.is_aware(value)):
        default_timezone = timezone.get_default_timezone()
        value = timezone.make_naive(value, default_timezone)
    # datetime_safe has an improper repr value
    if isinstance(value, datetime_safe.datetime):
        value = datetime.datetime(*value.utctimetuple()[:7])
    # converting a date value to a datetime to be able to handle
    # timezones later gracefully
    elif isinstance(value, (datetime.date, datetime_safe.date)):
        value = datetime.datetime(*value.timetuple()[:3])
    # Now, apply the converter func if there is one
    if "converter" in options:
        value = options['converter'](value)
    # Return the final value
    if options.get('is_django_function', False):
        return value
    else:
        return repr(value)
Beispiel #47
0
 def get_modified_time(self, name):
     name = self._normalize_name(clean_name(name))
     blob = self._get_blob(name)
     updated = blob.updated
     return updated if setting('USE_TZ') else timezone.make_naive(updated)
Beispiel #48
0
 def test_make_naive_no_tz(self):
     self.assertEqual(
         timezone.make_naive(
             datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT)),
         datetime.datetime(2011, 9, 1, 5, 20, 30))
Beispiel #49
0
def _make_naive(value):
    if settings.USE_TZ and timezone.is_aware(value):
        default_timezone = timezone.get_default_timezone()
        value = timezone.make_naive(value, default_timezone)
    return value
Beispiel #50
0
from django.utils import timezone

t = timezone.get_current_timezone()

for s in Sleep.objects.all():
    if timezone.is_aware(s.start_time): s.start_time = timezone.make_naive(s.start_time, t)
    if timezone.is_aware(s.end_time): s.end_time = timezone.make_naive(s.end_time,t)
    s.save()
Beispiel #51
0
    def get_json_data(self):
        q = {'event_type': self.event_type.value}
        try:
            # filter out old entries
            date_from = int(self.request.GET['from']
                            ) + 1  # offset because of timestamp rounding
            q['date_created__gt'] = timezone.make_aware(
                datetime.datetime.fromtimestamp(date_from), timezone.utc)
        except (ValueError, KeyError):
            pass

        contestant = self.object
        try:
            latest_date = contestant.corrections.filter(
                event_type=self.event_type.value).latest().date_created_utc
        except contest.models.ContestantCorrection.DoesNotExist:
            latest_date = None
        # we order by date_created *asc* as it will be *prepended* on the frontend
        corrections_qs = contestant.corrections.select_related(
            'author').filter(**q).order_by('date_created').distinct()
        corr_tpl = get_template('correction/chunk-correction.html')
        corrections = [
            corr_tpl.render({'corr': corr}) for corr in corrections_qs
        ]
        changes = {}
        latest_correction = corrections_qs.last()
        if latest_correction and latest_correction.changes:
            changes = latest_correction.changes

        # presence, through redis
        online_users = [self.request.user]
        online_alive = False
        try:
            self_pk = self.request.user.pk
            timeout = settings.CORRECTION_LIVE_UPDATE_TIMEOUT
            now = timezone.make_naive(timezone.now(), timezone.utc).timestamp()
            moments_ago = now - timeout
            key = settings.CORRECTION_LIVE_UPDATE_REDIS_KEY.format(
                key=contestant.pk)
            client = StrictRedis(**settings.PROLOGIN_UTILITY_REDIS_STORE)
            client.expire(
                key, timeout * 2
            )  # garbage collect the whole set after a bit if no further updates
            client.zadd(key, {self_pk: now})  # add self to the set
            members = client.zrangebyscore(
                key, moments_ago, '+inf')  # list people that are recent enough
            members = set(int(pk)
                          for pk in members) - {self_pk}  # exclude self
            online_users.extend(
                User.objects.filter(pk__in=members).order_by('pk'))
            online_alive = True
        except Exception:
            # it's not worth trying harder
            pass

        user_tpl = get_template('correction/chunk-online-user.html')
        online_users = [(user.pk, user_tpl.render({'user': user}))
                        for user in online_users]

        return {
            'changes': changes,
            'corrections': corrections,
            'online': online_users,
            'online_alive': online_alive,
            'from': latest_date,
            'delay': 1000 * settings.CORRECTION_LIVE_UPDATE_POLL_INTERVAL,
        }
Beispiel #52
0
 def activity_time(self):
     atime = self.created_at
     if is_aware(self.created_at):
         atime = make_naive(atime, pytz.utc)
     return atime
Beispiel #53
0
 def get_initial(self):
     return {'publish_at': make_naive(self.object.publish_at)\
         .strftime(BROWSER_DATETIME_FORMAT)}
 def modified_time(self, name):
     """Returns a naive datetime object containing the last modified time."""
     mtime = self.get_modified_time(name)
     if timezone.is_naive(mtime):
         return mtime
     return timezone.make_naive(mtime)
Beispiel #55
0
 def make_naive(value):
     if getattr(settings, "USE_TZ", False):
         default_tz = timezone.get_default_timezone()
         value = timezone.make_naive(value, default_tz)
     return value
Beispiel #56
0
 def get_modified_time(self, name):
     dt = tz.make_aware(parse_ts(self._get_key(name).last_modified), tz.utc)
     return dt if setting('USE_TZ') else tz.make_naive(dt)
Beispiel #57
0
def details(request, id):
    event = get_object_or_404(SuggestedEvent, pk=id)
    if event.user != request.user:
        return http.HttpResponseBadRequest('Not your event')

    try:
        discussion = SuggestedDiscussion.objects.get(event=event)
    except SuggestedDiscussion.DoesNotExist:
        discussion = None

    if request.method == 'POST':
        form = forms.DetailsForm(request.POST, instance=event)
        if form.is_valid():
            event = form.save()
            # the start_time comes to us as a string, e.g. '2014-01-01
            # 12:00:00' and that'll be converted into '2014-01-01
            # 12:00:00 tzinfo=UTC' automatically. But that's not what we want
            # so we change it first.
            event.start_time = tz_apply(event.start_time,
                                        pytz.timezone(event.location.timezone))
            event.save()
            next_url = reverse('suggest:placeholder', args=(event.pk, ))

            if form.cleaned_data['enable_discussion']:
                if discussion:
                    # make sure it's enabled
                    discussion.enabled = True
                    # discussion.moderate_all = (
                    #     event.privacy != Event.PRIVACY_COMPANY
                    # )
                    discussion.save()
                else:
                    discussion = SuggestedDiscussion.objects.create(
                        event=event,
                        enabled=True,
                        notify_all=True,
                        # moderate_all=event.privacy != Event.PRIVACY_COMPANY
                    )
                if request.user not in discussion.moderators.all():
                    discussion.moderators.add(request.user)

                next_url = reverse('suggest:discussion', args=(event.pk, ))

            elif SuggestedDiscussion.objects.filter(event=event):
                discussion = SuggestedDiscussion.objects.get(event=event)
                discussion.enabled = False
                discussion.save()

            return redirect(next_url)
    else:
        if event.location and event.start_time:
            # Because the modelform is going present our user
            # without input widgets' that are datetimes in
            # naive format, when it does this is does so using the
            # settings.TIME_ZONE and when saved it applies the
            # settings.TIME_ZONE back again.
            # Normally in Django templates, this is solved with
            #  {% timezone "Europe/Paris" %}
            #    {{ form.as_p }}
            #  {% endtimezone %}
            # But that's not going to work when working with jinja
            # so we do it manually from the view code.
            event.start_time = make_naive(
                event.start_time, pytz.timezone(event.location.timezone))
        initial = {'enable_discussion': not (event and not discussion)}
        form = forms.DetailsForm(instance=event, initial=initial)

    data = {'form': form, 'event': event}
    return render(request, 'suggest/details.html', data)
Beispiel #58
0
 def modified_time(self, name):
     dt = tz.make_aware(parse_ts(self._get_key(name).last_modified), tz.utc)
     return tz.make_naive(dt)
Beispiel #59
0
def date_time_format_form(datetime_var):
    return str(
        datetime.datetime.strftime(
            make_naive(datetime_var, get_default_timezone()),
            "%d de %B de %Y a las %I:%M %p"))