コード例 #1
0
  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
コード例 #2
0
ファイル: company.py プロジェクト: alkihis/promo-app-server
    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
コード例 #3
0
    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())
コード例 #4
0
  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)
コード例 #5
0
    def get_last_actives_job():
        stu = get_student_or_none()

        if not stu:
            return ERRORS.STUDENT_NOT_FOUND

        jobs = Emploi.query.filter_by(id_etu=stu.id_etu, fin=None).order_by(
            Emploi.debut.desc()).all()

        if not jobs:
            return flask.jsonify([])

        return flask.jsonify(jobs)
コード例 #6
0
  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)
コード例 #7
0
    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)
コード例 #8
0
    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 ""
コード例 #9
0
  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 ""
コード例 #10
0
    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
コード例 #11
0
    def make_emploi():
        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

        user_id = stu.id_etu
        data = r.json

        if not {
                'start', 'end', 'contract', 'salary', 'level', 'company',
                'domain', 'contact'
        } <= set(data):
            return ERRORS.MISSING_PARAMETERS

        start, end, contract, salary, level, id_entreprise = data[
            'start'], data['end'], data['contract'], data['salary'], data[
                'level'], data['company']
        domain, id_contact = data['domain'], data['contact']

        list_d: List[Domaine] = Domaine.query.filter_by(domaine=domain).all()

        if not len(list_d):
            return ERRORS.DOMAIN_NOT_FOUND
        try:
            id_entreprise = int(id_entreprise)

            if salary is not None:
                salary = int(salary)

            if id_contact is not None:
                id_contact = int(id_contact)
        except:
            return ERRORS.INVALID_INPUT_TYPE

        id_domain = list_d[0].id_domaine

        try:
            start = convert_date(start)
        except:
            return ERRORS.INVALID_DATE

        if end is not None:
            try:
                end = convert_date(end)
            except:
                return ERRORS.INVALID_DATE

        # Teste si l'entreprise existe
        e = Entreprise.query.filter_by(
            id_entreprise=id_entreprise).one_or_none()
        if not e:
            return ERRORS.COMPANY_NOT_FOUND

        # Teste si le contact existe
        if id_contact is not None:
            c = Contact.query.filter_by(id_contact=id_contact).one_or_none()
            if not c:
                return ERRORS.CONTACT_NOT_FOUND

        #CHECK Contract in ENUM
        if type(contract) is not str:
            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:
            return ERRORS.UNEXPECTED_INPUT_VALUE

        #CHECK Level in ENUM
        if type(level) is not str:
            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:
            return ERRORS.UNEXPECTED_INPUT_VALUE
        # Create new emploi
        stu.refresh_update()
        emp = Emploi.create(debut=start,
                            fin=end,
                            contrat=contract,
                            niveau=level,
                            id_entreprise=id_entreprise,
                            id_domaine=id_domain,
                            id_contact=id_contact,
                            id_etu=user_id,
                            salaire=salary)
        db_session.add(emp)
        db_session.commit()

        return flask.jsonify(emp), 201
コード例 #12
0
    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)
コード例 #13
0
    def update_student():
        r = get_request()
        student = get_student_or_none()

        if not r.is_json:
            return ERRORS.BAD_REQUEST

        if not student:
            return ERRORS.STUDENT_NOT_FOUND

        data = r.json

        if 'first_name' in data:
            special_check = r"^[\w_ -]+$"
            if not re.match(special_check, data['first_name']):
                return ERRORS.INVALID_INPUT_VALUE

            student.prenom = data['first_name']

        if 'last_name' in data:
            special_check = r"^[\w_ -]+$"
            if not re.match(special_check, data['last_name']):
                return ERRORS.INVALID_INPUT_VALUE

            student.nom = data['last_name']

        if 'get_auto_mail' in data and type(data['get_auto_mail']) is bool:
            student.recoit_mail_auto = data['get_auto_mail']

        if 'year_in' and 'year_out' in data and data['year_out'] is not None:
            try:
                year_in = int(data['year_in'])
            except:
                return ERRORS.INVALID_DATE

            try:
                year_out = int(data['year_out'])
            except:
                return ERRORS.INVALID_DATE

            if year_in > year_out:
                return ERRORS.INVALID_DATE

            student.annee_entree = data['year_in']
            student.annee_sortie = data['year_out']

        if 'public' in data and type(data['public']) is bool:
            student.visible = data['public']

        if 'year_in' in data:
            try:
                year_in = int(data['year_in'])
            except:
                return ERRORS.INVALID_DATE

            if student.annee_sortie and year_in >= int(student.annee_sortie):
                return ERRORS.INVALID_DATE

            student.annee_entree = data['year_in']

        if 'year_out' in data:
            if data['year_out'] is None:
                student.annee_sortie = None
            else:
                try:
                    year_out = int(data['year_out'])
                except:
                    return ERRORS.INVALID_DATE

                if int(student.annee_entree) >= year_out:
                    return ERRORS.INVALID_DATE

                student.annee_sortie = data['year_out']

        if 'email' in data:
            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, data['email']):
                return ERRORS.INVALID_EMAIL

            student.mail = data['email']

        if 'previous_formation' in data:
            if type(data['previous_formation']) == int:
                # Check existance of formation
                desired = Formation.query.filter_by(
                    id_form=data['previous_formation']).one_or_none()

                if desired:
                    student.cursus_anterieur = data['previous_formation']
                else:
                    db_session.rollback()
                    return ERRORS.FORMATION_NOT_FOUND
            elif data['previous_formation'] is None:
                student.cursus_anterieur = None
            else:
                db_session.rollback()
                return ERRORS.INVALID_INPUT_TYPE
        if 'next_formation' in data:
            if type(data['next_formation']) == int:
                # Check existance of formation
                desired = Formation.query.filter_by(
                    id_form=data['next_formation']).one_or_none()

                if desired:
                    student.reorientation = data['next_formation']
                else:
                    db_session.rollback()
                    return ERRORS.FORMATION_NOT_FOUND
            elif data['next_formation'] is None:
                student.reorientation = None
            else:
                db_session.rollback()
                return ERRORS.INVALID_INPUT_TYPE
        if 'entered_in' in data:
            if data['entered_in'] != 'M1' and data['entered_in'] != 'M2':
                db_session.rollback()
                return ERRORS.UNEXPECTED_INPUT_VALUE

            student.entree_en_m1 = data['entered_in'] == 'M1'
        if 'graduated' in data and type(data['graduated']) == bool:
            student.diplome = data['graduated']

        # Save changes
        db_session.commit()

        return flask.jsonify(student)
コード例 #14
0
    def get_self_logged():
        if is_teacher():
            return ERRORS.INVALID_CREDENTIALS

        return flask.jsonify(get_student_or_none())
コード例 #15
0
  def fetch_intership():
    stu: Etudiant = get_student_or_none()
    if not stu:
      return ERRORS.STUDENT_NOT_FOUND

    return flask.jsonify(Stage.query.filter_by(id_etu=stu.id_etu).all())