from minibase.database.models import Company, Company_relation, Company_legal_entity from minibase.database.models import Industry, Countries from minibase.database.models import Person, Person_role, Person_competence from minibase.database.models import Project, Project_element, Project_status, Project_note from minibase.database.models import Product from minibase import db from numpy import genfromtxt selectedCompany=0 selectedProject=0 # Gets the id of company from the formated output defined at models.py for the Company Model # The argument formatedCompanySelection is formated by SQLAlchemy in models.py files # Please look there before changing anything here. def getCompanyId(formatedCompanySelection): text = formatedCompanySelection.split(",") return text[2] # Corresponds to the ID of the Company # Gets the id of Person's role def getPersonRoleId(nameForId): selection = Person_role.query.filter_by(name=nameForId).first() return selection.id # Gets the id of Person's competence def getPersonCompetenceId(nameForId): selection = Person_competence.query.filter_by(name=nameForId).first() return selection.id # Gets the name based on Person's id def getPersonName(personId): selection = Person.query.filter_by(id=personId).first() return selection # Gets the country of the company based on it's id def getCompanyCountry(companyId): selection = Company.query.filter_by(id=companyId).first() return selection.country_bill # Gets the state of the company based on it's id def getCompanyState(companyId): selection = Company.query.filter_by(id=companyId).first() return selection.street_bill # Gets the city of the company based on it's id def getCompanyCity(companyId): selection = Company.query.filter_by(id=companyId).first() return selection.city_bill # Gets the Postal Code of the company based on it's id def getCompanyPostCode(companyId): selection = Company.query.filter_by(id=companyId).first() return selection.post_code_bill # Gets the Name of the street of the company based on it's id def getCompanyStreetName(companyId): selection = Company.query.filter_by(id=companyId).first() return selection.street_bill # Gets the Number of the street of the company based on it's id def getCompanyStreetNo(companyId): selection = Company.query.filter_by(id=companyId).first() return selection.street_no_bill # Gets the names the all the companies def getCompanyNames(): selection = Company.query.order_by(Company.name.asc()) return selection # Gets the Name of the street of the company based on it's id def getCompanyName(companyId): selection = Company.query.filter_by(id=companyId).first() return selection.name #################################################################################################### # Gets the Project class of based on it's id def getProject(projectId): selection = Project.query.filter_by(id=projectId).first() return selection # Gets the names the all the projects def getProjectNames(): selection = Project.query.order_by(Project.name.asc()) return selection # Gets the awailable Statuses of all the projects def getProjectStatuses(): selection = Project_status.query.order_by(Project_status.name.asc()) return selection # Gets the Name of the street of the company based on it's id def getProjectName(projectId): selection = Project.query.filter_by(id=projectId).first() return selection.name # Defines the selected company for the current request. This is important to fill so that some # Templates knows if they have to show something and warns the user if no compani was selected def setSelectedCompany(company_id): global selectedCompany selectedCompany = int(company_id) # Returns the Selected company def getSelectedCompany(): global selectedCompany return selectedCompany # Get the list of projects for the selected Company and if no company was selected # Prints an error that will be put in the selection form. def getProjectOfSelectedCompany(): global selectedCompany if selectedCompany: selection = Project.query.filter_by(company_id=selectedCompany) return selection return ["No company is selected"] # Get the list of projects for the selected Company and if no company was selected # Prints an error that will be put in the selection form. def getEmployeesOfSelectedCompany(): global selectedCompany if selectedCompany: selection = Person.query.filter_by(company_id=selectedCompany) return selection return ["No company is selected"] # Defines the selected project for the current request. This is important to fill so that some # Templates knows if they have to show something and warns the user if no compani was selected def setSelectedProject(project_id): global selectedProject selectedProject = int(project_id) # Returns the Selected project def getSelectedProject(): return selectedProject def getSelectedProjectOwner(): global selectedProject if selectedProject: selection = Project.query.filter_by(id=selectedProject).first() return getPersonName(selection.project_responsible.id) return ["No Project is selected"] def getSelectedProjectNotes(): global selectedProject if selectedProject: selection = Project_note.query.filter_by(project_id=selectedProject) return selection return ["No Project is selected"] # Get the project id based on the project's name and compan's id # As they can be project's that have the same name but corresponds to different companies. def getProjectId(companyId, projectName): global selectedCompany selection = Project.query.filter_by(company_id=companyId, name=projectName).first() return selection.id # Returns the query of all awailable companie names on the table named Project # Note that the formating is done during the SQLAlchemy Table declaration. def person_role_choices(): choices = Person_role.query.all() return choices # Returns the query of all awailable companie names on the table named Company # Note that the formating is done during the SQLAlchemy Table declaration. def person_competence_choices(): choices = Person_competence.query.all() return choices # Retunrs the qurry of all awailable industrie names on the table named Industry # Note that the formating is done during the SQLAlchemy Table declaration. def getIndustryNames(): choices = Industry.query.all() return choices # Retunrs the query of all awailable legal entity names on the table named Company_legal_entity # Note that the formating is done during the SQLAlchemy Table declaration. def company_legal_entity_choices(): choices = Company_legal_entity.query.all() return choices # Retunrs the query of all awailable Relation names on the table named Company_relation # Note that the formating is done during the SQLAlchemy Table declaration. def company_relation_choices(): choices = Company_relation.query.all() return choices # The Company Model has Industry Column as a foreign key and it requires the Industry's ID # And not the name. so this function returns the right ID of the name shown at the # Register Company Form def getIndustryId(nameForId): selection = Industry.query.filter_by(name=nameForId).first() # Gets the id of Role return selection.id # The Company Model has Relation Column as a foreign key and it requires the Industry's ID # And not the name. so this function returns the right ID of the name shown at the # Register Company Form def getRelationId(nameForId): selection = Company_relation.query.filter_by(name=nameForId).first() # Gets the id of Role return selection.id # The Company Model has Legal Entity Column as a foreign key and it requires the Industry's ID # And not the name. so this function returns the right ID of the name shown at the # Register Company Form def getLegalEntityId(nameForId): selection = Company_legal_entity.query.filter_by(name=nameForId).first() # Gets the id of Role return selection.id # Retunrs the query of all awailable Country names on the table named Countries # Note that the formating is done during the SQLAlchemy Table declaration. # Important note This table is ImporteD externally from a modifier SQL version of # Github : https://github.com/dr5hn/countries-states-cities-database def getCountryNames(): choices = Countries.query.all() return choices ################################################################################################### # CSV manipulation ################################################################################################### def openCsv(filename): data = genfromtxt(filename, delimiter=',', skip_header=1, dtype=None, encoding='UTF-8') return data.tolist() def db_add_name_and_description(csv, table): try: csv_list = openCsv(csv) for i in csv_list: record = table(**{ 'name': i[0], 'description': i[1]}) db.session.add(record) # Add all the records db.session.commit() # Attempt to commit all the records except Exception as error: db.session.rollback() # Rollback the changes on error print(csv) print("Error Ocured during <> upload to DB") print(error) def db_add_company(csv): try: csv_list = openCsv(csv) for i in csv_list: record = Company(**{ 'name': i[0], 'legal_entity_id': i[1], 'relation_id': i[2], 'industry_id': i[3], 'status_id': i[4], 'website': i[5], 'street_bill': i[6], 'street_no_bill': i[7], 'city_bill': i[8], 'post_code_bill': i[9], 'state_bill': i[10], 'country_bill': i[11], 'street_ship': i[12], 'street_no_ship': i[13], 'city_ship': i[14], 'post_code_ship': i[15], 'state_ship': i[16], 'country_ship': i[17]}) db.session.add(record) # Add all the records db.session.commit() # Attempt to commit all the records except Exception as error: db.session.rollback() # Rollback the changes on error print("Error Ocured during <> upload to DB") print(error) def db_add_person(csv): try: csv_list = openCsv(csv) for i in csv_list: record = Person(**{ 'name': i[0], 'last_name': i[1], 'company_id': i[2], 'role_id': i[3], 'competence_id': i[4], 'mail_prof': i[5], 'mail_priv': i[6], 'tel_prof_mobile': i[7], 'street_name': i[8], 'street_no': i[9], 'city': i[10], 'post_code': i[11], 'state': i[12], 'country': i[13] }) db.session.add(record) # Add all the records db.session.commit() # Attempt to commit all the records except Exception as error: db.session.rollback() # Rollback the changes on error print("Error Ocured during <> upload to DB") print(error) def db_add_project(csv): try: csv_list = openCsv(csv) for i in csv_list: record = Project(**{ 'name': i[0], 'description': i[1], 'company_id': i[2], 'status_id': i[3], 'industry_id': i[4], 'owner_id': i[5], 'qte_prototype': i[6], 'qte_start': i[7], 'qte_production': i[8] }) db.session.add(record) # Add all the records db.session.commit() # Attempt to commit all the records except Exception as error: db.session.rollback() # Rollback the changes on error print(csv) print("Error Ocured during <> upload to DB") print(error) def db_add_project_element(csv): try: csv_list = openCsv(csv) for i in csv_list: record = Project_element(**{ 'name': i[0], 'description': i[1], 'qte_per_project': i[2], 'project_id': i[3], 'owner_id': i[4], 'status_id': i[5]}) db.session.add(record) # Add all the records db.session.commit() # Attempt to commit all the records except Exception as error: db.session.rollback() # Rollback the changes on error print(csv) print("Error Ocured during <> upload to DB") print(error) def db_add_product(csv): try: csv_list = openCsv(csv) for i in csv_list: record = Product(**{ 'name': i[0], 'ordering_code': i[1], 'buy_cost': i[2], 'currency': i[3], 'description': i[4], 'lead_time_days': i[5], 'minimum_awarding_quantity': i[6], 'minimum_order_quantity': i[7], 'minimum_quote_quantity': i[8], 'classification_id': i[9], 'domain_id': i[10], 'category_id': i[11], 'sub_category_id': i[12], 'status_id': i[13], 'eligibility_id': i[14], 'packaging_id': i[15], 'physical_id': i[16], 'manufacturer_id': i[17]}) db.session.add(record) # Add all the records db.session.commit() # Attempt to commit all the records except Exception as error: db.session.rollback() # Rollback the changes on error print(csv) print("Error Ocured during <> upload to DB") print(error)