diff --git a/backend/src/modules/events/repository.py b/backend/src/modules/events/repository.py index 938603a..267154c 100644 --- a/backend/src/modules/events/repository.py +++ b/backend/src/modules/events/repository.py @@ -16,6 +16,11 @@ async def read_one(self, id: str) -> Event | None: async def read_all(self) -> list[Event] | None: return await Event.all().to_list() + async def read_all_locations(self) -> list: + s = await Event.get_motor_collection().aggregate([{"$project": {"location": 1}}]).to_list() + s = [i["location"] for i in s] + return s + async def create_many(self, events: list[Event]) -> bool: res = await Event.insert_many(events) if not res.acknowledged: diff --git a/backend/src/modules/events/routes.py b/backend/src/modules/events/routes.py index 984aaf8..5b55ce9 100644 --- a/backend/src/modules/events/routes.py +++ b/backend/src/modules/events/routes.py @@ -93,16 +93,32 @@ async def get_all_filters_locations() -> list[LocationsFilterVariants]: Get all locations. """ # From all 'location' fields of events, get unique values - # TODO: Write Mongo request countries: dict[str, dict[str, RegionsFilterVariants]] = {} - for event in await events_repository.read_all(): - for location in event.location: - if location.country not in countries: - countries[location.country] = {} - if location.region not in countries[location.country]: - countries[location.country][location.region] = RegionsFilterVariants(region=location.region, cities=[]) - if location.city not in countries[location.country][location.region].cities: - countries[location.country][location.region].cities.append(location.city) + unique_locs = set() + _ = await events_repository.read_all_locations() + for locations in _: + for loc in locations: + unique_locs.add((loc.get("country"), loc.get("region"), loc.get("city"))) + unique_locs = sorted(unique_locs, key=lambda x: (x[0], x[1] or "", x[2] or "")) + + for country, region, city in unique_locs: + if region is None and city in ( + "городской округ", + "деревня", + "железнодорожной станции", + "поселок", + "поселок городского типа", + "село", + ): + continue + + if country not in countries: + countries[country] = {} + if region not in countries[country]: + countries[country][region] = RegionsFilterVariants(region=region, cities=[]) + if city not in countries[country][region].cities: + countries[country][region].cities.append(city) + return [ LocationsFilterVariants( country=country, diff --git a/backend/src/storages/mongo/events.py b/backend/src/storages/mongo/events.py index baecfc4..cade906 100644 --- a/backend/src/storages/mongo/events.py +++ b/backend/src/storages/mongo/events.py @@ -69,4 +69,14 @@ class Settings: default_language="russian", name="text_index", ), + IndexModel([("start_date", pymongo.ASCENDING)]), + IndexModel([("end_date", pymongo.ASCENDING)]), + IndexModel([("location.country", pymongo.ASCENDING)]), + IndexModel([("location.region", pymongo.ASCENDING)]), + IndexModel([("location.city", pymongo.ASCENDING)]), + IndexModel([("sport", pymongo.ASCENDING)]), + IndexModel([("discipline", pymongo.ASCENDING)]), + IndexModel([("gender", pymongo.HASHED)]), + IndexModel([("age_min", pymongo.ASCENDING)]), + IndexModel([("age_max", pymongo.ASCENDING)]), ]