class Article(db.Model): __tablename__ = 'scholar_article' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(400)) abstract = db.Column(db.Text()) file_url = db.Column(db.String(400)) theme = db.Column(db.String(250)) postage_date = db.Column(db.DateTime) approval_status = db.Column(db.Boolean) authors = db.relationship('AuthorScholar', backref='scholar_article', lazy='eager', cascade='all, delete-orphan') keywords = db.relationship('KeyWord', secondary=article_keyword_table, backref=db.backref('articles', lazy='dynamic')) def authors_str(self): author_names = [author.name for author in self.authors] return ', '.join(author_names) def keywords_str(self): keyword_names = [keyword.name for keyword in self.keywords] return ', '.join(keyword_names) def date_str(self): return self.postage_date.strftime('%d/%m/%Y') def __repr__(self): return '<Article %r>' % (self.title)
class Publication(db.Model): # TODO - Alter publication.thumb column to db.Column(db.String(400)) __tablename__ = 'news_publication' __searchable__ = ['title', 'author', 'main_subject', 'text_call'] id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(400)) author = db.Column(db.String(100)) text_call = db.Column(db.String(500)) text_content = db.Column(db.Text(4194304)) thumb = db.Column(db.Text(4194304)) thumb_src = db.Column(db.String(400)) publish_date = db.Column(db.DateTime) last_modification = db.Column(db.DateTime) active = db.Column(db.Boolean) show_home = db.Column(db.Boolean) language = db.Column(db.String(2)) main_subject = db.Column(db.String(50)) subjects = db.relationship("PublicationSubject", secondary=association_table, backref=db.backref('publications', lazy='dynamic')) def date(self): return self.publish_date.strftime('%d/%m/%Y') def __repr__(self): return '<Publication %r>' % (self.title) def add_subjects(self, subjects_input, language): for subject_input in subjects_input: subject = PublicationSubject.query.filter_by( name=subject_input, language=language).first() if not subject: self.subjects.append( PublicationSubject(subject_input, language)) else: self.subjects.append(subject) if subject_input == subjects_input[0]: self.main_subject = subject_input
class Question(db.Model, AutoSerialize): __tablename__ = 'ask_question' id = db.Column(db.Integer, primary_key = True) language = db.Column(db.String(2)) user_id = db.Column(db.Integer, db.ForeignKey(User.id)) question = db.Column(db.String(140)) slug = db.Column(db.String(140)) app = db.Column(db.String(60)) body = db.Column(db.Text()) timestamp = db.Column(db.DateTime) status_id = db.Column(db.Integer, db.ForeignKey('ask_status.id'), default = 1) type_id = db.Column(db.Integer, db.ForeignKey('ask_type.id'), default = 1) status_notes = db.Column(db.Text()) tags = db.relationship('Tag', secondary=question_tags, backref=db.backref('question', lazy='dynamic')) replies = db.relationship("Reply", backref = 'question', lazy = 'dynamic', order_by="Reply.parent_id") votes = db.relationship("Vote", primaryjoin= "and_(Question.id==Vote.type_id, Vote.type=={0})".format(TYPE_QUESTION), foreign_keys=[Vote.type_id], backref = 'question', lazy = 'dynamic') @staticmethod def make_unique_slug(question): """Generates an slightly worse ASCII-only slug.""" _punct_re = re.compile(r'[\t !"#$%&\'()*\-/<=>?@\[\\\]^_`{|},.]+') delim = "_" result = [] for word in _punct_re.split(question.lower()): word = normalize('NFKD', word).encode('ascii', 'ignore') if word: result.append(word) slug = unicode(delim.join(result)) """Check if slug is unique otherwise append the last inserted ID +1""" if Question.query.filter_by(slug = slug).first() is not None: last_q = Question.query.order_by(Question.id.desc()).first() slug = str(last_q.id) + delim + slug return slug def _find_or_create_tag(self, attr_type, attr_id): t = Tag.query.filter_by(attr_type=attr_type, attr_id=attr_id).first() if not(t): t = Tag(attr_type=attr_type, attr_id=attr_id) return t def str_tags(self, tag_list): # clear the list first while self.tags: del self.tags[0] # next add the new tags for tag in tag_list: attr_type, attr_id = tag.split(":") self.tags.append(self._find_or_create_tag(attr_type, attr_id)) def __repr__(self): return '<Question %r>' % (self.question) def serialize(self): auto_serialized = super(Question, self).serialize() try: auto_serialized["timestamp"] = self.timestamp.isoformat() except AttributeError: auto_serialized["timestamp"] = None auto_serialized["user"] = self.user.serialize() auto_serialized["votes"] = len(self.votes.all()) auto_serialized["replies"] = len(self.replies.filter_by(hidden=0).all()) + len(self.replies.filter_by(hidden=1).all()) return auto_serialized
class Bra(db.Model, AutoSerialize, BasicAttr): __tablename__ = 'attrs_bra' id = db.Column(db.String(10), primary_key=True) id_ibge = db.Column(db.Integer(7)) distance = 0 # SECEX relations ymb = db.relationship("Ymb", backref='bra', lazy='dynamic') ymbp = db.relationship("Ymbp", backref='bra', lazy='dynamic') ymbw = db.relationship("Ymbw", backref='bra', lazy='dynamic') ymbpw = db.relationship("Ymbpw", backref='bra', lazy='dynamic') # RAIS relations yb_rais = db.relationship("Yb_rais", backref='bra', lazy='dynamic') ybi = db.relationship("Ybi", backref='bra', lazy='dynamic') ybo = db.relationship("Ybo", backref='bra', lazy='dynamic') ybio = db.relationship("Ybio", backref='bra', lazy='dynamic') # HEDU relations ybu = db.relationship("Ybu", backref='bra', lazy='dynamic') # SC relations ybc_sc = db.relationship("Ybc_sc", backref='bra', lazy='dynamic') # Neighbors neighbors = db.relationship( 'Distances', primaryjoin="(Bra.id == Distances.bra_id_origin)", backref='bra_origin', lazy='dynamic') bb = db.relationship('Distances', primaryjoin="(Bra.id == Distances.bra_id_dest)", backref='bra', lazy='dynamic') # Planning Regions pr = db.relationship('Bra', secondary=bra_pr, primaryjoin=(bra_pr.c.pr_id == id), secondaryjoin=(bra_pr.c.bra_id == id), backref=db.backref('bra', lazy='dynamic'), lazy='dynamic') pr2 = db.relationship('Bra', secondary=bra_pr, primaryjoin=(bra_pr.c.bra_id == id), secondaryjoin=(bra_pr.c.pr_id == id), backref=db.backref('bra2', lazy='dynamic'), lazy='dynamic') def icon(self): if len(self.id) == 1: return None return "/static/img/icons/bra/bra_%s.png" % (self.id[:3]) def get_neighbors(self, dist, remove_self=False): if dist == 0: return [] q = self.neighbors.filter(Distances.distance <= dist).order_by( Distances.distance) if remove_self: q = q.filter(Distances.bra_id_dest != self.id) # filter out self return q.all() def url(self): return "/profiles/bra/{}/".format(self.id) def __repr__(self): return '<Bra %r>' % (self.name_en)
class Build(db.Model, AutoSerialize): __tablename__ = 'apps_build' id = db.Column(db.Integer, primary_key=True) dataset = db.Column(db.String(20)) bra = db.Column(db.String(20)) filter1 = db.Column(db.String(20)) filter2 = db.Column(db.String(20)) output = db.Column(db.String(20)) title_en = db.Column(db.String(120)) title_pt = db.Column(db.String(120)) app_id = db.Column(db.Integer, db.ForeignKey(App.id)) ui = db.relationship('UI', secondary=build_ui, backref=db.backref('Builds'), lazy='dynamic') app = db.relationship('App', backref=db.backref('Builds', lazy='dynamic')) def get_ui(self, ui_type): return self.ui.filter(UI.type == ui_type).first() def set_bra(self, bra_id): '''If build requires 2 bras and only 1 is given, supply a 2nd''' if isinstance(self.bra, list): return if bra_id == 'bra': bra_id = 'all' if "_" in self.bra and "_" not in bra_id: if bra_id == "rj": bra_id = bra_id + "_mg" else: bra_id = bra_id + "_rj" elif "_" not in self.bra and "_" in bra_id: bra_id = bra_id.split("_")[0] self.bra = [] for i, b in enumerate(bra_id.split("_")): if b == "all": self.bra.append(Wld.query.get("sabra")) self.bra[i].id = "all" else: if "." in b: split = b.split(".") b = split[0] dist = split[1] else: dist = 0 state = b[:2] if self.output == "bra" and len(b) == 8 and dist == 0: b = state dist = 0 self.bra.append(Bra.query.get(b)) self.bra[i].distance = dist self.bra[i].neighbor_ids = [ b.bra_id_dest for b in self.bra[i].get_neighbors(dist) ] # raise Exception([b.id for b in self.bra[i].pr.all()]) self.bra[i].pr_ids = [b.id for b in self.bra[i].pr.all()] def set_filter1(self, filter): if self.filter1 != "all": if self.dataset == "rais": self.isic = [] for i, f in enumerate(filter.split("_")): if Isic.query.get(f): self.isic.append(Isic.query.get(f)) else: self.isic.append(Isic.query.get('r9000')) self.filter1 = "_".join([i.id for i in set(self.isic)]) elif self.dataset == "secex": self.hs = [] for i, f in enumerate(filter.split("_")): if Hs.query.get(f): self.hs.append(Hs.query.get(f)) else: self.hs.append(Hs.query.get('178703')) self.filter1 = "_".join([h.id for h in set(self.hs)]) def set_filter2(self, filter): if self.filter2 != "all": if self.dataset == "rais": self.cbo = [] for i, f in enumerate(filter.split("_")): if Cbo.query.get(f): self.cbo.append(Cbo.query.get(f)) else: self.cbo.append(Cbo.query.get('2211')) self.filter2 = "_".join([c.id for c in set(self.cbo)]) elif self.dataset == "secex": self.wld = [] for i, f in enumerate(filter.split("_")): if Wld.query.get(f): self.wld.append(Wld.query.get(f)) else: self.wld.append(Wld.query.get('aschn')) self.filter2 = "_".join([w.id for w in set(self.wld)]) '''Returns the URL for the specific build.''' def url(self, **kwargs): if isinstance(self.bra, (list, tuple)): bras = [] for b in self.bra: if b.id != "all" and b.distance > 0: bras.append(b.id + "." + b.distance) else: bras.append(b.id) bra_id = "_".join(bras) else: bra_id = "<bra>" url = '{0}/{1}/{2}/{3}/{4}/{5}/'.format(self.app.type, self.dataset, bra_id, self.filter1, self.filter2, self.output) return url '''Returns the data URL for the specific build. This URL will return the data required for building a viz of this app. ''' def data_url(self, **kwargs): bras = [] if isinstance(self.bra, (list, tuple)): for b in self.bra: if b.id != "all" and b.distance > 0: bras.append(b.id + "." + b.distance) else: bras.append(b.id) bra = "_".join(bras) else: bra = "<bra>" if self.output == "bra": if bra == "all" and self.app.type == "geo_map": bra = "show.2" elif bra == "all": bra = "show.8" else: bra = bra + ".show.8" filter1 = self.filter1 if filter1 == "all" or self.app.type == "rings": if self.output == "isic": filter1 = "show.5" elif self.output == "hs": filter1 = "show.6" filter2 = self.filter2 if filter2 == "all" or self.app.type == "rings": if self.output == "cbo": filter2 = "show.4" elif self.output == "wld": filter2 = "show.5" data_url = '{0}/all/{1}/{2}/{3}/'.format(self.dataset, bra, filter1, filter2) return data_url '''Returns the data table required for this build''' def data_table(self): from dataviva.rais.models import Ybi, Ybo, Yio, Yb_rais, Yi, Yo from dataviva.secex.models import Ybp, Ybw, Ypw, Yb_secex, Yp, Yw # raise Exception(self.output) if self.dataset == "rais": # raise Exception(self.bra[0], self.filter1, self.filter2, self.output) if self.bra[0].id == "all" and self.output != "bra": return Yio elif self.output == "isic" or (self.output == "bra" and self.filter2 == "all"): return Ybi elif self.output == "cbo" or (self.output == "bra" and self.filter1 == "all"): return Ybo elif self.dataset == "secex": if self.bra[0].id == "all" and self.output != "bra": return Ypw elif self.output == "hs" or (self.output == "bra" and self.filter2 == "all"): return Ybp elif self.output == "wld" or (self.output == "bra" and self.filter1 == "all"): return Ybw if self.filter1 == "all": return Ybw elif self.filter1 == "all": return Ybp '''Returns the english language title of this build.''' def title(self, **kwargs): lang = g.locale if "lang" in kwargs: lang = kwargs["lang"] title_lang = "title_en" if lang == "en" else "title_pt" name_lang = "name_en" if lang == "en" else "name_pt" title = getattr(self, title_lang) depths = { "en": { "plural": {}, "single": {} }, "pt": { "plural": {}, "single": {} } } depths["en"]["single"] = { "2": u"State", "4": u"Mesoregion", "8": u"Municipality" } depths["en"]["plural"] = { "2": u"States", "4": u"Mesoregions", "8": u"Municipalities" } depths["pt"]["single"] = { "2": u"Estado", "4": u"Mesorregião", "8": u"Município" } depths["pt"]["plural"] = { "2": u"Estados", "4": u"Mesorregiões", "8": u"Municípios" } if "depth" in kwargs and u"bra_" in kwargs["depth"][ 0] and kwargs["depth"][0] != "bra_8": if depths[lang]["plural"]["8"] in title: title = title.replace( depths[lang]["plural"]["8"], depths[lang]["plural"][kwargs["depth"][0][4:]]) if depths[lang]["single"]["8"] in title: title = title.replace( depths[lang]["single"]["8"], depths[lang]["single"][kwargs["depth"][0][4:]]) if self.output == "bra" and isinstance( self.bra, (list, tuple)) and self.bra[0].id == "all": title = title.replace(depths[lang]["plural"]["8"], depths[lang]["plural"]["2"]) title = title.replace(depths[lang]["single"]["8"], depths[lang]["single"]["2"]) if self.app_id != 2: if "year" in kwargs: year = kwargs["year"] else: year = __latest_year__[self.dataset] title += " ({0})".format(year) def get_article(attr, article): if attr.article_pt: if attr.gender_pt == "m": if article == "em": new_article = "no" if article == "de": new_article = "do" if article == "para": new_article = "para o" elif attr.gender_pt == "f": if article == "em": new_article = "na" if article == "de": new_article = "da" if article == "para": new_article = "para a" if attr.plural_pt: new_article = new_article + "s" return new_article else: return article if title: if lang == "pt": joiner = " e " else: joiner = " and " if "<bra>" in title and isinstance(self.bra, (list, tuple)): bras = [] for b in self.bra: name = title_case(getattr(b, name_lang)) if b.id != "all" and b.distance > 0: name = name + " " + b.distance + "km" bras.append(name) article_search = re.search('<bra_(\w+)>', title) if article_search: title = title.replace(" <bra>", "") title = title.replace( article_search.group(0), joiner.join([ get_article(b, article_search.group(1)) + " " + bras[i] for i, b in enumerate(self.bra) ])) else: title = title.replace("<bra>", joiner.join(bras)) if "<isic>" in title and hasattr(self, "isic"): title = title.replace( "<isic>", joiner.join([ title_case(getattr(i, name_lang)) for i in self.isic ])) article_search = re.search('<isic_(\w+)>', title) if article_search: title = title.replace( article_search.group(0), joiner.join([ get_article(b, article_search.group(1)) for b in self.isic ])) if "<hs>" in title and hasattr(self, "hs"): title = title.replace( "<hs>", joiner.join( [title_case(getattr(h, name_lang)) for h in self.hs])) article_search = re.search('<hs_(\w+)>', title) if article_search: title = title.replace( article_search.group(0), joiner.join([ get_article(b, article_search.group(1)) for b in self.hs ])) if "<cbo>" in title and hasattr(self, "cbo"): title = title.replace( "<cbo>", joiner.join( [title_case(getattr(c, name_lang)) for c in self.cbo])) article_search = re.search('<cbo_(\w+)>', title) if article_search: title = title.replace( article_search.group(0), joiner.join([ get_article(b, article_search.group(1)) for b in self.cbo ])) if "<wld>" in title and hasattr(self, "wld"): title = title.replace( "<wld>", joiner.join( [title_case(getattr(w, name_lang)) for w in self.wld])) article_search = re.search('<wld_(\w+)>', title) if article_search: title = title.replace( article_search.group(0), joiner.join([ get_article(b, article_search.group(1)) for b in self.wld ])) return title def serialize(self, **kwargs): auto_serialized = super(Build, self).serialize() if isinstance(self.bra, (list, tuple)): auto_serialized["bra"] = [b.serialize() for b in self.bra] for i, b in enumerate(auto_serialized["bra"]): if b["id"] != "all" and self.bra[i].distance: b["distance"] = self.bra[i].distance b["neighbor_ids"] = self.bra[i].neighbor_ids elif b["id"] != "all" and len(self.bra[i].pr_ids): b["pr_ids"] = self.bra[i].pr_ids if hasattr(self, "isic"): auto_serialized["isic"] = [i.serialize() for i in self.isic] if hasattr(self, "hs"): auto_serialized["hs"] = [h.serialize() for h in self.hs] if hasattr(self, "cbo"): auto_serialized["cbo"] = [c.serialize() for c in self.cbo] if hasattr(self, "wld"): auto_serialized["wld"] = [w.serialize() for w in self.wld] del auto_serialized["title_en"] del auto_serialized["title_pt"] auto_serialized["title"] = self.title() #auto_serialized["id_item"] = self.title() auto_serialized["data_url"] = self.data_url() auto_serialized["url"] = self.url() auto_serialized["ui"] = [ui.serialize() for ui in self.ui.all()] auto_serialized["app"] = self.app.serialize() return auto_serialized def __repr__(self): return '<Build %s:%r: %s/%s/%s>' % ( self.id, self.app.type, self.filter1, self.filter2, self.output)
class Build(db.Model, AutoSerialize): __tablename__ = 'apps_build' id = db.Column(db.Integer, primary_key=True) dataset = db.Column(db.String(20)) bra = db.Column(db.String(20)) filter1 = db.Column(db.String(20)) filter2 = db.Column(db.String(20)) output = db.Column(db.String(20)) title_en = db.Column(db.String(120)) title_pt = db.Column(db.String(120)) slug_en = db.Column(db.String(60)) slug_pt = db.Column(db.String(60)) slug2_en = db.Column(db.String(80)) slug2_pt = db.Column(db.String(80)) app_id = db.Column(db.Integer, db.ForeignKey(App.id)) ui = db.relationship('UI', secondary=build_ui, backref=db.backref('Builds'), lazy='dynamic') app = db.relationship('App', backref=db.backref('Builds', lazy='dynamic')) params = None def get_ui(self, ui_type): return self.ui.filter(UI.type == ui_type).first() # def brazil_allowed(self): # return (self.app_id not in [6,8]) and ((self.app_id not in [4,5]) or self.dataset != "rais") def limit_bra(self, bra): if isinstance(bra, Wld): # if not self.brazil_allowed(): # return Bra.query.get("4mg") # else: bra.id = "all" return bra def set_bra(self, bra_id): if isinstance(bra_id, (Bra, Wld)): bra_id = self.limit_bra(bra_id) bra_id = [bra_id] elif isinstance(bra_id, (list)) and isinstance(bra_id[0], (Bra, Wld)): bra_id = [self.limit_bra(b) for b in bra_id] else: if bra_id == "bra": bra_id = "all" # if bra_id == "all" and not self.brazil_allowed(): # bra_id = "4mg" if "_" in self.bra and "_" not in bra_id: if bra_id == "4rj": bra_id = bra_id + "_4mg" else: bra_id = bra_id + "_4rj" elif "_" not in self.bra and "_" in bra_id: bra_id = bra_id.split("_")[0] bra_id = bra_id.split("_") self.bra = [] for i, b in enumerate(bra_id): if isinstance(b, (Bra, Wld)): # if b.id.startswith("4mgplr"): # b.pr_ids = [pr.id for pr in b.pr.all()] # else: # b.pr_ids = [] self.bra.append(b) elif b == "all": self.bra.append(Wld.query.get("sabra")) self.bra[i].id = "all" else: if "." in b: split = b.split(".") b = split[0] dist = split[1] else: dist = 0 state = b[:3] if self.output == "bra" and len(b) == 9 and dist == 0: b = state dist = 0 self.bra.append(Bra.query.get(b)) self.bra[i].distance = dist self.bra[i].neighbor_ids = [ b.bra_id_dest for b in self.bra[i].get_neighbors(dist) ] # if b.startswith("4mg"): # self.bra[i].pr_ids = [b.id for b in self.bra[i].pr.all()] # else: # self.bra[i].pr_ids = [] def set_filter1(self, filter): if self.filter1 != "all": if self.dataset == "rais": name = "cnae" default = "r90019" elif self.dataset == "secex": name = "hs" default = "178703" elif self.dataset == "hedu": name = "university" default = "00575" items = [] attr = globals()[name.capitalize()] if isinstance(filter, globals()[name.capitalize()]): items = [filter] elif isinstance(filter, (str, unicode)): for i, f in enumerate(filter.split("_")): if attr.query.get(f): items.append(attr.query.get(f)) else: items.append(attr.query.get(default)) else: items.append(attr.query.get(default)) self.filter1 = "_".join([c.id for c in set(items)]) setattr(self, name, items) def set_filter2(self, filter): if self.filter2 != "all": if self.dataset == "rais": name = "cbo" default = "2211" elif self.dataset == "secex": name = "wld" default = "aschn" elif self.dataset == "hedu": name = "course_hedu" default = "345A01" elif self.dataset == "sc": name = "course_sc" default = "13182" items = [] attr = globals()[name.capitalize()] if isinstance(filter, globals()[name.capitalize()]): items = [filter] elif isinstance(filter, (str, unicode)): for i, f in enumerate(filter.split("_")): if attr.query.get(f): items.append(attr.query.get(f)) else: items.append(attr.query.get(default)) else: items.append(attr.query.get(default)) self.filter2 = "_".join([c.id for c in set(items)]) setattr(self, name, items) '''Returns the URL for the specific build.''' def url(self, **kwargs): fill = kwargs.get("fill", True) if fill: if isinstance(self.bra, (list, tuple)): bras = [] for b in self.bra: if b.id != "all" and b.distance > 0: bras.append(b.id + "." + b.distance) else: bras.append(b.id) bra_id = "_".join(bras) else: bra_id = self.bra f1 = self.filter1 f2 = self.filter2 else: bra_id = self.bra if self.app.type == "compare": bra_id = "<bra>_<bra_1>" f1 = self.filter1 if f1 != "all": if self.dataset == "rais": f1 = "<cnae>" elif self.dataset == "secex": f1 = "<hs>" elif self.dataset == "hedu": f1 = "<university>" f2 = self.filter2 if f2 != "all": if self.dataset == "rais": f2 = "<cbo>" elif self.dataset == "secex": f2 = "<wld>" elif self.dataset == "hedu": f2 = "<course_hedu>" elif self.dataset == "sc": f2 = "<course_sc>" if self.params: params = "?{}".format(urllib.urlencode(self.params)) else: params = "" return "{}/{}/{}/{}/{}/{}/{}".format(self.app.type, self.dataset, bra_id, f1, f2, self.output, params) '''Returns the data URL for the specific build. This URL will return the data required for building a viz of this app. ''' def data_url(self, **kwargs): bras = [] if isinstance(self.bra, (list, tuple)): for b in self.bra: if b.id != "all" and b.distance > 0: bras.append(b.id + "." + b.distance) else: bras.append(b.id) bra = "_".join(bras) else: bra = "<bra>" if self.output == "bra" and self.dataset != "ei": if bra == "all": if self.app.type == "geo_map": bra = "show.3" elif self.app.type == "bar": bra = "show.1" else: bra = "show.9" elif self.app.type == "bar": bra = "{}.show.{}".format(bra, len(bra)) else: bra = bra + ".show.9" filter1 = self.filter1 filter1_out = None depths = {"cnae": 6, "hs": 6, "university": 5, "school": 8} if self.output in depths: if self.app.type == "bar" and filter1 != "all": depth = len(filter1) else: depth = depths[self.output] filter1_out = "show.{}".format(depth) if filter1_out: if filter1 == "all": filter1 = filter1_out else: filter1 = "{}.{}".format(filter1, filter1_out) filter2 = self.filter2 filter2_out = None depths = {"cbo": 4, "wld": 5, "course_sc": 5, "course_hedu": 6} if self.output in depths: if self.app.type == "bar" and filter2 != "all": depth = len(filter2) else: depth = depths[self.output] filter2_out = "show.{}".format(depth) if filter2_out: if filter2 == "all": filter2 = filter2_out else: filter2 = "{}.{}".format(filter2, filter2_out) if self.output in ("balance", "time", "type"): if bra != "all": bra = bra + ".show.{}".format(len(bra)) elif filter1 != "all": filter1 = filter1 + ".show.{}".format(len(filter1)) elif filter2 != "all": filter2 = filter2 + ".show.{}".format(len(filter2)) else: bra = "show.1" elif self.output == "age": filter1 = "show.8" filter2 = "xx.show.5" elif self.output == "basic": filter2 = "xx.show.5" elif self.output == "adm": filter1 = "show.8" params = "" if self.output == "course_sc" and self.filter2 == "all": params = "?exclude=xx%" if self.dataset == "ei": if self.output == "bra": return "ei/all/show.9/{}/{}".format(bra, params) else: return "ei/all/{}/show.9/{}".format(bra, params) return "{}/all/{}/{}/{}/{}".format(self.dataset, bra, filter1, filter2, params) '''Returns the data table required for this build''' def data_table(self): from dataviva.rais.models import Ybi, Ybo, Yio, Yb_rais, Yi, Yo from dataviva.secex.models import Ymbp, Ymbw, Ympw, Ymb, Ymp, Ymw # raise Exception(self.output) if self.dataset == "rais": # raise Exception(self.bra[0], self.filter1, self.filter2, self.output) if self.bra[0].id == "all" and self.output != "bra": return Yio elif self.output == "cnae" or (self.output == "bra" and self.filter2 == "all"): return Ybi elif self.output == "cbo" or (self.output == "bra" and self.filter1 == "all"): return Ybo elif self.dataset == "secex": if self.bra[0].id == "all" and self.output != "bra": return Ympw elif self.output == "hs" or (self.output == "bra" and self.filter2 == "all"): return Ymbp elif self.output == "wld" or (self.output == "bra" and self.filter1 == "all"): return Ymbw if self.filter1 == "all": return Ymbw elif self.filter1 == "all": return Ymbp def format_text(self, title, kwargs): if kwargs.get("dumb") == True: return title lookup = dictionary() depth = kwargs.get("depth", None) year = kwargs.get("year", None) munic = lookup["bra_9"] munics = lookup["bra_9_plural"] if depth and u"bra_" in depth[0] and depth[0] != "bra_8": if munics in title: title = title.replace(munics, lookup["bra_{}_plural".format(depth[0])]) if munic in title: title = title.replace(munic, lookup["bra_{}".format(depth[0])]) if self.output == "bra" and isinstance( self.bra, (list, tuple)) and self.bra[0].id == "all": title = title.replace(munics, lookup["bra_3_plural"]) title = title.replace(munic, lookup["bra_3"]) flow = kwargs.get("size", None) if not flow: flow = kwargs.get("y", None) if not flow: flow = kwargs.get("axes", None) if not flow: flow = "export_val" flow = "{}s".format(flow[:-4]) impexp = u"{}/{}".format(lookup["import_val"], lookup["export_val"]) if impexp in title and flow in lookup: title = title.replace(impexp, lookup[flow]) impexp = u"{}/{}".format(lookup["origin"], lookup["destination"]) if impexp in title: if flow == "imports": rep = lookup["origins"] else: rep = lookup["destination"] title = title.replace(impexp, rep) impexp = u"{}/{}".format(lookup["origins"], lookup["destinations"]) if impexp in title: if flow == "imports": rep = lookup["origins"] else: rep = lookup["destination"] title = title.replace(impexp, rep) if not year: if self.app_id in [2, 9]: year = "_".join(__year_range__[self.dataset]) else: year = __year_range__[self.dataset][1] monthly = self.dataset != "secex" or self.app_id in [2, 9] def format_date(d): if "-" in d: y, m = d.split("-") if monthly: if m == "0": m = "1" d = "{} {}".format(lookup["month_{}".format(m)], y) else: d = y return d year = [format_date(y) for y in year.split("_")] title += " ({0})".format("-".join(year)) return title def slug(self, **kwargs): slug = getattr(self, "slug_{}".format(g.locale)) slug = self.format_text(slug, kwargs) return slug def slug2(self): return getattr(self, "slug2_{}".format(g.locale)) '''Returns the english language title of this build.''' def title(self, **kwargs): title = getattr(self, "title_{}".format(g.locale)) title = self.format_text(title, kwargs) if title: for f in [ "bra", "cnae", "hs", "cbo", "wld", "university", "course_hedu", "course_sc" ]: if hasattr(self, f): attr = getattr(self, f) if not isinstance(attr, (unicode, str)): title = title_format(title, attr) return title def json(self, **kwargs): return { "app": self.app.serialize(), "dataset": self.dataset, "id": int(self.id), "slug": self.slug(**kwargs), "slug2": self.slug2(), "title": self.title(**kwargs), "url": self.url(**kwargs) } def serialize(self, **kwargs): auto_serialized = super(Build, self).serialize() if isinstance(self.bra, (list, tuple)): auto_serialized["bra"] = [b.serialize() for b in self.bra] for i, b in enumerate(auto_serialized["bra"]): if b["id"] != "all" and self.bra[i].distance: b["distance"] = self.bra[i].distance b["neighbor_ids"] = self.bra[i].neighbor_ids # elif b["id"].startswith("4mg"): # b["pr_ids"] = self.bra[i].pr_ids for f in [ "cnae", "cbo", "hs", "wld", "university", "course_hedu", "course_sc" ]: if hasattr(self, f): auto_serialized[f] = [i.serialize() for i in getattr(self, f)] del auto_serialized["title_en"] del auto_serialized["title_pt"] auto_serialized["id"] = int(self.id) auto_serialized["title"] = self.title() auto_serialized["slug"] = self.slug() auto_serialized["data_url"] = self.data_url() auto_serialized["url"] = self.url() auto_serialized["ui"] = [ui.serialize() for ui in self.ui.all()] auto_serialized["app"] = self.app.serialize() if self.dataset == "ei": auto_serialized["output_attr"] = "bra" elif self.output == "age" or self.output == "basic": auto_serialized["output_attr"] = "course_sc" elif self.output == "adm": auto_serialized["output_attr"] = "school" else: auto_serialized["output_attr"] = self.output return auto_serialized def __repr__(self): return '<Build %s:%r: %s/%s/%s>' % ( self.id, self.app.type, self.filter1, self.filter2, self.output)
class Bra(db.Model, AutoSerialize, Stats): __tablename__ = 'attrs_bra' id = db.Column(db.String(10), primary_key=True) id_ibge = db.Column(db.Integer(7)) name_en = db.Column(db.String(200)) name_pt = db.Column(db.String(200)) color = db.Column(db.String(7)) gender_pt = db.Column(db.String(1)) plural_pt = db.Column(db.Boolean()) article_pt = db.Column(db.Boolean()) distance = 0 # SECEX relations yb_secex = db.relationship("Yb_secex", backref='bra', lazy='dynamic') ybp = db.relationship("Ybp", backref='bra', lazy='dynamic') ybw = db.relationship("Ybw", backref='bra', lazy='dynamic') ybpw = db.relationship("Ybpw", backref='bra', lazy='dynamic') # RAIS relations yb_rais = db.relationship("Yb_rais", backref='bra', lazy='dynamic') ybi = db.relationship("Ybi", backref='bra', lazy='dynamic') ybo = db.relationship("Ybo", backref='bra', lazy='dynamic') ybio = db.relationship("Ybio", backref='bra', lazy='dynamic') # Neighbors neighbors = db.relationship( 'Distances', primaryjoin="(Bra.id == Distances.bra_id_origin)", backref='bra_origin', lazy='dynamic') bb = db.relationship('Distances', primaryjoin="(Bra.id == Distances.bra_id_dest)", backref='bra', lazy='dynamic') # Planning Regions pr = db.relationship('Bra', secondary=bra_pr, primaryjoin=(bra_pr.c.pr_id == id), secondaryjoin=(bra_pr.c.bra_id == id), backref=db.backref('bra', lazy='dynamic'), lazy='dynamic') pr2 = db.relationship('Bra', secondary=bra_pr, primaryjoin=(bra_pr.c.bra_id == id), secondaryjoin=(bra_pr.c.pr_id == id), backref=db.backref('bra2', lazy='dynamic'), lazy='dynamic') def name(self): lang = getattr(g, "locale", "en") return title_case(getattr(self, "name_" + lang)) def icon(self): return "/static/img/icons/bra/bra_%s.png" % (self.id[:2]) def get_neighbors(self, dist, remove_self=False): q = self.neighbors.filter(Distances.distance <= dist).order_by( Distances.distance) if remove_self: q = q.filter(Distances.bra_id_dest != self.id) # filter out self return q.all() def __repr__(self): return '<Bra %r>' % (self.name_en)