Пример #1
0
    def prepara_certificado_txt(self, cert_txt):
        #
        # Para dar certo a leitura pelo xmlsec, temos que separar o certificado
        # em linhas de 64 caracteres de extensão...
        #
        cert_txt = cert_txt.replace('\n', '')
        cert_txt = cert_txt.replace('-----BEGIN CERTIFICATE-----', '')
        cert_txt = cert_txt.replace('-----END CERTIFICATE-----', '')

        linhas_certificado = ['-----BEGIN CERTIFICATE-----\n']
        for i in range(0, len(cert_txt), 64):
            linhas_certificado.append(cert_txt[i:i+64] + '\n')
        linhas_certificado.append('-----END CERTIFICATE-----\n')

        self.certificado = ''.join(linhas_certificado)

        cert_openssl = crypto.load_certificate(crypto.FILETYPE_PEM, self.certificado)
        self.cert_openssl = cert_openssl

        self._emissor = dict(cert_openssl.get_issuer().get_components())
        self._proprietario = dict(cert_openssl.get_subject().get_components())
        self._numero_serie = cert_openssl.get_serial_number()
        self._data_inicio_validade = datetime.strptime(cert_openssl.get_notBefore(), '%Y%m%d%H%M%SZ')
        self._data_inicio_validade = UTC.localize(self._data_inicio_validade)
        self._data_fim_validade    = datetime.strptime(cert_openssl.get_notAfter(), '%Y%m%d%H%M%SZ')
        self._data_fim_validade    = UTC.localize(self._data_fim_validade)

        for i in range(cert_openssl.get_extension_count()):
            extensao = cert_openssl.get_extension(i)
            self._extensoes[extensao.get_short_name()] = extensao.get_data()
Пример #2
0
def unixtimestamp(datetime):
    """Get unix time stamp from that given datetime. If datetime
    is not tzaware then it's assumed that it is UTC
    """
    epoch = UTC.localize(datetime.utcfromtimestamp(0))
    if not datetime.tzinfo:
        dt = UTC.localize(datetime)
    else:
        dt = UTC.normalize(datetime)
    delta = dt - epoch
    return total_seconds(delta)
Пример #3
0
def _convert_timestamp(timestamp, precision=None):
    if isinstance(timestamp, Integral):
        return timestamp  # assume precision is correct if timestamp is int

    if isinstance(_get_unicode(timestamp), text_type):
        timestamp = parse(timestamp)

    if isinstance(timestamp, datetime):
        if not timestamp.tzinfo:
            timestamp = UTC.localize(timestamp)

        ns = (timestamp - EPOCH).total_seconds() * 1e9
        if precision is None or precision == 'n':
            return ns
        elif precision == 'u':
            return ns / 1e3
        elif precision == 'ms':
            return ns / 1e6
        elif precision == 's':
            return ns / 1e9
        elif precision == 'm':
            return ns / 1e9 / 60
        elif precision == 'h':
            return ns / 1e9 / 3600

    raise ValueError(timestamp)
Пример #4
0
    def build_feed(self):
        "Build the feed given our existing URL"
        # Get all the episodes
        page_content = str(requests.get(self.url).content)
        parser = BassdriveParser()
        parser.feed(page_content)
        links = parser.get_links()

        # And turn them into something usable
        fg = FeedGenerator()
        fg.id(self.url)
        fg.title(self.title)
        fg.description(self.title)
        fg.author({'name': self.dj})
        fg.language('en')
        fg.link({'href': self.url, 'rel': 'alternate'})
        fg.logo(self.logo)

        for link in links:
            fe = fg.add_entry()
            fe.author({'name': self.dj})
            fe.title(link[0])
            fe.description(link[0])
            fe.enclosure(self.url + link[1], 0, 'audio/mpeg')

            # Bassdrive always uses date strings of
            # [yyyy.mm.dd] with 0 padding on days and months,
            # so that makes our lives easy
            date_start = link[0].find('[')
            date_str = link[0][date_start:date_start+12]
            published = datetime.strptime(date_str, '[%Y.%m.%d]')
            fe.pubdate(UTC.localize(published))
            fe.guid((link[0]))

        return fg
