示例#1
0
    def delete(self):
        data = load_json()

        # validate the admin
        token = load_header_token()

        message, error_code = validate_admin_token(token)

        if message:
            return message, error_code

        # find the user to delete via email
        alumni_id = data['alumni_id']
        alumni_first_name = data['first_name']
        alumni_last_name = data['last_name']
        alumni = AlumniModel.query.filter_by(id=alumni_id).first()

        # if no user, return so
        if not alumni:
            return {
                'message':
                f"no account associated with {alumni_id}, {alumni_first_name}, {alumni_last_name}"
            }, 406

        # else delete the user
        db.session.delete(alumni)
        db.session.commit()

        return {
            'status':
            'success',
            'message':
            f"Deleted {alumni_id}, {alumni_first_name}, {alumni_last_name}"
        }, 204
示例#2
0
    def get(self):
        # get the admin_data and authenticate the admin
        token = load_header_token()

        # validate the admin
        message, error_code = validate_admin_token(token)
        if message:
            return message, error_code

        # get the data from all the users
        users = [{'email': user.email} for user in UserModel.query.all()]

        return {'users': users}, 201
示例#3
0
    def get(self):
        # get the admin_data and authenticate the admin
        token = load_header_token()

        # validate the admin
        message, error_code = validate_admin_token(token)
        if message:
            return message, error_code

        # get all the constants
        constants = [object_as_dict(constant)
                     for constant in FloatConstantModel.query.all()]

        return {'status': 'success', 'constants': constants}, 201
示例#4
0
    def get(self):
        token = load_header_token()

        privileges, code = validate_admin_token(token)

        if code >= 400:
            return privileges, code

        # else get all the data
        streamers_raw = StreamerModel.query.all()

        # format the streamers raw into a viable dictionary
        streamer_dicts = [
            object_as_dict(streamer) for streamer in streamers_raw
        ]

        return {'status': 'success', 'streamers': streamer_dicts}, 201
示例#5
0
    def post(self):

        data = load_json()

        token = load_header_token()

        message, error_code = validate_admin_token(token)

        if message:
            return message, error_code

        # get the data
        try:
            first_name = data['first_name']
            last_name = data['last_name']

        except KeyError:
            return {'message': f"must include first_name and last_name"}, 400

        # check if the alumni exists
        test_alumni = AlumniModel.query.filter_by(
            first_name=first_name).filter_by(last_name=last_name).first()

        if test_alumni:
            return {
                'message':
                f"There is already an account associated with {last_name}, {first_name}."
            }, 403

        new_alumni = AlumniModel(first_name=first_name, last_name=last_name)

        try:
            linkedin_url = data['linkedin_url']
            new_alumni.linkedin_url = linkedin_url
        except KeyError:
            linkedin_url = ""

        try:
            job_title = data['job_title']
            new_alumni.job_title = job_title
        except KeyError:
            job_title = ""

        try:
            job_title_role = data['job_title_role']
            new_alumni.job_title_role = job_title_role
        except KeyError:
            job_title_role = ""

        try:
            job_company = data['job_company']
            new_alumni.job_company = job_company
        except KeyError:
            job_company = ""

        try:
            job_company_industry = data['job_company_industry']
            new_alumni.job_company_industry = job_company_industry
        except KeyError:
            job_company_industry = ""

        try:
            job_company_locations_locality = data[
                'job_company_locations_locality']
            new_alumni.job_company_locations_locality = job_company_locations_locality
        except KeyError:
            job_company_locations_locality = ""

        try:
            phone_numbers = data['phone_numbers']
        except KeyError:
            phone_numbers = []

        try:
            emails = data['emails']
        except KeyError:
            emails = []

        try:
            interests = data['interests']
        except KeyError:
            interests = []

        try:
            experiences = data['experiences']
        except KeyError:
            experiences = []

        try:
            education = data['education']
        except KeyError:
            education = []
        '''
        new_alumni = AlumniModel(first_name=first_name, last_name=last_name, linkedin_url=linkedin_url,
                                 job_title=job_title, job_title_role=job_title_role, job_company_industry=job_company_industry,
                                 job_company_locations_locality=job_company_locations_locality)
        '''

        for number in phone_numbers:
            new_alumni.phone_numbers.append(
                PhoneNumberModel(ph_number=number, alumni_id=new_alumni.id))

        for email in emails:
            new_alumni.emails.append(
                EmailModel(email=email, alumni_id=new_alumni.id))

        for interest in interests:
            new_alumni.interests.append(
                InterestModel(interest=interest, alumni_id=new_alumni.id))

        for experience in experiences:

            try:

                new_experience = ExperienceModel(
                    start_date=dt.strptime(experience['start_date'],
                                           "%Y-%m-%d"),
                    end_date=dt.strptime(experience['end_date'], "%Y-%m-%d"),
                    title_name=experience['title_name'],
                    title_role=experience['title_role'],
                    alumni_id=new_alumni.id)

                new_location = LocationModel(
                    city=experience['company']['location']['city'],
                    state=experience['company']['location']['state'],
                    country=experience['company']['location']['country'],
                    zipcode=experience['company']['location']['zipcode'],
                    locality=experience['company']['location']['locality'])

                new_company = CompanyModel(name=experience['company']['name'],
                                           experience_id=new_experience.id)

                new_location.company_id = new_company.id
                new_company.location = [new_location]
                new_experience.company = [new_company]

                new_alumni.experiences.append(new_experience)

            except KeyError:
                return {'message': f"key error with experiences"}, 500

            except:
                return {'message': f"error adding experience"}, 500

        for school in education:

            try:
                new_school = SchoolModel(
                    name=school['name'],
                    start_date=dt.strptime(school['start_date'], "%Y-%m-%d"),
                    end_date=dt.strptime(school['end_date'], "%Y-%m-%d"),
                    alumni_id=new_alumni.id)

                for major in school['majors']:
                    new_major = MajorModel(name=major['name'],
                                           school_id=new_school.id)
                    new_school.majors.append(new_major)

                for minor in school['minors']:
                    new_minor = MinorModel(name=minor['name'],
                                           school_id=new_school.id)
                    new_school.minors.append(new_minor)

                new_alumni.education.append(new_school)

            except:
                return {'message': f"error adding education"}, 500

        db.session.add(new_alumni)
        db.session.commit()

        return {
            'status': 'success',
            'message': f"Added {new_alumni.first_name}, {new_alumni.last_name}"
        }, 201