def post(self): participant_schema = ParticipantSchema() json_data = request.get_json(force=True) if not json_data: return { "message": "No input data provided" }, HTTPStatus.BAD_REQUEST try: data = participant_schema.load(json_data) except ValidationError as err: return (err.messages), HTTPStatus.BAD_REQUEST if Participant.query.filter_by(email=json_data["email"]).first(): return ( { "message": "User with this email already exists." }, HTTPStatus.CONFLICT, ) if Participant.query.filter_by(github=json_data["github"]).first(): return ( { "message": "User with this Github login already exists." }, HTTPStatus.CONFLICT, ) participant = Participant(**data) db.session.add(participant) db.session.commit() return participant_schema.dump(participant), HTTPStatus.CREATED
def test_create_new_participant(new_participant): """Test participant model.""" new_participant = Participant(**new_participant) assert new_participant.first_name == 'Jon' assert new_participant.last_name == 'Doe' assert new_participant.email == '*****@*****.**' assert new_participant.phone == '123456789'
def test_create_new_participant(new_participant): """Test participant model.""" new_participant = Participant(**new_participant) assert new_participant.first_name == "Jon" assert new_participant.last_name == "Doe" assert new_participant.email == "*****@*****.**" assert new_participant.phone == "123456789"
def test_create_new_participant(new_participant): """Test participant model.""" new_participant = Participant(name=new_participant['name'], lastname=new_participant['lastname'], email=new_participant['email'], phone=new_participant['phone']) assert new_participant.name == 'Jon' assert new_participant.lastname == 'Doe' assert new_participant.email == '*****@*****.**' assert new_participant.phone == '123456789'
def test_add_new_participant_to_db(_db, new_participant): """Test addind participant do DB with valid data.""" db = _db assert len(db.session.query(Participant).all()) == 0 participant = Participant(**new_participant) db.session.add(participant) db.session.commit() participant_db = db.session.query(Participant).filter_by(first_name="Jon").first() for key, value in new_participant.items(): assert getattr(participant_db, key) == value
def test_add_new_participant_to_db(_db, new_participant): """Test addind participant do DB with valid data.""" db = _db assert len(db.session.query(Participant).all()) == 0 new_participant = Participant(**new_participant) db.session.add(new_participant) db.session.commit() participant = db.session.query(Participant).filter_by(first_name="Jon").first() assert participant.first_name == 'Jon' assert participant.last_name == 'Doe' assert participant.email == '*****@*****.**' assert participant.phone == '123456789'
def post(self): participant_schema = ParticipantSchema() json_data = request.get_json(force=True) if not json_data: return { "message": "No input data provided" }, HTTPStatus.BAD_REQUEST try: data = participant_schema.load(json_data) except ValidationError as err: return (err.messages), HTTPStatus.BAD_REQUEST participant = Participant(**data) db.session.add(participant) db.session.commit() return participant_schema.dump(participant), HTTPStatus.CREATED
def post(self): participant_schema = ParticipantSchema() json_data = request.get_json(force=True) if not json_data: return {'message': 'No input data provided'}, \ HTTPStatus.BAD_REQUEST try: data = participant_schema.load(json_data) except ValidationError as err: return (err.messages), HTTPStatus.BAD_REQUEST participant = Participant(**data) db.session.add(participant) db.session.commit() return { "message": "Participant created successfully.", "participant": data }, HTTPStatus.CREATED
def test_try_create_participant_with_existing_github(auth_client, new_participant, _db): """Test try to create new participant with existing github nickname.""" db = _db participant = Participant(**new_participant) db.session.add(participant) db.session.commit() payload = { "first_name": "JonX", "last_name": "DoeX", "email": "[email protected]", "phone": "1234567890", "github": "wihajster", } rv = auth_client.post("/api/participants/", json=payload) response = rv.get_json() assert rv.status_code == HTTPStatus.CONFLICT assert "User with this Github login already exists." in response["message"]
def test_create_new_participant(new_participant): """Test participant model.""" participant = Participant(**new_participant) for key, value in new_participant.items(): assert getattr(participant, key) == value
def import_attendance_list(): """Import attendance list.""" rows = [] list_of_participants = [] # open file from exported csv with open("./temp/list.csv") as csv_file: csv_reader = csv.reader(csv_file, delimiter=",") for row in csv_reader: rows.append(row) # get dates of historical hacknights dates = rows[0][5:] click.echo(f"Created list of dates: {dates}") # skip header rows = rows[1:] # prepare users' data for item in rows: users_dict = {} _id, github, name, phone, email, *_ = item try: first_name = name.split()[0] last_name = name.split()[1] except: first_name = "Unknown" last_name = "Unknown" # skip when _id == '' try: users_dict["id"] = int(_id) except: continue users_dict["github"] = github users_dict["first_name"] = first_name users_dict["last_name"] = last_name users_dict["email"] = email users_dict["phone"] = phone list_of_participants.append(users_dict) # create hacknights for idx, date in enumerate(dates, 1): day, month, year = date.split(".") day, month = day.zfill(2), month.zfill(2) hacknight = Hacknight(id=idx, date=f"{year}-{month}-{day}") db.session.add(hacknight) db.session.commit() click.echo(f"Imported {len(dates)} hacknights") # create participants for idx, participant in enumerate(list_of_participants): if participant["email"] == "": participant["email"] = f"unknown_email_{idx}" if participant["github"] == "": participant["github"] = f"unknown_github_{idx}" new_participant = Participant(**participant) db.session.add(new_participant) db.session.commit() click.echo(f"Imported {len(list_of_participants)} participants") # assign attendance list for participant_idx, row in enumerate(rows, 1): participant = Participant.query.get(participant_idx) for hacknight_index, element in enumerate(row[5:], 1): if "1" in element and participant: hacknight = Hacknight.query.get(hacknight_index) hacknight.participants.append(participant) email = participant.email if participant else "???" click.echo(f"Added {email} to hacknight {hacknight.date}") db.session.flush() db.session.commit()