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 make_internship(): 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 {'promo_year', 'company', 'domain', 'contact'} <= set(data): return ERRORS.MISSING_PARAMETERS promo_year, id_entreprise, domain, id_contact = data['promo_year'], data['company'], data['domain'], data['contact'] # Check if promo_year is okay for this student ! if stu.annee_sortie: if not int(stu.annee_entree) <= int(data['promo_year']) <= int(stu.annee_sortie): return ERRORS.INVALID_DATE else: if not int(stu.annee_entree) <= int(data['promo_year']): return ERRORS.INVALID_DATE ## Check company id ent: Entreprise = Entreprise.query.filter_by(id_entreprise=id_entreprise).one_or_none() if not ent: return ERRORS.COMPANY_NOT_FOUND ## Domain to id dom: Domaine = Domaine.query.filter_by(domaine=domain).one_or_none() if not dom: return ERRORS.DOMAIN_NOT_FOUND id_domain = dom.id_domaine ## Check contact id if id_contact is not None: cont: Contact = Contact.query.filter_by(id_contact=id_contact).one_or_none() if not cont: return ERRORS.CONTACT_NOT_FOUND # Create new internship stu.refresh_update() inter = Stage.create( promo=promo_year, id_entreprise=int(id_entreprise), id_domaine=id_domain, id_contact=int(id_contact) if id_contact is not None else None, id_etu=stu.id_etu ) db_session.add(inter) db_session.commit() return flask.jsonify(inter), 201
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 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 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 create_a_student_from_token(): r = get_request() if not r.is_json: return ERRORS.BAD_REQUEST data = r.json if 'token' not in data: return ERRORS.BAD_REQUEST token = data['token'] a: AskCreation = AskCreation.query.filter_by(token=token).one_or_none() if not a: # Token does not exists return ERRORS.RESOURCE_NOT_FOUND etu = create_a_student(data) if type(etu) is not Etudiant: return etu # this is an error AskCreation.query.filter_by(token=token).delete() db_session.commit() return flask.jsonify(etu)
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 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 make_entreprise(): 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 {'name', 'city', 'size', 'status'} <= set(data): return ERRORS.MISSING_PARAMETERS name, city, size, status = data['name'], data['city'], data[ 'size'], data['status'] ## Search for similar company f = Entreprise.query.filter( and_(Entreprise.nom.ilike(f"{name}"), Entreprise.ville.ilike(f"{city}"))).all() if len(f): return flask.jsonify(f[0]) 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 # Checks for size and status (enum voir TS interfaces.ts) 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 # Checks for status (enum voir TS interfaces.ts) 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 gps_coords = get_location_of_company(city) # Create new company comp = Entreprise.create(nom=name, ville=city, taille=size, statut=status, lat=gps_coords[0], lng=gps_coords[1]) db_session.add(comp) db_session.commit() return flask.jsonify(comp), 201
def fetch_jobs_for_student(): r = get_request() stu: Etudiant = get_student_or_none() if not stu: return ERRORS.STUDENT_NOT_FOUND return flask.jsonify(Emploi.query.filter_by(id_etu=stu.id_etu).all())
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 login_for_teacher(): r = get_request() if r.is_json and 'password' in r.json and bcrypt.check_password_hash(get_teacher_password_hash(), r.json['password']): # Cherche si un token existe t: Token = get_or_create_token_for(None, True) return flask.jsonify({'token': t.token}) else: return ERRORS.INVALID_PASSWORD
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 fetch_contact(): r = get_request() id_e = None if 'company' in r.args: try: id_e = int(r.args['company']) except: pass if not id_e or id_e < 0: return ERRORS.MISSING_PARAMETERS return flask.jsonify(Contact.query.filter_by(id_entreprise=id_e).all())
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 lost_token(): r = get_request() if not 'email' in r.args or type(r.args['email']) is not str: return ERRORS.BAD_REQUEST email = r.args['email'] st: Etudiant = Etudiant.query.filter_by(mail=email).one_or_none() if not st: return ERRORS.STUDENT_NOT_FOUND send_welcome_mail(st.id_etu) return ""
def recover_token(): r = get_request() if not 'email' in r.args: return ERRORS.BAD_REQUEST # test si un étudiant avec cet email existe e: Etudiant = Etudiant.query.filter_by(mail=r.args['email']).one_or_none() if not e: # Etu introuvable return ERRORS.STUDENT_NOT_FOUND send_welcome_mail(e.id_etu) return ""
def login_for_student(): r = get_request() if r.is_json and 'token' in r.json: token = r.json['token'] # Check for token t: Token = Token.query.filter_by(token=token).one_or_none() if not t: return ERRORS.INVALID_TOKEN # Empty HTTP 200 return flask.jsonify({"is_teacher": t.teacher}) else: return ERRORS.BAD_REQUEST
def create_student(): # Check for content type r = get_request() if not r.is_json: return ERRORS.BAD_REQUEST # Check presence of required arguments # Required are first_name, last_name, email, year_in, birthdate data = r.json etu = create_a_student(data) if type(etu) is not Etudiant: return etu # This is an error # Return the newly created student return flask.jsonify(etu)
def fetch_contact_of_location(): r = get_request() cmps: List[Entreprise] = [] if 'town' in r.args: cmps = Entreprise.query.filter_by(ville=r.args['town']).all() contacts: List[Contact] = [] for c in cmps: contacts_of_cmp = Contact.query.filter_by( id_entreprise=c.id_entreprise).all() for contact in contacts_of_cmp: contacts.append(contact) return flask.jsonify( [contact.to_json(full=True) for contact in contacts])
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 export_students(): r = get_request() ids = None data = r.form if not 'token' in data: return ERRORS.INVALID_CREDENTIALS token = data['token'] tk: Token = Token.query.filter_by(token=token).one_or_none() if not tk or not tk.teacher: return ERRORS.INVALID_CREDENTIALS if 'students' in data: ids = list(map(lambda x: int(x), str(data['students']).split(','))) return export_all_data_in_csv(ids)
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 make_contact(): r = get_request() if not r.is_json: return ERRORS.BAD_REQUEST data = r.json if not {'name', 'mail', 'id_entreprise'} <= set(data): return ERRORS.MISSING_PARAMETERS name, mail, id_entreprise = data['name'], data['mail'], data[ 'id_entreprise'] ## Search for similar contact f = Contact.query.filter( and_(Contact.nom.ilike(f"{name}"), Contact.mail.ilike(f"{mail}"), Contact.id_entreprise.ilike(f"{id_entreprise}"))).all() if len(f): return flask.jsonify(f[0]), 200 #Check id_entreprise ent: Entreprise = Entreprise.query.filter_by( id_entreprise=id_entreprise).one_or_none() if not ent: return ERRORS.COMPANY_NOT_FOUND special_check = r"^[\w_ -]+$" if not re.match(special_check, name): return ERRORS.INVALID_INPUT_VALUE 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 cont = Contact.create(nom=name, mail=mail, id_entreprise=id_entreprise) db_session.add(cont) db_session.commit() return flask.jsonify(cont), 201
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 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 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 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 create_formation(): r = get_request() stu: Etudiant = get_student_or_none() if not stu or not r.is_json: return ERRORS.BAD_REQUEST data = r.json if not {'name', 'location', 'level'} <= set(data): return ERRORS.MISSING_PARAMETERS branch, location, level = data['name'], data['location'], data['level'] if type(branch) is not str: return ERRORS.INVALID_INPUT_TYPE 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 ## Search for similar formations f = Formation.query.filter( and_(Formation.filiere.ilike(f"{branch}"), Formation.lieu.ilike(f"{location}"), Formation.niveau.ilike(f"{level}"))).all() if len(f): return flask.jsonify(f[0]) # Create new formation form = Formation.create(filiere=branch, lieu=location, niveau=level) db_session.add(form) db_session.commit() return flask.jsonify(form), 201