def create_skill(cls, skill): # import pdb; pdb.set_trace() # print(skill) if 'name' not in skill: raise InvalidData('name is not provided') return cls(**skill)
def sanitize_company(cls, company): error = '' if 'name' not in company: error += 'name not provided' if error: raise InvalidData(error) return company
def sanitize_job(cls, job): error = '' if 'company_id' not in job: error = 'company not provided' if 'end_date' not in job: error += ' end_date not provided' if 'start_date' not in job: error = ' start date not provided' if error: raise InvalidData(error) return job
def set_job(cls, job={}): job = cls.sanitize_job(job) job['start_date'] = datetime.fromisoformat(job['start_date']) job['end_date'] = datetime.fromisoformat(job['end_date']) company = Company.get_company(job.get('company_id')) if company: new_job = cls(**job) db.session.add(new_job) db.session.commit() return cls.serialize_job(new_job) else: raise InvalidData('Invalid data')
def sanitize_user(cls, user): error = '' if 'email' not in user: error += 'email not provided' if 'first_name' not in user: error += 'first name is not provided' if 'last_name' not in user: error += 'last name is not provided' if 'password' not in user: error += 'password is not provided' if error: raise InvalidData(error) return user
def sanitize_accomplishment(cls, accomplishment): error = '' if 'rank' not in accomplishment: error += 'rank not provided' if 'job_id' not in accomplishment: error += ' job_id not provided' if 'skills' not in accomplishment: error += ' skills not provided' if 'description' not in accomplishment: error += ' description not provided' if error: raise InvalidData(error) return accomplishment
def create_user(user): user = User.sanitize_user(user) stored_user = User.query.filter_by(email=user.get('email')).first() if stored_user: raise InvalidData('User with this email already exists') else: first_name = user.get('first_name') last_name = user.get('last_name') email = user.get('email') password = user.get('password') new_user = User(first_name=first_name, last_name=last_name, email=email) new_user.set_password(password) db.session.add(new_user) db.session.commit() serialized_user = User.serialize_user(new_user) return serialized_user
def create_accomplishment(cls, accomplishment): # sanitize accomplishment accomplishment = cls.sanitize_accomplishment(accomplishment) description = accomplishment.get('description') rank = accomplishment.get('rank') job_id = accomplishment.get('job_id') acc_skills = accomplishment.get('skills') job = Job.query.get(job_id) if not job: raise InvalidData('Invalid job_id provided') accomplishment = cls(description=description, rank=rank, job_id=job_id) for skill in acc_skills: accomplishment.add_skill(skill) db.session.add(accomplishment) db.session.commit() serialized_accomplishment = cls.serialize_accomplishment( accomplishment) return serialized_accomplishment pass