from flask import render_template, url_for, flash, redirect, request, Blueprint from flask_login import login_required, current_user from minibase.app import db import minibase.theme as theme from minibase.blueprints.company.models import Companies import minibase.blueprints.database.utils as dbUtils import minibase.blueprints.main.utils as mainUtils import minibase.blueprints.geography.utils as geoUtils import minibase.blueprints.company.utils as companyUtils from minibase.blueprints.company.forms import updateCompanyForm from minibase.blueprints.user.utils import save_picture from flask_wtf import FlaskForm # Declaring a blueprint company = Blueprint('company', __name__, template_folder='templates') @company.route("/", methods=['GET', 'POST']) def list(): page=request.args.get('page', 1, type=int) table=dbUtils.table_printable_paginate(Companies, page, 20, 'edit/', 'id') return(render_template('view.html', theme=theme, table=table, title="Companies")) @company.route("/edit/", methods=['GET', 'POST']) @login_required def edit_company(companyId): if companyId: company = Companies.query.get_or_404(companyId) form = updateCompanyForm() form.city.choices = [(row.id, row.name) for row in geoUtils.queryCiytNamesOfStateWithDefId(company.city_id, company.state_id)] form.state.choices = [(row.id, row.name) for row in geoUtils.queryStateNamesOfCuntryWithDefId(company.state_id, company.country_id)] form.country.choices = [(row.id, row.name) for row in geoUtils.queryCountryNamesWithDefault(company.country_id)] form.industry.choices = [(row.id, row.name) for row in mainUtils.queryIndustryNamesWithDefault(company.industry_id)] form.legal_entity.choices = [(row.id, row.name) for row in companyUtils.queryLegalEntityNamesWithDefault(company.legal_entity_id)] form.type.choices = [(row.id, row.name) for row in companyUtils.queryTypeNamesWithDefault(company.type_id)] form.relation.choices = [(row.id, row.name) for row in companyUtils.queryRelationNamesWithDefault(company.relation_id)] form.status.choices = [(row.id, row.name) for row in companyUtils.queryStatusNamesWithDefault(company.status_id)] if form.validate_on_submit(): company.name = form.name.data company.street = form.street.data company.website = form.website.data company.street_no = form.street_no.data company.post_code = form.post_code.data company.city_id = form.city.data company.state_id = form.state.data company.country_id = form.country.data company.industry_id = form.industry.data company.legal_entity_id = form.legal_entity.data company.type_id = form.type.data company.relation_id = form.relation.data company.status_id = form.status.data company.comment = form.comment.data flash('Company Has been successfully updated', 'success') return redirect(url_for('company.edit', companyId=companyId)) elif request.method == 'GET': form.name.data = company.name form.website.data = company.website form.street.data = company.street form.street_no.data = company.street_no form.post_code.data = company.post_code form.comment.data = company.comment form.legal_entity.data = company.legal_entity.name form.type.data = company.type.name image_file = url_for('static', filename='pics/' + companyUtils.queryImageById(companyId)) return render_template('company/account.html', theme=theme, image_file=image_file, form=form) else: flash('You need to select a company id', 'alarm') return redirect(url_for('company./')) # This could be very interesting but neds more time to think ! #for field in form: # if field.name != 'csrf_token' and hasattr(company, field.name): # attr = getattr(company, field.name) # field.data = attr