def test_parse_with_timezone(self): d = datetime.utcnow().replace(tzinfo=pytz.utc) self.assertEqual(parse_iso8601(d.isoformat()), d) # 2013-06-07T20:12:51.775877+00:00 iso = d.isoformat() iso1 = iso.replace('+00:00', '-01:00') d1 = parse_iso8601(iso1) self.assertEqual(d1.tzinfo._minutes, -60) iso2 = iso.replace('+00:00', '+01:00') d2 = parse_iso8601(iso2) self.assertEqual(d2.tzinfo._minutes, +60)
def test_parse_with_timezone(self): d = datetime.utcnow().replace(tzinfo=pytz.utc) assert parse_iso8601(d.isoformat()) == d # 2013-06-07T20:12:51.775877+00:00 iso = d.isoformat() iso1 = iso.replace('+00:00', '-01:00') d1 = parse_iso8601(iso1) assert d1.tzinfo._minutes == -60 iso2 = iso.replace('+00:00', '+01:00') d2 = parse_iso8601(iso2) assert d2.tzinfo._minutes == +60 iso3 = iso.replace('+00:00', 'Z') d3 = parse_iso8601(iso3) assert d3.tzinfo == pytz.UTC
def maybe_iso8601(dt): """Either ``datetime | str -> datetime`` or ``None -> None``.""" if not dt: return if isinstance(dt, datetime): return dt return parse_iso8601(dt)
def click(request, site_id=None, tool=None): """ 2014-09-21T20:50:54 Addnow post-click hook https://confluence.gravity4.com/display/AN/AddNow+-+Tr.im+integration POST, JSON payload shortcut string Shortcut of the URL. (required) long_url string The original URL that the user was redirected to. (required) clicked_at datetime The date and time of the click event. This will be important if the post-click hook is delayed for whatever reason. (required) """ headers = {} query = {} real_ip = None service = request.GET.get('service') uuid = request.GET.get('uuid') title = request.GET.get('title') logger.info('click hook fired for site id %s (tool: %s, service: %s, uuid: %s) %s', site_id, tool, service, uuid, request.body) try: data = json.loads(request.body) except ValueError: logger.exception('json decode error') return HttpResponseBadRequest() url, clicked_at = data['long_url'], parse_iso8601(data['clicked_at']) if not (site_id and tool and url): logger.error('bad request', extra=locals()) return HttpResponseBadRequest() try: site_id = int(Site.get_id_by_hash(site_id)) except BadSignature: logger.exception("site_id can't be converted to int: %s", repr(site_id)) return HttpResponseBadRequest() query['e_c'] = constants.Events.CLICK.value query['e_n'] = url query['e_t'] = tool query['e_a'] = service or constants.SOURCE_DARK_SOCIAL if uuid: query['e_u'] = uuid if title: query['e_i'] = urllib.unquote_plus(title) if 'referrer_host' in data: headers['HTTP_REFERER'] = data['referrer_host'] if 'ip' in data: real_ip = data['ip'] track_event_task.delay(site_id, clicked_at, query, headers=headers, real_ip=real_ip) return HttpResponse(status=204)
def _get_day(day): """Get the start and end time with support for celery-encoded strings, dates, and datetimes.""" start_date = get_ad_day() if day: log.info("Got day: %s", day) if not isinstance(day, (datetime.datetime, datetime.date)): log.info("Converting day from string") day = parse_iso8601(day) start_date = day.replace(hour=0, minute=0, second=0, microsecond=0) end_date = start_date + datetime.timedelta(days=1) return (start_date, end_date)