def create_user(): form = SignupForm() if form.validate() is False: return make_response(jsonify(form.errors), 400) else: if (User.query.filter_by(email=form.email.data).count() > 0): return Response(dictionary()["email_already_exists"], status=400, mimetype='application/json') try: confirmation_code = _gen_confirmation_code(form.email.data) user = User( fullname=form.fullname.data, email=form.email.data, password=sha512(form.password.data), confirmation_code=confirmation_code, agree_mailer=form.agree_mailer.data ) db.session.add(user) db.session.commit() except: return Response(dictionary()["Sorry, an unexpected error has occured. Please try again"], status=500, mimetype='application/json') user_srl = user.serialize() send_confirmation(user) message = dictionary()["check_your_inbox"] + ' ' + user.email return Response(message, status=200, mimetype='application/json')
def create_user(): form = SignupForm() if form.validate() is False: return make_response(jsonify(form.errors), 400) else: if (User.query.filter_by(email=form.email.data).count() > 0): return Response(dictionary()["email_already_exists"], status=400, mimetype='application/json') try: confirmation_code = _gen_confirmation_code(form.email.data) user = User(fullname=form.fullname.data, email=form.email.data, password=sha512(form.password.data), confirmation_code=confirmation_code, agree_mailer=form.agree_mailer.data) db.session.add(user) db.session.commit() except: return Response( dictionary() ["Sorry, an unexpected error has occured. Please try again"], status=500, mimetype='application/json') user_srl = user.serialize() send_confirmation(user) message = dictionary()["check_your_inbox"] + ' ' + user.email return Response(message, status=200, mimetype='application/json')
def create(): form = ContactForm() if form.validate() is False: for error_type in form.errors: if form.errors[error_type][0] in dictionary(): form.errors[error_type][0] = dictionary()[ form.errors[error_type][0]] return render_template('contact/index.html', form=form, action=url_for('contact.create')) else: contact = Form() contact.name = form.name.data contact.email = form.email.data contact.subject = form.subject.data contact.message = form.message.data contact.postage_date = datetime.now().strftime('%Y-%m-%d %H:%M:%S') message_tpl = render_template('contact/message_template.html', contact=contact) db.session.add(contact) db.session.commit() send_mail("Contato - DataViva", [admin_email], message_tpl) message = gettext( "Your message has been sent successfully. We will soon get back to you." ) flash(message, 'success') return redirect(url_for('contact.create'))
def create(): form = ContactForm() if form.validate() is False: for error_type in form.errors: if form.errors[error_type][0] in dictionary(): form.errors[error_type][0] = dictionary()[form.errors[error_type][0]] return render_template('contact/index.html', form=form, action=url_for('contact.create')) else: contact = Form() contact.name = form.name.data contact.email = form.email.data contact.subject = form.subject.data contact.message = form.message.data contact.postage_date = datetime.now().strftime('%Y-%m-%d %H:%M:%S') message_tpl = render_template('contact/message_template.html', contact=contact) db.session.add(contact) db.session.commit() send_mail("Contato - DataViva", [admin_email], message_tpl) message = gettext("Your message has been sent successfully. We will soon get back to you.") flash(message, 'success') return redirect(url_for('contact.create'))
def change(): form = ChangePasswordForm() user = load_user(session["user_id"]) if form.validate(): if user.password == sha512(form.current_password.data): user.password = sha512(form.new_password.data) db.session.commit() flash(dictionary()["updated_password"], "success") else: flash(dictionary()["invalid_password"], "danger") return render_template("user/change_password.html", form=form)
def index(dataset=None, filter0=None, filter1=None, filter2=None): view = request.args.get('view') graph = request.args.get('graph') compare = request.args.get('compare') metadata = None build_query = Build.query.join(App).filter( Build.dataset == dataset, Build.filter1.like(parse_filter_id(filter1)), Build.filter2.like(parse_filter_id(filter2)), Build.slug2_en == view, App.type == graph) if graph: build = build_query.first_or_404() build.set_bra(filter0) if filter1 != 'all': build.set_filter1(filter1) if filter2 != 'all': build.set_filter2(filter2) service_id = filter1 if filter1 != u'all' else None year = ' - ' if dataset else '' if dataset == 'rais': year += str(CnaeService(service_id).get_year()) elif dataset == 'secex': year += str(SecexService(service_id).year()) elif dataset == 'hedu': year += str(HeduService(service_id).year()) elif dataset == 'sc': year += str(ScService(service_id).course_year()) title = re.sub(r'\s\(.*\)', r'', build.title()) metadata = { 'view': title, 'graph': dictionary()[graph], 'dataset': dictionary()[dataset] + year, } return render_template( 'build_graph/index.html', dataset=dataset, filter0=filter0, filter1=filter1, filter2=filter2, graph=graph, view=view, compare=compare, metadata=metadata)
def index(dataset, circles, focus): filters = [] services = {'id_ibge': location_service, 'product': product_service, 'occupation_family': occupation_service, 'industry_class': industry_service} if circles in services: focus = services[circles](focus)[1] for key, value in request.args.items(): if key == 'id_ibge': filters.append(location_service(value)) title_attrs[services[key](value)[0]] = services[key](value)[1] else: filters.append((key, value)) title_attrs = {circles: focus} filters = urllib.urlencode(filters) title, subtitle = get_title(dataset, circles, 'rings', title_attrs) return render_template('rings/index.html', dataset=dataset, circles=circles, focus=focus, filters=filters, title=title or '', subtitle=subtitle or '', dictionary=json.dumps(dictionary()))
def index(dataset, line, y_value): filters = [] title_attrs = {} services = {'product': product_service, 'id_ibge': location_service, 'wld': wld_service, 'occupation': occupation_service, 'industry': industry_service} for key, value in request.args.items(): if key == 'type': title_attrs['type'] = value if key in ['depths', 'values', 'group']: continue if value and key in services: filters.append(services[key](value)) title_attrs[services[key](value)[0]] = services[key](value)[1] else: if key == 'hedu_course' or key == 'university': title_attrs[key] = value filters.append((key, value)) filters = urllib.urlencode(filters) title, subtitle = get_title(dataset, line, 'line', title_attrs) return render_template('line/index.html', dataset=dataset, line=line, y_value=y_value, filters=filters, title=title or '', subtitle=subtitle or '', dictionary=json.dumps(dictionary()))
def before_request(): g.user = current_user g.accounts = True if ACCOUNTS in ["True","true","Yes","yes","Y","y",1] else False g.color = "#af1f24" g.dictionary = json.dumps(dictionary()) g.attr_version = 15 g.production = False if DEBUG else True g.contact_form = ContactForm() g.signup_form = SignupForm() g.signin_form = LoginForm() g.s3_host = s3_host g.s3_bucket = s3_bucket if request.endpoint != 'static': url = urlparse(request.url) url_path = url.path.split('/') g.locale = get_locale(lang=url_path[1]) # Check if the user is logged in, if so give the global object # a reference to the user from DB # if g.user.is_authenticated: # g.user.last_seen = datetime.utcnow() # db.session.add(g.user) # db.session.commit() if url_path[1] not in data_viva_apis: if g.locale not in url_path: if url.query: new_url= "{}://{}/{}{}?{}".format(url.scheme, url.netloc, g.locale, url.path, url.query) else: new_url= "{}://{}/{}{}".format(url.scheme, url.netloc, g.locale, url.path) return redirect(new_url)
def index(dataset, squares, size): filters = [] title_attrs = {} services = {'product': product_service, 'id_ibge': location_service, 'wld': wld_service, 'occupation': occupation_service, 'industry': industry_service, 'basic_course': sc_service} for key, value in request.args.items(): if key == 'type': title_attrs['type'] = value if key in ['depths', 'sizes', 'group', 'depth', 'color', 'filter', 'filters', 'hierarchy']: continue if value and key in services: filters.append(services[key](value)) title_attrs[services[key](value)[0]] = services[key](value)[1] else: if key in ['establishment', 'hedu_course', 'university', 'sc_course_field']: title_attrs[key] = value filters.append((key, value)) filters = urllib.urlencode(filters) title, subtitle = get_title(dataset, squares, 'tree_map', title_attrs) return render_template('tree_map/index.html', dataset=dataset, squares=squares, size=size, filters=filters, title=title or '', subtitle=subtitle or '', dictionary=json.dumps(dictionary()))
def before_request(): g.user = current_user g.accounts = True if ACCOUNTS in ["True","true","Yes","yes","Y","y",1] else False g.color = "#af1f24" g.dictionary = json.dumps(dictionary()) g.attr_version = 15 g.production = False if DEBUG else True g.contact_form = ContactForm() g.signup_form = SignupForm() if request.endpoint != 'static': url = urlparse(request.url) url_path = url.path.split('/') g.locale = get_locale(lang=url_path[1]) # Check if the user is logged in, if so give the global object # a reference to the user from DB # if g.user.is_authenticated: # g.user.last_seen = datetime.utcnow() # db.session.add(g.user) # db.session.commit() if url_path[1] not in data_viva_apis: if g.locale not in url_path: if url.query: new_url= "{}://{}/{}{}?{}".format(url.scheme, url.netloc, g.locale, url.path, url.query) else: new_url= "{}://{}/{}{}".format(url.scheme, url.netloc, g.locale, url.path) return redirect(new_url)
def index(dataset, circles, focus): filters = [] title_attrs = {} if dataset == 'rais' and circles == 'occupation_family': title_attrs[circles] = focus elif dataset == 'rais' and circles == 'industry_class': title_attrs[circles] = focus[1:6] elif dataset == 'secex' and circles == 'product': title_attrs[circles] = focus[2:6] services = {'id_ibge': location_service, 'product': product_service, 'occupation_family': occupation_service, 'industry_class': industry_service} if circles in services: focus = services[circles](focus)[1] for key, value in request.args.items(): if key == 'id_ibge': filters.append(location_service(value)) title_attrs[services[key](value)[0]] = services[key](value)[1] else: filters.append((key, value)) filters = urllib.urlencode(filters) title, subtitle = get_title(dataset, circles, 'rings', title_attrs) return render_template('rings/index.html', dataset=dataset, circles=circles, focus=focus, filters=filters, title=title or '', subtitle=subtitle or '', dictionary=json.dumps(dictionary()))
def before_request(): url = urlparse(request.url) url_path = url.path.split('/') g.locale = get_locale(lang=url_path[1]) if url_path[1] not in data_api: if request.endpoint != 'static' and g.locale not in url_path: if url.query: new_url= "{}://{}/{}{}?{}".format(url.scheme, url.netloc, g.locale, url.path, url.query) else: new_url= "{}://{}/{}{}".format(url.scheme, url.netloc, g.locale, url.path) return redirect(new_url) g.accounts = True if ACCOUNTS in ["True","true","Yes","yes","Y","y",1] else False g.color = "#af1f24" g.page_type = mod.name g.dictionary = json.dumps(dictionary()) g.attr_version = 14 g.production = False if DEBUG else True # Check if the user is logged in, if so give the global object # a reference to the user from DB g.user = current_user if g.user.is_authenticated() and request.endpoint != 'static': g.user.last_seen = datetime.utcnow() db.session.add(g.user) db.session.commit()
def errorLogin(html_form, status, form): if status == 401: message = dictionary()["confirmation_pending"] if html_form: flash(message, 'danger') else: return Response(message, status=401, mimetype='application/json') return redirect('/user/confirm_pending/' + form.email.data) elif status == 400: message = dictionary()["invalid_email_or_password"] if html_form: flash(message, 'danger') else: return Response(message, status=400, mimetype='application/json') return render_template('session/login.html', form=form)
def create(): csrf_token = request.form.get('csrf_token') upload_folder = os.path.join(app.config['UPLOAD_FOLDER'], mod.name, csrf_token, 'files') form = RegistrationForm() if not os.path.exists(upload_folder): flash(u'Selecione o arquivo do artigo para enviá-lo.', 'danger') return render_template('scholar/new.html', form=form) if form.validate() is False: form.set_choices(approved_articles_keywords()) return render_template('scholar/new.html', form=form) else: article = Article() article.title = form.title.data article.theme = form.theme.data article.abstract = form.abstract.data article.postage_date = datetime.now().strftime('%Y-%m-%d %H:%M:%S') article.approval_status = 0 author_input_list = form.authors.data.replace(', ', ',').split(',') for author_input in author_input_list: article.authors.append(AuthorScholar(author_input)) for keyword_input in form.keywords.data: keyword = KeyWord.query.filter_by(name=keyword_input).first() if not keyword: article.keywords.append(KeyWord(keyword_input)) else: article.keywords.append(keyword) db.session.add(article) db.session.flush() if os.path.exists(upload_folder): file_name = [file for file in os.listdir(upload_folder)][0] article.file_url = upload_helper.upload_s3_file( os.path.join(upload_folder, file_name), os.path.join('scholar/', str(article.id), 'files/', 'article'), { 'ContentType': "application/pdf", 'ContentDisposition': 'attachment; filename=dataviva-article-' + str(article.id) + '.pdf' } ) shutil.rmtree(os.path.split(upload_folder)[0]) db.session.commit() upload_helper.log_operation(module=mod.name, operation='create', user=(g.user.id, g.user.email), objs=[(article.id, article.title)]) new_article_advise(article, request.url_root) message = dictionary()["article_submission"] flash(message, 'success') return redirect(url_for('scholar.index'))
def render(self, type): if self.is_number(self.text): num = float(self.text) if "." in str(self.text) else int(self.text) return Markup(num_format(num, type)) else: dict = dictionary() if self.text in dict: return Markup(dict[self.text]) else: return Markup(title_case(self.text))
def reset_password(): form = ForgotPasswordForm() try: user = User.query.filter_by(email=form.email.data)[-1] pwd = md5(str(datetime.now()) + form.email.data).hexdigest()[0:5] user.password = sha512(pwd) db.session.commit() email_tp = render_template('user/mail/forgot.html', user=user.serialize(), new_pwd=pwd) send_mail("Forgot Password", [user.email], email_tp) flash(dictionary()["new_password_sent"], "success") except: flash(dictionary()["couldnt_find_user"], "danger") return render_template("user/forgot_password.html", form=form) return redirect(url_for('user.reset_password'))
def confirm(code): try: user = User.query.filter_by(confirmation_code=code)[-1] user.confirmed = True db.session.commit() login_user(user, remember=True) flash(dictionary()["complete_profile"], "info") except IndexError: abort(404, 'User not found') return redirect(url_for('user.edit'))
def resend_confirmation(user_email): '''Used to regen the confirmation_code and send the email again to the user ''' try: user = User.query.filter_by(email=user_email, confirmed=False)[-1] except IndexError: abort(404, 'Entry not found') user.confirmation_code = _gen_confirmation_code(user.email) db.session.commit() send_confirmation(user) flash(dictionary()["check_your_inbox"] + ' ' + user_email, 'success') return redirect(url_for('user.confirm_pending', user_email=user.email))
def change_profile(): form = ProfileForm() if form.validate(): try: user = g.user user.profile = form.profile.data user.fullname = form.fullname.data user.email = form.email.data user.birthday = form.birthday.data user.country = form.country.data user.state_province_region = form.state_province_region.data user.city = form.city.data user.occupation = form.occupation.data user.institution = form.institution.data user.agree_mailer = form.agree_mailer.data db.session.commit() flash(dictionary()["updated_profile"], "success") except: flash(dictionary()["500"], "danger") return render_template("user/edit.html", form=form)
def create(): form = SignupForm() if request.method == "POST": if form.validate() is False: if 'fullname' in form.errors: return Response(form.errors['fullname'], status=400, mimetype='application/json') if 'email' in form.errors: return Response(form.errors['email'], status=400, mimetype='application/json') if 'password' in form.errors: return Response(form.errors['password'], status=400, mimetype='application/json') return Response('Error in Form.', status=400, mimetype='application/json') else: if (User.query.filter_by(email=form.email.data).count() > 0): return Response(dictionary()["email_already_exists"], status=400, mimetype='application/json') try: confirmation_code = _gen_confirmation_code(form.email.data) user = User( nickname=form.email.data.split('@')[0], fullname=form.fullname.data, email=form.email.data, password=sha512(form.password.data), confirmation_code=confirmation_code, agree_mailer=form.agree_mailer.data ) db.session.add(user) db.session.commit() except: return Response(dictionary()["500"], status=500, mimetype='application/json') send_confirmation(user) message = dictionary()["check_your_inbox"] + ' ' + user.email return Response(message, status=200, mimetype='application/json') return render_template('user/new.html', form=form)
def index(dataset, area, value): group = request.args.get('group', '') type = request.args.get('type', '') depths = request.args.get('depths', '') values = request.args.get('values', value) title_attrs = {} filters = [] services = { 'product': product_service, 'id_ibge': location_service, 'wld': wld_service, 'occupation': occupation_service, 'industry': industry_service, 'basic_course': sc_service, } for key, value in request.args.items(): if key in ['type', 'establishment', 'university', 'hedu_course']: title_attrs[key] = value if key in ['depths', 'values', 'group', 'filters', 'hierarchy' ] and value: continue if value and key in services: filters.append(services[key](value)) title_attrs[services[key](value)[0]] = services[key](value)[1] else: filters.append((key, value)) filters = urllib.urlencode(filters) title, subtitle = get_title(dataset, area, 'stacked', title_attrs) return render_template('stacked/index.html', dataset=dataset, area=area, type=type, group=group, depths=depths, values=values, title=title or '', subtitle=subtitle or '', filters=filters, dictionary=json.dumps(dictionary()))
def index(dataset, area, value): group = request.args.get('group', '') type = request.args.get('type', '') depths = request.args.get('depths', '') values = request.args.get('values', value) title_attrs = {} filters = [] services = { 'product': product_service, 'id_ibge': location_service, 'wld': wld_service, 'occupation': occupation_service, 'industry': industry_service, 'basic_course': sc_service, } for key, value in request.args.items(): if key in ['type', 'establishment', 'university', 'hedu_course']: title_attrs[key] = value if key in ['depths', 'values', 'group', 'filters', 'hierarchy'] and value: continue if value and key in services: filters.append(services[key](value)) title_attrs[services[key](value)[0]] = services[key](value)[1] else: filters.append((key, value)) filters = urllib.urlencode(filters) title, subtitle = get_title(dataset, area, 'stacked', title_attrs) return render_template('stacked/index.html', dataset=dataset, area=area, type=type, group=group, depths=depths, values=values, title=title or '', subtitle=subtitle or '', filters=filters, dictionary=json.dumps(dictionary()))
def index(dataset, squares, size): filters = [] title_attrs = {} services = { 'product': product_service, 'id_ibge': location_service, 'wld': wld_service, 'occupation': occupation_service, 'industry': industry_service, 'basic_course': sc_service } for key, value in request.args.items(): if key == 'type': title_attrs['type'] = value if key in [ 'depths', 'sizes', 'group', 'depth', 'color', 'filter', 'filters', 'hierarchy' ]: continue if value and key in services: filters.append(services[key](value)) title_attrs[services[key](value)[0]] = services[key](value)[1] else: if key in [ 'establishment', 'hedu_course', 'university', 'sc_course_field' ]: title_attrs[key] = value filters.append((key, value)) filters = urllib.urlencode(filters) title, subtitle = get_title(dataset, squares, 'tree_map', title_attrs) return render_template('tree_map/index.html', dataset=dataset, squares=squares, size=size, filters=filters, title=title or '', subtitle=subtitle or '', dictionary=json.dumps(dictionary()))
def signin(): form = SigninForm() if request.method == "POST": try: user = User.query.filter_by( email=form.email.data, password=sha512(form.password.data) )[-1] login_user(user, remember=True) redir = request.args.get("next", "/") return redirect(redir) except: return Response(dictionary()["invalid_password"], status=400, mimetype='application/json') else: next = request.args.get("next", "") return render_template('account/signin.html', form=form, next=next)
def signin(): form = SigninForm() if request.method == "POST": try: user = User.query.filter_by(email=form.email.data, password=sha512( form.password.data))[-1] login_user(user, remember=True) redir = request.args.get("next", "/") return redirect(redir) except: return Response(dictionary()["invalid_password"], status=400, mimetype='application/json') else: next = request.args.get("next", "") return render_template('account/signin.html', form=form, next=next)
def index(dataset, value, id_ibge): filters = [] title_attrs = {} services = { 'product': product_service, 'id_ibge': location_service, 'wld': wld_service, 'occupation': occupation_service, 'industry': industry_service, 'basic_course': sc_service, } for k, v in request.args.items(): if k not in ['values', 'filters', 'count', 'year']: if v and k in services: filters.append(services[k](v)) title_attrs[services[k](v)[0]] = services[k](v)[1] else: if k != 'colors': filters.append((k, v)) title_attrs[k] = v if id_ibge: location = location_service(id_ibge)[0] filters.append((location, id_ibge)) state = '' if location == 'region' else id_ibge[:2] title_attrs[location] = id_ibge else: state = id_ibge location = 'municipality' filters = urllib.urlencode(filters) title, subtitle = get_title(dataset, value, 'map', title_attrs) return render_template('map/index.html', dataset=dataset, value=value, state=state, filters=filters, title=title or '', subtitle=subtitle or '', dictionary=json.dumps(dictionary()))
def before_request(): g.accounts = True if ACCOUNTS in ["True","true","Yes","yes","Y","y",1] else False g.color = "#af1f24" g.page_type = mod.name g.dictionary = json.dumps(dictionary()) g.attr_version = 14 g.production = False if DEBUG else True # Check if the user is logged in, if so give the global object # a reference to the user from DB g.user = current_user if g.user.is_authenticated() and request.endpoint != 'static': g.user.last_seen = datetime.utcnow() db.session.add(g.user) db.session.commit() # Set the locale to either 'pt' or 'en' on the global object if request.endpoint != 'static': # Determine subdomain (if specified) url = urlparse(request.url) subdomain = None domain = url.netloc.split('.') if domain[0] == "en" or domain[0] == "pt": subdomain = domain.pop(0) domain = u".".join(domain) # Get lang w/ subdomain trumping all g.locale = get_locale(lang=subdomain) # If subdomain not specified redirect TO subdomain w/ "best match" lang if not subdomain: if url.query: new_url = "{}://{}.{}{}?{}".format(url.scheme, g.locale, domain, url.path, url.query) else: new_url = "{}://{}.{}{}".format(url.scheme, g.locale, domain, url.path) return redirect(new_url)
def index(dataset, line, y_value): filters = [] title_attrs = {} services = { 'product': product_service, 'id_ibge': location_service, 'wld': wld_service, 'occupation': occupation_service, 'industry': industry_service } for key, value in request.args.items(): if key == 'type': title_attrs['type'] = value if key in ['depths', 'values', 'group']: continue if value and key in services: filters.append(services[key](value)) title_attrs[services[key](value)[0]] = services[key](value)[1] else: if key == 'hedu_course' or key == 'university': title_attrs[key] = value filters.append((key, value)) filters = urllib.urlencode(filters) title, subtitle = get_title(dataset, line, 'line', title_attrs) return render_template('line/index.html', dataset=dataset, line=line, y_value=y_value, filters=filters, title=title or '', subtitle=subtitle or '', dictionary=json.dumps(dictionary()))
def index(wld_id, tab): bra_id = request.args.get('bra_id') menu = request.args.get('menu') url = request.args.get('url') graph = {} if menu: graph['menu'] = menu if url: graph['url'] = url trade_partner = Wld.query.filter_by(id=wld_id).first_or_404() location = Bra.query.filter_by(id=bra_id).first() max_year_query = db.session.query( func.max(Ymw.year)).filter_by(wld_id=wld_id) if bra_id: trade_partner_service = TradePartner(wld_id, bra_id) municipalities_service = TradePartnerMunicipalities(wld_id, bra_id) products_service = TradePartnerProducts(wld_id, bra_id) export_rank_query = Ymbw.query.join(Wld).filter( Ymbw.wld_id_len == len(wld_id), Ymbw.bra_id == bra_id, Ymbw.month == 0, Ymbw.year == max_year_query).order_by(Ymbw.export_val.desc()) import_rank_query = Ymbw.query.join(Wld).filter( Ymbw.wld_id_len == len(wld_id), Ymbw.bra_id == bra_id, Ymbw.month == 0, Ymbw.year == max_year_query).order_by(Ymbw.import_val.desc()) else: trade_partner_service = TradePartner(wld_id, None) municipalities_service = TradePartnerMunicipalities(wld_id, None) products_service = TradePartnerProducts(wld_id, None) export_rank_query = Ymw.query.join(Wld).filter( Ymw.wld_id_len == len(wld_id), Ymw.month == 0, Ymw.year == max_year_query).order_by(Ymw.export_val.desc()) import_rank_query = Ymw.query.join(Wld).filter( Ymw.wld_id_len == len(wld_id), Ymw.month == 0, Ymw.year == max_year_query).order_by(Ymw.import_val.desc()) export_rank = export_rank_query.all() import_rank = import_rank_query.all() if not bra_id: header = { 'continent_id': wld_id[0:2], 'name': trade_partner_service.country_name(), 'year': trade_partner_service.year(), 'trade_balance': trade_partner_service.trade_balance(), 'total_exported': trade_partner_service.total_exported(), 'unity_weight_export_price': trade_partner_service.unity_weight_export_price(), 'total_imported': trade_partner_service.total_imported(), 'unity_weight_import_price': trade_partner_service.unity_weight_import_price(), 'wld_id': wld_id, 'bra_id': bra_id } else: header = { 'continent_id': wld_id[0:2], 'name': trade_partner_service.country_name(), 'year': trade_partner_service.year(), 'trade_balance': trade_partner_service.trade_balance(), 'total_exported': trade_partner_service.total_exported(), 'unity_weight_export_price': trade_partner_service.unity_weight_export_price(), 'total_imported': trade_partner_service.total_imported(), 'unity_weight_import_price': trade_partner_service.unity_weight_import_price(), 'wld_id': wld_id, 'bra_id': bra_id, 'location_name': trade_partner_service.location_name(), 'location_type': dictionary()['bra_'+str(len(bra_id))] } body = { 'municipality_with_more_exports': municipalities_service.municipality_with_more_exports(), 'municipality_with_more_exports_state': municipalities_service.municipality_with_more_exports_state(), 'highest_export_value': municipalities_service.highest_export_value(), 'municipality_with_more_imports': municipalities_service.municipality_with_more_imports(), 'municipality_with_more_imports_state': municipalities_service.municipality_with_more_imports_state(), 'highest_import_value': municipalities_service.highest_import_value(), 'product_with_more_imports': products_service.product_with_more_imports(), 'product_with_highest_import_value': products_service.highest_import_value(), 'product_with_more_exports': products_service.product_with_more_exports(), 'product_with_highest_export_value': products_service.highest_export_value(), 'product_with_highest_balance': products_service.product_with_highest_balance(), 'highest_balance': products_service.highest_balance(), 'product_with_lowest_balance': products_service.product_with_lowest_balance(), 'lowest_balance': products_service.lowest_balance() } tabs = { 'general': [], 'international-trade': [ 'trade-balance-partner-line', 'exports-municipality-tree_map', 'exports-municipality-stacked', 'exports-destination-tree_map', 'exports-destination-stacked', 'exports-destination-geo_map', 'imports-municipality-tree_map', 'imports-municipality-stacked', 'imports-origin-tree_map', 'imports-origin-stacked', 'imports-origin-geo_map', ], } for index, trade_partner_ranking in enumerate(export_rank): if export_rank[index].wld_id == wld_id: header['export_rank'] = index + 1 break for index, trade_partner_ranking in enumerate(import_rank): if import_rank[index].wld_id == wld_id: header['import_rank'] = index + 1 break secex_max_year = db.session.query(func.max(Ymw.year)).filter( Ymw.month == 12).first()[0] if body['highest_export_value'] is None and body['highest_import_value'] is None: abort(404) if secex_max_year != header['year']: abort(404) if tab not in tabs: abort(404) if menu and menu not in tabs[tab]: abort(404) return render_template('trade_partner/index.html', body_class='perfil-estado', header=header, body=body, trade_partner=trade_partner, location=location, tab=tab, graph=graph)
def index(dataset, x, y): product = request.args.get('product') id_ibge = request.args.get('id_ibge') type = request.args.get('type') wld = request.args.get('wld') establishment = request.args.get('establishment') occupation = request.args.get('occupation') industry = request.args.get('industry') counts = request.args.getlist('count') title_attrs = {} options = request.args.get('options') subtitle = request.args.get('subtitle', '') filters = [] for count in counts: filters.append(('count', count)) if type: filters.append(('type', type)) title_attrs['type'] = type if request.args.get('filters'): filters.append(('filters', request.args.get('filters'))) if wld: filters.append(wld_service(wld)) title_attrs[wld_service(wld)[0]] = wld_service(wld)[1] if occupation: filters.append(occupation_service(occupation)) title_attrs[occupation_service(occupation)[0]] = occupation_service( occupation)[1] if industry: filters.append(industry_service(industry)) title_attrs[industry_service(industry)[0]] = industry_service( industry)[1] if product: filters.append(product_service(product)) title_attrs[product_service(product)[0]] = product_service(product)[1] if id_ibge: filters.append(location_service(id_ibge)) title_attrs[location_service(id_ibge)[0]] = location_service( id_ibge)[1] if establishment: filters.append(('establishment', establishment)) filters = urllib.urlencode(filters) graph_title, graph_subtitle = get_title(dataset, y.split(',')[0], 'bar', title_attrs) return render_template('bar/index.html', dataset=dataset, x=x, y=y, filters=filters, options=options, subtitle=subtitle, graph_title=graph_title or '', graph_subtitle=graph_subtitle or '', dictionary=json.dumps(dictionary()))
def index(wld_id): bra_id = request.args.get('bra_id') trade_partner = Wld.query.filter_by(id=wld_id).first_or_404() location = Bra.query.filter_by(id=bra_id).first() max_year_query = db.session.query(func.max( Ymw.year)).filter_by(wld_id=wld_id) if bra_id: trade_partner_service = TradePartner(wld_id, bra_id) municipalities_service = TradePartnerMunicipalities(wld_id, bra_id) products_service = TradePartnerProducts(wld_id, bra_id) export_rank_query = Ymbw.query.join(Wld).filter( Ymbw.wld_id_len == len(wld_id), Ymbw.bra_id == bra_id, Ymbw.month == 0, Ymbw.year == max_year_query).order_by(Ymbw.export_val.desc()) import_rank_query = Ymbw.query.join(Wld).filter( Ymbw.wld_id_len == len(wld_id), Ymbw.bra_id == bra_id, Ymbw.month == 0, Ymbw.year == max_year_query).order_by(Ymbw.import_val.desc()) else: trade_partner_service = TradePartner(wld_id, None) municipalities_service = TradePartnerMunicipalities(wld_id, None) products_service = TradePartnerProducts(wld_id, None) export_rank_query = Ymw.query.join(Wld).filter( Ymw.wld_id_len == len(wld_id), Ymw.month == 0, Ymw.year == max_year_query).order_by(Ymw.export_val.desc()) import_rank_query = Ymw.query.join(Wld).filter( Ymw.wld_id_len == len(wld_id), Ymw.month == 0, Ymw.year == max_year_query).order_by(Ymw.import_val.desc()) export_rank = export_rank_query.all() import_rank = import_rank_query.all() if not bra_id: header = { 'continent_id': wld_id[0:2], 'name': trade_partner_service.country_name(), 'year': trade_partner_service.year(), 'trade_balance': trade_partner_service.trade_balance(), 'total_exported': trade_partner_service.total_exported(), 'unity_weight_export_price': trade_partner_service.unity_weight_export_price(), 'total_imported': trade_partner_service.total_imported(), 'unity_weight_import_price': trade_partner_service.unity_weight_import_price(), 'wld_id': wld_id, 'bra_id': bra_id } else: header = { 'continent_id': wld_id[0:2], 'name': trade_partner_service.country_name(), 'year': trade_partner_service.year(), 'trade_balance': trade_partner_service.trade_balance(), 'total_exported': trade_partner_service.total_exported(), 'unity_weight_export_price': trade_partner_service.unity_weight_export_price(), 'total_imported': trade_partner_service.total_imported(), 'unity_weight_import_price': trade_partner_service.unity_weight_import_price(), 'wld_id': wld_id, 'bra_id': bra_id, 'location_name': trade_partner_service.location_name(), 'location_type': dictionary()['bra_' + str(len(bra_id))] } body = { 'municipality_with_more_exports': municipalities_service.municipality_with_more_exports(), 'municipality_with_more_exports_state': municipalities_service.municipality_with_more_exports_state(), 'highest_export_value': municipalities_service.highest_export_value(), 'municipality_with_more_imports': municipalities_service.municipality_with_more_imports(), 'municipality_with_more_imports_state': municipalities_service.municipality_with_more_imports_state(), 'highest_import_value': municipalities_service.highest_import_value(), 'product_with_more_imports': products_service.product_with_more_imports(), 'product_with_highest_import_value': products_service.highest_import_value(), 'product_with_more_exports': products_service.product_with_more_exports(), 'product_with_highest_export_value': products_service.highest_export_value(), 'product_with_highest_balance': products_service.product_with_highest_balance(), 'highest_balance': products_service.highest_balance(), 'product_with_lowest_balance': products_service.product_with_lowest_balance(), 'lowest_balance': products_service.lowest_balance() } for index, trade_partner_ranking in enumerate(export_rank): if export_rank[index].wld_id == wld_id: header['export_rank'] = index + 1 break for index, trade_partner_ranking in enumerate(import_rank): if import_rank[index].wld_id == wld_id: header['import_rank'] = index + 1 break secex_max_year = db.session.query(func.max( Ymw.year)).filter(Ymw.month == 12).first()[0] if body['highest_export_value'] is None and body[ 'highest_import_value'] is None: abort(404) if secex_max_year != header['year']: abort(404) else: return render_template('trade_partner/index.html', body_class='perfil-estado', header=header, body=body, trade_partner=trade_partner, location=location)
def index(data_type="rais", year="all", bra_id=None, filter_1=None, filter_2=None): # /hedu/all/show.3/01298.show.5/all/ def get_filter_by_id(f): match = [v for v in filters if v["id"] == f] if match: return match[0] return None datasets = [["rais", _('Wages and Employment')], ["secex", _('International Trade')], ["hedu", _('Higher Education')], ["sc", _('School Census')]] # ["ei", _('Electronic Invoice')]] for d in datasets: start_year, end_year = __year_range__[d[0]] years = reversed( range(int(start_year.split("-")[0]), int(end_year.split("-")[0]) + 1)) d.append(years) if "-" in start_year: months = range(int(start_year.split("-")[1]), int(end_year.split("-")[1]) + 1) months = [(m, dt.strptime(str(m), "%m").strftime("%B")) for m in months] d.append(months) trans_lookup = dictionary() filters = [ { "id": "bra", "datasets": "rais secex hedu sc", "nestings": [1, 3, 5, 7, 9] }, # {"id": "bra_r", "datasets": "ei", "nestings": [1, 3, 5, 7, 9]}, # {"id": "bra_s", "datasets": "ei", "nestings": [1, 3, 5, 7, 9]}, { "id": "cbo", "datasets": "rais", "nestings": [1, 4] }, { "id": "cnae", "datasets": "rais", "nestings": [1, 3, 6] }, { "id": "hs", "datasets": "secex", "nestings": [2, 6] }, { "id": "wld", "datasets": "secex", "nestings": [2, 5] }, { "id": "university", "datasets": "hedu", "nestings": [5] }, { "id": "course_hedu", "datasets": "hedu", "nestings": [2, 6] }, { "id": "course_sc", "datasets": "sc", "nestings": [2, 5] } ] for f in filters: if "nestings" in f: if "bra" in f["id"]: attr_type = f["id"].replace("_r", "").replace("_s", "") else: attr_type = f["id"] f["nestings"] = [(n, trans_lookup["{}_{}".format(attr_type, n)]) for n in f["nestings"]] f["name"] = trans_lookup[f["id"]] selected_filters = [] ''' Determine the selected dataset/filters/output from URL ''' output = {"filters": []} # get selected dataset output["dataset"] = [v for v in datasets if v[0] == data_type][0] # get filters filter_type_lookup = { "rais": [("bra", Bra), ("cnae", Cnae), ("cbo", Cbo)], "secex": [("bra", Bra), ("hs", Hs), ("wld", Wld)], "hedu": [("bra", Bra), ("university", University), ("course_hedu", Course_hedu)], "sc": [("bra", Bra), None, ("course_sc", Course_sc)] # "ei": [("bra_r", Bra), ("bra_s", Bra)] } # parse year if year == "all": output["year"] = year elif "-" in year: y, m = year.split("-") if y != "all": output["year"] = int(y) else: output["year"] = y output["month"] = int(m) else: output["year"] = int(year) if bra_id and filter_1: for i, f in enumerate([bra_id, filter_1, filter_2]): filter = f depth = None if f == "all" or f is None: continue if f == "show": filter = None if "show" in f: output_filter = filter_type_lookup[output["dataset"][0]][i][0] output["output_filter"] = get_filter_by_id(output_filter) if "." in f: f_split = f.split(".") if len(f_split) == 3: filter, show, depth = f_split if len(f_split) == 2: filter = None show, depth = f_split if depth: output["nesting"] = int(depth) if filter: filter_type = filter_type_lookup[output["dataset"][0]][i] possible_datasets = get_filter_by_id( filter_type[0])["datasets"] filter = filter_type[1].query.get(filter) output["filters"].append({ "id": filter_type[0], "datasets": possible_datasets, "filter": filter }) else: output["output_filter"] = get_filter_by_id("bra") output["nesting"] = 9 filter = Bra.query.get("4mg") output["filters"].append({ "id": "bra", "datasets": "rais secex hedu sc", "filter": filter }) # raise Exception(output["dataset"], datasets, output["dataset"] == datasets[0]) return render_template("data/index.html", datasets=datasets, filters=filters, selected_filters=selected_filters, output=output)
def index(data_type="rais", year="all", bra_id=None, filter_1=None, filter_2=None): # /hedu/all/show.3/01298.show.5/all/ def get_filter_by_id(f): match = [v for v in filters if v["id"] == f] if match: return match[0] return None datasets = [["rais", _('Wages and Employment')], ["secex", _('International Trade')], ["hedu", _('Higher Education')], ["sc", _('School Census')]] for d in datasets: start_year, end_year = __year_range__[d[0]] years = reversed(range(int(start_year.split("-")[0]), int(end_year.split("-")[0])+1)) d.append(years) if "-" in start_year: months = range(int(start_year.split("-")[1]), int(end_year.split("-")[1])+1) months = [(m, dt.strptime(str(m),"%m").strftime("%B")) for m in months] d.append(months) trans_lookup = dictionary() filters = [ {"id": "bra", "datasets": "rais secex hedu sc", "nestings": [1, 3, 5, 7, 9]}, {"id": "cbo", "datasets": "rais", "nestings":[1, 4]}, {"id": "cnae", "datasets": "rais", "nestings":[1, 3, 6]}, {"id": "hs", "datasets": "secex", "nestings":[2, 6]}, {"id": "wld", "datasets": "secex", "nestings":[2, 5]}, {"id": "university", "datasets": "hedu", "nestings":[5]}, {"id": "course_hedu", "datasets": "hedu", "nestings":[2, 6]}, {"id": "course_sc", "datasets": "sc", "nestings": [2, 5]} ] for f in filters: if "nestings" in f: if "bra" in f["id"]: attr_type = f["id"].replace("_r", "").replace("_s", "") else: attr_type = f["id"] f["nestings"] = [(n, trans_lookup["{}_{}".format(attr_type, n)]) for n in f["nestings"]] f["name"] = trans_lookup[f["id"]] selected_filters = [] ''' Determine the selected dataset/filters/output from URL ''' output = {"filters":[]} # get selected dataset output["dataset"] = [v for v in datasets if v[0] == data_type][0] # get filters filter_type_lookup = { "rais": [("bra", Bra), ("cnae", Cnae), ("cbo", Cbo)], "secex": [("bra", Bra), ("hs", Hs), ("wld", Wld)], "hedu": [("bra", Bra), ("university", University), ("course_hedu", Course_hedu)], "sc": [("bra", Bra), None, ("course_sc", Course_sc)] } # parse year if year == "all": output["year"] = year elif "-" in year: y, m = year.split("-") if y != "all": output["year"] = int(y) else: output["year"] = y output["month"] = int(m) else: output["year"] = int(year) if bra_id and filter_1: for i, f in enumerate([bra_id, filter_1, filter_2]): filter = f depth = None if f == "all" or f is None: continue if f == "show": filter = None if "show" in f: output_filter = filter_type_lookup[output["dataset"][0]][i][0] output["output_filter"] = get_filter_by_id(output_filter) if "." in f: f_split = f.split(".") if len(f_split) == 3: filter, show, depth = f_split if len(f_split) == 2: filter = None show, depth = f_split if depth: output["nesting"] = int(depth) if filter: filter_type = filter_type_lookup[output["dataset"][0]][i] possible_datasets = get_filter_by_id(filter_type[0])["datasets"] filter = filter_type[1].query.get(filter) output["filters"].append({"id":filter_type[0], "datasets":possible_datasets, "filter":filter}) else: output["output_filter"] = get_filter_by_id("bra") output["nesting"] = 9 filter = Bra.query.get("4mg") output["filters"].append({"id":"bra", "datasets":"rais secex hedu sc", "filter":filter}) # raise Exception(output["dataset"], datasets, output["dataset"] == datasets[0]) return render_template("data/index.html", datasets=datasets, filters=filters, selected_filters=selected_filters, output=output)
def index(wld_id, tab): bra_id = request.args.get('bra_id') menu = request.args.get('menu') url = request.args.get('url') graph = {} if menu: graph['menu'] = menu if url: url_prefix = menu.split( '-')[-1] + '/' if menu and menu.startswith('new-api-') else 'embed/' graph['url'] = url_prefix + url if wld_id == 'sabra': abort(404) trade_partner = Wld.query.filter_by(id=wld_id).first_or_404() location = Bra.query.filter_by(id=bra_id).first() max_year_query = db.session.query( func.max(Ymw.year)).filter_by(wld_id=wld_id) bra_id = bra_id if bra_id != 'all' else None if not bra_id: depth = None id_ibge = None else: depth = location_depth(bra_id) id_ibge = location_service(depth, location) if bra_id: trade_partner_service = TradePartner(wld_id, bra_id) municipalities_service = TradePartnerMunicipalities(wld_id, bra_id) products_service = TradePartnerProducts(wld_id, bra_id) export_rank_query = Ymbw.query.join(Wld).filter( Ymbw.wld_id_len == len(wld_id), Ymbw.bra_id == bra_id, Ymbw.month == 0, Ymbw.year == max_year_query).order_by(Ymbw.export_val.desc()) import_rank_query = Ymbw.query.join(Wld).filter( Ymbw.wld_id_len == len(wld_id), Ymbw.bra_id == bra_id, Ymbw.month == 0, Ymbw.year == max_year_query).order_by(Ymbw.import_val.desc()) else: trade_partner_service = TradePartner(wld_id, None) municipalities_service = TradePartnerMunicipalities(wld_id, None) products_service = TradePartnerProducts(wld_id, None) export_rank_query = Ymw.query.join(Wld).filter( Ymw.wld_id_len == len(wld_id), Ymw.month == 0, Ymw.year == max_year_query).order_by(Ymw.export_val.desc()) import_rank_query = Ymw.query.join(Wld).filter( Ymw.wld_id_len == len(wld_id), Ymw.month == 0, Ymw.year == max_year_query).order_by(Ymw.import_val.desc()) export_rank = export_rank_query.all() import_rank = import_rank_query.all() if not bra_id: header = { 'name': trade_partner_service.country_name(), 'year': trade_partner_service.year(), 'total_exported': trade_partner_service.total_exported(), 'total_imported': trade_partner_service.total_imported(), 'wld_id': wld_id, 'bra_id': bra_id } else: header = { 'name': trade_partner_service.country_name(), 'year': trade_partner_service.year(), 'total_exported': trade_partner_service.total_exported(), 'total_imported': trade_partner_service.total_imported(), 'wld_id': wld_id, 'bra_id': bra_id, 'location_name': trade_partner_service.location_name(), 'location_type': dictionary()['bra_' + str(len(bra_id))] } body = { 'highest_export_value': municipalities_service.highest_export_value(), 'product_with_more_exports': products_service.product_with_more_exports(), 'product_with_highest_export_value': products_service.highest_export_value(), } tabs = { 'general': [], 'international-trade': [ 'trade-balance-partner-line', 'new-api-trade-balance-partner-line', 'exports-municipality-tree_map', 'new-api-exports-municipality-tree_map', 'exports-municipality-stacked', 'new-api-exports-municipality-stacked', 'new-api-expots-port-line', 'exports-destination-tree_map', 'new-api-exports-destination-tree_map', 'exports-destination-stacked', 'new-api-exports-destination-stacked', 'exports-destination-geo_map', 'imports-municipality-tree_map', 'new-api-imports-municipality-tree_map', 'imports-municipality-stacked', 'new-api-imports-municipality-stacked', 'new-api-imports-port-line', 'imports-origin-tree_map', 'new-api-imports-origin-tree_map', 'imports-origin-stacked', 'new-api-imports-origin-stacked', 'imports-origin-geo_map', ], } for index, trade_partner_ranking in enumerate(export_rank): if export_rank[index].wld_id == wld_id: header['export_rank'] = index + 1 break for index, trade_partner_ranking in enumerate(import_rank): if import_rank[index].wld_id == wld_id: header['import_rank'] = index + 1 break if body['highest_export_value'] is None and body['highest_import_value'] is None: abort(404) if tab not in tabs: abort(404) if menu and menu not in tabs[tab]: abort(404) return render_template('trade_partner/index.html', body_class='perfil-estado', header=header, body=body, trade_partner=trade_partner, location=location, tab=tab, graph=graph, id_ibge=id_ibge)
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 index(dataset, x, y): product = request.args.get('product') id_ibge = request.args.get('id_ibge') type = request.args.get('type') wld = request.args.get('wld') excluded_year= request.args.get('excluded_year') establishment = request.args.get('establishment') occupation = request.args.get('occupation') industry = request.args.get('industry') counts = request.args.getlist('count') title_attrs = {} options = request.args.get('options') subtitle = request.args.get('subtitle', '') filters = [] for count in counts: filters.append(('count', count)) if type: filters.append(('type', type)) title_attrs['type'] = type if excluded_year: filters.append(('year!', excluded_year)) if request.args.get('filters'): filters.append(('filters', request.args.get('filters'))) if wld: filters.append(wld_service(wld)) title_attrs[wld_service(wld)[0]] = wld_service(wld)[1] if occupation: filters.append(occupation_service(occupation)) title_attrs[occupation_service(occupation)[0]] = occupation_service(occupation)[1] if industry: filters.append(industry_service(industry)) title_attrs[industry_service(industry)[0]] = industry_service(industry)[1] if product: filters.append(product_service(product)) title_attrs[product_service(product)[0]] = product_service(product)[1] if id_ibge: filters.append(location_service(id_ibge)) title_attrs[location_service(id_ibge)[0]] = location_service(id_ibge)[1] if establishment: filters.append(('establishment', establishment)) filters = urllib.urlencode(filters) graph_title, graph_subtitle = get_title(dataset, y.split(',')[0], 'bar', title_attrs) return render_template('bar/index.html', dataset=dataset, x=x, y=y, filters=filters, options=options, subtitle=subtitle, graph_title=graph_title or '', graph_subtitle=graph_subtitle or '', dictionary=json.dumps(dictionary()))
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 index(wld_id, tab): bra_id = request.args.get('bra_id') menu = request.args.get('menu') url = request.args.get('url') graph = {} if menu: graph['menu'] = menu if url: url_prefix = menu.split('-')[-1] + '/' if menu and menu.startswith( 'new-api-') else 'embed/' graph['url'] = url_prefix + url if wld_id == 'sabra': abort(404) trade_partner = Wld.query.filter_by(id=wld_id).first_or_404() location = Bra.query.filter_by(id=bra_id).first() max_year_query = db.session.query(func.max( Ymw.year)).filter_by(wld_id=wld_id) bra_id = bra_id if bra_id != 'all' else None if not bra_id: depth = None id_ibge = None else: depth = location_depth(bra_id) id_ibge = location_service(depth, location) if bra_id: trade_partner_service = TradePartner(wld_id, bra_id) municipalities_service = TradePartnerMunicipalities(wld_id, bra_id) products_service = TradePartnerProducts(wld_id, bra_id) export_rank_query = Ymbw.query.join(Wld).filter( Ymbw.wld_id_len == len(wld_id), Ymbw.bra_id == bra_id, Ymbw.month == 0, Ymbw.year == max_year_query).order_by(Ymbw.export_val.desc()) import_rank_query = Ymbw.query.join(Wld).filter( Ymbw.wld_id_len == len(wld_id), Ymbw.bra_id == bra_id, Ymbw.month == 0, Ymbw.year == max_year_query).order_by(Ymbw.import_val.desc()) else: trade_partner_service = TradePartner(wld_id, None) municipalities_service = TradePartnerMunicipalities(wld_id, None) products_service = TradePartnerProducts(wld_id, None) export_rank_query = Ymw.query.join(Wld).filter( Ymw.wld_id_len == len(wld_id), Ymw.month == 0, Ymw.year == max_year_query).order_by(Ymw.export_val.desc()) import_rank_query = Ymw.query.join(Wld).filter( Ymw.wld_id_len == len(wld_id), Ymw.month == 0, Ymw.year == max_year_query).order_by(Ymw.import_val.desc()) export_rank = export_rank_query.all() import_rank = import_rank_query.all() if not bra_id: header = { 'continent_id': wld_id[0:2], 'name': trade_partner_service.country_name(), 'year': trade_partner_service.year(), 'trade_balance': trade_partner_service.trade_balance(), 'total_exported': trade_partner_service.total_exported(), 'unity_weight_export_price': trade_partner_service.unity_weight_export_price(), 'total_imported': trade_partner_service.total_imported(), 'unity_weight_import_price': trade_partner_service.unity_weight_import_price(), 'wld_id': wld_id, 'bra_id': bra_id } else: header = { 'continent_id': wld_id[0:2], 'name': trade_partner_service.country_name(), 'year': trade_partner_service.year(), 'trade_balance': trade_partner_service.trade_balance(), 'total_exported': trade_partner_service.total_exported(), 'unity_weight_export_price': trade_partner_service.unity_weight_export_price(), 'total_imported': trade_partner_service.total_imported(), 'unity_weight_import_price': trade_partner_service.unity_weight_import_price(), 'wld_id': wld_id, 'bra_id': bra_id, 'location_name': trade_partner_service.location_name(), 'location_type': dictionary()['bra_' + str(len(bra_id))] } body = { 'municipality_with_more_exports': municipalities_service.municipality_with_more_exports(), 'municipality_with_more_exports_state': municipalities_service.municipality_with_more_exports_state(), 'highest_export_value': municipalities_service.highest_export_value(), 'municipality_with_more_imports': municipalities_service.municipality_with_more_imports(), 'municipality_with_more_imports_state': municipalities_service.municipality_with_more_imports_state(), 'highest_import_value': municipalities_service.highest_import_value(), 'product_with_more_imports': products_service.product_with_more_imports(), 'product_with_highest_import_value': products_service.highest_import_value(), 'product_with_more_exports': products_service.product_with_more_exports(), 'product_with_highest_export_value': products_service.highest_export_value(), 'product_with_highest_balance': products_service.product_with_highest_balance(), 'highest_balance': products_service.highest_balance(), 'product_with_lowest_balance': products_service.product_with_lowest_balance(), 'lowest_balance': products_service.lowest_balance() } tabs = { 'general': [], 'international-trade': [ 'trade-balance-partner-line', 'new-api-trade-balance-partner-line', 'exports-municipality-tree_map', 'new-api-exports-municipality-tree_map', 'exports-municipality-stacked', 'new-api-exports-municipality-stacked', 'new-api-expots-port-line', 'exports-destination-tree_map', 'new-api-exports-destination-tree_map', 'exports-destination-stacked', 'new-api-exports-destination-stacked', 'exports-destination-geo_map', 'imports-municipality-tree_map', 'new-api-imports-municipality-tree_map', 'imports-municipality-stacked', 'new-api-imports-municipality-stacked', 'new-api-imports-port-line', 'imports-origin-tree_map', 'new-api-imports-origin-tree_map', 'imports-origin-stacked', 'new-api-imports-origin-stacked', 'imports-origin-geo_map', ], } for index, trade_partner_ranking in enumerate(export_rank): if export_rank[index].wld_id == wld_id: header['export_rank'] = index + 1 break for index, trade_partner_ranking in enumerate(import_rank): if import_rank[index].wld_id == wld_id: header['import_rank'] = index + 1 break secex_max_year = db.session.query(func.max( Ymw.year)).filter(Ymw.month == 12).first()[0] if body['highest_export_value'] is None and body[ 'highest_import_value'] is None: abort(404) if secex_max_year != header['year']: abort(404) if tab not in tabs: abort(404) if menu and menu not in tabs[tab]: abort(404) return render_template('trade_partner/index.html', body_class='perfil-estado', header=header, body=body, trade_partner=trade_partner, location=location, tab=tab, graph=graph, id_ibge=id_ibge)