Пример #5
0
 def test_timezone(self):
     dt = datetime(2009, 11, 10, 23, 0, 0, 123456)
     utc = UTC.localize(dt)
     berlin = timezone('Europe/Berlin').localize(dt)
     eastern = berlin.astimezone(timezone('US/Eastern'))
     data = {
         "points": [
             {"measurement": "A", "fields": {"val": 1},
              "time": 0},
             {"measurement": "A", "fields": {"val": 1},
              "time": "2009-11-10T23:00:00.123456Z"},
             {"measurement": "A", "fields": {"val": 1}, "time": dt},
             {"measurement": "A", "fields": {"val": 1}, "time": utc},
             {"measurement": "A", "fields": {"val": 1}, "time": berlin},
             {"measurement": "A", "fields": {"val": 1}, "time": eastern},
         ]
     }
     self.assertEqual(
         line_protocol.make_lines(data),
         '\n'.join([
             'A val=1i 0',
             'A val=1i 1257894000123456000',
             'A val=1i 1257894000123456000',
             'A val=1i 1257894000123456000',
             'A val=1i 1257890400123456000',
             'A val=1i 1257890400123456000',
         ]) + '\n'
     )
Пример #6
0
 def handle(self, *args, **options):
     if Vaccine.objects.exists() or Pet.objects.exists():
         print('Pet data already loaded...exiting.')
         print(ALREADY_LOADED_ERROR_MESSAGE)
         return
     print("Creating vaccine data")
     for vaccine_name in VACCINES_NAMES:
         vac = Vaccine(name=vaccine_name)
         vac.save()
     print("Loading pet data for pets available for adoption")
     for row in DictReader(open('./pet_data.csv')):
         pet = Pet()
         pet.name = row['Pet']
         pet.submitter = row['Submitter']
         pet.species = row['Species']
         pet.breed = row['Breed']
         pet.description = row['Pet Description']
         pet.sex = row['Sex']
         pet.age = row['Age']
         raw_submission_date = row['submission date']
         submission_date = UTC.localize(
             datetime.strptime(raw_submission_date, DATETIME_FORMAT))
         pet.submission_date = submission_date
         pet.save()
         raw_vaccination_names = row['vaccinations']
         vaccination_names = [name for name in raw_vaccination_names.split('| ') if name]
         for vac_name in vaccination_names:
             vac = Vaccine.objects.get(name=vac_name)
             pet.vaccinations.add(vac)
         pet.save()
Пример #7
0
 def to_mobile_date(self, cr, uid, model_date):
     user = self.pool.get('res.users').browse(cr, uid, uid)
     local_tz = pytz.timezone(user.partner_id.tz)
     fecha = datetime.strptime(model_date, tools.DEFAULT_SERVER_DATETIME_FORMAT)
     fecha = UTC.localize(fecha, is_dst=False)
     fecha = fecha.astimezone(local_tz)
     return fecha.strftime('%d/%m/%Y %H:%M:%S')
Пример #8
0
def to_utc(dt):
    if not isinstance(dt, datetime):
        dt = datetime(dt.year, dt.month, dt.day)
    if dt.tzinfo is None:
        return UTC.localize(dt)
    else:
        return dt.astimezone(UTC)
Пример #9
0
    def load_resp(self, resp, is_download):
        """
        Loads json response from API.

        :param resp: Response from API
        :type resp: dictionary
        :param is_download: Calculates time taken based on 'updated' field in response if upload, and based on stop time if download
        :type is_download: boolean
        """

        assert isinstance(resp, dict)
        setattr(self, 'resp', resp)
        setattr(self, 'size', humanize.naturalsize(int(resp['size'])))

        if is_download:
            updated_at = datetime.now(UTC)
        else:
            updated_at = UTC.localize(datetime.strptime(resp['updated'], '%Y-%m-%dT%H:%M:%S.%fZ'))

        setattr(self, 'time_taken', dict(zip(
            ('m', 's'),
            divmod((updated_at - getattr(self, 'start_time')).seconds if updated_at > getattr(self, 'start_time') else 0, 60)
        )))

        setattr(self, 'full_path', 'gs://%s/%s' % (resp['bucket'], resp['name']))
