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 from minibase import db from numpy import genfromtxt # 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 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 # 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_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 # 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 company_choices(): choices = Company.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 company_industry_choices(): 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 country_choices(): 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: db.session.rollback() # Rollback the changes on error print("Error Ocured during <> upload to DB") 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) print(csv_list) 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) print(csv_list) 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) print(csv_list) 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)