Exemplo n.º 1
0
    def run(self):
        """
        Runs the database seeder.

        Raises:
            geodatabr.dataset.seeders.NothingToSeedError:
                If the mesoregions table is not empty
        """
        if MesoregionRepository.count():
            raise NothingToSeedError

        states = StateRepository.findAll()

        for state in states:
            mesoregions = self._sidra \
                .findChildren(SIDRA_MESOREGION,
                              SIDRA_STATE,
                              state.id)

            for mesoregion in mesoregions:
                MesoregionRepository.add(
                    Mesoregion(id=mesoregion.id,
                               state_id=state.id,
                               name=mesoregion.name))

        Database.commit()
Exemplo n.º 2
0
    def run(self):
        """
        Runs the database seeder.

        Raises:
            geodatabr.dataset.seeders.NothingToSeedError:
                If the subdistricts table is not empty
        """
        if SubdistrictRepository.count():
            raise NothingToSeedError

        districts = DistrictRepository.findAll()

        for district in districts:
            subdistricts = self._sidra \
                .findChildren(SIDRA_SUBDISTRICT,
                              SIDRA_DISTRICT,
                              district.id)

            for subdistrict in subdistricts:
                SubdistrictRepository.add(
                    Subdistrict(id=subdistrict.id,
                                state_id=district.state_id,
                                mesoregion_id=district.mesoregion_id,
                                microregion_id=district.microregion_id,
                                municipality_id=district.municipality_id,
                                district_id=district.id,
                                name=subdistrict.name))

        Database.commit()
Exemplo n.º 3
0
    def run(self):
        """
        Runs the database seeder.

        Raises:
            geodatabr.dataset.seeders.NothingToSeedError:
                If the municipalities table is not empty
        """
        if MunicipalityRepository.count():
            raise NothingToSeedError

        microregions = MicroregionRepository.findAll()

        for microregion in microregions:
            municipalities = self._sidra \
                .findChildren(SIDRA_MUNICIPALITY,
                              SIDRA_MICROREGION,
                              microregion.id)

            for municipality in municipalities:
                MunicipalityRepository.add(
                    Municipality(id=municipality.id,
                                 state_id=microregion.state_id,
                                 mesoregion_id=microregion.mesoregion_id,
                                 microregion_id=microregion.id,
                                 name=municipality.name))

        Database.commit()
Exemplo n.º 4
0
    def run(self):
        """
        Runs the database seeder.

        Raises:
            geodatabr.dataset.seeders.NothingToSeedError:
                If the districts table is not empty
        """
        if DistrictRepository.count():
            raise NothingToSeedError

        municipalities = MunicipalityRepository.findAll()

        for municipality in municipalities:
            districts = self._sidra \
                .findChildren(SIDRA_DISTRICT,
                              SIDRA_MUNICIPALITY,
                              municipality.id)

            for district in districts:
                DistrictRepository.add(
                    District(id=district.id,
                             state_id=municipality.state_id,
                             mesoregion_id=municipality.mesoregion_id,
                             microregion_id=municipality.microregion_id,
                             municipality_id=municipality.id,
                             name=district.name))

        Database.commit()
Exemplo n.º 5
0
    def add(cls, instance):
        """
        Saves an entity instance.

        Args:
            instance (geodatabr.dataset.schema.Entity):
                The entity instance to save
        """
        Database.add(instance)
Exemplo n.º 6
0
    def run(self):
        """
        Runs the database seeder.

        Raises:
            geodatabr.dataset.seeders.NothingToSeedError:
                If the states table is not empty
        """
        if StateRepository.count():
            raise NothingToSeedError

        states = self._sidra.findAll(SIDRA_STATE)

        for state in states:
            StateRepository.add(State(id=state.id, name=state.name))

        Database.commit()
Exemplo n.º 7
0
    def findAll(cls):
        """
        Retrieves all entity items.

        Returns:
            list: A list with all entity items
        """
        return Database.query(cls.entity).all()
Exemplo n.º 8
0
    def count(cls):
        """
        Returns the total entity items count.

        Returns:
            int: The total entity items count
        """
        return Database.query(cls.entity).count()
Exemplo n.º 9
0
    def loadAll(cls):
        """
        Retrieves all districts with relationships loaded.

        Returns:
            list: A list with all districts with relationships loaded
        """
        return Database.query(District) \
            .options(subqueryload(District.subdistricts)) \
            .all()
Exemplo n.º 10
0
    def loadAll(cls):
        """
        Retrieves all municipalities with relationships loaded.

        Returns:
            list: A list with all municipalities with relationships loaded
        """
        return Database.query(Municipality) \
            .options(subqueryload(Municipality.districts),
                     subqueryload(Municipality.subdistricts)) \
            .all()
Exemplo n.º 11
0
    def findByName(cls, name):
        """
        Retrieves a single entity item by name.

        Args:
            name (str): The entity item name

        Returns:
            geodatabr.dataset.schema.Entity: An entity item
        """
        return Database.query(cls.entity) \
            .filter(cls.entity.name == name) \
            .first()
Exemplo n.º 12
0
    def findById(cls, _id):
        """
        Retrieves a single entity item by ID.

        Args:
            _id (int): The entity item ID

        Returns:
            geodatabr.dataset.schema.Entity: An entity item
        """
        return Database.query(cls.entity) \
            .filter(cls.entity.id == _id) \
            .first()
Exemplo n.º 13
0
    def loadAll(cls):
        """
        Retrieves all mesoregions with relationships loaded.

        Returns:
            list: A list with all mesoregions with relationships loaded
        """
        return Database.query(Mesoregion) \
            .options(subqueryload(Mesoregion.microregions),
                     subqueryload(Mesoregion.municipalities),
                     subqueryload(Mesoregion.districts),
                     subqueryload(Mesoregion.subdistricts)) \
            .all()
Exemplo n.º 14
0
 def delete(cls):
     """Removes all entity items."""
     Database.query(cls.entity).delete()