Пример #10
0
    def load(self, timestamp):
        """Return (stored_datetime, loaded_datetime).

        The stored_datetime is the timestamp actually stored in
        Postgres, which may or may not be the timestamp we saved. The
        loaded_datetime is the timestamp we end up with using this
        method.
        """
        select = {
            'timestamp_explicit':
            "timestamp at time zone '{}'".format(self.tz.zone),
            'timestamp_stored': "timestamp at time zone 'UTC'",
        }

        loaded_attr = ('timestamp' if self.conversion == 'implicit'
                       else 'timestamp_explicit')

        qs = Timestamp.objects.extra(select=select)

        timestamp = qs.get(pk=timestamp.pk)

        stored_datetime = UTC.localize(timestamp.timestamp_stored)
        loaded_datetime = self.tz.localize(getattr(timestamp, loaded_attr))

        return stored_datetime, loaded_datetime
Пример #11
0
def list_events():
    """List events for ajax supplied start and end."""
    start = request.args.get('start', 0, type=int)
    end = request.args.get('end', 0, type=int)
    # make a datetime from the timestamp
    start = datetime.datetime.fromtimestamp(start)
    end = datetime.datetime.fromtimestamp(end)
    # Fetch the events from MongoDB
    _events = Event.query.filter(Event.timestamp >= start, Event.timestamp <= end)
    # Build the json so FullCalendar will swallow it
    events = []
    for event in _events:
        # Localize the time and get rid of the microseconds
        event_start = UTC.localize(event.timestamp).astimezone(timezone('US/Central')).replace(microsecond=0)
        # Make the event 5 minutes
        event_end = (event_start + datetime.timedelta(minutes=5)).replace(microsecond=0)
        event_dict = {'title' : event.context.description,
                      'id' : event.id,
                      'allDay' : False,
                      'start' : event_start.isoformat(),
                      'end' : event_end.isoformat(),
                      'url' : '/event/{0}'.format(event.id)} # Url for the event detail
        if event.context.status != 0:
            event_dict.update({'color' : 'red'})
        events.append(event_dict)
    return json.dumps(events)
Пример #12
0
    def on_topicinfo(self, c, e):
        model.Session.remove()
        args = e.arguments()
        log.debug(args)
        channel = args[0].lstrip(''.join(CHANNEL_PREFIXES))
        changed_by = args[1]
        changed_on = UTC.localize(datetime.utcfromtimestamp(float(args[2])))

        channel_participation_id = self.channels[channel]
        channel_participation = model.Session.query(model.ChannelParticipation) \
            .get(channel_participation_id)
        channel_info = model.Session.query(model.ChannelTopicInfo) \
                        .get(channel_participation_id)
        if not channel_info:
            log.debug('on_topicinfo ChannelTopicInfo for %s non-existant' % channel)
            channel_info = model.ChannelTopicInfo(channel_participation,
                                                  changed_by, changed_on)
        else:
            if channel_info.changed_by != changed_by or \
                channel_info.changed_on != changed_on:
                log.debug(self.__class__.__name__ + ' updating topic info for channel %s...' % channel)
                if channel_info.changed_by != changed_by:
                    channel_info.changed_by = changed_by
                if channel_info.changed_on != changed_on:
                    channel_info.changed_on = changed_on
        model.Session.save(channel_info)
        model.Session.commit()
Пример #13
0
def format_date(date, tzinfo=None):

    ### TODO: calling this function with the Plone default datetime (year=1000)
    ### will generate exceptions.  This case must be handled
    ### This can be aggrivated by requesting an article that does not exist, 
    ### through articles.xml view, among other ways.

    #Are we a datetime or a DateTime?
    if hasattr(date, 'utcdatetime'): #we are a Zope DateTime!
        date = date.utcdatetime()

    #else: #we are a python datetime!
    #    date = date

    #calling this function 'naive' will attach a UTC timezone
    if date.tzinfo is None and tzinfo is None:
        date = UTC.localize(date)

    #calling this function with a tzinfo arg will:
    elif tzinfo is not None:
        if date.tzinfo is None: #localize an existing naive datetime
            date = tzinfo.localize(date)
        else: #convert a non-naive datetime into the provided timezone
            date = date.astimezone(tzinfo)

    if date.tzinfo.tzname(date) is not None:
        return date.strftime("%a, %d %b %Y %H:%M:%S %Z")
    else: #we only have a UTC offset
        return date.strftime("%a, %d %b %Y %H:%M:%S %z")
