diff --git a/calendar_backend/routes/event/event.py b/calendar_backend/routes/event/event.py index c33d0ed2..a8dba341 100644 --- a/calendar_backend/routes/event/event.py +++ b/calendar_backend/routes/event/event.py @@ -108,18 +108,35 @@ async def create_events( result = [] for event in events: event_dict = event.model_dump() - rooms = [Room.get(room_id, session=db.session) for room_id in event_dict.pop("room_id", [])] - lecturers = [Lecturer.get(lecturer_id, session=db.session) for lecturer_id in event_dict.pop("lecturer_id", [])] - groups = [Group.get(group_id, session=db.session) for group_id in event_dict.pop("group_id", [])] - result.append( - Event.create( - **event_dict, - room=rooms, - lecturer=lecturers, - group=groups, - session=db.session, - ) + existing_events_query = ( + Event.get_all(session=db.session) + .filter(Event.name == event_dict.get("name")) + .filter(Event.start_ts == event_dict.get("start_ts")) + .filter(Event.end_ts == event_dict.get("end_ts")) ) + is_unique = True + for existing_event in existing_events_query.all(): + if ( + {column.id for column in existing_event.group} == set(event_dict["group_id"]) + and {column.id for column in existing_event.room} == set(event_dict["room_id"]) + and {column.id for column in existing_event.lecturer} == set(event_dict["lecturer_id"]) + ): + is_unique = False + if is_unique: + rooms = [Room.get(room_id, session=db.session) for room_id in event_dict.pop("room_id", [])] + lecturers = [ + Lecturer.get(lecturer_id, session=db.session) for lecturer_id in event_dict.pop("lecturer_id", []) + ] + groups = [Group.get(group_id, session=db.session) for group_id in event_dict.pop("group_id", [])] + result.append( + Event.create( + **event_dict, + room=rooms, + lecturer=lecturers, + group=groups, + session=db.session, + ) + ) db.session.commit() adapter = TypeAdapter(list[EventGet]) return adapter.validate_python(result) diff --git a/tests/event/event.py b/tests/event/event.py index a048c649..60e98343 100644 --- a/tests/event/event.py +++ b/tests/event/event.py @@ -98,6 +98,52 @@ def test_create_many(client_auth: TestClient, dbsession: Session, room_factory, assert [row.id for row in response_model.group] == request_obj[1]["group_id"] +def test_create_many_clones(client_auth: TestClient, dbsession: Session, room_factory, group_factory, lecturer_factory): + time_stamp = datetime.datetime.now() + name1 = f"name1_{time_stamp}" + room_path1 = room_factory(client_auth) + group_path1 = group_factory(client_auth) + lecturer_path1 = lecturer_factory(client_auth) + name2 = f"name2_{time_stamp}" + room_path2 = room_factory(client_auth) + group_path2 = group_factory(client_auth) + lecturer_path2 = lecturer_factory(client_auth) + room_id1 = int(room_path1.split("/")[-1]) + group_id1 = int(group_path1.split("/")[-1]) + lecturer_id1 = int(lecturer_path1.split("/")[-1]) + room_id2 = int(room_path2.split("/")[-1]) + group_id2 = int(group_path2.split("/")[-1]) + lecturer_id2 = int(lecturer_path2.split("/")[-1]) + request_obj = [ + { + "name": name1, + "room_id": [room_id1], + "group_id": [group_id1], + "lecturer_id": [lecturer_id1], + "start_ts": "2022-08-26T22:32:38.575Z", + "end_ts": "2022-08-26T22:32:38.575Z", + }, + { + "name": name2, + "room_id": [room_id2], + "group_id": [group_id2], + "lecturer_id": [lecturer_id2], + "start_ts": "2022-08-26T22:32:38.575Z", + "end_ts": "2022-08-26T22:32:38.575Z", + }, + ] + response = client_auth.post(f"{RESOURCE}bulk", json=request_obj) + assert response.status_code == status.HTTP_200_OK, response.json() + assert response.json()[0]["name"] == request_obj[0]["name"] + response = client_auth.post(f"{RESOURCE}bulk", json=request_obj) + assert response.status_code == status.HTTP_200_OK + assert len(response.json()) == 0 + events = dbsession.query(Event).filter(Event.name == name1).all() + assert len(events) == 1 + events = dbsession.query(Event).filter(Event.name == name2).all() + assert len(events) == 1 + + def test_delete(client_auth: TestClient, dbsession: Session, room_path, lecturer_path, group_path): room_id = int(room_path.split("/")[-1]) group_id = int(group_path.split("/")[-1])