def __init__(self, app):
     self.app = app
     self._news_db = NewsDB(app)
     self._umbria = UmbriaEvents(app)
     self._toscana = ToscanaEvents(app)
     self._tre = UpdateDB(app, self._news_db, self._umbria, self._toscana)
     self.__insert_cities()
     self.start_update_news()
class CitiesManager(object):
    """Class to interact with the database alchemy
    """

    def __init__(self, app):
        self.app = app
        self._news_db = NewsDB(app)
        self._umbria = UmbriaEvents(app)
        self._toscana = ToscanaEvents(app)
        self._tre = UpdateDB(app, self._news_db, self._umbria, self._toscana)
        self.__insert_cities()
        self.start_update_news()

    def delete_all(self):
        """Clears completly the db
        """
        self._tre.runnable = False
        self._tre.join()
        self._news_db.delete_all()
        return {"message": "Db erased"}

    def init_db(self):
        """Recovers the initial state of the db
        and restart the update
        """
        self.stop_update_news()
        self._news_db.delete_all()
        self._news_db.base_population()
        self.__insert_cities()
        self.restart_update_news()
        return {"message": "app restarted"}

    def __insert_cities(self):
        """Insert cities in the database
        """
        session = self._news_db.session_m()
        regions = session.query(Regions).all()
        self.app.logger.debug("Start download cities")
        for region in regions:
            cities = None
            if region.name == "Umbria":
                cities = self._umbria.download_cities().items()
            elif region.name == "Toscana":
                cities = self._toscana.download_cities().items()

            for name, link in cities:
                self.app.logger.debug("Add city: " + unidecode(name))
                new_city = {"name": name, "link": link, "region": region.name}
                self._news_db.insert(Cities, new_city)
        session.close()
        # Debug
        # self._news_db.test_search()

    def restart_update_news(self):
        """Start global update and if is already started
        he stops the update and restarts it
        """
        self._tre.runnable = False
        self._tre.join()
        del self._tre
        self._news_db.delete_all_news()
        self._tre = UpdateDB(self.app, self._news_db, self._umbria, self._toscana)
        self._tre.start()
        return {"message": "Update restarted"}

    def stop_update_news(self):
        """Stop the update"""
        self._tre.stop()
        return {"message": "Update stopped"}

    def start_update_news(self):
        """Start the update"""
        # Debug
        # self.app.logger.debug(self._tre.isAlive())
        if not self._tre.isAlive():
            try:
                self._tre.start()
            except RuntimeError:  # if is already started
                pass
        if self._tre.runnable is False:
            self._tre.start_()
        return {"message": "Update started"}

    def city_events(self, city):
        """Return a list of events and a descriptio for a city
        """
        session = self._news_db.session_m()
        news_l = list()
        news_res = session.query(News).join(Cities).filter(Cities.name == city).order_by(News.id_).all()
        for news in news_res:
            news_l.append(news.web_return())
        session.close()
        return {"feeds": news_l}

    def cities_names(self, region):
        """Return a list of cities names in the regions selected
        """
        session = self._news_db.session_m()
        cities = session.query(Cities).join(Regions).filter(Regions.name == region).order_by(Cities.name).all()
        session.close()
        return {"cities": [city.name for city in cities]}

    def insert(self, type_, obj):
        if type_ == "Regions":
            return {"message": self._news_db.insert(Regions, obj)}
        elif type_ == "Cities":
            return {"message": self._news_db.insert(Cities, obj)}
        elif type_ == "News":
            return {"message": self._news_db.insert(News, obj)}

    def regions_names(self,):
        """Return a list of all regions avaible
        """
        session = self._news_db.session_m()
        regions = session.query(Regions).all()
        session.close()
        return {"regions": [region.name for region in regions]}

    def delete_region_news(self, region):
        """Delete all news of a region
        """
        return self._news_db.delete_region_news(region)

    def delete_city_news(self, city):
        """Delete all news of a city
        """
        return self._news_db.delete_city_news(city)