Пример #14
0
def apply_tzdatabase_timezone(date_time, pytz_string):
    date_time = UTC.localize(date_time)
    usr_timezone = timezone(pytz_string)

    if date_time.tzinfo != usr_timezone:
        date_time = date_time.astimezone(usr_timezone)

    return date_time.replace(tzinfo=None)
def login_patch(auth, username=None, password=None):
    if ((username is None and password is None) or
        (username == 'jane' and password == 'secret')):
        auth._token = '1a2b3c'
        auth._lifespan = 3600
        auth._expires = UTC.localize(datetime.now() + timedelta(hours=1))
        return True
    raise AuthenticationFailed()
Пример #16
0
def localize_timezone(datetime, tz=None):
    if not datetime.tzinfo:
        datetime = UTC.localize(datetime)
    if not tz:
        tz = get_timezone()
    if isinstance(tz, basestring):
        tz = timezone(tz)
    return datetime.astimezone(tz)
Пример #17
0
 def _assert_release_by_creator(creator, spr):
     release_records = store.find(
         LatestPersonSourcePackageReleaseCache,
         LatestPersonSourcePackageReleaseCache.creator_id == creator.id)
     [record] = list(release_records)
     self.assertEqual(spr.creator, record.creator)
     self.assertIsNone(record.maintainer_id)
     self.assertEqual(
         spr.dateuploaded, UTC.localize(record.dateuploaded))
Пример #18
0
def apply_timezone(date_time, tz_string):
    if not date_time.tzinfo:
        date_time = UTC.localize(date_time)

    new_datetime = apply_dateparser_timezone(date_time, tz_string)

    if not new_datetime:
        new_datetime = apply_tzdatabase_timezone(date_time, tz_string)

    return new_datetime
Пример #19
0
 def __init__(self, channel_participation, type, source, message, subtype=None):
     self.channel_participation = channel_participation
     self.channel_participation_id = channel_participation.id
     self.type = type
     self.subtype = subtype
     self.source = source
     self.msg = message
     self.stamp = UTC.localize(datetime.datetime.utcnow())
     self.channel_participation.channel.update_last_entry(self.stamp)
     Session.save(self.channel_participation.channel)
Пример #20
0
 def update_index(self):
     last_index_dt = UTC.localize(datetime(*gmtime(self.last_index)[:6]))
     now = UTC.localize(datetime.utcnow())
     idx = self.get_index()
     writer = idx.writer()
     try:
         chmgr = IRCChannelManager(self.env)
         for channel in chmgr.channels():
             for line in channel.events_in_range(last_index_dt, now):
                 if line['type'] == 'comment': 
                     content = "<%s> %s"%(line['nick'], 
                             line['comment'])
                     writer.add_document(
                         channel=channel.name(),
                         timestamp=line['timestamp'].strftime(
                             self.TIMESTAMP_FORMAT),
                         content=content
                     )
                 if line['type'] == 'action':
                     content = "* %s %s"%(line['nick'], line['action'])
                     writer.add_document(
                         channel=channel.name(),
                         timestamp=line['timestamp'].strftime(
                             self.TIMESTAMP_FORMAT),
                         content=content
                     )
         # START BULLSHIT
         # Python can't turn a nonlocal datetime to a epoch time AFIACT
         # This pisses me off to no end.  Who knows what kind of f****d
         # up side effects this has.
         os.environ['TZ'] = 'UTC'
         tzset()
         # END BULLSHIT
         epoch_now = int(mktime(now.timetuple()))
         self.config['irclogs'].set('last_index', epoch_now)
         self.config.save()
         writer.commit()
         idx.close()
     except Exception, e:
         writer.cancel()
         idx.close()
         raise e
