def post(self): data = UserRegister.parser.parse_args() item = UserModel.find_by_username(data["username"]) if item: return {"message": "username already exists"}, 400 user = UserModel(**data) user.save() return {"message": "user created successfully"}
def add_participant(workshop_id, data) -> (dict, int): workshop = WorkshopModel.find_by_id(workshop_id=workshop_id) # Check if given workshop_id exists in DB if workshop is None: raise EntityNotFoundError # Deserialize data schema = ParticipantSchema() data, err_msg, err_code = schema.loads_or_400(data) if err_msg: return err_msg, err_code # Check if there is already a participant with this email in DB user = UserModel.find_by_email(data.get("email")) if user is None: participant = UserModel( email=data.get("email"), firstName=data.get("firstName"), lastName=data.get("lastName"), role=[Roles.PARTICIPANT.value], workshopParticipations=[workshop_id], ) status = WorkshopParticipantStatus.CREATED.value else: # If user already exists, check if it's a participant if Roles.PARTICIPANT.value in user.role: # Raise error if participant already registred in workshop if user.userId in [p.user.id for p in workshop.participants]: raise InvalidDataError( msg="Participant already registered for this workshop" ) status = WorkshopParticipantStatus.EXISTING.value else: # add participant role otherwise user.add_participant_role() status = WorkshopParticipantStatus.CREATED.value participant = user participant.workshopParticipations.append(workshop_id) # Append participant to workshop workshop.participants.append( WorkshopParticipantModel(user=participant.userId, status=status) ) # Persist in DB participant.save() workshop.save() return schema.dump(participant), 200
def participant(db, request): participant = UserModel( email="*****@*****.**", firstName="participant_first_name_1", lastName="participant_last_name_1", role=[Roles.PARTICIPANT.value], ) participant.save() # Delete participant at the end def teardown(): participant.delete() request.addfinalizer(teardown) return participant
def coach(db, request): coach = UserModel( firstName="coach_first_name", lastName="coach_last_name", email="*****@*****.**", password=generate_password_hash("password"), role=[Roles.COACH.value], city=Cities.PARIS.value, ) coach.save() # Delete coach at the end def teardown(): coach.delete() request.addfinalizer(teardown) return coach
def create_coach(data: bytes) -> (dict, int): schema = CoachSchema() data, err_msg, err_code = schema.loads_or_400(data) if err_msg: return err_msg, err_code role = data.pop("role") # Check if user already exist user = UserModel.find_by_email(data["email"]) if user is not None: if Roles.COACH.value in user.role: # raise exception if user is already a coach raise UserAlreadyExistsError else: # Otherwise, update role user.role.append(Roles.COACH.value) else: user = UserModel(**data) user.role = [Roles.COACH.value] # Setting admin role if needed if role == "admin": user.role.append(Roles.ADMIN.value) # Encrypt password if user.password and user.password != "": user.password = generate_password_hash(user.password) # Create user in DB user.save() # Generate default action card batches default_action_card_batches = ActionCardBatchModel.find_default_batches() for default_action_card_batch in default_action_card_batches: ActionCardBatchModel( coachId=user.id, name=default_action_card_batch.name, actionCardIds=default_action_card_batch.actionCardIds, type=default_action_card_batch.type, ).save() return schema.dump(user), 200
def admin(db, request): """ Create an admin for the scope of a function Remove it at the end """ admin = UserModel( firstName="admin_first_name", lastName="admin_last_name", email="*****@*****.**", password=generate_password_hash("password"), role=[Roles.ADMIN.value, Roles.COACH.value], city=Cities.PARIS.value, ) admin.save() # Delete admin at the end def teardown(): admin.delete() request.addfinalizer(teardown) return admin
def coaches(db, request): coach1 = UserModel( firstName="firstName1", lastName="lastName1", email="*****@*****.**", password="******", role=[Roles.COACH.value], city=Cities.PARIS.value, ) coach2 = UserModel( firstName="firstName2", lastName="lastName2", email="*****@*****.**", password="******", role=[Roles.COACH.value], city=Cities.PARIS.value, ) coach3 = UserModel( firstName="firstName3", lastName="lastName3", email="*****@*****.**", password="******", role=[Roles.COACH.value], city=Cities.PARIS.value, ) coach1.save() coach2.save() coach3.save() def teardown(): coach1.delete() coach2.delete() coach3.delete() request.addfinalizer(teardown) return [coach1, coach2, coach3]