from sqlalchemy import Column from sqlalchemy import Integer from sqlalchemy import String from bulkperformancerights.connectors import mysql class Country(mysql.ArtRelationsBaseModel): __tablename__ = 'country' # We only ever need id/name from the country table id = Column(Integer, primary_key=True) name = Column(String(80), unique=True) def get_all_valid_countries(): """Convenience method wrapping session open/close """ session = mysql.art_relations_session() valid_countries = as_list(session) session.close() return valid_countries def as_list(session): """Country names represented as list Conditional Cache decorator is used to allow environment specific toggling of whether the cache(memcache at this time) is enabled Return: (list): country names """ all_countries = session.query(Country.name).all() return [c for (c, ) in all_countries] def as_dict(session): """dictionary of id: country Conditional Cache decorator is used to allow environment specific toggling of whether the cache(memcache at this time) is enabled Return: (dict): {{Country.id, Country.name},{...}} """ all_countries = session.query(Country.name, Country.id).all() return dict(all_countries)