Пример #21
0
 def search(self, terms):
     chmgr = IRCChannelManager(self.env)
     ix = self.get_index()
     searcher = ix.searcher()
     parsed_terms = self.PARSER.parse(' or '.join(terms))
     if terms:
         for f in searcher.search(parsed_terms):
             timestamp = strptime(f['timestamp'], self.TIMESTAMP_FORMAT)
             f['timestamp'] = \
                     UTC.localize(datetime(*timestamp[:6]))
             yield f
Пример #22
0
    def on_topic(self, c, e):
        args = e.arguments()
        if len(args) < 2:
            channel = e.target().lstrip(''.join(CHANNEL_PREFIXES))
            topic = args[0]
        else:
            channel = args[0].lstrip(''.join(CHANNEL_PREFIXES))
            topic = args[1]
        changed_by = irclib.nm_to_n(e.source())
        channel_participation_id = self.channels[channel]
        channel_participation = model.Session.query(model.ChannelParticipation) \
            .get(channel_participation_id)
        channel_topic = model.Session.query(model.ChannelTopic) \
                        .get(channel_participation_id)
        channel = channel_participation.channel
        if not channel_topic:
            log.debug("Setting topic for channel %s%s by %s." % \
                      (channel.channel_prefix, channel.channel_name, changed_by))
            channel_topic = model.ChannelTopic(channel_participation, topic)
            model.Session.save(channel_topic)
            model.Session.commit()
        elif channel_topic.topic != topic:
            log.debug('Topic for channel %s%s changed by %s, updating DB' % \
                      (channel.channel_prefix, channel.channel_name, changed_by))
            channel_topic.topic = topic
            model.Session.save(channel_topic)
            model.Session.commit()

        channel_topic_info = model.Session.query(model.ChannelTopicInfo) \
                            .get(channel_participation_id)

        changed_on = UTC.localize(datetime.utcnow())

        if not channel_topic_info:
            log.debug('on_topic ChannelTopicInfo for %s non-existant' % channel)
            channel_topic_info = model.ChannelTopicInfo(channel_participation,
                                                        changed_by, changed_on)
        else:
            if channel_topic_info.changed_by != changed_by or \
                channel_topic_info.changed_on != changed_on:
                log.debug(self.__class__.__name__ +
                          ' updating topic info for channel %s...' % channel)
                if channel_topic_info.changed_by != changed_by:
                    channel_topic_info.changed_by = changed_by
                if channel_topic_info.changed_on != changed_on:
                    channel_topic_info.changed_on = changed_on
        model.Session.save(channel_topic_info)
        model.Session.commit()

        message = "%s changed the topic to '%s'" % (changed_by, topic)
        self.write_event(channel.channel_name, 'topic', '*****', message)

        model.Session.remove()
 def addNewsItem(self, aNewsItem):
     self.checkNewsTable()
     if(aNewsItem.isSuccessful()):
         ticker = aNewsItem.fullfeed.ticker
         service = aNewsItem.service
         fullsource = aNewsItem.fullsource
         title = aNewsItem.feedentry.title
         desc = aNewsItem.feedentry.description
         pub = UTC.localize(datetime.strptime(aNewsItem.feedentry.published, "%a, %d %b %Y %H:%M:%S %Z")).isoformat()
         newsid = aNewsItem.feedentry.id
         link = aNewsItem.url
         self.cursor.execute("INSERT INTO News VALUES (?,?,?,?,?,?,?,?)", (ticker, service, title, pub, desc, link, newsid, fullsource,))
         self.databaseconnection.commit()
