You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
4.2 KiB
102 lines
4.2 KiB
from flask import render_template, url_for, flash, redirect, request, Blueprint
|
|
from flask_login import login_required, current_user
|
|
import minibase.theme as theme
|
|
from minibase.blueprints.company.models import Companies
|
|
from minibase.blueprints.geography.models import City
|
|
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, addCompanyForm
|
|
|
|
# Declaring a blueprint
|
|
company = Blueprint('company', __name__, template_folder='templates')
|
|
|
|
@company.route("/list", methods=['GET', 'POST'])
|
|
def list():
|
|
page=request.args.get('page', 1, type=int)
|
|
table=dbUtils.table_printable_paginate(Companies, page, 20, 'account/', 'id')
|
|
return(render_template('view.html', theme=theme, table=table, title="Companies"))
|
|
|
|
|
|
@company.route("/account/<int:companyId>", methods=['GET', 'POST'])
|
|
@login_required
|
|
def account(companyId):
|
|
if companyId:
|
|
company = Companies.query.get_or_404(companyId)
|
|
form = updateCompanyForm()
|
|
|
|
form.populate_choices(company)
|
|
|
|
if form.validate_on_submit():
|
|
if form.image_file.data:
|
|
picture_file = mainUtils.save_picture(form.image_file.data)
|
|
company.image_file = picture_file
|
|
|
|
for item in form:
|
|
mainUtils.modelFill(company, item, ["image_file"])
|
|
|
|
dbUtils.dbCommit()
|
|
flash('Company Has been successfully updated', 'success')
|
|
return redirect(url_for('company.account', companyId=companyId))
|
|
|
|
elif request.method == 'GET':
|
|
for item in form:
|
|
item.data = mainUtils.formFill(item, company)
|
|
|
|
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.list'))
|
|
|
|
@company.route("/add", methods=['GET', 'POST'])
|
|
@login_required
|
|
def add():
|
|
form = addCompanyForm()
|
|
|
|
form.country.choices = [(row.id, row.name) for row in geoUtils.queryCountryNames()]
|
|
|
|
# This is for the htmx implementation. please be careful how of there is data we switch the funtion needed
|
|
if form.country.data:
|
|
form.city.choices = [(row.id, row.name) for row in geoUtils.queryCityNamesWithCountryId(form.country.data)]
|
|
form.state.choices = [(row.id, row.name) for row in geoUtils.queryStateNamesWithCountryId(form.country.data)]
|
|
else:
|
|
form.city.choices = []
|
|
form.state.choices = []
|
|
|
|
form.industry.choices = [(row.id, row.name) for row in mainUtils.queryIndustryNames()]
|
|
form.legal_entity.choices = [(row.id, row.name) for row in companyUtils.queryLegalEntityNames()]
|
|
form.type.choices = [(row.id, row.name) for row in companyUtils.queryTypeNames()]
|
|
form.relation.choices = [(row.id, row.name) for row in companyUtils.queryRelationNames()]
|
|
form.status.choices = [(row.id, row.name) for row in companyUtils.queryStatusNames()]
|
|
|
|
if form.validate_on_submit():
|
|
company = Companies(
|
|
name = form.name.data,
|
|
website = form.website.data,
|
|
country_id = form.country.data,
|
|
city_id = form.city.data,
|
|
state_id = form.state.data,
|
|
post_code = form.post_code.data,
|
|
street = form.street.data,
|
|
street_no = form.street_no.data,
|
|
industry_id = form.industry.data,
|
|
legal_entity_id = form.legal_entity.data,
|
|
type_id = form.type.data,
|
|
relation_id = form.relation.data,
|
|
status_id = form.status.data,
|
|
comment = form.comment.data)
|
|
dbUtils.dbAddAndCommit(company)
|
|
flash('Company Has been successfully added', 'success')
|
|
return redirect(url_for('company.list'))
|
|
|
|
return render_template('edit.html',
|
|
title="Add Company",
|
|
theme=theme,
|
|
form=form)
|