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.

124 lines
5.9 KiB

from flask import render_template, url_for, flash, redirect, request, Blueprint, session
from minibase import db
from minibase.config import themeMinibase, globalVars
from minibase.project.forms import getProjectNameForm, projectUpdateForm, projectRegisterForm
import minibase.database.utils as dbUtils
from minibase.database.models import Project
# Declaring a blueprint
project = Blueprint('project', __name__)
@project.route("/select_project", methods=['GET', 'POST'])
def select_project():
if globalVars.selectedCompany: # If a company is sected
form = getProjectNameForm()
if form.validate_on_submit():
projectId = dbUtils.getProjectId(globalVars.selectedCompany.id, form.project.data)
dbUtils.setSelectedProject(projectId)
return redirect(url_for('project.show_project'))
return render_template('project/select_project.html',
title='Select Project Name for : ' + globalVars.selectedCompany.name,
theme=themeMinibase,
globalVars=globalVars,
form=form)
else:
flash(f'{"Please selecet a company first"}', 'warning')
return redirect(url_for('company.select_company'))
@project.route("/show_project", methods=['GET', 'POST'])
def show_project():
if globalVars.selectedProject: # If a company is sected
form = projectUpdateForm()
# I can't use globalVars here for sqlAlchemy to be able to make a session an sql query must be made and returned
project = dbUtils.getSelectedProject()
image_file = url_for('static', filename='pics/' + project.image_file)
notes = dbUtils.getSelectedProjectNotes()
# To initiate choises as a list allow us to match it with the id's
form.responsible.choices = [(row.id, row.name +' '+row.last_name) for row in dbUtils.getEmployeesOfSelectedCompany()]
form.status.choices = [(row.id, row.name) for row in dbUtils.getProjectStatuses()]
form.industry.choices = [(row.id, row.name) for row in dbUtils.getIndustryNames()]
if form.validate_on_submit():
flash(f'{"Project Updated"}', 'success')
project.description = form.description.data
project.industry_id = int(form.industry.data)
project.status_id = int(form.status.data)
project.owner_id = int(form.responsible.data)
project.qte_prototype = form.qtePrototype.data
project.qte_start = form.qteStart.data
project.qte_production = form.qteProduction.data
project.date_prototype = form.datePrototype.data
project.date_start = form.dateStart.data
project.date_production = form.dateProduction.data
db.session.add(project)
db.session.commit()
return redirect(url_for('project.show_project'))
elif request.method == 'GET':
form.responsible.data = str(project.owner_id)
form.status.data = str(project.status.id)
form.industry.data = str(project.industry_id)
form.description.data = project.description
form.qtePrototype.data = project.qte_prototype
form.qteStart.data = project.qte_start
form.qteProduction.data = project.qte_production
form.datePrototype.data = project.date_prototype
form.dateStart.data = project.date_start
form.dateProduction.data = project.date_production
return render_template('project/show_project.html',
title=project.belongs_to.name + ' : ' + project.name,
companyName=project.belongs_to.name,
projectName=project.name,
projectId=project.id,
notes=notes,
image_file=image_file,
theme=themeMinibase,
globalVars=globalVars,
form=form)
else:
flash(f'{"Please selecet a project first"}', 'warning')
return redirect(url_for('project.select_project'))
@project.route("/register_project", methods=['GET', 'POST'])
def register_project():
form = projectRegisterForm()
form.company.choices = [(row.id, row.name) for row in dbUtils.getCompanies()]
form.status.choices = [(row.id, row.name) for row in dbUtils.getProjectStatuses()]
form.industry.choices = [(row.id, row.name) for row in dbUtils.getIndustryNames()]
if form.validate_on_submit():
flash(f'{"Project Updated"}', 'success')
project = Project(
name=form.name.data,
company_id=form.company.data,
description=form.description.data,
industry_id=form.industry.data,
status_id=form.status.data,
qte_prototype=form.qtePrototype.data,
qte_start=form.qteStart.data,
qte_production=form.qteProduction.data,
date_prototype=form.datePrototype.data,
date_start=form.dateStart.data,
date_production=form.dateProduction.data)
# Adds project to the database
db.session.add(project)
db.session.commit()
# Prepares the globalVars for redirection.
dbUtils.setSelectedCompany(form.company.data)
projectId = dbUtils.getProjectId(globalVars.selectedCompany.id, form.name.data)
# Prepares the globalVars for redirection.
dbUtils.setSelectedProject(projectId)
flash(f'{"Project Registered"}', 'success')
# Wihtout the selectedProject and selectedCompny Varibale the redirect would give an error
return redirect(url_for('project.show_project'))
return render_template('register_form.html',
title="Register Project",
theme=themeMinibase,
globalVars=globalVars,
form=form)