Пример #24
0
def get_tzinfo(user):
    if user.is_authenticated():
        from pytz import UnknownTimeZoneError, UTC
        try:
            tz = user.get_profile().timezone
            # delt = tz.utcoffset(datetime.now()) # NOTICE: Not to use utcoffset, value could be wrong on server side
            now = datetime.now()
            delt = UTC.localize(now) - tz.localize(now)
            # timedelta attr: days, seconds, microseconds
            offset = delt.days*86400 + delt.seconds
            return {'tzname': str(tz), 'offset': offset}
        except UserProfile.DoesNotExist, UnknownTimeZoneError:
            return None
    def handle(self, *args, **options):

        for ws in WeatherStation.objects.all():
            try:
                required_days = [i.strftime('%Y-%m-%d') for i in pd.date_range(start = datetime(2009, 1, 1, tzinfo = UTC),
                                                                               end = timezone.now())]
                stored_days = ws.get_list_of_days()
                days_to_load = [i for i in required_days if i not in stored_days]
                for j in days_to_load:
                    result = ws.load_weather_day(UTC.localize(datetime.strptime(j,'%Y-%m-%d')))
            except:
                print 'Failed to completely update WeatherStation %s.' % ws.name
            else:
                print 'Updated WeatherStation %s.' % ws.name
Пример #26
0
    def name_get(self, cr, uid, ids, context=None):
        result = {}
        local_tz = pytz.timezone('America/Guayaquil')
        if context and "tz" in context.keys():
            local_tz = pytz.timezone(context["tz"])

        for route in self.browse(cr, uid, ids, context=context):
            if route.customer_partner_id and route.request_date:
                fecha_ruta = datetime.strptime(route.request_date, '%Y-%m-%d %H:%M:%S')
                fecha_ruta = UTC.localize(fecha_ruta, is_dst=False)
                result[route.id] = route.customer_partner_id.name_get()[0][1] + "[" + fecha_ruta.astimezone(local_tz).strftime('%Y-%m-%d %H:%M:%S')+ "]"
            else:
                result[route.id] = "Ruta:"+route.id
        return result.items()
Пример #27
0
    def serialize(self, request):
        href = request.urlgen(
            "mediagoblin.api.object",
            object_type=self.object_type,
            id=self.id,
            qualified=True
        )
        published = UTC.localize(self.published)
        updated = UTC.localize(self.updated)
        obj = {
            "id": href,
            "actor": self.get_actor.serialize(request),
            "verb": self.verb,
            "published": published.isoformat(),
            "updated": updated.isoformat(),
            "content": self.content,
            "url": self.get_url(request),
            "object": self.get_object.serialize(request),
            "objectType": self.object_type,
            "links": {
                "self": {
                    "href": href,
                },
            },
        }

        if self.generator:
            obj["generator"] = self.get_generator.serialize(request)

        if self.title:
            obj["title"] = self.title

        target = self.get_target
        if target is not None:
            obj["target"] = target.serialize(request)

        return obj
    def handle(self, *args, **options):
        dt = options['date'] + ' ' + options['time']

        measurement = SQTVaisalaMeasurement(
            time = UTC.localize(datetime.strptime(dt, '%Y-%m-%d %H:%M:%S')),
            wind_speed = options['wind_speed'],
            wind_direction = options['wind_direction'],
            air_temperature = options['air_temperature'],
            air_humidity = options['air_humidity'],
            air_pressure = options['air_pressure'],
            rain_amount = options['rain_amount'],
            heater_temperature = options['heater_temperature'],
            heater_voltage = options['heater_voltage']
        )
        
        measurement.save()
Пример #29
0
 def _format_link(self, formatter, ns, target, label):
    m = self.date_re.match(target)
    if not m:
        return system_message('Invalid IRC Log Link: '
                  'Must be of the format channel-UTCYYYY-MM-DDTHH:MM:SS %s')
    if not m.group('datetime'):
        return html.a(label, title=label, href=formatter.href.irclogs(
            m.group('channel')))
    else:
        ch_mgr = IRCChannelManager(self.env)
        t = strptime(m.group('datetime'), self.date_format)
        dt = UTC.localize(datetime(*t[:6]))
        dt = ch_mgr.to_user_tz(formatter.req, dt)
        timestr = dt.strftime(self.time_format)
        return html.a(label, title=label, href=formatter.href.irclogs(
                  m.group('channel'), '%02d'%dt.year, '%02d'%dt.month, 
                  '%02d'%dt.day,) + '#%s'%timestr)
