Ejemplo n.º 1
0
    def build_database(self):
        metadata.drop_all(self._session.connection())
        metadata.create_all(self._session.connection())

        for p in self.config.participants:
            pobj = Participant(id=p.id, name=p.name, color=p.color)
            self.session.merge(pobj)

        for l in self.config.levels:
            lobj = Level(id=l.id,
                         name=l.name,
                         geometry=l.geometry,
                         zoom=l.zoom)
            self.session.merge(lobj)

            for d in l.datasources:
                ds = DATASOURCE_BY_NAME[d.type](config=d, **d.data)
                for obj in ds.read():
                    self._session.merge(obj)

        for r in self.session.query(Result):
            r.update_vote_persent()

        Result.update_calc_fields(self.session)

        self._session.commit()
Ejemplo n.º 2
0
    def build_database(self):
        metadata.drop_all(self._session.connection())
        metadata.create_all(self._session.connection())

        for p in self.config.participants:
            pobj = Participant(id=p.id, name=p.name, color=p.color)
            self.session.merge(pobj)

        for l in self.config.levels:
            lobj = Level(
                id=l.id, name=l.name,
                geometry=l.geometry, zoom=l.zoom
            )
            self.session.merge(lobj)

            for d in l.datasources:
                ds = DATASOURCE_BY_NAME[d.type](config=d, **d.data)
                for obj in ds.read():
                    self._session.merge(obj)

        for r in self.session.query(Result):
            r.update_vote_persent()

        Result.update_calc_fields(self.session)

        self._session.commit()
Ejemplo n.º 3
0
 def read(self):
     fn = os.path.join(self._config.parent.parent.basedir, self.__filename)
     with open(fn, 'r') as f:
         data = geojson.loads(f.read(),
                              object_hook=geojson.GeoJSON.to_instance)
         for f in data.features:
             obj = Result(id=f.properties['ID'],
                          level_id=self._config.parent.id)
             if self._geometry == 'area':
                 obj.area_wkt = asShape(f.geometry).wkt
             yield obj
Ejemplo n.º 4
0
 def read(self):
     fn = os.path.join(self._config.parent.parent.basedir, self.__filename)
     with open(fn, 'r') as f:
         data = geojson.loads(
             f.read(),
             object_hook=geojson.GeoJSON.to_instance
         )
         for f in data.features:
             obj = Result(
                 id=f.properties['ID'],
                 level_id=self._config.parent.id
             )
             if self._geometry == 'area':
                 obj.area_wkt = asShape(f.geometry).wkt
             yield obj
Ejemplo n.º 5
0
    def __read_row(self, header, row):
        obj = Result(level_id=self._config.parent.id)

        record = self._process_record(dict(zip(header, row)))
        for k, v in record.items():
            if v == '':
                v = None
            if k in ('id', 'parent_id', 'name') and v:
                v = v.decode('utf-8')
            if not k.startswith('vote_count_') and hasattr(obj, k):
                setattr(obj, k, v)

        yield obj

        for k, v in record.items():
            if k.startswith('vote_count_'):
                vobj = ResultVote(result_id=obj.id,
                                  participant_id=int(
                                      k.replace('vote_count_', '')),
                                  vote_count=int(v))
                yield vobj