def email_validate(self, key, email: str): if not email: raise ConflictException(msg="Marketplace email is empty") if not match(r'(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)', email): raise ConflictException(msg="Marketplace email is invalid") return email
def price_validate(self, key, price: str): if not price: raise ConflictException(msg='Product price is empty') if re.findall('[a-zA-Z_+-,@#$%¨&*^~`´]', price): raise ConflictException( msg='Use only numbers and point in product price') return price
def create_school( *, db: Session = Depends(deps.get_db), school_in: schemas.SchoolCreate, current_admin: models.Admin = Depends( deps.get_current_admin_with_permission("school")), ) -> Any: """ Create new school. """ # Check if the school already exists; raise Exception with error code 409 if it does if crud.school.get_by_name(db, name=school_in.name): raise ConflictException( detail="A school with this name already exists in the system.", ) # Check if a school with this head already exists; raise Exception with error code 409 if it does if crud.school.get_by_head(db, head=school_in.head): raise ConflictException( detail="A school with this head already exists in the system.", ) # Create new school logging.info( f"Admin {current_admin.user_id} ({current_admin.user.email}) is creating School {school_in.__dict__}" ) school = crud.school.create(db, obj_in=school_in) return school
def phone_validate(self, key, phone: str): if not phone: raise ConflictException(msg="Marketplace phone number is empty") if len(phone) < 9: raise ConflictException(msg='Marketplace phone number invalid') if not phone.isdigit(): raise ConflictException( msg='Use only numbers in marketplace phone') return phone
def remove_student_from_term( *, db: Session = Depends(deps.get_db), division_id: str, student_id: str, current_admin: models.Admin = Depends( deps.get_current_admin_with_permission("term")), ) -> Any: if division := crud.division.get(db, division_id): if student := crud.student.get(db, student_id): if student in division.students: logging.info( f"Admin {current_admin.user_id} ({current_admin.user.email}) " f"is deleting Student {student_id} ({student.user.email}) " f"from Division {division_id} ({division.course.name} {division.division_code})" ) division.students.remove(student) try: db.commit() except exc.IntegrityError as e: logging.error(e) db.rollback() raise ConflictException(detail=e.__str__()) except Exception as e: logging.error(e) db.rollback() raise BadRequestException(detail=e.__str__()) return student raise NotFoundException( detail= f"Student with id {student_id} not found in Division with if {division_id}" )
def create_year( *, db: Session = Depends(deps.get_db), year_in: schemas.YearCreate, current_admin: models.Admin = Depends(deps.get_current_admin_with_permission("year")), ) -> Any: if crud.year.get_by_details( db, name=year_in.name, school_id=year_in.school_id, start_year=year_in.start_year, end_year=year_in.end_year ): raise ConflictException( detail="The year with these details already exists in the system!", ) logging.info(f"Admin {current_admin.user_id} ({current_admin.user.email}) is creating Year {year_in.__dict__}") return crud.year.create(db, obj_in=year_in)
def create_division( *, db: Session = Depends(deps.get_db), division_in: schemas.DivisionCreate, current_admin: models.Admin = Depends( deps.get_current_admin_with_permission("course")), ) -> Any: if crud.division.get_by_details(db, course_id=division_in.course_id, division_code=division_in.division_code): raise ConflictException( detail= "The division with these details already exists in the system!", ) logging.info( f"Admin {current_admin.user_id} ({current_admin.user.email}) is creating Division {division_in.__dict__}" ) return crud.division.create(db, obj_in=division_in)
def create_course( *, db: Session = Depends(deps.get_db), course_in: schemas.CourseCreate, current_admin: models.Admin = Depends(deps.get_current_admin_with_permission("course")), ) -> Any: if crud.course.get_by_details( db, name=course_in.name, course_code=course_in.course_code, term_id=course_in.term_id, ): raise ConflictException( detail="The course with these details already exists in the system!", ) logging.info(f"Admin {current_admin.user_id} ({current_admin.user.email}) is creating Course {course_in.__dict__}") return crud.course.create(db, obj_in=course_in)
def get_user_from_token(token: str, token_type: str, db: Session) -> models.User: try: payload = jwt.decode(token, settings.SECRET_KEY, algorithms=[security.ALGORITHM]) token_data = schemas.TokenPayload(**payload) if token_data.type != token_type: raise BadRequestException( detail="Invalid token", ) except ExpiredSignatureError: raise ForbiddenException(detail="Token has expired") except (jwt.JWTError, ValidationError) as e: logging.error(e.__str__()) raise ForbiddenException( detail="Could not validate credentials", ) if user := crud.user.get(db, id=token_data.sub): if user.is_active: return user raise ConflictException("User account is disabled")
def create_timeslot( *, db: Session = Depends(deps.get_db), timeslot_in: schemas.TimeSlotCreate, current_admin: models.Admin = Depends( deps.get_current_admin_with_permission("school")), ) -> Any: if crud.timeslot.get_by_details(db, start_time=timeslot_in.start_time, end_time=timeslot_in.end_time, school_id=timeslot_in.school_id): raise ConflictException( detail= "The timeslot with these details already exists in the system!", ) logging.info( f"Admin {current_admin.user_id} ({current_admin.user.email}) is creating timeslot {timeslot_in.__dict__}" ) return crud.timeslot.create(db, obj_in=timeslot_in)
def create_lecture( *, db: Session = Depends(deps.get_db), lecture_in: schemas.LectureCreate, current_admin: models.Admin = Depends(deps.get_current_admin_with_permission("school")), ) -> Any: if crud.lecture.get_by_details( db, day=lecture_in.day, time_slot_id=lecture_in.time_slot_id, division_id=lecture_in.division_id, type=lecture_in.type, room_number=lecture_in.room_number, ): raise ConflictException( detail="A lecture with these details already exists in the system!", ) logging.info( f"Admin {current_admin.user_id} ({current_admin.user.email}) is creating lecture {lecture_in.__dict__}" ) return crud.lecture.create(db, obj_in=lecture_in)
def create_term( *, db: Session = Depends(deps.get_db), term_in: schemas.TermCreate, current_admin: models.Admin = Depends( deps.get_current_admin_with_permission("term")), ) -> Any: if crud.term.get_by_details( db, name=term_in.name, year_id=term_in.year_id, current_year_term=term_in.current_year_term, start_date=term_in.start_date, end_date=term_in.end_date, ): raise ConflictException( detail="The term with these details already exists in the system!", ) logging.info( f"Admin {current_admin.user_id} ({current_admin.user.email}) is creating Term {term_in.__dict__}" ) return crud.term.create(db, obj_in=term_in)
def site_validate(self, key, site: str): if not site: raise ConflictException(msg='Marketplace site is empty') return site
else: errors["no student object"].append(user_id) else: errors["different schools"].append(user_id) else: errors["not a student"].append(user_id) else: errors["not a user"].append(user_id) # Commit all the students added to the student try: db.commit() except exc.IntegrityError as e: logging.error(e.__str__()) db.rollback() raise ConflictException(detail=e.__str__()) except Exception as e: logging.error(e.__str__()) db.rollback() raise BadRequestException(detail=e.__str__()) if errors.keys(): response["errors"] = errors return response raise NotFoundException(detail="The division with this ID does not exist!") @router.delete("/{division_id}/students/{student_id}", response_model=schemas.Student) def remove_student_from_term(
def description_validate(self, key, description: str): if not description: raise ConflictException(msg='Category description is empty') return description
def name_validate(self, key, name: str): if not name: raise ConflictException(msg='Category name is empty') return name
deps.get_current_admin_with_permission("user")), ) -> Any: """ Create new user. """ if current_admin_user := crud.user.get(db, current_admin.user_id): if user_in.type == "superuser" and not crud.user.is_superuser( current_admin_user): raise ForbiddenException( detail="Only superusers can create more superusers.", ) # Check if the user already exists; raise Exception with error code 409 if it does user = crud.user.get_by_email(db, email=user_in.email) if user: raise ConflictException( detail="The user with this email already exists in the system.", ) # Create new user logging.info( f"Admin {current_admin.user_id} ({current_admin.user.email}) is creating User {user_in.__dict__}" ) user = crud.user.create(db, obj_in=user_in) if settings.EMAILS_ENABLED and user_in.email: send_new_account_email(email_to=user_in.email, username=user_in.email, password=user_in.password) return user @router.put("/me", response_model=schemas.User)
def name_validate(self, key, name: str): if not name: raise ConflictException(msg='Marketplace name is empty') return name
def test_conflict_exception_should_be_code_404_and_msg_is_default_value( self): ex = ConflictException() self.assertEqual(ex.code, 409) self.assertEqual(ex.description, 'Conflict Exception')
def technical_contact_validate(self, key, technical_contact: str): if not technical_contact: raise ConflictException( msg='Marketplace technical contact is empty') return technical_contact
def description_validate(self, key, description: str): if not description: raise ConflictException(msg='Marketplace description is empty') return description