Пример #30
0
    def default(self, obj):  # pylint: disable=method-hidden
        """
        Serialize datetime and date objects of iso format.

        datatime objects are converted to UTC.
        """

        if isinstance(obj, datetime):
            if obj.tzinfo is None:
                # Localize to UTC naive datetime objects
                obj = UTC.localize(obj)
            else:
                # Convert to UTC datetime objects from other timezones
                obj = obj.astimezone(UTC)
            return obj.isoformat()
        elif isinstance(obj, date):
            return obj.isoformat()

        return super(DateTimeJSONEncoder, self).default(obj)
Пример #31
0
 def utc(self):
     dt = self._obj
     if dt.tzinfo is None:
         return self.from_datetime(UTC.localize(dt))
     return self.from_datetime(dt.astimezone(UTC))
Пример #32
0
    def import_model(self,
                     session,
                     model_import,
                     selected_model='',
                     date_format="%Y-%m-%d"):
        model = Session.resolve_model(selected_model)
        message = ''

        cols = {
            col.name: getattr(model, col.name)
            for col in model.__table__.columns
        }
        result = csv.DictReader(
            model_import.file.read().decode('utf-8').split('\n'))
        id_list = []

        for row in result:
            if 'id' in row:
                id = row.pop('id')  # id needs special treatment

                try:
                    # get the instance if it already exists
                    model_instance = getattr(session, selected_model)(
                        id, allow_invalid=True)
                except Exception:
                    session.rollback()
                    # otherwise, make a new one and add it to the session for when we commit
                    model_instance = model()
                    session.add(model_instance)

            for colname, val in row.items():
                col = cols[colname]
                if not val:
                    # in a lot of cases we'll just have the empty string, so we'll just
                    # do nothing for those cases
                    continue
                if isinstance(col.type, Boolean):
                    if isinstance(val, six.string_types):
                        val = val.strip().lower() not in ('f', 'false', 'n',
                                                          'no', '0')
                    else:
                        val = bool(val)
                elif isinstance(col.type, Choice):
                    # the export has labels, and we want to convert those back into their
                    # integer values, so let's look that up (note: we could theoretically
                    # modify the Choice class to do this automatically in the future)
                    label_lookup = {
                        val: key
                        for key, val in col.type.choices.items()
                    }
                    val = label_lookup[val]
                elif isinstance(col.type, MultiChoice):
                    # the export has labels separated by ' / ' and we want to convert that
                    # back into a comma-separate list of integers
                    label_lookup = {val: key for key, val in col.type.choices}
                    vals = [label_lookup[label] for label in val.split(' / ')]
                    val = ','.join(map(str, vals))
                elif isinstance(col.type, UTCDateTime):
                    # we'll need to make sure we use whatever format string we used to
                    # export this date in the first place
                    try:
                        val = UTC.localize(
                            datetime.strptime(val, date_format + ' %H:%M:%S'))
                    except Exception:
                        val = UTC.localize(datetime.strptime(val, date_format))
                elif isinstance(col.type, Date):
                    val = datetime.strptime(val, date_format).date()
                elif isinstance(col.type, Integer):
                    val = int(val)

                # now that we've converted val to whatever it actually needs to be, we
                # can just set it on the model
                setattr(model_instance, colname, val)

            try:
                session.commit()
            except Exception:
                log.error('ImportError', exc_info=True)
                session.rollback()
                message = 'Import unsuccessful'

            id_list.append(model_instance.id)

        all_instances = session.query(model).filter(
            model.id.in_(id_list)).all() if id_list else None

        return self.index(message, all_instances)
Пример #33
0
def localized_utcnow():
    """Helper function to return localized utcnow()."""
    return UTC.localize(datetime.utcnow())  # pylint: disable=no-value-for-parameter
Пример #34
0
def to_utc_time(t):
    if not isinstance(t, time):
        t = time(t.hour, t.minute, t.second, t.microsecond)

    return UTC.localize(t)
Пример #35
0
 def inicio_validade(self):
     """Pega a data inicial de validade do certificado"""
     return UTC.localize(self.cert.not_valid_before)
