From bfcb4c0f1bce0204ef75ee1bfb9c7c40267216ee Mon Sep 17 00:00:00 2001 From: Fernanda Scovino Date: Thu, 30 Mar 2023 22:25:15 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80=20release/2.3.1=20(#166)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 + mobilidade_rio/dev_local/Dockerfile_local | 43 ------ .../dev_local/docker-compose_local.yml | 43 ------ mobilidade_rio/mobilidade_rio/pontos/views.py | 131 +++++++++--------- .../mobilidade_rio/settings/base.py | 2 +- mobilidade_rio/mobilidade_rio/urls.py | 8 -- 6 files changed, 71 insertions(+), 159 deletions(-) delete mode 100644 mobilidade_rio/dev_local/Dockerfile_local delete mode 100644 mobilidade_rio/dev_local/docker-compose_local.yml diff --git a/.gitignore b/.gitignore index af16afb..e7eeaf2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +# Configurations to run dev local +mobilidade_rio/dev_local/ + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/mobilidade_rio/dev_local/Dockerfile_local b/mobilidade_rio/dev_local/Dockerfile_local deleted file mode 100644 index 25b4eec..0000000 --- a/mobilidade_rio/dev_local/Dockerfile_local +++ /dev/null @@ -1,43 +0,0 @@ -FROM python:3.8-slim - -# install postgresql-client -RUN apt-get update && apt-get install -y postgresql-client - -# Install virtualenv and create a virtual environment -RUN pip install --no-cache-dir virtualenv==20.6.0 && virtualenv /env --python=python3.8 -ENV PATH /env/bin:$PATH - -# Install pip requirements -WORKDIR /app -COPY requirements.txt . -RUN /env/bin/pip install --no-cache-dir -r requirements.txt - -# Install nginx and copy configuration -RUN apt-get update && apt-get install -y --no-install-recommends nginx \ - && apt-get clean \ - && rm -rf /var/lib/apt/lists/* -COPY /mobilidade_rio_nginx.conf /etc/nginx/sites-available/default - -# Copy app, generate static and set permissions -COPY . . -RUN /env/bin/python manage.py collectstatic --no-input --settings=mobilidade_rio.settings.base && \ - chown -R www-data:www-data /app - -# env -ENV mobilidade_rio="dev" -ENV DB_NAME="postgres" -ENV DB_USER="postgres" -ENV DB_PASSWORD="postgres" -ENV DB_HOST="db" -ENV DB_PORT=5432 - -## debug -ENV TST_DOCKERFILE_ENV=1 -ENV PYTHONUNBUFFERED=1 - -# Expose -EXPOSE 80 -STOPSIGNAL SIGTERM - -# Run commands -CMD ["/bin/bash", "-c", "'python manage.py runserver'"] \ No newline at end of file diff --git a/mobilidade_rio/dev_local/docker-compose_local.yml b/mobilidade_rio/dev_local/docker-compose_local.yml deleted file mode 100644 index f5ea517..0000000 --- a/mobilidade_rio/dev_local/docker-compose_local.yml +++ /dev/null @@ -1,43 +0,0 @@ -version: "3.9" - -services: - web: - build: - context: ../ - dockerfile: ./dev_local/Dockerfile_local - image: django - container_name: django_hd - volumes: - - .:/django - ports: - - 8010:8000 - depends_on: - - db - environment: - - DJANGO_SETTINGS_MODULE=mobilidade_rio.settings.local - command: > - bash -c " - echo 'DOCKER_ENV: $TST_DOCKERFILE_ENV' - python manage.py makemigrations --noinput - python manage.py migrate - python manage.py qcluster & - python manage.py runserver 0.0.0.0:8000 - " - - db: - image: postgres - container_name: postgres_hd - ports: - - 5433:5432 - volumes: - - dpostgres_data:/var/lib/postgresql/data/ - environment: - - "POSTGRES_HOST_AUTH_METHOD=trust" - - "POSTGRES_DB=postgres" - - "POSTGRES_USER=postgres" - - "POSTGRES_PASSWORD=postgres" - #- \.env - -# For some reason, this definition is necesarry -volumes: - dpostgres_data: diff --git a/mobilidade_rio/mobilidade_rio/pontos/views.py b/mobilidade_rio/mobilidade_rio/pontos/views.py index 3a359fa..cadbc23 100644 --- a/mobilidade_rio/mobilidade_rio/pontos/views.py +++ b/mobilidade_rio/mobilidade_rio/pontos/views.py @@ -179,84 +179,87 @@ class StopTimesViewSet(viewsets.ModelViewSet): def get_queryset(self): # get real col names and stuff - TRIP_ID_COL = StopTimes._meta.get_field("trip_id").column - STOP_ID_COL = StopTimes._meta.get_field("stop_id").column - PARENT_STATION__STOPS = Stops._meta.get_field("parent_station").column - + # trip_id_col = StopTimes._meta.get_field("trip_id").column + # stop_id_col = StopTimes._meta.get_field("stop_id").column queryset = StopTimes.objects.all().order_by("trip_id") - query = queryset.query - - # increase performance if no need to raw query - raw_filter_used = False - - # get stop_id_all - stop_id__all = self.request.query_params.get("stop_id__all") - if stop_id__all is not None: - stop_id__all = stop_id__all.split(",") - # filter all trips that pass in all stops - query = qu.q_cols_match_all( - table=queryset.query, table_is_query=True, - unique_cols=[TRIP_ID_COL, STOP_ID_COL], - col_in={STOP_ID_COL: stop_id__all}, - col_match_all=[TRIP_ID_COL], - ) - raw_filter_used = True - - # stop_id + + # add parameter to show all combinations (logical OR) + show_all = self.request.query_params.get("show_all") + + # filter by unique combinations (default - logical AND) + if not show_all: + unique = [ + "trip_id__trip_short_name", + "trip_id__direction_id", + "trip_id__service_id", + "stop_sequence", + ] + queryset = queryset.order_by(*unique).distinct(*unique) + + # filter trip_id + trip_id = self.request.query_params.get("trip_id") + if trip_id is not None: + trip_id = trip_id.split(',') + queryset = queryset.filter(trip_id__in=trip_id) + + # filter trip_short_name + trip_short_name = self.request.query_params.get("trip_short_name") + if trip_short_name is not None: + trip_short_name = trip_short_name.split(',') + queryset = queryset.filter(trip_id__trip_short_name__in=trip_short_name) + + # filter direction_id + direction_id = self.request.query_params.get("direction_id") + if direction_id is not None: + direction_id = direction_id.split(',') + queryset = queryset.filter(trip_id__direction_id__in=direction_id) + + # filter service_id + service_id = self.request.query_params.get("service_id") + if service_id is not None: + service_id = service_id.split(',') + queryset = queryset.filter(trip_id__service_id__in=service_id) + + # filter stop_id stop_id = self.request.query_params.get("stop_id") if stop_id is not None: stop_id = stop_id.split(",") - - # filter location_type location_type = Stops.objects.filter( stop_id__in=stop_id).values_list("location_type", flat=True) - # prevent error on searching inexistent stop_id - # TODO: filter stop_id or children individually - if len(location_type): + # TODO: filter stop parent and children individually + if location_type is not None: # if stop is parent (station), return its children if location_type[0] == 1: - if raw_filter_used: - query = f""" - SELECT * FROM ({query}) AS {qu.q_random_hash()} - WHERE {STOP_ID_COL} IN ( - SELECT stop_id FROM pontos_stops - WHERE {PARENT_STATION__STOPS} IN ({str(list(stop_id))[1:-1]}) - ) - """ - else: - queryset = queryset.filter( - stop_id__in=Stops.objects.filter( - parent_station__in=stop_id).values_list("stop_id", flat=True) - ) + queryset = queryset.filter( + stop_id__in=Stops.objects.filter( + parent_station__in=stop_id).values_list("stop_id", flat=True) + ) # if stop is child (platform), return searched stops if location_type[0] == 0: - if raw_filter_used: - query = f""" - SELECT * FROM ({query}) AS {qu.q_random_hash()} - WHERE {STOP_ID_COL} IN ({str(list(stop_id))[1:-1]}) - """ - else: - queryset = queryset.filter(stop_id__in=stop_id) - - # trip_id - trip_id = self.request.query_params.get("trip_id") - if trip_id is not None: - trip_id = trip_id.split(",") - - if raw_filter_used: - query = f""" - SELECT * FROM ({query}) AS {qu.q_random_hash()} - WHERE {TRIP_ID_COL} IN ({str(list(trip_id))[1:-1]}) - ORDER BY {TRIP_ID_COL} - """ + queryset = queryset.filter(stop_id__in=stop_id) else: - queryset = queryset.filter( - trip_id__in=trip_id).order_by("trip_id") + queryset = queryset.none() # stop id not found + + + # filter for trips passing by all given stops + # query = queryset.query + # raw_filter_used = False + # stop_id__all = self.request.query_params.get("stop_id__all") + # if stop_id__all is not None: + # stop_id__all = stop_id__all.split(",") + # query = qu.q_cols_match_all( + # table=query, table_is_query=True, + # unique_cols=[trip_id_col, stop_id_col], + # col_in={stop_id_col: stop_id__all}, + # col_match_all=[trip_id_col], + # ) + # raw_filter_used = True # execute query - if raw_filter_used: - queryset = queryset.raw(query) + # if raw_filter_used: + # queryset = queryset.raw(query) + return queryset diff --git a/mobilidade_rio/mobilidade_rio/settings/base.py b/mobilidade_rio/mobilidade_rio/settings/base.py index 014454a..1a8212c 100644 --- a/mobilidade_rio/mobilidade_rio/settings/base.py +++ b/mobilidade_rio/mobilidade_rio/settings/base.py @@ -125,7 +125,7 @@ 'DEFAULT_PERMISSION_CLASSES': [ 'rest_framework.permissions.IsAuthenticatedOrReadOnly', ], - 'PAGE_SIZE': 20, + 'PAGE_SIZE': 100, } # djoser diff --git a/mobilidade_rio/mobilidade_rio/urls.py b/mobilidade_rio/mobilidade_rio/urls.py index b48cbf7..f5f515f 100644 --- a/mobilidade_rio/mobilidade_rio/urls.py +++ b/mobilidade_rio/mobilidade_rio/urls.py @@ -30,14 +30,6 @@ unified_router.registry.extend(pred_router.registry) unified_router.registry.extend(feedback_router.registry) -feedback_router = routers.DefaultRouter() -feedback_router.register(r"brt", fb.FeedbackBRTViewSet,basename="feedback_brt") - -unified_router = routers.DefaultRouter() -unified_router.registry.extend(gtfs_router.registry) -unified_router.registry.extend(pred_router.registry) -unified_router.registry.extend(feedback_router.registry) - # Wire up our API using automatic URL routing. # Additionally, we include login URLs for the browsable API.