Exemple #1
0
def add_countries():
    if config.QUERYAT_URL is None or config.GEOCODE_BATCH <= 0:
        return
    database.connect()
    with database.atomic():
        q = Change.select().where(
            (Change.country >> None) & (Change.action != 'a')
            & (~Change.changes.startswith('[[null, null]'))).limit(
                config.GEOCODE_BATCH + 200)
        count = config.GEOCODE_BATCH
        for ch in q:
            coord = ch.changed_coord()
            if coord is not None:
                country = geocode(coord[0], coord[1])
                if country is not None:
                    ch.country = country[:150]
                    ch.save()
            else:
                # print('Empty coordinates: {0} {1} {2}'.format(ch.id, ch.action, ch.changes.encode('utf-8')))
                pass

            # We request more because of these empty coordinates errors
            count -= 1
            if count <= 0:
                break
def create_tables():
    database.connect()
    tables = [Interaction, User, Company, Client]
    for table in tables:
        try:
            database.create_table(table)
        except OperationalError:
            pass
Exemple #3
0
def create_tables():
  database.connect()
  Category.drop_table(fail_silently=True)
  Thread.drop_table(fail_silently=True)
  Post.drop_table(fail_silently=True)
  Role.drop_table(fail_silently=True)
  Profile.drop_table(fail_silently=True)
  database.create_tables([Category, Thread, Post, Profile, Role])
  database.close()

  if settings.DEBUG:
    setup_temp_data()
def before_request():
    create_tables()
    if 'company' in session:
        try:
            g.company = Company().get(Company.pk == session['company'])
        except Company.DoesNotExist:
            g.company = None
            session.pop('company')
    if 'recent_clients' in session:
        g.recent_clients = session['recent_clients']
    if current_user.is_authenticated():
        g.company = current_user.company
        session['company'] = g.company.pk
    database.connect()
Exemple #5
0
    def render(self,option):
        from conf.models import configOption
        from db import database
        flag = False
        try:
            database.connect()
        except:
            flag = True
            pass
        if configOption.select().where(configOption.name == option).exists():
            if not flag:
                database.close()
            return configOption.get(configOption.name == option).value

        else:
            if not flag:
                database.close()
            return ''
Exemple #6
0
def process_notes():
    database.connect()
    if not check_update():
        return

    response = urllib2.urlopen(NOTES_URI)
    # Parsing bz2 through a temporary file
    tmpfile = TemporaryFile()
    while True:
        chunk = response.read(512*1024)
        if not chunk:
            break
        tmpfile.write(chunk)
    tmpfile.seek(0)

    with database.atomic():
        with BZ2File(tmpfile) as f:
            for event, element in etree.iterparse(f):
                if element.tag == 'note':
                    if len(element) > 0 and element[0].text and '#mapsme' in element[0].text:
                        note_id = element.get('id')
                        try:
                            ch = Change.get(Change.changeset == note_id, Change.action == 'n')
                            if element[-1].get('action') == 'closed' and ch.processed is None:
                                print('Found closed note {0}'.format(note_id))
                                ch.processed = hour_difference(ch.timestamp, element[-1].get('timestamp'))
                                ch.save()
                        except Change.DoesNotExist:
                            ch = Change()
                            ch.action = 'n'
                            ch.version = ''
                            ch.changeset = note_id
                            ch.user = element[0].get('user') if element[0].get('uid') else 'Anonymous Note'
                            print('Found new note {0} by {1}'.format(note_id, ch.user.encode('utf-8')))
                            ch.timestamp = datetime.strptime(element[0].get('timestamp'), '%Y-%m-%dT%H:%M:%SZ')
                            if element[-1].get('action') == 'closed' and ch.processed is None:
                                ch.processed = hour_difference(ch.timestamp, element[-1].get('timestamp'))
                            changes = [(element.get('lon'), element.get('lat')), {'note': element[0].text}]
                            ch.changes = json.dumps(changes, ensure_ascii=False)
                            ch.save()
                    element.clear()
Exemple #7
0
def add_countries():
    if config.QUERYAT_URL is None or config.GEOCODE_BATCH <= 0:
        return
    database.connect()
    with database.atomic():
        q = Change.select().where((Change.country >> None) & (Change.action != 'a') & (~Change.changes.startswith('[[null, null]'))).limit(
            config.GEOCODE_BATCH + 200)
        count = config.GEOCODE_BATCH
        for ch in q:
            coord = ch.changed_coord()
            if coord is not None:
                country = geocode(coord[0], coord[1])
                if country is not None:
                    ch.country = country[:150]
                    ch.save()
            else:
                # print('Empty coordinates: {0} {1} {2}'.format(ch.id, ch.action, ch.changes.encode('utf-8')))
                pass

            # We request more because of these empty coordinates errors
            count -= 1
            if count <= 0:
                break
Exemple #8
0
    if now == last_reminder:
        return
    while last_reminder != now:
        send_reminder(bot, '{:02}:{:02}'.format(*last_reminder))
        last_reminder[1] += 1
        if last_reminder[1] >= 60:
            last_reminder[0] += 1
            last_reminder[1] -= 60
            if last_reminder[0] >= 24:
                last_reminder[0] -= 24
    try:
        with open(config.TELEGRAM_STATE, 'w') as f:
            f.write('{:02}:{:02}'.format(*last_reminder))
    except IOError as e:
        logging.warn('Could not write state file: %s', e)


if __name__ == '__main__':
    logging.basicConfig(level=logging.WARNING,
                        format='%(asctime)s %(message)s',
                        datefmt='%H:%M:%S')
    database.connect()
    bot = telepot.DelegatorBot(config.TELEGRAM_TOKEN, [
        pave_event_space()(
            per_chat_id(types=['private']), create_open, Player, timeout=10),
    ])
    MessageLoop(bot).run_as_thread()
    while 1:
        send_reminders(bot)
        time.sleep(10)
Exemple #9
0
def before_request():
    database.connect()
    load_user_language()
Exemple #10
0
def before_request():
    database.connect()
    load_user_language()
Exemple #11
0
        sys.stdout.write('\rProcessing {0} {1:.1f}m{2}'.format(typ, ident / 1000000.0, ' ' * 10))
        sys.stdout.flush()
        self.flush()

    def got_coords(self, coords_list):
        for coords in coords_list:
            self.print_state('node', coords[0])
            changelib.store_node_coords_fast(coords[0], coords[2], coords[1])

    def got_way(self, ways):
        for way in ways:
            self.print_state('way', way[0])
            changelib.update_way_nodes(way[0], way[2])

    def got_relation(self, relations):
        for rel in relations:
            self.print_state('relation', rel[0])
            members = [x[1][0] + str(x[0]) for x in rel[2]]
            changelib.update_relation_members(rel[0], members)

database.connect()
database.create_tables([NodeRef, WayRelRef, Members], safe=True)
changelib.CACHE = False
changelib.open(path)
p = ParserForChange()
op = OSMParser(concurrency=1, coords_callback=p.got_coords,
               ways_callback=p.got_way, relations_callback=p.got_relation)
op.parse(sys.argv[1])
print
changelib.close()
Exemple #12
0
 def prepare(self):
     try:
         database.connect()
     except:
         pass
     return super(BaseHandler, self).prepare()
Exemple #13
0
def before_request():
  database.connect()
Exemple #14
0
def before_request():
    database.connect()