From c0823236d20e801550fccdbb021d8aabb56d59c0 Mon Sep 17 00:00:00 2001 From: Amrou Bellalouna Date: Thu, 18 Aug 2022 16:58:08 +0100 Subject: add collection of keywords per rule In reference to issue #2108, this commit aims to add an initial set of keywords per rule. These keywords will be later in the "rule" bot command in order to make rule identification easier --- pydis_site/apps/api/views.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/pydis_site/apps/api/views.py b/pydis_site/apps/api/views.py index ad2d948e..f96d6a8d 100644 --- a/pydis_site/apps/api/views.py +++ b/pydis_site/apps/api/views.py @@ -124,35 +124,44 @@ class RulesView(APIView): return Response([ ( - f"Follow the {pydis_coc}." + f"Follow the {pydis_coc}.", + {"coc", "conduct", "code"} ), ( - f"Follow the {discord_community_guidelines} and {discord_tos}." + f"Follow the {discord_community_guidelines} and {discord_tos}.", + {"guidelines", "discord_tos"} ), ( - "Respect staff members and listen to their instructions." + "Respect staff members and listen to their instructions.", + {"staff", "instructions"} ), ( "Use English to the best of your ability. " - "Be polite if someone speaks English imperfectly." + "Be polite if someone speaks English imperfectly.", + {"english", "language"} ), ( "Do not provide or request help on projects that may break laws, " - "breach terms of services, or are malicious or inappropriate." + "breach terms of services, or are malicious or inappropriate.", + {"infraction", "tos", "breach", "malicious", "inappropriate"} ), ( - "Do not post unapproved advertising." + "Do not post unapproved advertising.", + {"ads", "advertising"} ), ( "Keep discussions relevant to the channel topic. " - "Each channel's description tells you the topic." + "Each channel's description tells you the topic.", + {"off-topic", "topic", "relevance"} ), ( "Do not help with ongoing exams. When helping with homework, " - "help people learn how to do the assignment without doing it for them." + "help people learn how to do the assignment without doing it for them.", + {"exams", "assignment", "assignments", "homework"} ), ( - "Do not offer or ask for paid work of any kind." + "Do not offer or ask for paid work of any kind.", + {"work", "money"} ), ]) -- cgit v1.2.3 From b8f64167aeb93156eb6e066415116a3ce01ac0f9 Mon Sep 17 00:00:00 2001 From: Hassan Abouelela Date: Sat, 17 Sep 2022 23:22:20 +0400 Subject: Fix Poetry 1.2 Support Poetry 1.2 introduced a regression which broke pip `--user` installs. These types of install where the main way we did installations in docker and CI, which made it much more convenient to control their location, availability, and caching. Poetry's team does not recognize this as a supported use case, so major changes were required to get everything working again. Most of the changes were consolidated into chrislovering/python-poetry-base for docker, and hassanabouelela/setup-python for CI. Signed-off-by: Hassan Abouelela --- .github/workflows/lint-test.yaml | 62 ++++------------------------------------ Dockerfile | 17 ++++------- 2 files changed, 11 insertions(+), 68 deletions(-) diff --git a/.github/workflows/lint-test.yaml b/.github/workflows/lint-test.yaml index a167ce32..699f85ee 100644 --- a/.github/workflows/lint-test.yaml +++ b/.github/workflows/lint-test.yaml @@ -10,76 +10,26 @@ on: jobs: lint-test: runs-on: ubuntu-latest - env: - # Configure pip to cache dependencies and do a user install - PIP_NO_CACHE_DIR: false - PIP_USER: 1 - - # Make sure package manager does not use virtualenv - POETRY_VIRTUALENVS_CREATE: false - - # Specify explicit paths for python dependencies and the pre-commit - # environment so we know which directories to cache - POETRY_CACHE_DIR: ${{ github.workspace }}/.cache/py-user-base - PYTHONUSERBASE: ${{ github.workspace }}/.cache/py-user-base - PRE_COMMIT_HOME: ${{ github.workspace }}/.cache/pre-commit-cache steps: - - name: Add custom PYTHONUSERBASE to PATH - run: echo '${{ env.PYTHONUSERBASE }}/bin/' >> $GITHUB_PATH - - name: Checkout repository uses: actions/checkout@v2 - - name: Setup python - id: python - uses: actions/setup-python@v2 + - name: Install Python Dependencies + uses: HassanAbouelela/actions/setup-python@setup-python_v1.3.1 with: - python-version: '3.9' + dev: true + python_version: '3.9' # Start the database early to give it a chance to get ready before # we start running tests. - name: Run database using docker-compose run: docker-compose run -d -p 7777:5432 --name pydis_web postgres - # This step caches our Python dependencies. To make sure we - # only restore a cache when the dependencies, the python version, - # the runner operating system, and the dependency location haven't - # changed, we create a cache key that is a composite of those states. - # - # Only when the context is exactly the same, we will restore the cache. - - name: Python Dependency Caching - uses: actions/cache@v2 - id: python_cache - with: - path: ${{ env.PYTHONUSERBASE }} - key: "python-0-${{ runner.os }}-${{ env.PYTHONUSERBASE }}-\ - ${{ steps.python.outputs.python-version }}-\ - ${{ hashFiles('./pyproject.toml', './poetry.lock') }}" - - # Install our dependencies if we did not restore a dependency cache - - name: Install dependencies using poetry - if: steps.python_cache.outputs.cache-hit != 'true' - run: | - pip install poetry - poetry install - - # This step caches our pre-commit environment. To make sure we - # do create a new environment when our pre-commit setup changes, - # we create a cache key based on relevant factors. - - name: Pre-commit Environment Caching - uses: actions/cache@v2 - with: - path: ${{ env.PRE_COMMIT_HOME }} - key: "precommit-0-${{ runner.os }}-${{ env.PRE_COMMIT_HOME }}-\ - ${{ steps.python.outputs.python-version }}-\ - ${{ hashFiles('./.pre-commit-config.yaml') }}" - # We will not run `flake8` here, as we will use a separate flake8 - # action. As pre-commit does not support user installs, we set - # PIP_USER=0 to not do a user install. + # action. - name: Run pre-commit hooks - run: export PIP_USER=0; SKIP=flake8 pre-commit run --all-files + run: SKIP=flake8 pre-commit run --all-files # Run flake8 and have it format the linting errors in the format of # the GitHub Workflow command to register error annotations. This diff --git a/Dockerfile b/Dockerfile index 2b039fab..1d50e11d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,21 +1,14 @@ -FROM --platform=linux/amd64 python:3.9-slim-buster +FROM ghcr.io/chrislovering/python-poetry-base:3.9-slim # Allow service to handle stops gracefully STOPSIGNAL SIGQUIT -# Set pip to have cleaner logs and no saved cache -ENV PIP_NO_CACHE_DIR=false \ - POETRY_VIRTUALENVS_CREATE=false - -# Install poetry -RUN pip install -U poetry - # Copy the project files into working directory WORKDIR /app # Install project dependencies COPY pyproject.toml poetry.lock ./ -RUN poetry install --no-dev +RUN poetry install --without dev # Set Git SHA environment variable ARG git_sha="development" @@ -34,14 +27,14 @@ RUN \ SECRET_KEY=dummy_value \ DATABASE_URL=postgres://localhost \ METRICITY_DB_URL=postgres://localhost \ - python manage.py collectstatic --noinput --clear + poetry run python manage.py collectstatic --noinput --clear # Build static files if we are doing a static build ARG STATIC_BUILD=false RUN if [ $STATIC_BUILD = "TRUE" ] ; \ - then SECRET_KEY=dummy_value python manage.py distill-local build --traceback --force ; \ + then SECRET_KEY=dummy_value poetry run python manage.py distill-local build --traceback --force ; \ fi # Run web server through custom manager -ENTRYPOINT ["python", "manage.py"] +ENTRYPOINT ["poetry", "run", "python", "manage.py"] CMD ["run"] -- cgit v1.2.3 From a75ba77bc8b83af97decde6c5ac4317bb3b5253d Mon Sep 17 00:00:00 2001 From: Amrou Bellalouna Date: Sun, 18 Sep 2022 18:38:02 +0100 Subject: add docstrings explaining the values that the Rules view returns --- pydis_site/apps/api/views.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pydis_site/apps/api/views.py b/pydis_site/apps/api/views.py index f96d6a8d..66f4b18c 100644 --- a/pydis_site/apps/api/views.py +++ b/pydis_site/apps/api/views.py @@ -100,6 +100,7 @@ class RulesView(APIView): # `format` here is the result format, we have a link format here instead. def get(self, request, format=None): # noqa: D102,ANN001,ANN201 + """Returns a list of our community rules coupled with their keywords.""" link_format = request.query_params.get('link_format', 'md') if link_format not in ('html', 'md'): raise ParseError( -- cgit v1.2.3 From 672ba65b02fb111235fd3b928d5c84ee8b59cc54 Mon Sep 17 00:00:00 2001 From: Amrou Bellalouna Date: Sun, 18 Sep 2022 18:43:05 +0100 Subject: add suggested keywords --- pydis_site/apps/api/views.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/pydis_site/apps/api/views.py b/pydis_site/apps/api/views.py index 66f4b18c..836f11ce 100644 --- a/pydis_site/apps/api/views.py +++ b/pydis_site/apps/api/views.py @@ -126,43 +126,43 @@ class RulesView(APIView): return Response([ ( f"Follow the {pydis_coc}.", - {"coc", "conduct", "code"} + ["coc", "conduct", "code"] ), ( f"Follow the {discord_community_guidelines} and {discord_tos}.", - {"guidelines", "discord_tos"} + ["discord", "guidelines", "discord_tos"] ), ( "Respect staff members and listen to their instructions.", - {"staff", "instructions"} + ["respect", "staff", "instructions"] ), ( "Use English to the best of your ability. " "Be polite if someone speaks English imperfectly.", - {"english", "language"} + ["english", "language"] ), ( "Do not provide or request help on projects that may break laws, " "breach terms of services, or are malicious or inappropriate.", - {"infraction", "tos", "breach", "malicious", "inappropriate"} + ["infraction", "tos", "breach", "malicious", "inappropriate"] ), ( "Do not post unapproved advertising.", - {"ads", "advertising"} + ["ad", "ads", "advert", "advertising"] ), ( "Keep discussions relevant to the channel topic. " "Each channel's description tells you the topic.", - {"off-topic", "topic", "relevance"} + ["off-topic", "topic", "relevance"] ), ( "Do not help with ongoing exams. When helping with homework, " "help people learn how to do the assignment without doing it for them.", - {"exams", "assignment", "assignments", "homework"} + ["exam", "exams", "assignment", "assignments", "homework"] ), ( "Do not offer or ask for paid work of any kind.", - {"work", "money"} + ["paid", "work", "money"] ), ]) -- cgit v1.2.3 From f07f03abd53789062045afb027ef2d5ecfd63f11 Mon Sep 17 00:00:00 2001 From: Amrou Bellalouna Date: Sun, 18 Sep 2022 18:44:33 +0100 Subject: make docstring more explicit about the returned content --- pydis_site/apps/api/views.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pydis_site/apps/api/views.py b/pydis_site/apps/api/views.py index 836f11ce..f76a78ff 100644 --- a/pydis_site/apps/api/views.py +++ b/pydis_site/apps/api/views.py @@ -100,7 +100,12 @@ class RulesView(APIView): # `format` here is the result format, we have a link format here instead. def get(self, request, format=None): # noqa: D102,ANN001,ANN201 - """Returns a list of our community rules coupled with their keywords.""" + """ + Returns a list of our community rules coupled with their keywords. + + Each item in the returned list is a tuple with the rule as first item + and a list of keywords that match that rules as second item. + """ link_format = request.query_params.get('link_format', 'md') if link_format not in ('html', 'md'): raise ParseError( -- cgit v1.2.3 From 13ae666f0281c7dcfbbe79fdf431f28cd822ec19 Mon Sep 17 00:00:00 2001 From: Amrou Bellalouna Date: Sun, 18 Sep 2022 18:55:18 +0100 Subject: update the RulesView class' docstrings --- pydis_site/apps/api/views.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/pydis_site/apps/api/views.py b/pydis_site/apps/api/views.py index f76a78ff..3cb7e8bd 100644 --- a/pydis_site/apps/api/views.py +++ b/pydis_site/apps/api/views.py @@ -37,12 +37,15 @@ class RulesView(APIView): ## Routes ### GET /rules - Returns a JSON array containing the server's rules: + Returns a JSON array containing the server's rules + coupled with a list of keywords that will be used + when searching for particular rules upon using the + bot's `!rule` command >>> [ - ... "Eat candy.", - ... "Wake up at 4 AM.", - ... "Take your medicine." + ... ("Eat candy.", ["candy", "sweet"]), + ... ("Wake up at 4 AM.", ["wake_up", "early", "early_bird"]), + ... ("Take your medicine.", ["medicine", "health"]) ... ] Since some of the the rules require links, this view -- cgit v1.2.3 From 49909a3165344b0fdbd81300cc752327e08f0ff5 Mon Sep 17 00:00:00 2001 From: Amrou Bellalouna Date: Sun, 18 Sep 2022 18:57:16 +0100 Subject: replace sweet with "sweets" to refer more to candy --- pydis_site/apps/api/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pydis_site/apps/api/views.py b/pydis_site/apps/api/views.py index 3cb7e8bd..2c5343de 100644 --- a/pydis_site/apps/api/views.py +++ b/pydis_site/apps/api/views.py @@ -43,7 +43,7 @@ class RulesView(APIView): bot's `!rule` command >>> [ - ... ("Eat candy.", ["candy", "sweet"]), + ... ("Eat candy.", ["candy", "sweets"]), ... ("Wake up at 4 AM.", ["wake_up", "early", "early_bird"]), ... ("Take your medicine.", ["medicine", "health"]) ... ] -- cgit v1.2.3 From e6db60717efe799e0900ad6cf60f4962f1aef7a1 Mon Sep 17 00:00:00 2001 From: Amrou Bellalouna Date: Sun, 18 Sep 2022 19:11:36 +0100 Subject: restrict the RulesView docstrings to just what it does --- pydis_site/apps/api/views.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pydis_site/apps/api/views.py b/pydis_site/apps/api/views.py index 2c5343de..9e5ad73c 100644 --- a/pydis_site/apps/api/views.py +++ b/pydis_site/apps/api/views.py @@ -38,9 +38,8 @@ class RulesView(APIView): ## Routes ### GET /rules Returns a JSON array containing the server's rules - coupled with a list of keywords that will be used - when searching for particular rules upon using the - bot's `!rule` command + and keywords relating to each rule. + Example response: >>> [ ... ("Eat candy.", ["candy", "sweets"]), -- cgit v1.2.3 From 7fd81a80049763f1dc71815da27d65ce4344a191 Mon Sep 17 00:00:00 2001 From: Amrou Bellalouna Date: Sun, 18 Sep 2022 22:18:50 +0100 Subject: replace parenthesis with square brackets since tuples are serialized are lists --- pydis_site/apps/api/views.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pydis_site/apps/api/views.py b/pydis_site/apps/api/views.py index 9e5ad73c..34167a38 100644 --- a/pydis_site/apps/api/views.py +++ b/pydis_site/apps/api/views.py @@ -42,9 +42,9 @@ class RulesView(APIView): Example response: >>> [ - ... ("Eat candy.", ["candy", "sweets"]), - ... ("Wake up at 4 AM.", ["wake_up", "early", "early_bird"]), - ... ("Take your medicine.", ["medicine", "health"]) + ... ["Eat candy.", ["candy", "sweets"]], + ... ["Wake up at 4 AM.", ["wake_up", "early", "early_bird"]], + ... ["Take your medicine.", ["medicine", "health"]] ... ] Since some of the the rules require links, this view -- cgit v1.2.3