def get_all_students(): if not is_teacher(): return ERRORS.INVALID_CREDENTIALS full = False r = get_request() if 'full' in r.args: full = is_truthy(r.args['full']) if not full: return flask.jsonify(Etudiant.query.all()) all_stu = [e.to_json(full) for e in Etudiant.query.all()] companies = {} for student in all_stu: for job in student['jobs']: if job['company']['id'] not in companies: companies[job['company']['id']] = job['company'] job['company'] = job['company']['id'] for internship in student['internships']: if internship['company']['id'] not in companies: companies[internship['company'] ['id']] = internship['company'] internship['company'] = internship['company']['id'] return flask.jsonify({'students': all_stu, 'companies': companies})
def see_token(): page = 0 length = 20 r = get_request() if r.args.get('page') is not None: try: choosen_page = int(r.args.get('page')) if choosen_page >= 0: page = choosen_page except: return ERRORS.BAD_REQUEST if r.args.get('count') is not None: try: choosen_count = int(r.args.get('count')) if 0 < choosen_count <= 100: length = choosen_count except: return ERRORS.BAD_REQUEST start = page * length end = (page + 1) * length # Teachers are allowed to see tokens of all users (may be heavy) if is_teacher(): return flask.jsonify(Token.query.all()[start:end]) # Send all tokens of logged user id_etu = get_user().id_etu return flask.jsonify(Token.query.filter_by(id_etu=id_etu).all()[start:end])
def make_domain(): r = get_request() if not is_teacher(): return ERRORS.INVALID_CREDENTIALS if not r.is_json: return ERRORS.BAD_REQUEST data = r.json if not {'domain', 'name'} <= set(data): return ERRORS.MISSING_PARAMETERS domain, nom = data['domain'], data['name'] ## Search for similar domains f = Domaine.query.filter(Domaine.domaine.ilike(f"{domain}")).all() if len(f): return flask.jsonify(f[0]), 200 # Create new domain dom = Domaine.create(domaine=domain, nom=nom) db_session.add(dom) db_session.commit() return flask.jsonify(dom), 201
def modify_entreprise(): if not is_teacher(): return ERRORS.INVALID_CREDENTIALS r = get_request() if not r.is_json: return ERRORS.INVALID_INPUT_TYPE data = r.json if not {'name', 'town', 'size', 'status', 'id'} <= set(data): return ERRORS.MISSING_PARAMETERS if type(data['id']) is not int: return ERRORS.INVALID_INPUT_TYPE e: Entreprise = Entreprise.query.filter_by( id_entreprise=int(data['id'])).one_or_none() if not e: return ERRORS.COMPANY_NOT_FOUND name, city, size, status = data['name'], data['town'], data[ 'size'], data['status'] if type(name) is not str: return ERRORS.INVALID_INPUT_TYPE special_check = r"^[\w_ -]+$" if not re.match(special_check, name): return ERRORS.INVALID_INPUT_VALUE e.nom = name if city != e.ville: gps_coords = get_location_of_company(city) e.ville = city e.lat = gps_coords[0] e.lng = gps_coords[1] if type(size) is not str: return ERRORS.INVALID_INPUT_TYPE valid_comp_size = {"small", "big", "medium", "very_big"} if size not in valid_comp_size: return ERRORS.UNEXPECTED_INPUT_VALUE e.taille = size if type(status) is not str: return ERRORS.INVALID_INPUT_TYPE valid_comp_status = {"public", "private"} if status not in valid_comp_status: return ERRORS.UNEXPECTED_INPUT_VALUE e.statut = status db_session.commit() return flask.jsonify(e)
def delete_domain(id: int): if not is_teacher(): return ERRORS.INVALID_CREDENTIALS d: Domaine = Domaine.query.filter_by(id_domaine=id).one_or_none() if not d: return "" other_domain: Domaine = Domaine.query.filter_by( domaine="other").one_or_none() domain_id = None if other_domain: domain_id = other_domain.id_domaine if other_domain.id_domaine == id: return ERRORS.BAD_REQUEST Stage.query.filter_by(id_domaine=id).update({"id_domaine": domain_id}) Emploi.query.filter_by(id_domaine=id).update({"id_domaine": domain_id}) db_session.delete(d) db_session.commit() return ""
def home_statistics(): if not is_teacher(): return ERRORS.INVALID_CREDENTIALS # Obtient le nombre d'étudiants enregistrés stu_count = Etudiant.query.count() # Nombre d'étudiant occupant un emploi actuellement stu_with_job_count = Etudiant.query.join(Emploi).filter_by( fin=None).distinct(Etudiant.id_etu).count() # Nombre d'entreprises dans lesquelles les étudiants occupent actuellement un emploi entreprises_count = Entreprise.query.join(Emploi).filter_by( fin=None).distinct(Entreprise.id_entreprise).count() # Nombre d'étudiants diplômés stu_graduated_count = Etudiant.query.filter_by(diplome=True).count() # Nombre d'étudiants en formation stu_in_formation_count = Etudiant.query.filter_by( diplome=False, annee_sortie=None).count() # Nombre d'étudiants en thèse / ayant fait une thèse stu_in_thesis_count = Etudiant.query.join(Emploi).filter_by( contrat="these").count() return flask.jsonify({ "students": stu_count, "students_currently_working": stu_with_job_count, "companies_with_work": entreprises_count, "graduated": stu_graduated_count, "thesis": stu_in_thesis_count, "in_formation": stu_in_formation_count, })
def create_token_ask(): if not is_teacher(): return ERRORS.INVALID_CREDENTIALS r = get_request() if not r.is_json: return ERRORS.BAD_REQUEST data = r.json if 'mail' not in data: return ERRORS.BAD_REQUEST e = Etudiant.query.filter_by(mail=data['mail']).one_or_none() if e: return ERRORS.CONFLICT mail = data['mail'] # Send the mail send_invite_create_profile_mail(mail) return ""
def send_mails(): if not is_teacher(): return ERRORS.INVALID_CREDENTIALS r = get_request() if not r.is_json: return ERRORS.BAD_REQUEST data = r.json if not {'content', 'to', 'object'} <= set(data): return ERRORS.MISSING_PARAMETERS content, to, obj = data['content'], data['to'], data['object'] # If $to is not a list, or $to is a empty list, or some $to elements are not strings if type(to) is not list or len(to) == 0 or any( map(lambda x: type(x) is not str, to)): return ERRORS.INVALID_INPUT_TYPE # Send the mail... send_basic_mail(content, to, obj) return ""
def modify_internship(): r = get_request() stu = get_student_or_none() if not stu or not r.is_json: return ERRORS.BAD_REQUEST data = r.json if not 'internship' in data: return ERRORS.MISSING_PARAMETERS internship: Stage = Stage.query.filter_by(id_stage=data['internship']).one_or_none() if not internship: return ERRORS.RESOURCE_NOT_FOUND if not is_teacher() and internship.id_etu != stu.id_etu: return ERRORS.INVALID_CREDENTIALS if 'promo_year' in data: internship.promo = data['promo_year'] if 'company' in data: ent: Entreprise = Entreprise.query.filter_by(id_entreprise=data['company']).one_or_none() if not ent: db_session.rollback() return ERRORS.COMPANY_NOT_FOUND internship.id_entreprise = ent.id_entreprise if 'domain' in data: dom: Domaine = Domaine.query.filter_by(domaine=data['domain']).one_or_none() if not dom: db_session.rollback() return ERRORS.DOMAIN_NOT_FOUND internship.id_domaine = dom.id_domaine if 'contact' in data: if data['contact'] is None: internship.id_contact = None else: cont: Contact = Contact.query.filter_by(id_contact=data['contact']).one_or_none() if not cont: db_session.rollback() return ERRORS.CONTACT_NOT_FOUND internship.id_contact = cont.id_contact stu.refresh_update() db_session.commit() return flask.jsonify(internship)
def get_id(id: int): if not is_teacher(): return ERRORS.INVALID_CREDENTIALS e: Etudiant = Etudiant.query.filter_by(id_etu=id).one_or_none() if not e: return ERRORS.STUDENT_NOT_FOUND return flask.jsonify(e)
def get_a_internship(id: int): internship: Stage = Stage.query.filter_by(id_stage=id).one_or_none() if internship is None: return ERRORS.RESOURCE_NOT_FOUND stu = get_student_or_none() if not is_teacher(): if not stu or stu.id_etu != internship.id_etu: return ERRORS.INVALID_CREDENTIALS return flask.jsonify(internship)
def merge_companies(): if not is_teacher(): return ERRORS.INVALID_CREDENTIALS r = get_request() if not r.is_json: return ERRORS.BAD_REQUEST data = r.json if not {'main', 'children'} <= set(data): return ERRORS.MISSING_PARAMETERS main, children = data['main'], data['children'] if type(main) is not int or type(children) is not list: return ERRORS.BAD_REQUEST main_company: Entreprise = Entreprise.query.filter_by( id_entreprise=main).one_or_none() if not main_company: return ERRORS.COMPANY_NOT_FOUND children_companies: List[Entreprise] = [] for c in children: if type(c) is not int: return ERRORS.BAD_REQUEST ent = Entreprise.query.filter_by(id_entreprise=c).one_or_none() if not ent: return ERRORS.COMPANY_NOT_FOUND children_companies.append(ent) # For each job/internship relied to children_companies, set main_company for c in children_companies: Emploi.query.filter_by(id_entreprise=c.id_entreprise).update( {'id_entreprise': main_company.id_entreprise}) Stage.query.filter_by(id_entreprise=c.id_entreprise).update( {'id_entreprise': main_company.id_entreprise}) # Delete every children company for c in children_companies: Contact.query.filter_by(id_entreprise=c.id_entreprise).update( {'id_entreprise': main_company.id_entreprise}) db_session.delete(c) db_session.commit() return ""
def merge_domains(): if not is_teacher(): return ERRORS.INVALID_CREDENTIALS r = get_request() if not r.is_json: return ERRORS.BAD_REQUEST data = r.json if not {'main', 'children'} <= set(data): return ERRORS.MISSING_PARAMETERS main, children = data['main'], data['children'] if type(main) is not int or type(children) is not list: return ERRORS.BAD_REQUEST main_domaine: Domaine = Domaine.query.filter_by( id_domaine=main).one_or_none() if not main_domaine: return ERRORS.DOMAIN_NOT_FOUND children_domains: List[Domaine] = [] for c in children: if type(c) is not int: return ERRORS.BAD_REQUEST ent = Domaine.query.filter_by(id_domaine=c).one_or_none() if not ent: return ERRORS.DOMAIN_NOT_FOUND children_domains.append(ent) # For each domain relied to children_domains, set main_domaine for c in children_domains: Stage.query.filter_by(id_domaine=c.id_domaine).update( {'id_domaine': main_domaine.id_domaine}) Emploi.query.filter_by(id_domaine=c.id_domaine).update( {'id_domaine': main_domaine.id_domaine}) # Delete every children domain for c in children_formations: db_session.delete(c) db_session.commit() return ""
def merge_formations(): if not is_teacher(): return ERRORS.INVALID_CREDENTIALS r = get_request() if not r.is_json: return ERRORS.BAD_REQUEST data = r.json if not {'main', 'children'} <= set(data): return ERRORS.MISSING_PARAMETERS main, children = data['main'], data['children'] if type(main) is not int or type(children) is not list: return ERRORS.BAD_REQUEST main_formation: Formation = Formation.query.filter_by( id_form=main).one_or_none() if not main_formation: return ERRORS.FORMATION_NOT_FOUND children_formations: List[Formation] = [] for c in children: if type(c) is not int: return ERRORS.BAD_REQUEST ent = Formation.query.filter_by(id_form=c).one_or_none() if not ent: return ERRORS.FORMATION_NOT_FOUND children_formations.append(ent) # For each student relied to children_formations, set main_formation for c in children_formations: Etudiant.query.filter_by(reorientation=c.id_form).update( {'reorientation': main_formation.id_form}) Etudiant.query.filter_by(cursus_anterieur=c.id_form).update( {'cursus_anterieur': main_formation.id_form}) # Delete every children company for c in children_formations: db_session.delete(c) db_session.commit() return ""
def get_a_job(id: int): job: Emploi = Emploi.query.filter_by(id_emploi=id).one_or_none() if job is None: return ERRORS.RESOURCE_NOT_FOUND stu = get_student_or_none() if not stu: return ERRORS.STUDENT_NOT_FOUND if not is_teacher() and stu.id_etu != job.id_etu: return ERRORS.INVALID_CREDENTIALS return flask.jsonify(job)
def delete_contact(id: int): if not is_teacher(): return ERRORS.INVALID_CREDENTIALS c = Contact.query.filter_by(id_contact=id).one_or_none() if not c: return "" Emploi.query.filter_by(id_contact=id).update({"id_contact": None}) Stage.query.filter_by(id_contact=id).update({"id_contact": None}) db_session.delete(c) db_session.commit() return ""
def delete_student(id: int): if not is_teacher(): return ERRORS.INVALID_CREDENTIALS # Check if exists etu = Etudiant.query.filter_by(id_etu=id).one_or_none() if not etu: return "" Etudiant.query.filter_by(id_etu=id).delete() # delete cascade does not work?? Token.query.filter_by(id_etu=id).delete() db_session.commit() return ""
def modify_contact(): if not is_teacher(): return ERRORS.INVALID_CREDENTIALS r = get_request() if not r.is_json: return ERRORS.BAD_REQUEST data = r.json if not {'name', 'mail', 'id'} <= set(data): return ERRORS.MISSING_PARAMETERS name, mail, id_contact = data['name'], data['mail'], data['id'] if type(id_contact) is not int: return ERRORS.INVALID_INPUT_TYPE c: Contact = Contact.query.filter_by( id_contact=id_contact).one_or_none() if not c: return ERRORS.CONTACT_NOT_FOUND if type(name) is not str: return ERRORS.INVALID_INPUT_TYPE special_check = r"^[\w_ -]+$" if not re.match(special_check, name): return ERRORS.INVALID_INPUT_VALUE if type(mail) is not str: return ERRORS.INVALID_INPUT_TYPE email_catch = r"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$" if not re.match(email_catch, mail): return ERRORS.INVALID_EMAIL # Create new contact c.mail = mail c.nom = name db_session.commit() return flask.jsonify(c)
def get_student_or_none() -> Optional[Etudiant]: r = get_request() if is_teacher(): if r.args.get('id') or r.args.get('user_id'): try: u_id = r.args.get('id') if r.args.get('id') else r.args.get( 'user_id') return Etudiant.query.filter_by(id_etu=int(u_id)).one_or_none() except: return None elif r.is_json and 'id' in r.json or 'user_id' in r.json: try: u_id = r.json['id'] if 'id' in r.json else r.json['user_id'] return Etudiant.query.filter_by(id_etu=int(u_id)).one_or_none() except: return None else: return get_etu_object_for_logged_user()
def delete_company(id: int): if not is_teacher(): return ERRORS.INVALID_CREDENTIALS c: Entreprise = Entreprise.query.filter_by( id_entreprise=id).one_or_none() if not c: return "" # Delete all manuel Stage.query.filter_by(id_entreprise=id).delete() Emploi.query.filter_by(id_entreprise=id).delete() Contact.query.filter_by(id_entreprise=id).delete() db_session.delete(c) db_session.commit() return ""
def modify_formation(): r = get_request() if not is_teacher() or not r.is_json: return ERRORS.BAD_REQUEST data = r.json if not {'branch', 'location', 'level', 'id'} <= set(data): return ERRORS.MISSING_PARAMETERS branch, location, level, id_formation = data['branch'], data[ 'location'], data['level'], data['id'] if type(id_formation) is not int: return ERRORS.INVALID_INPUT_TYPE # Check level: must be in ENUM f: Formation = Formation.query.filter_by( id_form=id_formation).one_or_none() if not f: return ERRORS.FORMATION_NOT_FOUND if type(branch) is not str: return ERRORS.INVALID_INPUT_TYPE f.filiere = branch # Query le lieu pr obtenir lat & long si lieu != location if f.lieu != location: f.lieu = location if type(level) is not str: return ERRORS.INVALID_INPUT_TYPE valid_levels = {"licence", "master", "phd", "other"} if level not in valid_levels: return ERRORS.UNEXPECTED_INPUT_VALUE f.niveau = level db_session.commit() return flask.jsonify(f)
def delete_job(id: int): job: Emploi = Emploi.query.filter_by(id_emploi=id).one_or_none() if job is None: return "" # 200 OK deleted stu = get_student_or_none() if not stu: return ERRORS.STUDENT_NOT_FOUND if not is_teacher() and stu.id_etu != job.id_etu: return ERRORS.INVALID_CREDENTIALS # Properly delete job (maybe cascade is not working) stu.refresh_update() db_session.delete(job) db_session.commit() return ""
def delete_internship(id: int): internship: Stage = Stage.query.filter_by(id_stage=id).one_or_none() if internship is None: return "" # 200 OK deleted stu = get_student_or_none() if not stu: return ERRORS.STUDENT_NOT_FOUND if not is_teacher() and stu.id_etu != internship.id_etu: return ERRORS.INVALID_CREDENTIALS # Properly delete internship (maybe cascade is not working) stu.refresh_update() db_session.delete(internship) db_session.commit() return ""
def invalidate_token(): r = get_request() token = r.headers.get('Authorization').replace('Bearer ', '', 1) if is_teacher(): Token.query.filter_by(token=token).delete() db_session.commit() return "" else: current_etu_id = get_user().id_etu t: Token = Token.query.filter_by(token=token).one_or_none() if not t: return ERRORS.NOT_FOUND if t.id_etu == current_etu_id: db_session.delete(t) db_session.commit() else: return ERRORS.INVALID_CREDENTIALS
def delete_formation(id: int): # Get logged etudiant if not is_teacher(): return ERRORS.INVALID_CREDENTIALS form: Formation = Formation.query.filter_by(id_form=id).one_or_none() if not form: return "" # Supprime les formations des étudiants for etu in Etudiant.query.filter_by(cursus_anterieur=id).all(): etu.cursus_anterieur = None for etu in Etudiant.query.filter_by(reorientation=id).all(): e.reorientation = None db_session.commit() return ""
def ask_refresh(): if not is_teacher(): return ERRORS.INVALID_CREDENTIALS r = get_request() data = r.json if not 'ids' in data or type(data['ids']) is not list: return ERRORS.BAD_REQUEST for id_etu in data['ids']: st: Etudiant = Etudiant.query.filter_by( id_etu=id_etu).one_or_none() if not st: return ERRORS.STUDENT_NOT_FOUND send_ask_relogin_mail(st.id_etu) return ""
def modify_domain(): r = get_request() if not is_teacher(): return ERRORS.INVALID_CREDENTIALS if not r.is_json: return ERRORS.BAD_REQUEST data = r.json if not {'domain', 'name', 'id'} <= set(data): return ERRORS.MISSING_PARAMETERS domain, nom, id_domaine = data['domain'], data['name'], data['id'] if type(id_domaine) is not int: print("Bad id") return ERRORS.BAD_REQUEST d: Domaine = Domaine.query.filter_by( id_domaine=id_domaine).one_or_none() if not d: return ERRORS.DOMAIN_NOT_FOUND if d.domaine == "other": return ERRORS.BAD_REQUEST search = Domaine.query.filter_by(domaine=domain).one_or_none() if search and d.domaine != search.domaine: return ERRORS.DOMAIN_ALREADY_EXISTS d.nom = nom d.domaine = domain # Refresh db_session.commit() return flask.jsonify(d)
def decorated_function(*args, **kwargs): if is_teacher(): return ERRORS.INVALID_CREDENTIALS return f(*args, **kwargs)
def get_self_logged(): if is_teacher(): return ERRORS.INVALID_CREDENTIALS return flask.jsonify(get_student_or_none())
def modify_job(): r = get_request() stu = get_student_or_none() if not r.is_json: return ERRORS.BAD_REQUEST if not stu: return ERRORS.STUDENT_NOT_FOUND data = r.json if not 'job' in data: return ERRORS.MISSING_PARAMETERS job_id = data['job'] try: job_id = int(data['job']) except: return ERRORS.INVALID_INPUT_TYPE job: Emploi = Emploi.query.filter_by(id_emploi=job_id).one_or_none() if not job: return ERRORS.RESOURCE_NOT_FOUND if not is_teacher() and job.id_etu != stu.id_etu: return ERRORS.INVALID_CREDENTIALS # Modification ! if 'domain' in data: domain = data['domain'] list_d: List[Domaine] = Domaine.query.filter_by( domaine=domain).all() if not len(list_d): return ERRORS.INVALID_INPUT_VALUE job.id_domaine = list_d[0].id_domaine if 'company' in data: try: id_entreprise = int(data['company']) except: db_session.rollback() return ERRORS.INVALID_INPUT_TYPE # Teste si l'entreprise existe e: Entreprise = Entreprise.query.filter_by( id_entreprise=id_entreprise).one_or_none() if not e: db_session.rollback() return ERRORS.COMPANY_NOT_FOUND job.id_entreprise = e.id_entreprise if 'start' in data: start = data['start'] try: start = convert_date(start) except: db_session.rollback() return ERRORS.INVALID_DATE job.debut = start if 'end' in data: if data['end'] is None: job.fin = None else: try: end = convert_date(data['end']) except: db_session.rollback() return ERRORS.INVALID_DATE job.fin = end if 'level' in data: level = data['level'] #CHECK Level in ENUM if type(level) is not str: db_session.rollback() return ERRORS.INVALID_INPUT_TYPE #as_describe in client part interfaces.ts joblevels valid_levels = { "technicien", "ingenieur", "doctorant", "alternant", "other" } if level not in valid_levels: db_session.rollback() return ERRORS.UNEXPECTED_INPUT_VALUE job.niveau = data['level'] if 'contract' in data: contract = data['contract'] #Check contract in ENUM if type(contract) is not str: db_session.rollback() return ERRORS.INVALID_INPUT_TYPE #as_describe in client part interfaces.ts jobtypes valid_contracts = {"cdi", "alternance", "cdd", "these", 'other'} if contract not in valid_contracts: db_session.rollback() return ERRORS.UNEXPECTED_INPUT_VALUE job.contrat = contract if 'salary' in data: if data['salary'] is None: job.salaire = None else: try: salaire = int(data['salary']) job.salaire = salaire except: db_session.rollback() return ERRORS.INVALID_INPUT_TYPE if 'contact' in data: if data['contact'] is None: job.id_contact = None else: try: id_contact = int(data['contact']) c = Contact.query.filter_by( id_contact=id_contact).one_or_none() if not c: db_session.rollback() return ERRORS.CONTACT_NOT_FOUND except: db_session.rollback() return ERRORS.INVALID_INPUT_TYPE stu.refresh_update() db_session.commit() return flask.jsonify(job)