Пример #36
0
 def handle_venue(self, venue: Venue) -> datetime.datetime:
     venue_id = venue.api_configuration.digital_pour_venue_id
     location_number = venue.api_configuration.digital_pour_location_number
     self.url = self.URL.format(venue_id, location_number, self.APIKEY)
     data = self.fetch()
     taps = {tap.tap_number: tap for tap in venue.taps.all()}
     self.update_date = UTC.localize(datetime.datetime(1970, 1, 1, 0, 0, 0))
     manufacturers = {}
     for entry in data:
         if not entry["Active"]:
             # in the cooler, not on tap
             continue
         # 1. parse the tap
         tap_info = self.parse_tap(entry)
         try:
             tap = taps[tap_info["tap_number"]]
         except KeyError:
             tap = Tap(venue=venue, tap_number=tap_info["tap_number"])
         tap.time_added = tap_info["added"]
         tap.time_updated = tap_info["updated"]
         if tap.time_updated and tap.time_updated > self.update_date:
             LOG.debug("Updating venue timestamp to %s", tap.time_updated)
             self.update_date = tap.time_updated
         tap.estimated_percent_remaining = tap_info["percent_full"]
         if tap_info["gas_type"] in [i[0] for i in Tap.GAS_CHOICES]:
             tap.gas_type = tap_info["gas_type"]
         else:
             tap.gas_type = ""
         # 2. parse the manufacturer, creating if needed
         parsed_manufacturer = self.parse_manufacturer(entry)
         try:
             manufacturer = manufacturers[parsed_manufacturer["name"]]
         except KeyError:
             defaults = {
                 field: parsed_manufacturer[field]
                 for field in [
                     "location",
                     "logo_url",
                     "twitter_handle",
                     "url",
                 ] if parsed_manufacturer[field]
             }
             manufacturer = self.get_manufacturer(
                 name=parsed_manufacturer["name"],
                 **defaults,
             )
             manufacturers[manufacturer.name] = manufacturer
         # 3. get the beer, creating if necessary
         parsed_beer = self.parse_beer(entry)
         name = parsed_beer.pop("name")
         color_html = parsed_beer.pop("color", "")
         if color_html:
             # convert 0xabcde into #0abcde
             color_html = f"#{color_html[2:]:0>6}"
             parsed_beer["color_html"] = color_html
         else:
             # clear the color if unknown
             parsed_beer["color_html"] = ""
         LOG.debug(
             "looking up beer: name %s, mfg %s, other data %s",
             name,
             manufacturer,
             parsed_beer,
         )
         if name.casefold().strip() == "N/A".casefold():
             if not parsed_beer.get("abv"):
                 # it's an empty tap
                 LOG.info("Tap %s is unused", tap.tap_number)
                 tap.beer = None
                 tap.save()
                 continue
         beer = self.get_beer(
             name,
             manufacturer,
             pricing=self.parse_pricing(entry),
             venue=venue,
             **parsed_beer,
         )
         # 4. assign the beer to the tap
         tap.beer = beer
         tap.save()
     return self.update_date
Пример #37
0
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from copy import copy
from datetime import datetime
from numbers import Integral

from pytz import UTC
from dateutil.parser import parse
from six import binary_type, text_type, integer_types, PY2

EPOCH = UTC.localize(datetime.utcfromtimestamp(0))


def _convert_timestamp(timestamp, precision=None):
    if isinstance(timestamp, Integral):
        return timestamp  # assume precision is correct if timestamp is int
    if isinstance(_get_unicode(timestamp), text_type):
        timestamp = parse(timestamp)
    if isinstance(timestamp, datetime):
        if not timestamp.tzinfo:
            timestamp = UTC.localize(timestamp)
        ns = (timestamp - EPOCH).total_seconds() * 1e9
        if precision is None or precision == 'n':
            return ns
        elif precision == 'u':
            return ns / 1e3
Пример #38
0
 def to_utc(self):
     dt_as_utc = self.datetime.astimezone(
         UTC) if self.datetime.tzinfo else UTC.localize(self.datetime)
     return Point(dt_as_utc, self.value)