diff options
author | 2022-01-23 16:38:03 +0100 | |
---|---|---|
committer | 2022-01-23 16:38:03 +0100 | |
commit | 605d9a0266a9a967f051fa244bf1c2d31776c119 (patch) | |
tree | a9f3218e355c8f6a106ba2604334ce8a34823715 | |
parent | Link icons belong close together. (diff) | |
parent | Merge pull request #640 from Krish-bhardwaj/main (diff) |
Merge branch 'main' into swfarnsworth/smarter-resources/merge-with-main
106 files changed, 2155 insertions, 909 deletions
diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index dc4b1a92..b6004466 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -6,17 +6,12 @@ pydis_site/apps/api/viewsets/bot/infraction.py @MarkKoz pydis_site/apps/home/** @ks129 # Django ORM -**/migrations/** @Akarys42 -**/models/** @Akarys42 @Den4200 +**/models/** @Den4200 # CI & Docker -.github/workflows/** @MarkKoz @Akarys42 @SebastiaanZ @Den4200 @ks129 -Dockerfile @MarkKoz @Akarys42 @Den4200 -docker-compose.yml @MarkKoz @Akarys42 @Den4200 - -# Tools -poetry.lock @Akarys42 -pyproject.toml @Akarys42 +.github/workflows/** @MarkKoz @SebastiaanZ @Den4200 @ks129 +Dockerfile @MarkKoz @Den4200 +docker-compose.yml @MarkKoz @Den4200 # Metricity pydis_site/apps/api/models/bot/metricity.py @jb3 diff --git a/.github/PULL_REQUEST_TEMPLATE/pull_request.md b/.github/PULL_REQUEST_TEMPLATE/pull_request.md new file mode 100644 index 00000000..358d2553 --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE/pull_request.md @@ -0,0 +1,22 @@ +## Summary +<!-- Please provide a brief description of this PR along with the related issue it fixes --> + + +## Description of changes +<!-- Describe what changes you've made, and how you implemented them --> + + +### I confirm I have: +<!-- Replace [ ] below with [X] to check the boxes --> +- [ ] Joined the [Python Discord community](discord.gg/python) +- [ ] Read the [Code of Conduct](https://www.pydis.com/pages/code-of-conduct) and agree to it +- [ ] I have discussed implementing this feature on the relevant service (Discord, GitHub, etc.) + + +### I have changed API models and I ensure I have: +<!-- Please remove this section if you haven't edited files under pydis_site/apps/api/models --> +- [ ] Opened a PR updating the model on the [API GitHub Repository](https://github.com/python-discord/api) + +**OR** + +- [ ] Opened an issue on the [API GitHub Repository](https://github.com/python-discord/api) explaining what changes need to be made diff --git a/.github/workflows/static-preview.yaml b/.github/workflows/static-preview.yaml new file mode 100644 index 00000000..52d7df5a --- /dev/null +++ b/.github/workflows/static-preview.yaml @@ -0,0 +1,77 @@ +name: Build & Publish Static Preview + +on: + push: + branches: + - main + pull_request: + +jobs: + build: + name: Build Static Preview + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + # Create a commit SHA-based tag for the container repositories + - name: Create SHA Container Tag + id: sha_tag + run: | + tag=$(cut -c 1-7 <<< $GITHUB_SHA) + echo "::set-output name=tag::$tag" + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + + - name: Login to Github Container Registry + uses: docker/login-action@v1 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + + # Build the container, including an inline cache manifest to + # allow us to use the registry as a cache source. + - name: Build Docker Image (Main) + uses: docker/build-push-action@v2 + if: github.ref == 'refs/heads/main' + with: + context: . + push: true + cache-from: type=registry,ref=ghcr.io/python-discord/static-site:latest + cache-to: type=inline + tags: | + ghcr.io/python-discord/static-site:latest + ghcr.io/python-discord/static-site:${{ steps.sha_tag.outputs.tag }} + build-args: | + git_sha=${{ github.sha }} + STATIC_BUILD=TRUE + + - name: Extract Build From Docker Image (Main) + if: github.ref == 'refs/heads/main' + run: | + mkdir docker_build \ + && docker run --entrypoint /bin/echo --name site \ + ghcr.io/python-discord/static-site:${{ steps.sha_tag.outputs.tag }} \ + && docker cp site:/app docker_build/ + + # Build directly to a local folder + - name: Build Docker Image (PR) + uses: docker/build-push-action@v2 + if: github.ref != 'refs/heads/main' + with: + context: . + push: false + cache-from: type=registry,ref=ghcr.io/python-discord/static-site:latest + outputs: type=local,dest=docker_build/ + build-args: | + git_sha=${{ github.sha }} + STATIC_BUILD=TRUE + + - name: Upload Build + uses: actions/upload-artifact@v2 + with: + name: static-build + path: docker_build/app/build/ + if-no-files-found: error @@ -126,3 +126,6 @@ staticfiles/ *.js.tmp log.* + +# Local Netlify folder +.netlify @@ -36,6 +36,12 @@ RUN \ METRICITY_DB_URL=postgres://localhost \ 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 ; \ +fi + # Run web server through custom manager ENTRYPOINT ["python", "manage.py"] CMD ["run"] diff --git a/docker-compose.yml b/docker-compose.yml index 37678949..eb987624 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -18,6 +18,13 @@ services: POSTGRES_DB: pysite POSTGRES_PASSWORD: pysite POSTGRES_USER: pysite + healthcheck: + test: ["CMD-SHELL", "pg_isready -U pysite"] + interval: 2s + timeout: 1s + retries: 5 + volumes: + - ./postgres/init.sql:/docker-entrypoint-initdb.d/init.sql web: build: @@ -33,7 +40,8 @@ services: ports: - "127.0.0.1:8000:8000" depends_on: - - postgres + postgres: + condition: service_healthy tty: true volumes: - .:/app:ro @@ -1,9 +1,8 @@ #!/usr/bin/env python import os -import socket +import platform import sys -import time -from urllib.parse import SplitResult, urlsplit +from pathlib import Path import django from django.contrib.auth import get_user_model @@ -55,21 +54,6 @@ class SiteManager: print("Starting in debug mode.") @staticmethod - def parse_db_url(db_url: str) -> SplitResult: - """Validate and split the given databse url.""" - db_url_parts = urlsplit(db_url) - if not all(( - db_url_parts.hostname, - db_url_parts.username, - db_url_parts.password, - db_url_parts.path - )): - raise ValueError( - "The DATABASE_URL environment variable is not a valid PostgreSQL database URL." - ) - return db_url_parts - - @staticmethod def create_superuser() -> None: """Create a default django admin super user in development environments.""" print("Creating a superuser.") @@ -99,36 +83,6 @@ class SiteManager: print(f"Existing bot token found: {token}") @staticmethod - def wait_for_postgres() -> None: - """Wait for the PostgreSQL database specified in DATABASE_URL.""" - print("Waiting for PostgreSQL database.") - - # Get database URL based on environmental variable passed in compose - database_url_parts = SiteManager.parse_db_url(os.environ["DATABASE_URL"]) - domain = database_url_parts.hostname - # Port may be omitted, 5432 is the default psql port - port = database_url_parts.port or 5432 - - # Attempt to connect to the database socket - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - - attempts_left = 10 - while attempts_left: - try: - # Ignore 'incomplete startup packet' - s.connect((domain, port)) - s.shutdown(socket.SHUT_RDWR) - print("Database is ready.") - break - except socket.error: - attempts_left -= 1 - print("Not ready yet, retrying.") - time.sleep(0.5) - else: - print("Database could not be found, exiting.") - sys.exit(1) - - @staticmethod def set_dev_site_name() -> None: """Set the development site domain in admin from default example.""" # import Site model now after django setup @@ -141,44 +95,8 @@ class SiteManager: name="pythondiscord.local:8000" ) - @staticmethod - def run_metricity_init() -> None: - """ - Initialise metricity relations and populate with some testing data. - - This is done at run time since other projects, like Python bot, - rely on the site initialising it's own db, since they do not have - access to the init.sql file to mount a docker-compose volume. - """ - import psycopg2 - from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT - - print("Initialising metricity.") - - db_url_parts = SiteManager.parse_db_url(os.environ["DATABASE_URL"]) - conn = psycopg2.connect( - host=db_url_parts.hostname, - port=db_url_parts.port, - user=db_url_parts.username, - password=db_url_parts.password, - database=db_url_parts.path[1:] - ) - # Required to create a db from `cursor.execute()` - conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) - - with conn.cursor() as cursor, open("postgres/init.sql", encoding="utf-8") as f: - cursor.execute( - f.read(), - ("metricity", db_url_parts.username, db_url_parts.password) - ) - conn.close() - def prepare_server(self) -> None: """Perform preparation tasks before running the server.""" - self.wait_for_postgres() - if self.debug: - self.run_metricity_init() - django.setup() print("Applying migrations.") @@ -231,21 +149,47 @@ class SiteManager: gunicorn.app.wsgiapp.run() +def clean_up_static_files(build_folder: Path) -> None: + """Recursively loop over the build directory and fix links.""" + for file in build_folder.iterdir(): + if file.is_dir(): + clean_up_static_files(file) + elif file.name.endswith(".html"): + # Fix parent host url + new = file.read_text(encoding="utf-8").replace(f"//{os.getenv('PARENT_HOST')}", "") + + # Fix windows paths if on windows + if platform.system() == "Windows": + new = new.replace("%5C", "/") + + file.write_text(new, encoding="utf-8") + + def main() -> None: """Entry point for Django management script.""" - # Always run metricity init when in CI, indicated by the CI env var - if os.environ.get("CI", "false").lower() == "true": - SiteManager.wait_for_postgres() - SiteManager.run_metricity_init() - # Use the custom site manager for launching the server if len(sys.argv) > 1 and sys.argv[1] == "run": SiteManager(sys.argv).run_server() # Pass any others directly to standard management commands else: + _static_build = "distill" in sys.argv[1] + + if _static_build: + # Build a static version of the site with no databases and API support + os.environ["STATIC_BUILD"] = "True" + if not os.getenv("PARENT_HOST"): + os.environ["PARENT_HOST"] = "REPLACE_THIS.HOST" + execute_from_command_line(sys.argv) + if _static_build: + # Clean up parent host in generated files + for arg in sys.argv[2:]: + if not arg.startswith("-"): + clean_up_static_files(Path(arg)) + break + if __name__ == '__main__': main() diff --git a/postgres/init.sql b/postgres/init.sql index 55bb468f..190a705c 100644 --- a/postgres/init.sql +++ b/postgres/init.sql @@ -1,29 +1,8 @@ --- The following function is from Stack Overflow --- https://stackoverflow.com/questions/18389124/simulate-create-database-if-not-exists-for-postgresql/36218838#36218838 --- User frankhommers (https://stackoverflow.com/users/971229/frankhommers) - -DO -$do$ -DECLARE - _db TEXT := %s; - _user TEXT := %s; - _password TEXT := %s; -BEGIN - CREATE EXTENSION IF NOT EXISTS dblink; - IF EXISTS (SELECT 1 FROM pg_database WHERE datname = _db) THEN - RAISE NOTICE 'Database already exists'; - ELSE - PERFORM dblink_connect( - 'host=localhost user=' || _user || - ' password=' || _password || - ' dbname=' || current_database() - ); - PERFORM dblink_exec('CREATE DATABASE ' || _db); - END IF; -END -$do$; - -CREATE TABLE IF NOT EXISTS users ( +CREATE DATABASE metricity; + +\c metricity; + +CREATE TABLE users ( id varchar, joined_at timestamp, primary key(id) @@ -32,14 +11,14 @@ CREATE TABLE IF NOT EXISTS users ( INSERT INTO users VALUES ( 0, current_timestamp -) ON CONFLICT (id) DO NOTHING; +); INSERT INTO users VALUES ( 1, current_timestamp -) ON CONFLICT (id) DO NOTHING; +); -CREATE TABLE IF NOT EXISTS channels ( +CREATE TABLE channels ( id varchar, name varchar, primary key(id) @@ -48,44 +27,44 @@ CREATE TABLE IF NOT EXISTS channels ( INSERT INTO channels VALUES( '267659945086812160', 'python-general' -) ON CONFLICT (id) DO NOTHING; +); INSERT INTO channels VALUES( '11', 'help-apple' -) ON CONFLICT (id) DO NOTHING; +); INSERT INTO channels VALUES( '12', 'help-cherry' -) ON CONFLICT (id) DO NOTHING; +); INSERT INTO channels VALUES( '21', 'ot0-hello' -) ON CONFLICT (id) DO NOTHING; +); INSERT INTO channels VALUES( '22', 'ot1-world' -) ON CONFLICT (id) DO NOTHING; +); INSERT INTO channels VALUES( '31', 'voice-chat-0' -) ON CONFLICT (id) DO NOTHING; +); INSERT INTO channels VALUES( '32', 'code-help-voice-0' -) ON CONFLICT (id) DO NOTHING; +); INSERT INTO channels VALUES( '1234', 'zebra' -) ON CONFLICT (id) DO NOTHING; +); -CREATE TABLE IF NOT EXISTS messages ( +CREATE TABLE messages ( id varchar, author_id varchar references users(id), is_deleted boolean, @@ -100,7 +79,7 @@ INSERT INTO messages VALUES( false, now(), '267659945086812160' -) ON CONFLICT (id) DO NOTHING; +); INSERT INTO messages VALUES( 1, @@ -108,7 +87,7 @@ INSERT INTO messages VALUES( false, now() + INTERVAL '10 minutes,', '1234' -) ON CONFLICT (id) DO NOTHING; +); INSERT INTO messages VALUES( 2, @@ -116,7 +95,7 @@ INSERT INTO messages VALUES( false, now(), '11' -) ON CONFLICT (id) DO NOTHING; +); INSERT INTO messages VALUES( 3, @@ -124,7 +103,7 @@ INSERT INTO messages VALUES( false, now(), '12' -) ON CONFLICT (id) DO NOTHING; +); INSERT INTO messages VALUES( 4, @@ -132,7 +111,7 @@ INSERT INTO messages VALUES( false, now(), '21' -) ON CONFLICT (id) DO NOTHING; +); INSERT INTO messages VALUES( 5, @@ -140,7 +119,7 @@ INSERT INTO messages VALUES( false, now(), '22' -) ON CONFLICT (id) DO NOTHING; +); INSERT INTO messages VALUES( 6, @@ -148,7 +127,7 @@ INSERT INTO messages VALUES( false, now(), '31' -) ON CONFLICT (id) DO NOTHING; +); INSERT INTO messages VALUES( 7, @@ -156,7 +135,7 @@ INSERT INTO messages VALUES( false, now(), '32' -) ON CONFLICT (id) DO NOTHING; +); INSERT INTO messages VALUES( 8, @@ -164,4 +143,4 @@ INSERT INTO messages VALUES( true, now(), '32' -) ON CONFLICT (id) DO NOTHING; +); diff --git a/pydis_site/apps/admin/urls.py b/pydis_site/apps/admin/urls.py index 146c6496..a4f3e517 100644 --- a/pydis_site/apps/admin/urls.py +++ b/pydis_site/apps/admin/urls.py @@ -2,6 +2,7 @@ from django.contrib import admin from django.urls import path +app_name = 'admin' urlpatterns = ( path('', admin.site.urls), ) diff --git a/pydis_site/apps/api/__init__.py b/pydis_site/apps/api/__init__.py index e69de29b..afa5b4d5 100644 --- a/pydis_site/apps/api/__init__.py +++ b/pydis_site/apps/api/__init__.py @@ -0,0 +1 @@ +default_app_config = 'pydis_site.apps.api.apps.ApiConfig' diff --git a/pydis_site/apps/api/apps.py b/pydis_site/apps/api/apps.py index 76810b2e..18eda9e3 100644 --- a/pydis_site/apps/api/apps.py +++ b/pydis_site/apps/api/apps.py @@ -4,4 +4,12 @@ from django.apps import AppConfig class ApiConfig(AppConfig): """Django AppConfig for the API app.""" - name = 'api' + name = 'pydis_site.apps.api' + + def ready(self) -> None: + """ + Gets called as soon as the registry is fully populated. + + https://docs.djangoproject.com/en/3.2/ref/applications/#django.apps.AppConfig.ready + """ + import pydis_site.apps.api.signals # noqa: F401 diff --git a/pydis_site/apps/api/migrations/0059_populate_filterlists.py b/pydis_site/apps/api/migrations/0059_populate_filterlists.py index 8c550191..273db3d1 100644 --- a/pydis_site/apps/api/migrations/0059_populate_filterlists.py +++ b/pydis_site/apps/api/migrations/0059_populate_filterlists.py @@ -60,35 +60,35 @@ domain_name_blacklist = [ ] filter_token_blacklist = [ - ("\bgoo+ks*\b", None, False), - ("\bky+s+\b", None, False), - ("\bki+ke+s*\b", None, False), - ("\bbeaner+s?\b", None, False), - ("\bcoo+ns*\b", None, False), - ("\bnig+lets*\b", None, False), - ("\bslant-eyes*\b", None, False), - ("\btowe?l-?head+s*\b", None, False), - ("\bchi*n+k+s*\b", None, False), - ("\bspick*s*\b", None, False), - ("\bkill* +(?:yo)?urself+\b", None, False), - ("\bjew+s*\b", None, False), - ("\bsuicide\b", None, False), - ("\brape\b", None, False), - ("\b(re+)tar+(d+|t+)(ed)?\b", None, False), - ("\bta+r+d+\b", None, False), - ("\bcunts*\b", None, False), - ("\btrann*y\b", None, False), - ("\bshemale\b", None, False), - ("fa+g+s*", None, False), - ("卐", None, False), - ("卍", None, False), - ("࿖", None, False), - ("࿕", None, False), - ("࿘", None, False), - ("࿗", None, False), - ("cuck(?!oo+)", None, False), - ("nigg+(?:e*r+|a+h*?|u+h+)s?", None, False), - ("fag+o+t+s*", None, False), + (r"\bgoo+ks*\b", None, False), + (r"\bky+s+\b", None, False), + (r"\bki+ke+s*\b", None, False), + (r"\bbeaner+s?\b", None, False), + (r"\bcoo+ns*\b", None, False), + (r"\bnig+lets*\b", None, False), + (r"\bslant-eyes*\b", None, False), + (r"\btowe?l-?head+s*\b", None, False), + (r"\bchi*n+k+s*\b", None, False), + (r"\bspick*s*\b", None, False), + (r"\bkill* +(?:yo)?urself+\b", None, False), + (r"\bjew+s*\b", None, False), + (r"\bsuicide\b", None, False), + (r"\brape\b", None, False), + (r"\b(re+)tar+(d+|t+)(ed)?\b", None, False), + (r"\bta+r+d+\b", None, False), + (r"\bcunts*\b", None, False), + (r"\btrann*y\b", None, False), + (r"\bshemale\b", None, False), + (r"fa+g+s*", None, False), + (r"卐", None, False), + (r"卍", None, False), + (r"࿖", None, False), + (r"࿕", None, False), + (r"࿘", None, False), + (r"࿗", None, False), + (r"cuck(?!oo+)", None, False), + (r"nigg+(?:e*r+|a+h*?|u+h+)s?", None, False), + (r"fag+o+t+s*", None, False), ] file_format_whitelist = [ diff --git a/pydis_site/apps/api/migrations/0070_auto_20210519_0545.py b/pydis_site/apps/api/migrations/0070_auto_20210519_0545.py new file mode 100644 index 00000000..dbd7ac91 --- /dev/null +++ b/pydis_site/apps/api/migrations/0070_auto_20210519_0545.py @@ -0,0 +1,23 @@ +# Generated by Django 3.0.14 on 2021-05-19 05:45 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0069_documentationlink_validators'), + ] + + operations = [ + migrations.AddField( + model_name='offtopicchannelname', + name='active', + field=models.BooleanField(default=True, help_text='Whether or not this name should be considered for naming channels.'), + ), + migrations.AlterField( + model_name='offtopicchannelname', + name='used', + field=models.BooleanField(default=False, help_text='Whether or not this name has already been used during this rotation.'), + ), + ] diff --git a/pydis_site/apps/api/migrations/0072_merge_20210724_1354.py b/pydis_site/apps/api/migrations/0072_merge_20210724_1354.py new file mode 100644 index 00000000..f12efab5 --- /dev/null +++ b/pydis_site/apps/api/migrations/0072_merge_20210724_1354.py @@ -0,0 +1,14 @@ +# Generated by Django 3.0.14 on 2021-07-24 13:54 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0071_increase_message_content_4000'), + ('api', '0070_auto_20210519_0545'), + ] + + operations = [ + ] diff --git a/pydis_site/apps/api/migrations/0073_otn_allow_GT_and_LT.py b/pydis_site/apps/api/migrations/0073_otn_allow_GT_and_LT.py new file mode 100644 index 00000000..09ad13da --- /dev/null +++ b/pydis_site/apps/api/migrations/0073_otn_allow_GT_and_LT.py @@ -0,0 +1,19 @@ +# Generated by Django 3.0.14 on 2021-09-27 20:38 + +import django.core.validators +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0072_doc_allow_blank_base_url'), + ] + + operations = [ + migrations.AlterField( + model_name='offtopicchannelname', + name='name', + field=models.CharField(help_text='The actual channel name that will be used on our Discord server.', max_length=96, primary_key=True, serialize=False, validators=[django.core.validators.RegexValidator(regex="^[a-z0-9\\U0001d5a0-\\U0001d5b9-ǃ?’'<>]+$")]), + ), + ] diff --git a/pydis_site/apps/api/migrations/0074_merge_20211105_0518.py b/pydis_site/apps/api/migrations/0074_merge_20211105_0518.py new file mode 100644 index 00000000..ebf5ae15 --- /dev/null +++ b/pydis_site/apps/api/migrations/0074_merge_20211105_0518.py @@ -0,0 +1,14 @@ +# Generated by Django 3.0.14 on 2021-11-05 05:18 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0072_merge_20210724_1354'), + ('api', '0073_otn_allow_GT_and_LT'), + ] + + operations = [ + ] diff --git a/pydis_site/apps/api/migrations/0074_reminder_failures.py b/pydis_site/apps/api/migrations/0074_reminder_failures.py new file mode 100644 index 00000000..2860046e --- /dev/null +++ b/pydis_site/apps/api/migrations/0074_reminder_failures.py @@ -0,0 +1,18 @@ +# Generated by Django 3.0.14 on 2021-10-27 17:44 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0073_otn_allow_GT_and_LT'), + ] + + operations = [ + migrations.AddField( + model_name='reminder', + name='failures', + field=models.IntegerField(default=0, help_text='Number of times we attempted to send the reminder and failed.'), + ), + ] diff --git a/pydis_site/apps/api/migrations/0075_add_redirects_filter.py b/pydis_site/apps/api/migrations/0075_add_redirects_filter.py new file mode 100644 index 00000000..23dc176f --- /dev/null +++ b/pydis_site/apps/api/migrations/0075_add_redirects_filter.py @@ -0,0 +1,18 @@ +# Generated by Django 3.0.14 on 2021-11-17 10:24 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0074_reminder_failures'), + ] + + operations = [ + migrations.AlterField( + model_name='filterlist', + name='type', + field=models.CharField(choices=[('GUILD_INVITE', 'Guild Invite'), ('FILE_FORMAT', 'File Format'), ('DOMAIN_NAME', 'Domain Name'), ('FILTER_TOKEN', 'Filter Token'), ('REDIRECT', 'Redirect')], help_text='The type of allowlist this is on.', max_length=50), + ), + ] diff --git a/pydis_site/apps/api/migrations/0075_infraction_dm_sent.py b/pydis_site/apps/api/migrations/0075_infraction_dm_sent.py new file mode 100644 index 00000000..c0ac709d --- /dev/null +++ b/pydis_site/apps/api/migrations/0075_infraction_dm_sent.py @@ -0,0 +1,18 @@ +# Generated by Django 3.0.14 on 2021-11-10 22:06 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0074_reminder_failures'), + ] + + operations = [ + migrations.AddField( + model_name='infraction', + name='dm_sent', + field=models.BooleanField(help_text='Whether a DM was sent to the user when infraction was applied.', null=True), + ), + ] diff --git a/pydis_site/apps/api/migrations/0076_merge_20211125_1941.py b/pydis_site/apps/api/migrations/0076_merge_20211125_1941.py new file mode 100644 index 00000000..097d0a0c --- /dev/null +++ b/pydis_site/apps/api/migrations/0076_merge_20211125_1941.py @@ -0,0 +1,14 @@ +# Generated by Django 3.0.14 on 2021-11-25 19:41 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0075_infraction_dm_sent'), + ('api', '0075_add_redirects_filter'), + ] + + operations = [ + ] diff --git a/pydis_site/apps/api/migrations/0077_use_generic_jsonfield.py b/pydis_site/apps/api/migrations/0077_use_generic_jsonfield.py new file mode 100644 index 00000000..9e8f2fb9 --- /dev/null +++ b/pydis_site/apps/api/migrations/0077_use_generic_jsonfield.py @@ -0,0 +1,25 @@ +# Generated by Django 3.1.13 on 2021-11-27 12:27 + +import django.contrib.postgres.fields +from django.db import migrations, models +import pydis_site.apps.api.models.utils + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0076_merge_20211125_1941'), + ] + + operations = [ + migrations.AlterField( + model_name='botsetting', + name='data', + field=models.JSONField(help_text='The actual settings of this setting.'), + ), + migrations.AlterField( + model_name='deletedmessage', + name='embeds', + field=django.contrib.postgres.fields.ArrayField(base_field=models.JSONField(validators=[pydis_site.apps.api.models.utils.validate_embed]), blank=True, help_text='Embeds attached to this message.', size=None), + ), + ] diff --git a/pydis_site/apps/api/migrations/0078_merge_20211213_0552.py b/pydis_site/apps/api/migrations/0078_merge_20211213_0552.py new file mode 100644 index 00000000..5ce0e871 --- /dev/null +++ b/pydis_site/apps/api/migrations/0078_merge_20211213_0552.py @@ -0,0 +1,14 @@ +# Generated by Django 3.1.14 on 2021-12-13 05:52 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0077_use_generic_jsonfield'), + ('api', '0074_merge_20211105_0518'), + ] + + operations = [ + ] diff --git a/pydis_site/apps/api/models/bot/bot_setting.py b/pydis_site/apps/api/models/bot/bot_setting.py index 2a3944f8..1bcb1ae6 100644 --- a/pydis_site/apps/api/models/bot/bot_setting.py +++ b/pydis_site/apps/api/models/bot/bot_setting.py @@ -1,4 +1,3 @@ -from django.contrib.postgres import fields as pgfields from django.core.exceptions import ValidationError from django.db import models @@ -24,6 +23,6 @@ class BotSetting(ModelReprMixin, models.Model): max_length=50, validators=(validate_bot_setting_name,) ) - data = pgfields.JSONField( + data = models.JSONField( help_text="The actual settings of this setting." ) diff --git a/pydis_site/apps/api/models/bot/filter_list.py b/pydis_site/apps/api/models/bot/filter_list.py index d279e137..d30f7213 100644 --- a/pydis_site/apps/api/models/bot/filter_list.py +++ b/pydis_site/apps/api/models/bot/filter_list.py @@ -12,6 +12,7 @@ class FilterList(ModelTimestampMixin, ModelReprMixin, models.Model): 'FILE_FORMAT ' 'DOMAIN_NAME ' 'FILTER_TOKEN ' + 'REDIRECT ' ) type = models.CharField( max_length=50, diff --git a/pydis_site/apps/api/models/bot/infraction.py b/pydis_site/apps/api/models/bot/infraction.py index 60c1e8dd..913631d4 100644 --- a/pydis_site/apps/api/models/bot/infraction.py +++ b/pydis_site/apps/api/models/bot/infraction.py @@ -57,6 +57,10 @@ class Infraction(ModelReprMixin, models.Model): default=False, help_text="Whether the infraction is a shadow infraction." ) + dm_sent = models.BooleanField( + null=True, + help_text="Whether a DM was sent to the user when infraction was applied." + ) def __str__(self): """Returns some info on the current infraction, for display purposes.""" diff --git a/pydis_site/apps/api/models/bot/message.py b/pydis_site/apps/api/models/bot/message.py index 60e2a553..bab3368d 100644 --- a/pydis_site/apps/api/models/bot/message.py +++ b/pydis_site/apps/api/models/bot/message.py @@ -48,7 +48,7 @@ class Message(ModelReprMixin, models.Model): blank=True ) embeds = pgfields.ArrayField( - pgfields.JSONField( + models.JSONField( validators=(validate_embed,) ), blank=True, diff --git a/pydis_site/apps/api/models/bot/message_deletion_context.py b/pydis_site/apps/api/models/bot/message_deletion_context.py index 1410250a..25741266 100644 --- a/pydis_site/apps/api/models/bot/message_deletion_context.py +++ b/pydis_site/apps/api/models/bot/message_deletion_context.py @@ -1,5 +1,5 @@ from django.db import models -from django_hosts.resolvers import reverse +from django.urls import reverse from pydis_site.apps.api.models.bot.user import User from pydis_site.apps.api.models.mixins import ModelReprMixin @@ -33,7 +33,7 @@ class MessageDeletionContext(ModelReprMixin, models.Model): @property def log_url(self) -> str: """Create the url for the deleted message logs.""" - return reverse('logs', host="staff", args=(self.id,)) + return reverse('staff:logs', args=(self.id,)) class Meta: """Set the ordering for list views to newest first.""" diff --git a/pydis_site/apps/api/models/bot/metricity.py b/pydis_site/apps/api/models/bot/metricity.py index 00076248..abd25ef0 100644 --- a/pydis_site/apps/api/models/bot/metricity.py +++ b/pydis_site/apps/api/models/bot/metricity.py @@ -4,13 +4,13 @@ from django.db import connections BLOCK_INTERVAL = 10 * 60 # 10 minute blocks -EXCLUDE_CHANNELS = [ +EXCLUDE_CHANNELS = ( "267659945086812160", # Bot commands "607247579608121354" # SeasonalBot commands -] +) -class NotFound(Exception): # noqa: N818 +class NotFoundError(Exception): # noqa: N818 """Raised when an entity cannot be found.""" pass @@ -37,7 +37,7 @@ class Metricity: values = self.cursor.fetchone() if not values: - raise NotFound() + raise NotFoundError() return dict(zip(columns, values)) @@ -46,19 +46,19 @@ class Metricity: self.cursor.execute( """ SELECT - COUNT(*) + COUNT(*) FROM messages WHERE - author_id = '%s' - AND NOT is_deleted - AND NOT %s::varchar[] @> ARRAY[channel_id] + author_id = '%s' + AND NOT is_deleted + AND channel_id NOT IN %s """, [user_id, EXCLUDE_CHANNELS] ) values = self.cursor.fetchone() if not values: - raise NotFound() + raise NotFoundError() return values[0] @@ -79,7 +79,7 @@ class Metricity: WHERE author_id='%s' AND NOT is_deleted - AND NOT %s::varchar[] @> ARRAY[channel_id] + AND channel_id NOT IN %s GROUP BY interval ) block_query; """, @@ -88,7 +88,7 @@ class Metricity: values = self.cursor.fetchone() if not values: - raise NotFound() + raise NotFoundError() return values[0] @@ -127,6 +127,6 @@ class Metricity: values = self.cursor.fetchall() if not values: - raise NotFound() + raise NotFoundError() return values diff --git a/pydis_site/apps/api/models/bot/off_topic_channel_name.py b/pydis_site/apps/api/models/bot/off_topic_channel_name.py index 403c7465..e9fec114 100644 --- a/pydis_site/apps/api/models/bot/off_topic_channel_name.py +++ b/pydis_site/apps/api/models/bot/off_topic_channel_name.py @@ -11,14 +11,19 @@ class OffTopicChannelName(ModelReprMixin, models.Model): primary_key=True, max_length=96, validators=( - RegexValidator(regex=r"^[a-z0-9\U0001d5a0-\U0001d5b9-ǃ?’']+$"), + RegexValidator(regex=r"^[a-z0-9\U0001d5a0-\U0001d5b9-ǃ?’'<>]+$"), ), help_text="The actual channel name that will be used on our Discord server." ) used = models.BooleanField( default=False, - help_text="Whether or not this name has already been used during this rotation", + help_text="Whether or not this name has already been used during this rotation.", + ) + + active = models.BooleanField( + default=True, + help_text="Whether or not this name should be considered for naming channels." ) def __str__(self): diff --git a/pydis_site/apps/api/models/bot/reminder.py b/pydis_site/apps/api/models/bot/reminder.py index 7d968a0e..173900ee 100644 --- a/pydis_site/apps/api/models/bot/reminder.py +++ b/pydis_site/apps/api/models/bot/reminder.py @@ -59,6 +59,10 @@ class Reminder(ModelReprMixin, models.Model): blank=True, help_text="IDs of roles or users to ping with the reminder." ) + failures = models.IntegerField( + default=0, + help_text="Number of times we attempted to send the reminder and failed." + ) def __str__(self): """Returns some info on the current reminder, for display purposes.""" diff --git a/pydis_site/apps/api/models/utils.py b/pydis_site/apps/api/models/utils.py index 0e220a1d..859394d2 100644 --- a/pydis_site/apps/api/models/utils.py +++ b/pydis_site/apps/api/models/utils.py @@ -103,11 +103,10 @@ def validate_embed(embed: Any) -> None: Example: - >>> from django.contrib.postgres import fields as pgfields >>> from django.db import models >>> from pydis_site.apps.api.models.utils import validate_embed >>> class MyMessage(models.Model): - ... embed = pgfields.JSONField( + ... embed = models.JSONField( ... validators=( ... validate_embed, ... ) diff --git a/pydis_site/apps/api/serializers.py b/pydis_site/apps/api/serializers.py index f47bedca..ac05ebd4 100644 --- a/pydis_site/apps/api/serializers.py +++ b/pydis_site/apps/api/serializers.py @@ -145,7 +145,16 @@ class InfractionSerializer(ModelSerializer): model = Infraction fields = ( - 'id', 'inserted_at', 'expires_at', 'active', 'user', 'actor', 'type', 'reason', 'hidden' + 'id', + 'inserted_at', + 'expires_at', + 'active', + 'user', + 'actor', + 'type', + 'reason', + 'hidden', + 'dm_sent' ) validators = [ UniqueTogetherValidator( @@ -200,25 +209,30 @@ class ExpandedInfractionSerializer(InfractionSerializer): return ret +class OffTopicChannelNameListSerializer(ListSerializer): + """Custom ListSerializer to override to_representation() when list views are triggered.""" + + def to_representation(self, objects: list[OffTopicChannelName]) -> list[str]: + """ + Return a list with all `OffTopicChannelName`s in the database. + + This returns the list of off topic channel names. We want to only return + the name attribute, hence it is unnecessary to create a nested dictionary. + Additionally, this allows off topic channel name routes to simply return an + array of names instead of objects, saving on bandwidth. + """ + return [obj.name for obj in objects] + + class OffTopicChannelNameSerializer(ModelSerializer): """A class providing (de-)serialization of `OffTopicChannelName` instances.""" class Meta: """Metadata defined for the Django REST Framework.""" + list_serializer_class = OffTopicChannelNameListSerializer model = OffTopicChannelName - fields = ('name',) - - def to_representation(self, obj: OffTopicChannelName) -> str: - """ - Return the representation of this `OffTopicChannelName`. - - This only returns the name of the off topic channel name. As the model - only has a single attribute, it is unnecessary to create a nested dictionary. - Additionally, this allows off topic channel name routes to simply return an - array of names instead of objects, saving on bandwidth. - """ - return obj.name + fields = ('name', 'used', 'active') class ReminderSerializer(ModelSerializer): @@ -231,7 +245,15 @@ class ReminderSerializer(ModelSerializer): model = Reminder fields = ( - 'active', 'author', 'jump_url', 'channel_id', 'content', 'expiration', 'id', 'mentions' + 'active', + 'author', + 'jump_url', + 'channel_id', + 'content', + 'expiration', + 'id', + 'mentions', + 'failures' ) diff --git a/pydis_site/apps/api/signals.py b/pydis_site/apps/api/signals.py new file mode 100644 index 00000000..5c26bfb6 --- /dev/null +++ b/pydis_site/apps/api/signals.py @@ -0,0 +1,12 @@ +from django.db.models.signals import post_delete +from django.dispatch import receiver + +from pydis_site.apps.api.models.bot import Role, User + + +@receiver(signal=post_delete, sender=Role) +def delete_role_from_user(sender: Role, instance: Role, **kwargs) -> None: + """Unassigns the Role (instance) that is being deleted from every user that has it.""" + for user in User.objects.filter(roles__contains=[instance.id]): + del user.roles[user.roles.index(instance.id)] + user.save() diff --git a/pydis_site/apps/api/tests/base.py b/pydis_site/apps/api/tests/base.py index 61c23b0f..c9f3cb7e 100644 --- a/pydis_site/apps/api/tests/base.py +++ b/pydis_site/apps/api/tests/base.py @@ -11,7 +11,7 @@ test_user, _created = User.objects.get_or_create( ) -class APISubdomainTestCase(APITestCase): +class AuthenticatedAPITestCase(APITestCase): """ Configures the test client. @@ -24,14 +24,13 @@ class APISubdomainTestCase(APITestCase): `self.client.force_authenticate(user=created_user)` to force authentication through the created user. - Using this performs the following niceties for you which ease writing tests: - - setting the `HTTP_HOST` request header to `api.pythondiscord.local:8000`, and + Using this performs the following nicety for you which eases writing tests: - forcing authentication for the test user. If you don't want to force authentication (for example, to test a route's response for an unauthenticated user), un-force authentication by using the following: - >>> from pydis_site.apps.api.tests.base import APISubdomainTestCase - >>> class UnauthedUserTestCase(APISubdomainTestCase): + >>> from pydis_site.apps.api.tests.base import AuthenticatedAPITestCase + >>> class UnauthedUserTestCase(AuthenticatedAPITestCase): ... def setUp(self): ... super().setUp() ... self.client.force_authentication(user=None) @@ -42,30 +41,26 @@ class APISubdomainTestCase(APITestCase): ... resp = self.client.delete('/my-publicly-readable-endpoint/42') ... self.assertEqual(resp.status_code, 401) - Make sure to include the `super().setUp(self)` call, otherwise, you may get - status code 404 for some URLs due to the missing `HTTP_HOST` header. - ## Example Using this in a test case is rather straightforward: - >>> from pydis_site.apps.api.tests.base import APISubdomainTestCase - >>> class MyAPITestCase(APISubdomainTestCase): + >>> from pydis_site.apps.api.tests.base import AuthenticatedAPITestCase + >>> class MyAPITestCase(AuthenticatedAPITestCase): ... def test_that_it_works(self): ... response = self.client.get('/my-endpoint') ... self.assertEqual(response.status_code, 200) - To reverse URLs of the API host, you need to use `django_hosts`: + To reverse URLs of the API host, you need to use `django.urls`: - >>> from django_hosts.resolvers import reverse - >>> from pydis_site.apps.api.tests.base import APISubdomainTestCase - >>> class MyReversedTestCase(APISubdomainTestCase): + >>> from django.urls import reverse + >>> from pydis_site.apps.api.tests.base import AuthenticatedAPITestCase + >>> class MyReversedTestCase(AuthenticatedAPITestCase): ... def test_my_endpoint(self): - ... url = reverse('user-detail', host='api') + ... url = reverse('api:user-detail') ... response = self.client.get(url) ... self.assertEqual(response.status_code, 200) """ def setUp(self): super().setUp() - self.client.defaults['HTTP_HOST'] = 'api.pythondiscord.local:8000' self.client.force_authenticate(test_user) diff --git a/pydis_site/apps/api/tests/test_deleted_messages.py b/pydis_site/apps/api/tests/test_deleted_messages.py index 40450844..1eb535d8 100644 --- a/pydis_site/apps/api/tests/test_deleted_messages.py +++ b/pydis_site/apps/api/tests/test_deleted_messages.py @@ -1,13 +1,13 @@ from datetime import datetime +from django.urls import reverse from django.utils import timezone -from django_hosts.resolvers import reverse -from .base import APISubdomainTestCase +from .base import AuthenticatedAPITestCase from ..models import MessageDeletionContext, User -class DeletedMessagesWithoutActorTests(APISubdomainTestCase): +class DeletedMessagesWithoutActorTests(AuthenticatedAPITestCase): @classmethod def setUpTestData(cls): cls.author = User.objects.create( @@ -40,14 +40,14 @@ class DeletedMessagesWithoutActorTests(APISubdomainTestCase): } def test_accepts_valid_data(self): - url = reverse('bot:messagedeletioncontext-list', host='api') + url = reverse('api:bot:messagedeletioncontext-list') response = self.client.post(url, data=self.data) self.assertEqual(response.status_code, 201) [context] = MessageDeletionContext.objects.all() self.assertIsNone(context.actor) -class DeletedMessagesWithActorTests(APISubdomainTestCase): +class DeletedMessagesWithActorTests(AuthenticatedAPITestCase): @classmethod def setUpTestData(cls): cls.author = cls.actor = User.objects.create( @@ -72,14 +72,14 @@ class DeletedMessagesWithActorTests(APISubdomainTestCase): } def test_accepts_valid_data_and_sets_actor(self): - url = reverse('bot:messagedeletioncontext-list', host='api') + url = reverse('api:bot:messagedeletioncontext-list') response = self.client.post(url, data=self.data) self.assertEqual(response.status_code, 201) [context] = MessageDeletionContext.objects.all() self.assertEqual(context.actor.id, self.actor.id) -class DeletedMessagesLogURLTests(APISubdomainTestCase): +class DeletedMessagesLogURLTests(AuthenticatedAPITestCase): @classmethod def setUpTestData(cls): cls.author = cls.actor = User.objects.create( @@ -94,6 +94,6 @@ class DeletedMessagesLogURLTests(APISubdomainTestCase): ) def test_valid_log_url(self): - expected_url = reverse('logs', host="staff", args=(1,)) + expected_url = reverse('staff:logs', args=(1,)) [context] = MessageDeletionContext.objects.all() self.assertEqual(context.log_url, expected_url) diff --git a/pydis_site/apps/api/tests/test_documentation_links.py b/pydis_site/apps/api/tests/test_documentation_links.py index 39fb08f3..4e238cbb 100644 --- a/pydis_site/apps/api/tests/test_documentation_links.py +++ b/pydis_site/apps/api/tests/test_documentation_links.py @@ -1,61 +1,61 @@ -from django_hosts.resolvers import reverse +from django.urls import reverse -from .base import APISubdomainTestCase +from .base import AuthenticatedAPITestCase from ..models import DocumentationLink -class UnauthedDocumentationLinkAPITests(APISubdomainTestCase): +class UnauthedDocumentationLinkAPITests(AuthenticatedAPITestCase): def setUp(self): super().setUp() self.client.force_authenticate(user=None) def test_detail_lookup_returns_401(self): - url = reverse('bot:documentationlink-detail', args=('whatever',), host='api') + url = reverse('api:bot:documentationlink-detail', args=('whatever',)) response = self.client.get(url) self.assertEqual(response.status_code, 401) def test_list_returns_401(self): - url = reverse('bot:documentationlink-list', host='api') + url = reverse('api:bot:documentationlink-list') response = self.client.get(url) self.assertEqual(response.status_code, 401) def test_create_returns_401(self): - url = reverse('bot:documentationlink-list', host='api') + url = reverse('api:bot:documentationlink-list') response = self.client.post(url, data={'hi': 'there'}) self.assertEqual(response.status_code, 401) def test_delete_returns_401(self): - url = reverse('bot:documentationlink-detail', args=('whatever',), host='api') + url = reverse('api:bot:documentationlink-detail', args=('whatever',)) response = self.client.delete(url) self.assertEqual(response.status_code, 401) -class EmptyDatabaseDocumentationLinkAPITests(APISubdomainTestCase): +class EmptyDatabaseDocumentationLinkAPITests(AuthenticatedAPITestCase): def test_detail_lookup_returns_404(self): - url = reverse('bot:documentationlink-detail', args=('whatever',), host='api') + url = reverse('api:bot:documentationlink-detail', args=('whatever',)) response = self.client.get(url) self.assertEqual(response.status_code, 404) def test_list_all_returns_empty_list(self): - url = reverse('bot:documentationlink-list', host='api') + url = reverse('api:bot:documentationlink-list') response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), []) def test_delete_returns_404(self): - url = reverse('bot:documentationlink-detail', args=('whatever',), host='api') + url = reverse('api:bot:documentationlink-detail', args=('whatever',)) response = self.client.delete(url) self.assertEqual(response.status_code, 404) -class DetailLookupDocumentationLinkAPITests(APISubdomainTestCase): +class DetailLookupDocumentationLinkAPITests(AuthenticatedAPITestCase): @classmethod def setUpTestData(cls): cls.doc_link = DocumentationLink.objects.create( @@ -71,27 +71,27 @@ class DetailLookupDocumentationLinkAPITests(APISubdomainTestCase): } def test_detail_lookup_unknown_package_returns_404(self): - url = reverse('bot:documentationlink-detail', args=('whatever',), host='api') + url = reverse('api:bot:documentationlink-detail', args=('whatever',)) response = self.client.get(url) self.assertEqual(response.status_code, 404) def test_detail_lookup_created_package_returns_package(self): - url = reverse('bot:documentationlink-detail', args=(self.doc_link.package,), host='api') + url = reverse('api:bot:documentationlink-detail', args=(self.doc_link.package,)) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), self.doc_json) def test_list_all_packages_shows_created_package(self): - url = reverse('bot:documentationlink-list', host='api') + url = reverse('api:bot:documentationlink-list') response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), [self.doc_json]) def test_create_invalid_body_returns_400(self): - url = reverse('bot:documentationlink-list', host='api') + url = reverse('api:bot:documentationlink-list') response = self.client.post(url, data={'i': 'am', 'totally': 'valid'}) self.assertEqual(response.status_code, 400) @@ -103,7 +103,7 @@ class DetailLookupDocumentationLinkAPITests(APISubdomainTestCase): 'inventory_url': 'totally an url' } - url = reverse('bot:documentationlink-list', host='api') + url = reverse('api:bot:documentationlink-list') response = self.client.post(url, data=body) self.assertEqual(response.status_code, 400) @@ -114,13 +114,13 @@ class DetailLookupDocumentationLinkAPITests(APISubdomainTestCase): with self.subTest(package_name=case): body = self.doc_json.copy() body['package'] = case - url = reverse('bot:documentationlink-list', host='api') + url = reverse('api:bot:documentationlink-list') response = self.client.post(url, data=body) self.assertEqual(response.status_code, 400) -class DocumentationLinkCreationTests(APISubdomainTestCase): +class DocumentationLinkCreationTests(AuthenticatedAPITestCase): def setUp(self): super().setUp() @@ -130,27 +130,27 @@ class DocumentationLinkCreationTests(APISubdomainTestCase): 'inventory_url': 'https://docs.example.com' } - url = reverse('bot:documentationlink-list', host='api') + url = reverse('api:bot:documentationlink-list') response = self.client.post(url, data=self.body) self.assertEqual(response.status_code, 201) def test_package_in_full_list(self): - url = reverse('bot:documentationlink-list', host='api') + url = reverse('api:bot:documentationlink-list') response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), [self.body]) def test_detail_lookup_works_with_package(self): - url = reverse('bot:documentationlink-detail', args=(self.body['package'],), host='api') + url = reverse('api:bot:documentationlink-detail', args=(self.body['package'],)) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), self.body) -class DocumentationLinkDeletionTests(APISubdomainTestCase): +class DocumentationLinkDeletionTests(AuthenticatedAPITestCase): @classmethod def setUpTestData(cls): cls.doc_link = DocumentationLink.objects.create( @@ -160,13 +160,13 @@ class DocumentationLinkDeletionTests(APISubdomainTestCase): ) def test_unknown_package_returns_404(self): - url = reverse('bot:documentationlink-detail', args=('whatever',), host='api') + url = reverse('api:bot:documentationlink-detail', args=('whatever',)) response = self.client.delete(url) self.assertEqual(response.status_code, 404) def test_delete_known_package_returns_204(self): - url = reverse('bot:documentationlink-detail', args=(self.doc_link.package,), host='api') + url = reverse('api:bot:documentationlink-detail', args=(self.doc_link.package,)) response = self.client.delete(url) self.assertEqual(response.status_code, 204) diff --git a/pydis_site/apps/api/tests/test_filterlists.py b/pydis_site/apps/api/tests/test_filterlists.py index 188c0fff..5a5bca60 100644 --- a/pydis_site/apps/api/tests/test_filterlists.py +++ b/pydis_site/apps/api/tests/test_filterlists.py @@ -1,9 +1,9 @@ -from django_hosts.resolvers import reverse +from django.urls import reverse from pydis_site.apps.api.models import FilterList -from pydis_site.apps.api.tests.base import APISubdomainTestCase +from pydis_site.apps.api.tests.base import AuthenticatedAPITestCase -URL = reverse('bot:filterlist-list', host='api') +URL = reverse('api:bot:filterlist-list') JPEG_ALLOWLIST = { "type": 'FILE_FORMAT', "allowed": True, @@ -16,7 +16,7 @@ PNG_ALLOWLIST = { } -class UnauthenticatedTests(APISubdomainTestCase): +class UnauthenticatedTests(AuthenticatedAPITestCase): def setUp(self): super().setUp() self.client.force_authenticate(user=None) @@ -27,7 +27,7 @@ class UnauthenticatedTests(APISubdomainTestCase): self.assertEqual(response.status_code, 401) -class EmptyDatabaseTests(APISubdomainTestCase): +class EmptyDatabaseTests(AuthenticatedAPITestCase): @classmethod def setUpTestData(cls): FilterList.objects.all().delete() @@ -39,7 +39,7 @@ class EmptyDatabaseTests(APISubdomainTestCase): self.assertEqual(response.json(), []) -class FetchTests(APISubdomainTestCase): +class FetchTests(AuthenticatedAPITestCase): @classmethod def setUpTestData(cls): FilterList.objects.all().delete() @@ -68,7 +68,7 @@ class FetchTests(APISubdomainTestCase): self.assertEquals(api_type[1], model_type[1]) -class CreationTests(APISubdomainTestCase): +class CreationTests(AuthenticatedAPITestCase): @classmethod def setUpTestData(cls): FilterList.objects.all().delete() @@ -103,7 +103,7 @@ class CreationTests(APISubdomainTestCase): self.assertEqual(response.status_code, 400) -class DeletionTests(APISubdomainTestCase): +class DeletionTests(AuthenticatedAPITestCase): @classmethod def setUpTestData(cls): FilterList.objects.all().delete() diff --git a/pydis_site/apps/api/tests/test_healthcheck.py b/pydis_site/apps/api/tests/test_healthcheck.py index b0fd71bf..650403ad 100644 --- a/pydis_site/apps/api/tests/test_healthcheck.py +++ b/pydis_site/apps/api/tests/test_healthcheck.py @@ -1,15 +1,15 @@ -from django_hosts.resolvers import reverse +from django.urls import reverse -from .base import APISubdomainTestCase +from .base import AuthenticatedAPITestCase -class UnauthedHealthcheckAPITests(APISubdomainTestCase): +class UnauthedHealthcheckAPITests(AuthenticatedAPITestCase): def setUp(self): super().setUp() self.client.force_authenticate(user=None) def test_can_access_healthcheck_view(self): - url = reverse('healthcheck', host='api') + url = reverse('api:healthcheck') response = self.client.get(url) self.assertEqual(response.status_code, 200) diff --git a/pydis_site/apps/api/tests/test_infractions.py b/pydis_site/apps/api/tests/test_infractions.py index 9aae16c0..b3dd16ee 100644 --- a/pydis_site/apps/api/tests/test_infractions.py +++ b/pydis_site/apps/api/tests/test_infractions.py @@ -4,44 +4,44 @@ from unittest.mock import patch from urllib.parse import quote from django.db.utils import IntegrityError -from django_hosts.resolvers import reverse +from django.urls import reverse -from .base import APISubdomainTestCase +from .base import AuthenticatedAPITestCase from ..models import Infraction, User from ..serializers import InfractionSerializer -class UnauthenticatedTests(APISubdomainTestCase): +class UnauthenticatedTests(AuthenticatedAPITestCase): def setUp(self): super().setUp() self.client.force_authenticate(user=None) def test_detail_lookup_returns_401(self): - url = reverse('bot:infraction-detail', args=(6,), host='api') + url = reverse('api:bot:infraction-detail', args=(6,)) response = self.client.get(url) self.assertEqual(response.status_code, 401) def test_list_returns_401(self): - url = reverse('bot:infraction-list', host='api') + url = reverse('api:bot:infraction-list') response = self.client.get(url) self.assertEqual(response.status_code, 401) def test_create_returns_401(self): - url = reverse('bot:infraction-list', host='api') + url = reverse('api:bot:infraction-list') response = self.client.post(url, data={'reason': 'Have a nice day.'}) self.assertEqual(response.status_code, 401) def test_partial_update_returns_401(self): - url = reverse('bot:infraction-detail', args=(6,), host='api') + url = reverse('api:bot:infraction-detail', args=(6,)) response = self.client.patch(url, data={'reason': 'Have a nice day.'}) self.assertEqual(response.status_code, 401) -class InfractionTests(APISubdomainTestCase): +class InfractionTests(AuthenticatedAPITestCase): @classmethod def setUpTestData(cls): cls.user = User.objects.create( @@ -92,7 +92,7 @@ class InfractionTests(APISubdomainTestCase): def test_list_all(self): """Tests the list-view, which should be ordered by inserted_at (newest first).""" - url = reverse('bot:infraction-list', host='api') + url = reverse('api:bot:infraction-list') response = self.client.get(url) self.assertEqual(response.status_code, 200) @@ -106,7 +106,7 @@ class InfractionTests(APISubdomainTestCase): self.assertEqual(infractions[4]['id'], self.ban_hidden.id) def test_filter_search(self): - url = reverse('bot:infraction-list', host='api') + url = reverse('api:bot:infraction-list') pattern = quote(r'^James(\s\w+){3},') response = self.client.get(f'{url}?search={pattern}') @@ -117,7 +117,7 @@ class InfractionTests(APISubdomainTestCase): self.assertEqual(infractions[0]['id'], self.ban_inactive.id) def test_filter_field(self): - url = reverse('bot:infraction-list', host='api') + url = reverse('api:bot:infraction-list') response = self.client.get(f'{url}?type=ban&hidden=true') self.assertEqual(response.status_code, 200) @@ -127,7 +127,7 @@ class InfractionTests(APISubdomainTestCase): self.assertEqual(infractions[0]['id'], self.ban_hidden.id) def test_filter_permanent_false(self): - url = reverse('bot:infraction-list', host='api') + url = reverse('api:bot:infraction-list') response = self.client.get(f'{url}?type=mute&permanent=false') self.assertEqual(response.status_code, 200) @@ -136,7 +136,7 @@ class InfractionTests(APISubdomainTestCase): self.assertEqual(len(infractions), 0) def test_filter_permanent_true(self): - url = reverse('bot:infraction-list', host='api') + url = reverse('api:bot:infraction-list') response = self.client.get(f'{url}?type=mute&permanent=true') self.assertEqual(response.status_code, 200) @@ -145,7 +145,7 @@ class InfractionTests(APISubdomainTestCase): self.assertEqual(infractions[0]['id'], self.mute_permanent.id) def test_filter_after(self): - url = reverse('bot:infraction-list', host='api') + url = reverse('api:bot:infraction-list') target_time = datetime.datetime.utcnow() + datetime.timedelta(hours=5) response = self.client.get(f'{url}?type=superstar&expires_after={target_time.isoformat()}') @@ -154,7 +154,7 @@ class InfractionTests(APISubdomainTestCase): self.assertEqual(len(infractions), 0) def test_filter_before(self): - url = reverse('bot:infraction-list', host='api') + url = reverse('api:bot:infraction-list') target_time = datetime.datetime.utcnow() + datetime.timedelta(hours=5) response = self.client.get(f'{url}?type=superstar&expires_before={target_time.isoformat()}') @@ -164,21 +164,21 @@ class InfractionTests(APISubdomainTestCase): self.assertEqual(infractions[0]['id'], self.superstar_expires_soon.id) def test_filter_after_invalid(self): - url = reverse('bot:infraction-list', host='api') + url = reverse('api:bot:infraction-list') response = self.client.get(f'{url}?expires_after=gibberish') self.assertEqual(response.status_code, 400) self.assertEqual(list(response.json())[0], "expires_after") def test_filter_before_invalid(self): - url = reverse('bot:infraction-list', host='api') + url = reverse('api:bot:infraction-list') response = self.client.get(f'{url}?expires_before=000000000') self.assertEqual(response.status_code, 400) self.assertEqual(list(response.json())[0], "expires_before") def test_after_before_before(self): - url = reverse('bot:infraction-list', host='api') + url = reverse('api:bot:infraction-list') target_time = datetime.datetime.utcnow() + datetime.timedelta(hours=4) target_time_late = datetime.datetime.utcnow() + datetime.timedelta(hours=6) response = self.client.get( @@ -191,7 +191,7 @@ class InfractionTests(APISubdomainTestCase): self.assertEqual(response.json()[0]["id"], self.superstar_expires_soon.id) def test_after_after_before_invalid(self): - url = reverse('bot:infraction-list', host='api') + url = reverse('api:bot:infraction-list') target_time = datetime.datetime.utcnow() + datetime.timedelta(hours=5) target_time_late = datetime.datetime.utcnow() + datetime.timedelta(hours=9) response = self.client.get( @@ -205,7 +205,7 @@ class InfractionTests(APISubdomainTestCase): self.assertIn("expires_after", errors) def test_permanent_after_invalid(self): - url = reverse('bot:infraction-list', host='api') + url = reverse('api:bot:infraction-list') target_time = datetime.datetime.utcnow() + datetime.timedelta(hours=5) response = self.client.get(f'{url}?permanent=true&expires_after={target_time.isoformat()}') @@ -214,7 +214,7 @@ class InfractionTests(APISubdomainTestCase): self.assertEqual("permanent", errors[0]) def test_permanent_before_invalid(self): - url = reverse('bot:infraction-list', host='api') + url = reverse('api:bot:infraction-list') target_time = datetime.datetime.utcnow() + datetime.timedelta(hours=5) response = self.client.get(f'{url}?permanent=true&expires_before={target_time.isoformat()}') @@ -223,7 +223,7 @@ class InfractionTests(APISubdomainTestCase): self.assertEqual("permanent", errors[0]) def test_nonpermanent_before(self): - url = reverse('bot:infraction-list', host='api') + url = reverse('api:bot:infraction-list') target_time = datetime.datetime.utcnow() + datetime.timedelta(hours=6) response = self.client.get( f'{url}?permanent=false&expires_before={target_time.isoformat()}' @@ -234,7 +234,7 @@ class InfractionTests(APISubdomainTestCase): self.assertEqual(response.json()[0]["id"], self.superstar_expires_soon.id) def test_filter_manytypes(self): - url = reverse('bot:infraction-list', host='api') + url = reverse('api:bot:infraction-list') response = self.client.get(f'{url}?types=mute,ban') self.assertEqual(response.status_code, 200) @@ -242,7 +242,7 @@ class InfractionTests(APISubdomainTestCase): self.assertEqual(len(infractions), 3) def test_types_type_invalid(self): - url = reverse('bot:infraction-list', host='api') + url = reverse('api:bot:infraction-list') response = self.client.get(f'{url}?types=mute,ban&type=superstar') self.assertEqual(response.status_code, 400) @@ -250,7 +250,7 @@ class InfractionTests(APISubdomainTestCase): self.assertEqual("types", errors[0]) def test_sort_expiresby(self): - url = reverse('bot:infraction-list', host='api') + url = reverse('api:bot:infraction-list') response = self.client.get(f'{url}?ordering=expires_at&permanent=false') self.assertEqual(response.status_code, 200) infractions = response.json() @@ -261,34 +261,34 @@ class InfractionTests(APISubdomainTestCase): self.assertEqual(infractions[2]['id'], self.ban_hidden.id) def test_returns_empty_for_no_match(self): - url = reverse('bot:infraction-list', host='api') + url = reverse('api:bot:infraction-list') response = self.client.get(f'{url}?type=ban&search=poop') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 0) def test_ignores_bad_filters(self): - url = reverse('bot:infraction-list', host='api') + url = reverse('api:bot:infraction-list') response = self.client.get(f'{url}?type=ban&hidden=maybe&foo=bar') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 2) def test_retrieve_single_from_id(self): - url = reverse('bot:infraction-detail', args=(self.ban_inactive.id,), host='api') + url = reverse('api:bot:infraction-detail', args=(self.ban_inactive.id,)) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(response.json()['id'], self.ban_inactive.id) def test_retrieve_returns_404_for_absent_id(self): - url = reverse('bot:infraction-detail', args=(1337,), host='api') + url = reverse('api:bot:infraction-detail', args=(1337,)) response = self.client.get(url) self.assertEqual(response.status_code, 404) def test_partial_update(self): - url = reverse('bot:infraction-detail', args=(self.ban_hidden.id,), host='api') + url = reverse('api:bot:infraction-detail', args=(self.ban_hidden.id,)) data = { 'expires_at': '4143-02-15T21:04:31+00:00', 'active': False, @@ -313,7 +313,7 @@ class InfractionTests(APISubdomainTestCase): self.assertEqual(infraction.hidden, self.ban_hidden.hidden) def test_partial_update_returns_400_for_frozen_field(self): - url = reverse('bot:infraction-detail', args=(self.ban_hidden.id,), host='api') + url = reverse('api:bot:infraction-detail', args=(self.ban_hidden.id,)) data = {'user': 6} response = self.client.patch(url, data=data) @@ -323,7 +323,7 @@ class InfractionTests(APISubdomainTestCase): }) -class CreationTests(APISubdomainTestCase): +class CreationTests(AuthenticatedAPITestCase): @classmethod def setUpTestData(cls): cls.user = User.objects.create( @@ -338,7 +338,7 @@ class CreationTests(APISubdomainTestCase): ) def test_accepts_valid_data(self): - url = reverse('bot:infraction-list', host='api') + url = reverse('api:bot:infraction-list') data = { 'user': self.user.id, 'actor': self.user.id, @@ -367,7 +367,7 @@ class CreationTests(APISubdomainTestCase): self.assertEqual(infraction.active, True) def test_returns_400_for_missing_user(self): - url = reverse('bot:infraction-list', host='api') + url = reverse('api:bot:infraction-list') data = { 'actor': self.user.id, 'type': 'kick', @@ -381,7 +381,7 @@ class CreationTests(APISubdomainTestCase): }) def test_returns_400_for_bad_user(self): - url = reverse('bot:infraction-list', host='api') + url = reverse('api:bot:infraction-list') data = { 'user': 1337, 'actor': self.user.id, @@ -396,7 +396,7 @@ class CreationTests(APISubdomainTestCase): }) def test_returns_400_for_bad_type(self): - url = reverse('bot:infraction-list', host='api') + url = reverse('api:bot:infraction-list') data = { 'user': self.user.id, 'actor': self.user.id, @@ -411,7 +411,7 @@ class CreationTests(APISubdomainTestCase): }) def test_returns_400_for_bad_expired_at_format(self): - url = reverse('bot:infraction-list', host='api') + url = reverse('api:bot:infraction-list') data = { 'user': self.user.id, 'actor': self.user.id, @@ -430,7 +430,7 @@ class CreationTests(APISubdomainTestCase): }) def test_returns_400_for_expiring_non_expirable_type(self): - url = reverse('bot:infraction-list', host='api') + url = reverse('api:bot:infraction-list') for infraction_type in ('kick', 'warning'): data = { @@ -448,7 +448,7 @@ class CreationTests(APISubdomainTestCase): }) def test_returns_400_for_hidden_non_hideable_type(self): - url = reverse('bot:infraction-list', host='api') + url = reverse('api:bot:infraction-list') for infraction_type in ('superstar', 'warning'): data = { @@ -466,7 +466,7 @@ class CreationTests(APISubdomainTestCase): }) def test_returns_400_for_non_hidden_required_hidden_type(self): - url = reverse('bot:infraction-list', host='api') + url = reverse('api:bot:infraction-list') data = { 'user': self.user.id, @@ -484,7 +484,7 @@ class CreationTests(APISubdomainTestCase): def test_returns_400_for_active_infraction_of_type_that_cannot_be_active(self): """Test if the API rejects active infractions for types that cannot be active.""" - url = reverse('bot:infraction-list', host='api') + url = reverse('api:bot:infraction-list') restricted_types = ( ('note', True), ('warning', False), @@ -511,7 +511,7 @@ class CreationTests(APISubdomainTestCase): def test_returns_400_for_second_active_infraction_of_the_same_type(self): """Test if the API rejects a second active infraction of the same type for a given user.""" - url = reverse('bot:infraction-list', host='api') + url = reverse('api:bot:infraction-list') active_infraction_types = ('mute', 'ban', 'superstar') for infraction_type in active_infraction_types: @@ -550,7 +550,7 @@ class CreationTests(APISubdomainTestCase): def test_returns_201_for_second_active_infraction_of_different_type(self): """Test if the API accepts a second active infraction of a different type than the first.""" - url = reverse('bot:infraction-list', host='api') + url = reverse('api:bot:infraction-list') first_active_infraction = { 'user': self.user.id, 'actor': self.user.id, @@ -677,7 +677,7 @@ class CreationTests(APISubdomainTestCase): ) -class InfractionDeletionTests(APISubdomainTestCase): +class InfractionDeletionTests(AuthenticatedAPITestCase): @classmethod def setUpTestData(cls): cls.user = User.objects.create( @@ -694,20 +694,20 @@ class InfractionDeletionTests(APISubdomainTestCase): ) def test_delete_unknown_infraction_returns_404(self): - url = reverse('bot:infraction-detail', args=('something',), host='api') + url = reverse('api:bot:infraction-detail', args=('something',)) response = self.client.delete(url) self.assertEqual(response.status_code, 404) def test_delete_known_infraction_returns_204(self): - url = reverse('bot:infraction-detail', args=(self.warning.id,), host='api') + url = reverse('api:bot:infraction-detail', args=(self.warning.id,)) response = self.client.delete(url) self.assertEqual(response.status_code, 204) self.assertRaises(Infraction.DoesNotExist, Infraction.objects.get, id=self.warning.id) -class ExpandedTests(APISubdomainTestCase): +class ExpandedTests(AuthenticatedAPITestCase): @classmethod def setUpTestData(cls): cls.user = User.objects.create( @@ -735,7 +735,7 @@ class ExpandedTests(APISubdomainTestCase): self.assertTrue(field in obj, msg=f'field "{field}" missing from {key}') def test_list_expanded(self): - url = reverse('bot:infraction-list-expanded', host='api') + url = reverse('api:bot:infraction-list-expanded') response = self.client.get(url) self.assertEqual(response.status_code, 200) @@ -747,7 +747,7 @@ class ExpandedTests(APISubdomainTestCase): self.check_expanded_fields(infraction) def test_create_expanded(self): - url = reverse('bot:infraction-list-expanded', host='api') + url = reverse('api:bot:infraction-list-expanded') data = { 'user': self.user.id, 'actor': self.user.id, @@ -762,7 +762,7 @@ class ExpandedTests(APISubdomainTestCase): self.check_expanded_fields(response.json()) def test_retrieve_expanded(self): - url = reverse('bot:infraction-detail-expanded', args=(self.warning.id,), host='api') + url = reverse('api:bot:infraction-detail-expanded', args=(self.warning.id,)) response = self.client.get(url) self.assertEqual(response.status_code, 200) @@ -772,7 +772,7 @@ class ExpandedTests(APISubdomainTestCase): self.check_expanded_fields(infraction) def test_partial_update_expanded(self): - url = reverse('bot:infraction-detail-expanded', args=(self.kick.id,), host='api') + url = reverse('api:bot:infraction-detail-expanded', args=(self.kick.id,)) data = {'active': False} response = self.client.patch(url, data=data) @@ -783,7 +783,7 @@ class ExpandedTests(APISubdomainTestCase): self.check_expanded_fields(response.json()) -class SerializerTests(APISubdomainTestCase): +class SerializerTests(AuthenticatedAPITestCase): @classmethod def setUpTestData(cls): cls.user = User.objects.create( diff --git a/pydis_site/apps/api/tests/test_nominations.py b/pydis_site/apps/api/tests/test_nominations.py index 9cefbd8f..62b2314c 100644 --- a/pydis_site/apps/api/tests/test_nominations.py +++ b/pydis_site/apps/api/tests/test_nominations.py @@ -1,12 +1,12 @@ from datetime import datetime as dt, timedelta, timezone -from django_hosts.resolvers import reverse +from django.urls import reverse -from .base import APISubdomainTestCase +from .base import AuthenticatedAPITestCase from ..models import Nomination, NominationEntry, User -class CreationTests(APISubdomainTestCase): +class CreationTests(AuthenticatedAPITestCase): @classmethod def setUpTestData(cls): cls.user = User.objects.create( @@ -21,7 +21,7 @@ class CreationTests(APISubdomainTestCase): ) def test_accepts_valid_data(self): - url = reverse('bot:nomination-list', host='api') + url = reverse('api:bot:nomination-list') data = { 'actor': self.user.id, 'reason': 'Joe Dart on Fender Bass', @@ -46,7 +46,7 @@ class CreationTests(APISubdomainTestCase): self.assertEqual(nomination.active, True) def test_returns_200_on_second_active_nomination_by_different_user(self): - url = reverse('bot:nomination-list', host='api') + url = reverse('api:bot:nomination-list') first_data = { 'actor': self.user.id, 'reason': 'Joe Dart on Fender Bass', @@ -65,7 +65,7 @@ class CreationTests(APISubdomainTestCase): self.assertEqual(response2.status_code, 201) def test_returns_400_on_second_active_nomination_by_existing_nominator(self): - url = reverse('bot:nomination-list', host='api') + url = reverse('api:bot:nomination-list') data = { 'actor': self.user.id, 'reason': 'Joe Dart on Fender Bass', @@ -82,7 +82,7 @@ class CreationTests(APISubdomainTestCase): }) def test_returns_400_for_missing_user(self): - url = reverse('bot:nomination-list', host='api') + url = reverse('api:bot:nomination-list') data = { 'actor': self.user.id, 'reason': 'Joe Dart on Fender Bass', @@ -95,7 +95,7 @@ class CreationTests(APISubdomainTestCase): }) def test_returns_400_for_missing_actor(self): - url = reverse('bot:nomination-list', host='api') + url = reverse('api:bot:nomination-list') data = { 'user': self.user.id, 'reason': 'Joe Dart on Fender Bass', @@ -108,7 +108,7 @@ class CreationTests(APISubdomainTestCase): }) def test_returns_201_for_missing_reason(self): - url = reverse('bot:nomination-list', host='api') + url = reverse('api:bot:nomination-list') data = { 'user': self.user.id, 'actor': self.user.id, @@ -118,7 +118,7 @@ class CreationTests(APISubdomainTestCase): self.assertEqual(response.status_code, 201) def test_returns_400_for_bad_user(self): - url = reverse('bot:nomination-list', host='api') + url = reverse('api:bot:nomination-list') data = { 'user': 1024, 'reason': 'Joe Dart on Fender Bass', @@ -132,7 +132,7 @@ class CreationTests(APISubdomainTestCase): }) def test_returns_400_for_bad_actor(self): - url = reverse('bot:nomination-list', host='api') + url = reverse('api:bot:nomination-list') data = { 'user': self.user.id, 'reason': 'Joe Dart on Fender Bass', @@ -146,7 +146,7 @@ class CreationTests(APISubdomainTestCase): }) def test_returns_400_for_end_reason_at_creation(self): - url = reverse('bot:nomination-list', host='api') + url = reverse('api:bot:nomination-list') data = { 'user': self.user.id, 'reason': 'Joe Dart on Fender Bass', @@ -161,7 +161,7 @@ class CreationTests(APISubdomainTestCase): }) def test_returns_400_for_ended_at_at_creation(self): - url = reverse('bot:nomination-list', host='api') + url = reverse('api:bot:nomination-list') data = { 'user': self.user.id, 'reason': 'Joe Dart on Fender Bass', @@ -176,7 +176,7 @@ class CreationTests(APISubdomainTestCase): }) def test_returns_400_for_inserted_at_at_creation(self): - url = reverse('bot:nomination-list', host='api') + url = reverse('api:bot:nomination-list') data = { 'user': self.user.id, 'reason': 'Joe Dart on Fender Bass', @@ -191,7 +191,7 @@ class CreationTests(APISubdomainTestCase): }) def test_returns_400_for_active_at_creation(self): - url = reverse('bot:nomination-list', host='api') + url = reverse('api:bot:nomination-list') data = { 'user': self.user.id, 'reason': 'Joe Dart on Fender Bass', @@ -206,7 +206,7 @@ class CreationTests(APISubdomainTestCase): }) -class NominationTests(APISubdomainTestCase): +class NominationTests(AuthenticatedAPITestCase): @classmethod def setUpTestData(cls): cls.user = User.objects.create( @@ -236,7 +236,7 @@ class NominationTests(APISubdomainTestCase): ) def test_returns_200_update_reason_on_active_with_actor(self): - url = reverse('bot:nomination-detail', args=(self.active_nomination.id,), host='api') + url = reverse('api:bot:nomination-detail', args=(self.active_nomination.id,)) data = { 'reason': "He's one funky duck", 'actor': self.user.id @@ -252,7 +252,7 @@ class NominationTests(APISubdomainTestCase): self.assertEqual(nomination_entry.reason, data['reason']) def test_returns_400_on_frozen_field_update(self): - url = reverse('bot:nomination-detail', args=(self.active_nomination.id,), host='api') + url = reverse('api:bot:nomination-detail', args=(self.active_nomination.id,)) data = { 'user': "Theo Katzman" } @@ -264,7 +264,7 @@ class NominationTests(APISubdomainTestCase): }) def test_returns_400_update_end_reason_on_active(self): - url = reverse('bot:nomination-detail', args=(self.active_nomination.id,), host='api') + url = reverse('api:bot:nomination-detail', args=(self.active_nomination.id,)) data = { 'end_reason': 'He started playing jazz' } @@ -276,7 +276,7 @@ class NominationTests(APISubdomainTestCase): }) def test_returns_200_update_reason_on_inactive(self): - url = reverse('bot:nomination-detail', args=(self.inactive_nomination.id,), host='api') + url = reverse('api:bot:nomination-detail', args=(self.inactive_nomination.id,)) data = { 'reason': "He's one funky duck", 'actor': self.user.id @@ -292,7 +292,7 @@ class NominationTests(APISubdomainTestCase): self.assertEqual(nomination_entry.reason, data['reason']) def test_returns_200_update_end_reason_on_inactive(self): - url = reverse('bot:nomination-detail', args=(self.inactive_nomination.id,), host='api') + url = reverse('api:bot:nomination-detail', args=(self.inactive_nomination.id,)) data = { 'end_reason': 'He started playing jazz' } @@ -305,9 +305,8 @@ class NominationTests(APISubdomainTestCase): def test_returns_200_on_valid_end_nomination(self): url = reverse( - 'bot:nomination-detail', + 'api:bot:nomination-detail', args=(self.active_nomination.id,), - host='api' ) data = { 'active': False, @@ -328,9 +327,8 @@ class NominationTests(APISubdomainTestCase): def test_returns_400_on_invalid_field_end_nomination(self): url = reverse( - 'bot:nomination-detail', + 'api:bot:nomination-detail', args=(self.active_nomination.id,), - host='api' ) data = { 'active': False, @@ -344,9 +342,8 @@ class NominationTests(APISubdomainTestCase): def test_returns_400_on_missing_end_reason_end_nomination(self): url = reverse( - 'bot:nomination-detail', + 'api:bot:nomination-detail', args=(self.active_nomination.id,), - host='api' ) data = { 'active': False, @@ -360,9 +357,8 @@ class NominationTests(APISubdomainTestCase): def test_returns_400_on_invalid_use_of_active(self): url = reverse( - 'bot:nomination-detail', + 'api:bot:nomination-detail', args=(self.inactive_nomination.id,), - host='api' ) data = { 'active': False, @@ -376,9 +372,8 @@ class NominationTests(APISubdomainTestCase): def test_returns_404_on_get_unknown_nomination(self): url = reverse( - 'bot:nomination-detail', + 'api:bot:nomination-detail', args=(9999,), - host='api' ) response = self.client.get(url, data={}) @@ -389,9 +384,8 @@ class NominationTests(APISubdomainTestCase): def test_returns_404_on_patch_unknown_nomination(self): url = reverse( - 'bot:nomination-detail', + 'api:bot:nomination-detail', args=(9999,), - host='api' ) response = self.client.patch(url, data={}) @@ -401,7 +395,7 @@ class NominationTests(APISubdomainTestCase): }) def test_returns_405_on_list_put(self): - url = reverse('bot:nomination-list', host='api') + url = reverse('api:bot:nomination-list') response = self.client.put(url, data={}) self.assertEqual(response.status_code, 405) @@ -410,7 +404,7 @@ class NominationTests(APISubdomainTestCase): }) def test_returns_405_on_list_patch(self): - url = reverse('bot:nomination-list', host='api') + url = reverse('api:bot:nomination-list') response = self.client.patch(url, data={}) self.assertEqual(response.status_code, 405) @@ -419,7 +413,7 @@ class NominationTests(APISubdomainTestCase): }) def test_returns_405_on_list_delete(self): - url = reverse('bot:nomination-list', host='api') + url = reverse('api:bot:nomination-list') response = self.client.delete(url, data={}) self.assertEqual(response.status_code, 405) @@ -428,7 +422,7 @@ class NominationTests(APISubdomainTestCase): }) def test_returns_405_on_detail_post(self): - url = reverse('bot:nomination-detail', args=(self.active_nomination.id,), host='api') + url = reverse('api:bot:nomination-detail', args=(self.active_nomination.id,)) response = self.client.post(url, data={}) self.assertEqual(response.status_code, 405) @@ -437,7 +431,7 @@ class NominationTests(APISubdomainTestCase): }) def test_returns_405_on_detail_delete(self): - url = reverse('bot:nomination-detail', args=(self.active_nomination.id,), host='api') + url = reverse('api:bot:nomination-detail', args=(self.active_nomination.id,)) response = self.client.delete(url, data={}) self.assertEqual(response.status_code, 405) @@ -446,7 +440,7 @@ class NominationTests(APISubdomainTestCase): }) def test_returns_405_on_detail_put(self): - url = reverse('bot:nomination-detail', args=(self.active_nomination.id,), host='api') + url = reverse('api:bot:nomination-detail', args=(self.active_nomination.id,)) response = self.client.put(url, data={}) self.assertEqual(response.status_code, 405) @@ -455,7 +449,7 @@ class NominationTests(APISubdomainTestCase): }) def test_filter_returns_0_objects_unknown_user__id(self): - url = reverse('bot:nomination-list', host='api') + url = reverse('api:bot:nomination-list') response = self.client.get( url, @@ -470,7 +464,7 @@ class NominationTests(APISubdomainTestCase): self.assertEqual(len(infractions), 0) def test_filter_returns_2_objects_for_testdata(self): - url = reverse('bot:nomination-list', host='api') + url = reverse('api:bot:nomination-list') response = self.client.get( url, @@ -485,14 +479,14 @@ class NominationTests(APISubdomainTestCase): self.assertEqual(len(infractions), 2) def test_patch_nomination_set_reviewed_of_active_nomination(self): - url = reverse('api:nomination-detail', args=(self.active_nomination.id,), host='api') + url = reverse('api:bot:nomination-detail', args=(self.active_nomination.id,)) data = {'reviewed': True} response = self.client.patch(url, data=data) self.assertEqual(response.status_code, 200) def test_patch_nomination_set_reviewed_of_inactive_nomination(self): - url = reverse('api:nomination-detail', args=(self.inactive_nomination.id,), host='api') + url = reverse('api:bot:nomination-detail', args=(self.inactive_nomination.id,)) data = {'reviewed': True} response = self.client.patch(url, data=data) @@ -502,7 +496,7 @@ class NominationTests(APISubdomainTestCase): }) def test_patch_nomination_set_reviewed_and_end(self): - url = reverse('api:nomination-detail', args=(self.active_nomination.id,), host='api') + url = reverse('api:bot:nomination-detail', args=(self.active_nomination.id,)) data = {'reviewed': True, 'active': False, 'end_reason': "What?"} response = self.client.patch(url, data=data) @@ -512,7 +506,7 @@ class NominationTests(APISubdomainTestCase): }) def test_modifying_reason_without_actor(self): - url = reverse('api:nomination-detail', args=(self.active_nomination.id,), host='api') + url = reverse('api:bot:nomination-detail', args=(self.active_nomination.id,)) data = {'reason': 'That is my reason!'} response = self.client.patch(url, data=data) @@ -522,7 +516,7 @@ class NominationTests(APISubdomainTestCase): }) def test_modifying_reason_with_unknown_actor(self): - url = reverse('api:nomination-detail', args=(self.active_nomination.id,), host='api') + url = reverse('api:bot:nomination-detail', args=(self.active_nomination.id,)) data = {'reason': 'That is my reason!', 'actor': 90909090909090} response = self.client.patch(url, data=data) diff --git a/pydis_site/apps/api/tests/test_off_topic_channel_names.py b/pydis_site/apps/api/tests/test_off_topic_channel_names.py index 3ab8b22d..2d273756 100644 --- a/pydis_site/apps/api/tests/test_off_topic_channel_names.py +++ b/pydis_site/apps/api/tests/test_off_topic_channel_names.py @@ -1,33 +1,33 @@ -from django_hosts.resolvers import reverse +from django.urls import reverse -from .base import APISubdomainTestCase +from .base import AuthenticatedAPITestCase from ..models import OffTopicChannelName -class UnauthenticatedTests(APISubdomainTestCase): +class UnauthenticatedTests(AuthenticatedAPITestCase): def setUp(self): super().setUp() self.client.force_authenticate(user=None) def test_cannot_read_off_topic_channel_name_list(self): """Return a 401 response when not authenticated.""" - url = reverse('bot:offtopicchannelname-list', host='api') + url = reverse('api:bot:offtopicchannelname-list') response = self.client.get(url) self.assertEqual(response.status_code, 401) def test_cannot_read_off_topic_channel_name_list_with_random_item_param(self): """Return a 401 response when `random_items` provided and not authenticated.""" - url = reverse('bot:offtopicchannelname-list', host='api') + url = reverse('api:bot:offtopicchannelname-list') response = self.client.get(f'{url}?random_items=no') self.assertEqual(response.status_code, 401) -class EmptyDatabaseTests(APISubdomainTestCase): +class EmptyDatabaseTests(AuthenticatedAPITestCase): def test_returns_empty_object(self): """Return empty list when no names in database.""" - url = reverse('bot:offtopicchannelname-list', host='api') + url = reverse('api:bot:offtopicchannelname-list') response = self.client.get(url) self.assertEqual(response.status_code, 200) @@ -35,7 +35,7 @@ class EmptyDatabaseTests(APISubdomainTestCase): def test_returns_empty_list_with_get_all_param(self): """Return empty list when no names and `random_items` param provided.""" - url = reverse('bot:offtopicchannelname-list', host='api') + url = reverse('api:bot:offtopicchannelname-list') response = self.client.get(f'{url}?random_items=5') self.assertEqual(response.status_code, 200) @@ -43,7 +43,7 @@ class EmptyDatabaseTests(APISubdomainTestCase): def test_returns_400_for_bad_random_items_param(self): """Return error message when passing not integer as `random_items`.""" - url = reverse('bot:offtopicchannelname-list', host='api') + url = reverse('api:bot:offtopicchannelname-list') response = self.client.get(f'{url}?random_items=totally-a-valid-integer') self.assertEqual(response.status_code, 400) @@ -53,7 +53,7 @@ class EmptyDatabaseTests(APISubdomainTestCase): def test_returns_400_for_negative_random_items_param(self): """Return error message when passing negative int as `random_items`.""" - url = reverse('bot:offtopicchannelname-list', host='api') + url = reverse('api:bot:offtopicchannelname-list') response = self.client.get(f'{url}?random_items=-5') self.assertEqual(response.status_code, 400) @@ -62,56 +62,89 @@ class EmptyDatabaseTests(APISubdomainTestCase): }) -class ListTests(APISubdomainTestCase): +class ListTests(AuthenticatedAPITestCase): @classmethod def setUpTestData(cls): - cls.test_name = OffTopicChannelName.objects.create(name='lemons-lemonade-stand', used=False) - cls.test_name_2 = OffTopicChannelName.objects.create(name='bbq-with-bisk', used=True) + cls.test_name = OffTopicChannelName.objects.create( + name='lemons-lemonade-stand', used=False, active=True + ) + cls.test_name_2 = OffTopicChannelName.objects.create( + name='bbq-with-bisk', used=False, active=True + ) + cls.test_name_3 = OffTopicChannelName.objects.create( + name="frozen-with-iceman", used=True, active=False + ) def test_returns_name_in_list(self): """Return all off-topic channel names.""" - url = reverse('bot:offtopicchannelname-list', host='api') + url = reverse('api:bot:offtopicchannelname-list') response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual( - response.json(), - [ + set(response.json()), + { self.test_name.name, - self.test_name_2.name - ] + self.test_name_2.name, + self.test_name_3.name + } ) - def test_returns_single_item_with_random_items_param_set_to_1(self): + def test_returns_two_items_with_random_items_param_set_to_2(self): """Return not-used name instead used.""" - url = reverse('bot:offtopicchannelname-list', host='api') - response = self.client.get(f'{url}?random_items=1') + url = reverse('api:bot:offtopicchannelname-list') + response = self.client.get(f'{url}?random_items=2') self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.json()), 1) - self.assertEqual(response.json(), [self.test_name.name]) + self.assertEqual(len(response.json()), 2) + self.assertEqual(set(response.json()), {self.test_name.name, self.test_name_2.name}) def test_running_out_of_names_with_random_parameter(self): """Reset names `used` parameter to `False` when running out of names.""" - url = reverse('bot:offtopicchannelname-list', host='api') - response = self.client.get(f'{url}?random_items=2') + url = reverse('api:bot:offtopicchannelname-list') + response = self.client.get(f'{url}?random_items=3') self.assertEqual(response.status_code, 200) - self.assertEqual(response.json(), [self.test_name.name, self.test_name_2.name]) + self.assertEqual( + set(response.json()), + {self.test_name.name, self.test_name_2.name, self.test_name_3.name} + ) + + def test_returns_inactive_ot_names(self): + """Return inactive off topic names.""" + url = reverse('api:bot:offtopicchannelname-list') + response = self.client.get(f"{url}?active=false") + + self.assertEqual(response.status_code, 200) + self.assertEqual( + response.json(), + [self.test_name_3.name] + ) + + def test_returns_active_ot_names(self): + """Return active off topic names.""" + url = reverse('api:bot:offtopicchannelname-list') + response = self.client.get(f"{url}?active=true") + + self.assertEqual(response.status_code, 200) + self.assertEqual( + set(response.json()), + {self.test_name.name, self.test_name_2.name} + ) -class CreationTests(APISubdomainTestCase): +class CreationTests(AuthenticatedAPITestCase): def setUp(self): super().setUp() - url = reverse('bot:offtopicchannelname-list', host='api') + url = reverse('api:bot:offtopicchannelname-list') self.name = "abcdefghijklmnopqrstuvwxyz-0123456789" response = self.client.post(f'{url}?name={self.name}') self.assertEqual(response.status_code, 201) def test_returns_201_for_unicode_chars(self): """Accept all valid characters.""" - url = reverse('bot:offtopicchannelname-list', host='api') + url = reverse('api:bot:offtopicchannelname-list') names = ( '𝖠𝖡𝖢𝖣𝖤𝖥𝖦𝖧𝖨𝖩𝖪𝖫𝖬𝖭𝖮𝖯𝖰𝖱𝖲𝖳𝖴𝖵𝖶𝖷𝖸𝖹', 'ǃ?’', @@ -123,7 +156,7 @@ class CreationTests(APISubdomainTestCase): def test_returns_400_for_missing_name_param(self): """Return error message when name not provided.""" - url = reverse('bot:offtopicchannelname-list', host='api') + url = reverse('api:bot:offtopicchannelname-list') response = self.client.post(url) self.assertEqual(response.status_code, 400) self.assertEqual(response.json(), { @@ -132,7 +165,7 @@ class CreationTests(APISubdomainTestCase): def test_returns_400_for_bad_name_param(self): """Return error message when invalid characters provided.""" - url = reverse('bot:offtopicchannelname-list', host='api') + url = reverse('api:bot:offtopicchannelname-list') invalid_names = ( 'space between words', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', @@ -147,33 +180,33 @@ class CreationTests(APISubdomainTestCase): }) -class DeletionTests(APISubdomainTestCase): +class DeletionTests(AuthenticatedAPITestCase): @classmethod def setUpTestData(cls): cls.test_name = OffTopicChannelName.objects.create(name='lemons-lemonade-stand') cls.test_name_2 = OffTopicChannelName.objects.create(name='bbq-with-bisk') def test_deleting_unknown_name_returns_404(self): - """Return 404 reponse when trying to delete unknown name.""" - url = reverse('bot:offtopicchannelname-detail', args=('unknown-name',), host='api') + """Return 404 response when trying to delete unknown name.""" + url = reverse('api:bot:offtopicchannelname-detail', args=('unknown-name',)) response = self.client.delete(url) self.assertEqual(response.status_code, 404) def test_deleting_known_name_returns_204(self): """Return 204 response when deleting was successful.""" - url = reverse('bot:offtopicchannelname-detail', args=(self.test_name.name,), host='api') + url = reverse('api:bot:offtopicchannelname-detail', args=(self.test_name.name,)) response = self.client.delete(url) self.assertEqual(response.status_code, 204) def test_name_gets_deleted(self): """Name gets actually deleted.""" - url = reverse('bot:offtopicchannelname-detail', args=(self.test_name_2.name,), host='api') + url = reverse('api:bot:offtopicchannelname-detail', args=(self.test_name_2.name,)) response = self.client.delete(url) self.assertEqual(response.status_code, 204) - url = reverse('bot:offtopicchannelname-list', host='api') + url = reverse('api:bot:offtopicchannelname-list') response = self.client.get(url) self.assertNotIn(self.test_name_2.name, response.json()) diff --git a/pydis_site/apps/api/tests/test_offensive_message.py b/pydis_site/apps/api/tests/test_offensive_message.py index 0f3dbffa..3cf95b75 100644 --- a/pydis_site/apps/api/tests/test_offensive_message.py +++ b/pydis_site/apps/api/tests/test_offensive_message.py @@ -1,14 +1,14 @@ import datetime -from django_hosts.resolvers import reverse +from django.urls import reverse -from .base import APISubdomainTestCase +from .base import AuthenticatedAPITestCase from ..models import OffensiveMessage -class CreationTests(APISubdomainTestCase): +class CreationTests(AuthenticatedAPITestCase): def test_accept_valid_data(self): - url = reverse('bot:offensivemessage-list', host='api') + url = reverse('api:bot:offensivemessage-list') delete_at = datetime.datetime.now() + datetime.timedelta(days=1) data = { 'id': '602951077675139072', @@ -31,7 +31,7 @@ class CreationTests(APISubdomainTestCase): self.assertEqual(data['channel_id'], str(offensive_message.channel_id)) def test_returns_400_on_non_future_date(self): - url = reverse('bot:offensivemessage-list', host='api') + url = reverse('api:bot:offensivemessage-list') delete_at = datetime.datetime.now() - datetime.timedelta(days=1) data = { 'id': '602951077675139072', @@ -45,7 +45,7 @@ class CreationTests(APISubdomainTestCase): }) def test_returns_400_on_negative_id_or_channel_id(self): - url = reverse('bot:offensivemessage-list', host='api') + url = reverse('api:bot:offensivemessage-list') delete_at = datetime.datetime.now() + datetime.timedelta(days=1) data = { 'id': '602951077675139072', @@ -58,7 +58,7 @@ class CreationTests(APISubdomainTestCase): ) for field, invalid_value in cases: - with self.subTest(fied=field, invalid_value=invalid_value): + with self.subTest(field=field, invalid_value=invalid_value): test_data = data.copy() test_data.update({field: invalid_value}) @@ -69,7 +69,7 @@ class CreationTests(APISubdomainTestCase): }) -class ListTests(APISubdomainTestCase): +class ListTests(AuthenticatedAPITestCase): @classmethod def setUpTestData(cls): delete_at = datetime.datetime.now() + datetime.timedelta(days=1) @@ -100,7 +100,7 @@ class ListTests(APISubdomainTestCase): cls.messages[1]['delete_date'] = delete_at.isoformat() + 'Z' def test_get_data(self): - url = reverse('bot:offensivemessage-list', host='api') + url = reverse('api:bot:offensivemessage-list') response = self.client.get(url) self.assertEqual(response.status_code, 200) @@ -108,7 +108,7 @@ class ListTests(APISubdomainTestCase): self.assertEqual(response.json(), self.messages) -class DeletionTests(APISubdomainTestCase): +class DeletionTests(AuthenticatedAPITestCase): @classmethod def setUpTestData(cls): delete_at = datetime.datetime.now(tz=datetime.timezone.utc) + datetime.timedelta(days=1) @@ -121,7 +121,7 @@ class DeletionTests(APISubdomainTestCase): def test_delete_data(self): url = reverse( - 'bot:offensivemessage-detail', host='api', args=(self.valid_offensive_message.id,) + 'api:bot:offensivemessage-detail', args=(self.valid_offensive_message.id,) ) response = self.client.delete(url) @@ -132,7 +132,7 @@ class DeletionTests(APISubdomainTestCase): ) -class NotAllowedMethodsTests(APISubdomainTestCase): +class NotAllowedMethodsTests(AuthenticatedAPITestCase): @classmethod def setUpTestData(cls): delete_at = datetime.datetime.now(tz=datetime.timezone.utc) + datetime.timedelta(days=1) @@ -145,7 +145,7 @@ class NotAllowedMethodsTests(APISubdomainTestCase): def test_returns_405_for_patch_and_put_requests(self): url = reverse( - 'bot:offensivemessage-detail', host='api', args=(self.valid_offensive_message.id,) + 'api:bot:offensivemessage-detail', args=(self.valid_offensive_message.id,) ) not_allowed_methods = (self.client.patch, self.client.put) diff --git a/pydis_site/apps/api/tests/test_reminders.py b/pydis_site/apps/api/tests/test_reminders.py index 9dffb668..709685bc 100644 --- a/pydis_site/apps/api/tests/test_reminders.py +++ b/pydis_site/apps/api/tests/test_reminders.py @@ -1,52 +1,52 @@ from datetime import datetime from django.forms.models import model_to_dict -from django_hosts.resolvers import reverse +from django.urls import reverse -from .base import APISubdomainTestCase +from .base import AuthenticatedAPITestCase from ..models import Reminder, User -class UnauthedReminderAPITests(APISubdomainTestCase): +class UnauthedReminderAPITests(AuthenticatedAPITestCase): def setUp(self): super().setUp() self.client.force_authenticate(user=None) def test_list_returns_401(self): - url = reverse('bot:reminder-list', host='api') + url = reverse('api:bot:reminder-list') response = self.client.get(url) self.assertEqual(response.status_code, 401) def test_create_returns_401(self): - url = reverse('bot:reminder-list', host='api') + url = reverse('api:bot:reminder-list') response = self.client.post(url, data={'not': 'important'}) self.assertEqual(response.status_code, 401) def test_delete_returns_401(self): - url = reverse('bot:reminder-detail', args=('1234',), host='api') + url = reverse('api:bot:reminder-detail', args=('1234',)) response = self.client.delete(url) self.assertEqual(response.status_code, 401) -class EmptyDatabaseReminderAPITests(APISubdomainTestCase): +class EmptyDatabaseReminderAPITests(AuthenticatedAPITestCase): def test_list_all_returns_empty_list(self): - url = reverse('bot:reminder-list', host='api') + url = reverse('api:bot:reminder-list') response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), []) def test_delete_returns_404(self): - url = reverse('bot:reminder-detail', args=('1234',), host='api') + url = reverse('api:bot:reminder-detail', args=('1234',)) response = self.client.delete(url) self.assertEqual(response.status_code, 404) -class ReminderCreationTests(APISubdomainTestCase): +class ReminderCreationTests(AuthenticatedAPITestCase): @classmethod def setUpTestData(cls): cls.author = User.objects.create( @@ -64,7 +64,7 @@ class ReminderCreationTests(APISubdomainTestCase): 'channel_id': 123, 'mentions': [8888, 9999], } - url = reverse('bot:reminder-list', host='api') + url = reverse('api:bot:reminder-list') response = self.client.post(url, data=data) self.assertEqual(response.status_code, 201) self.assertIsNotNone(Reminder.objects.filter(id=1).first()) @@ -73,13 +73,13 @@ class ReminderCreationTests(APISubdomainTestCase): data = { 'author': self.author.id, # Missing multiple required fields } - url = reverse('bot:reminder-list', host='api') + url = reverse('api:bot:reminder-list') response = self.client.post(url, data=data) self.assertEqual(response.status_code, 400) self.assertRaises(Reminder.DoesNotExist, Reminder.objects.get, id=1) -class ReminderDeletionTests(APISubdomainTestCase): +class ReminderDeletionTests(AuthenticatedAPITestCase): @classmethod def setUpTestData(cls): cls.author = User.objects.create( @@ -97,20 +97,20 @@ class ReminderDeletionTests(APISubdomainTestCase): ) def test_delete_unknown_reminder_returns_404(self): - url = reverse('bot:reminder-detail', args=('something',), host='api') + url = reverse('api:bot:reminder-detail', args=('something',)) response = self.client.delete(url) self.assertEqual(response.status_code, 404) def test_delete_known_reminder_returns_204(self): - url = reverse('bot:reminder-detail', args=(self.reminder.id,), host='api') + url = reverse('api:bot:reminder-detail', args=(self.reminder.id,)) response = self.client.delete(url) self.assertEqual(response.status_code, 204) self.assertRaises(Reminder.DoesNotExist, Reminder.objects.get, id=self.reminder.id) -class ReminderListTests(APISubdomainTestCase): +class ReminderListTests(AuthenticatedAPITestCase): @classmethod def setUpTestData(cls): cls.author = User.objects.create( @@ -142,28 +142,28 @@ class ReminderListTests(APISubdomainTestCase): cls.rem_dict_two['expiration'] += 'Z' # Massaging a quirk of the response time format def test_reminders_in_full_list(self): - url = reverse('bot:reminder-list', host='api') + url = reverse('api:bot:reminder-list') response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertCountEqual(response.json(), [self.rem_dict_one, self.rem_dict_two]) def test_filter_search(self): - url = reverse('bot:reminder-list', host='api') + url = reverse('api:bot:reminder-list') response = self.client.get(f'{url}?search={self.author.name}') self.assertEqual(response.status_code, 200) self.assertCountEqual(response.json(), [self.rem_dict_one, self.rem_dict_two]) def test_filter_field(self): - url = reverse('bot:reminder-list', host='api') + url = reverse('api:bot:reminder-list') response = self.client.get(f'{url}?active=true') self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), [self.rem_dict_one]) -class ReminderRetrieveTests(APISubdomainTestCase): +class ReminderRetrieveTests(AuthenticatedAPITestCase): @classmethod def setUpTestData(cls): cls.author = User.objects.create( @@ -181,17 +181,17 @@ class ReminderRetrieveTests(APISubdomainTestCase): ) def test_retrieve_unknown_returns_404(self): - url = reverse('bot:reminder-detail', args=("not_an_id",), host='api') + url = reverse('api:bot:reminder-detail', args=("not_an_id",)) response = self.client.get(url) self.assertEqual(response.status_code, 404) def test_retrieve_known_returns_200(self): - url = reverse('bot:reminder-detail', args=(self.reminder.id,), host='api') + url = reverse('api:bot:reminder-detail', args=(self.reminder.id,)) response = self.client.get(url) self.assertEqual(response.status_code, 200) -class ReminderUpdateTests(APISubdomainTestCase): +class ReminderUpdateTests(AuthenticatedAPITestCase): @classmethod def setUpTestData(cls): cls.author = User.objects.create( @@ -211,7 +211,7 @@ class ReminderUpdateTests(APISubdomainTestCase): cls.data = {'content': 'Oops I forgot'} def test_patch_updates_record(self): - url = reverse('bot:reminder-detail', args=(self.reminder.id,), host='api') + url = reverse('api:bot:reminder-detail', args=(self.reminder.id,)) response = self.client.patch(url, data=self.data) self.assertEqual(response.status_code, 200) diff --git a/pydis_site/apps/api/tests/test_roles.py b/pydis_site/apps/api/tests/test_roles.py index 4d1a430c..73c80c77 100644 --- a/pydis_site/apps/api/tests/test_roles.py +++ b/pydis_site/apps/api/tests/test_roles.py @@ -1,10 +1,10 @@ -from django_hosts.resolvers import reverse +from django.urls import reverse -from .base import APISubdomainTestCase -from ..models import Role +from .base import AuthenticatedAPITestCase +from ..models import Role, User -class CreationTests(APISubdomainTestCase): +class CreationTests(AuthenticatedAPITestCase): @classmethod def setUpTestData(cls): cls.admins_role = Role.objects.create( @@ -35,6 +35,20 @@ class CreationTests(APISubdomainTestCase): permissions=6, position=0, ) + cls.role_to_delete = Role.objects.create( + id=7, + name="role to delete", + colour=7, + permissions=7, + position=0, + ) + cls.role_unassigned_test_user = User.objects.create( + id=8, + name="role_unassigned_test_user", + discriminator="0000", + roles=[cls.role_to_delete.id], + in_guild=True + ) def _validate_roledict(self, role_dict: dict) -> None: """Helper method to validate a dict representing a role.""" @@ -78,21 +92,21 @@ class CreationTests(APISubdomainTestCase): def test_role_list(self): """Tests the GET list-view and validates the contents.""" - url = reverse('bot:role-list', host='api') + url = reverse('api:bot:role-list') response = self.client.get(url) - self.assertContains(response, text="id", count=4, status_code=200) + self.assertContains(response, text="id", count=5, status_code=200) roles = response.json() self.assertIsInstance(roles, list) - self.assertEqual(len(roles), 4) + self.assertEqual(len(roles), 5) for role in roles: self._validate_roledict(role) def test_role_get_detail_success(self): """Tests GET detail view of an existing role.""" - url = reverse('bot:role-detail', host='api', args=(self.admins_role.id, )) + url = reverse('api:bot:role-detail', args=(self.admins_role.id, )) response = self.client.get(url) self.assertContains(response, text="id", count=1, status_code=200) @@ -107,7 +121,7 @@ class CreationTests(APISubdomainTestCase): def test_role_post_201(self): """Tests creation of a role with a valid request.""" - url = reverse('bot:role-list', host='api') + url = reverse('api:bot:role-list') data = { "id": 1234567890, "name": "Role Creation Test", @@ -120,7 +134,7 @@ class CreationTests(APISubdomainTestCase): def test_role_post_invalid_request_body(self): """Tests creation of a role with an invalid request body.""" - url = reverse('bot:role-list', host='api') + url = reverse('api:bot:role-list') data = { "name": "Role Creation Test", "permissions": 0b01010010101, @@ -133,7 +147,7 @@ class CreationTests(APISubdomainTestCase): def test_role_put_200(self): """Tests PUT role request with valid request body.""" - url = reverse('bot:role-detail', host='api', args=(self.admins_role.id,)) + url = reverse('api:bot:role-detail', args=(self.admins_role.id,)) data = { "id": 123454321, "name": "Role Put Alteration Test", @@ -153,7 +167,7 @@ class CreationTests(APISubdomainTestCase): def test_role_put_invalid_request_body(self): """Tests PUT role request with invalid request body.""" - url = reverse('bot:role-detail', host='api', args=(self.admins_role.id,)) + url = reverse('api:bot:role-detail', args=(self.admins_role.id,)) data = { "name": "Role Put Alteration Test", "permissions": 255, @@ -165,7 +179,7 @@ class CreationTests(APISubdomainTestCase): def test_role_patch_200(self): """Tests PATCH role request with valid request body.""" - url = reverse('bot:role-detail', host='api', args=(self.admins_role.id,)) + url = reverse('api:bot:role-detail', args=(self.admins_role.id,)) data = { "name": "Owners" } @@ -177,13 +191,19 @@ class CreationTests(APISubdomainTestCase): def test_role_delete_200(self): """Tests DELETE requests for existing role.""" - url = reverse('bot:role-detail', host='api', args=(self.admins_role.id,)) + url = reverse('api:bot:role-detail', args=(self.admins_role.id,)) response = self.client.delete(url) self.assertEqual(response.status_code, 204) + def test_role_delete_unassigned(self): + """Tests if the deleted Role gets unassigned from the user.""" + self.role_to_delete.delete() + self.role_unassigned_test_user.refresh_from_db() + self.assertEqual(self.role_unassigned_test_user.roles, []) + def test_role_detail_404_all_methods(self): """Tests detail view with non-existing ID.""" - url = reverse('bot:role-detail', host='api', args=(20190815,)) + url = reverse('api:bot:role-detail', args=(20190815,)) for method in ('get', 'put', 'patch', 'delete'): response = getattr(self.client, method)(url) diff --git a/pydis_site/apps/api/tests/test_rules.py b/pydis_site/apps/api/tests/test_rules.py index c94f89cc..d08c5fae 100644 --- a/pydis_site/apps/api/tests/test_rules.py +++ b/pydis_site/apps/api/tests/test_rules.py @@ -1,23 +1,23 @@ -from django_hosts.resolvers import reverse +from django.urls import reverse -from .base import APISubdomainTestCase +from .base import AuthenticatedAPITestCase from ..views import RulesView -class RuleAPITests(APISubdomainTestCase): +class RuleAPITests(AuthenticatedAPITestCase): def setUp(self): super().setUp() self.client.force_authenticate(user=None) def test_can_access_rules_view(self): - url = reverse('rules', host='api') + url = reverse('api:rules') response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertIsInstance(response.json(), list) def test_link_format_query_param_produces_different_results(self): - url = reverse('rules', host='api') + url = reverse('api:rules') markdown_links_response = self.client.get(url + '?link_format=md') html_links_response = self.client.get(url + '?link_format=html') self.assertNotEqual( @@ -30,6 +30,6 @@ class RuleAPITests(APISubdomainTestCase): RulesView._format_link("a", "b", "c") def test_get_returns_400_for_wrong_link_format(self): - url = reverse('rules', host='api') + url = reverse('api:rules') response = self.client.get(url + '?link_format=unknown') self.assertEqual(response.status_code, 400) diff --git a/pydis_site/apps/api/tests/test_users.py b/pydis_site/apps/api/tests/test_users.py index c43b916a..9b91380b 100644 --- a/pydis_site/apps/api/tests/test_users.py +++ b/pydis_site/apps/api/tests/test_users.py @@ -1,44 +1,45 @@ from unittest.mock import patch from django.core.exceptions import ObjectDoesNotExist -from django_hosts.resolvers import reverse +from django.urls import reverse -from .base import APISubdomainTestCase +from .base import AuthenticatedAPITestCase from ..models import Role, User -from ..models.bot.metricity import NotFound +from ..models.bot.metricity import NotFoundError +from ..viewsets.bot.user import UserListPagination -class UnauthedUserAPITests(APISubdomainTestCase): +class UnauthedUserAPITests(AuthenticatedAPITestCase): def setUp(self): super().setUp() self.client.force_authenticate(user=None) def test_detail_lookup_returns_401(self): - url = reverse('bot:user-detail', args=('whatever',), host='api') + url = reverse('api:bot:user-detail', args=('whatever',)) response = self.client.get(url) self.assertEqual(response.status_code, 401) def test_list_returns_401(self): - url = reverse('bot:user-list', host='api') + url = reverse('api:bot:user-list') response = self.client.get(url) self.assertEqual(response.status_code, 401) def test_create_returns_401(self): - url = reverse('bot:user-list', host='api') + url = reverse('api:bot:user-list') response = self.client.post(url, data={'hi': 'there'}) self.assertEqual(response.status_code, 401) def test_delete_returns_401(self): - url = reverse('bot:user-detail', args=('whatever',), host='api') + url = reverse('api:bot:user-detail', args=('whatever',)) response = self.client.delete(url) self.assertEqual(response.status_code, 401) -class CreationTests(APISubdomainTestCase): +class CreationTests(AuthenticatedAPITestCase): @classmethod def setUpTestData(cls): cls.role = Role.objects.create( @@ -57,7 +58,7 @@ class CreationTests(APISubdomainTestCase): ) def test_accepts_valid_data(self): - url = reverse('bot:user-list', host='api') + url = reverse('api:bot:user-list') data = { 'id': 42, 'name': "Test", @@ -78,7 +79,7 @@ class CreationTests(APISubdomainTestCase): self.assertEqual(user.in_guild, data['in_guild']) def test_supports_multi_creation(self): - url = reverse('bot:user-list', host='api') + url = reverse('api:bot:user-list') data = [ { 'id': 5, @@ -103,7 +104,7 @@ class CreationTests(APISubdomainTestCase): self.assertEqual(response.json(), []) def test_returns_400_for_unknown_role_id(self): - url = reverse('bot:user-list', host='api') + url = reverse('api:bot:user-list') data = { 'id': 5, 'name': "test man", @@ -117,7 +118,7 @@ class CreationTests(APISubdomainTestCase): self.assertEqual(response.status_code, 400) def test_returns_400_for_bad_data(self): - url = reverse('bot:user-list', host='api') + url = reverse('api:bot:user-list') data = { 'id': True, 'discriminator': "totally!" @@ -128,7 +129,7 @@ class CreationTests(APISubdomainTestCase): def test_returns_400_for_user_recreation(self): """Return 201 if User is already present in database as it skips User creation.""" - url = reverse('bot:user-list', host='api') + url = reverse('api:bot:user-list') data = [{ 'id': 11, 'name': 'You saw nothing.', @@ -140,7 +141,7 @@ class CreationTests(APISubdomainTestCase): def test_returns_400_for_duplicate_request_users(self): """Return 400 if 2 Users with same ID is passed in the request data.""" - url = reverse('bot:user-list', host='api') + url = reverse('api:bot:user-list') data = [ { 'id': 11, @@ -160,7 +161,7 @@ class CreationTests(APISubdomainTestCase): def test_returns_400_for_existing_user(self): """Returns 400 if user is already present in DB.""" - url = reverse('bot:user-list', host='api') + url = reverse('api:bot:user-list') data = { 'id': 11, 'name': 'You saw nothing part 3.', @@ -171,7 +172,7 @@ class CreationTests(APISubdomainTestCase): self.assertEqual(response.status_code, 400) -class MultiPatchTests(APISubdomainTestCase): +class MultiPatchTests(AuthenticatedAPITestCase): @classmethod def setUpTestData(cls): cls.role_developer = Role.objects.create( @@ -195,7 +196,7 @@ class MultiPatchTests(APISubdomainTestCase): ) def test_multiple_users_patch(self): - url = reverse("bot:user-bulk-patch", host="api") + url = reverse("api:bot:user-bulk-patch") data = [ { "id": 1, @@ -218,7 +219,7 @@ class MultiPatchTests(APISubdomainTestCase): self.assertEqual(user_2.name, data[1]["name"]) def test_returns_400_for_missing_user_id(self): - url = reverse("bot:user-bulk-patch", host="api") + url = reverse("api:bot:user-bulk-patch") data = [ { "name": "I am ghost user!", @@ -234,7 +235,7 @@ class MultiPatchTests(APISubdomainTestCase): self.assertEqual(response.status_code, 400) def test_returns_404_for_not_found_user(self): - url = reverse("bot:user-bulk-patch", host="api") + url = reverse("api:bot:user-bulk-patch") data = [ { "id": 1, @@ -252,7 +253,7 @@ class MultiPatchTests(APISubdomainTestCase): self.assertEqual(response.status_code, 404) def test_returns_400_for_bad_data(self): - url = reverse("bot:user-bulk-patch", host="api") + url = reverse("api:bot:user-bulk-patch") data = [ { "id": 1, @@ -268,7 +269,7 @@ class MultiPatchTests(APISubdomainTestCase): self.assertEqual(response.status_code, 400) def test_returns_400_for_insufficient_data(self): - url = reverse("bot:user-bulk-patch", host="api") + url = reverse("api:bot:user-bulk-patch") data = [ { "id": 1, @@ -282,7 +283,7 @@ class MultiPatchTests(APISubdomainTestCase): def test_returns_400_for_duplicate_request_users(self): """Return 400 if 2 Users with same ID is passed in the request data.""" - url = reverse("bot:user-bulk-patch", host="api") + url = reverse("api:bot:user-bulk-patch") data = [ { 'id': 1, @@ -297,7 +298,7 @@ class MultiPatchTests(APISubdomainTestCase): self.assertEqual(response.status_code, 400) -class UserModelTests(APISubdomainTestCase): +class UserModelTests(AuthenticatedAPITestCase): @classmethod def setUpTestData(cls): cls.role_top = Role.objects.create( @@ -353,11 +354,11 @@ class UserModelTests(APISubdomainTestCase): self.assertEqual(self.user_with_roles.username, "Test User with two roles#0001") -class UserPaginatorTests(APISubdomainTestCase): +class UserPaginatorTests(AuthenticatedAPITestCase): @classmethod def setUpTestData(cls): users = [] - for i in range(1, 10_001): + for i in range(1, UserListPagination.page_size + 1): users.append(User( id=i, name=f"user{i}", @@ -367,35 +368,37 @@ class UserPaginatorTests(APISubdomainTestCase): cls.users = User.objects.bulk_create(users) def test_returns_single_page_response(self): - url = reverse("bot:user-list", host="api") + url = reverse("api:bot:user-list") response = self.client.get(url).json() self.assertIsNone(response["next_page_no"]) self.assertIsNone(response["previous_page_no"]) def test_returns_next_page_number(self): + user_id = UserListPagination.page_size + 1 User.objects.create( - id=10_001, - name="user10001", + id=user_id, + name=f"user{user_id}", discriminator=1111, in_guild=True ) - url = reverse("bot:user-list", host="api") + url = reverse("api:bot:user-list") response = self.client.get(url).json() self.assertEqual(2, response["next_page_no"]) def test_returns_previous_page_number(self): + user_id = UserListPagination.page_size + 1 User.objects.create( - id=10_001, - name="user10001", + id=user_id, + name=f"user{user_id}", discriminator=1111, in_guild=True ) - url = reverse("bot:user-list", host="api") + url = reverse("api:bot:user-list") response = self.client.get(url, {"page": 2}).json() self.assertEqual(1, response["previous_page_no"]) -class UserMetricityTests(APISubdomainTestCase): +class UserMetricityTests(AuthenticatedAPITestCase): @classmethod def setUpTestData(cls): User.objects.create( @@ -413,12 +416,12 @@ class UserMetricityTests(APISubdomainTestCase): self.mock_metricity_user(joined_at, total_messages, total_blocks, []) # When - url = reverse('bot:user-metricity-data', args=[0], host='api') + url = reverse('api:bot:user-metricity-data', args=[0]) response = self.client.get(url) # Then self.assertEqual(response.status_code, 200) - self.assertEqual(response.json(), { + self.assertCountEqual(response.json(), { "joined_at": joined_at, "total_messages": total_messages, "voice_banned": False, @@ -430,7 +433,7 @@ class UserMetricityTests(APISubdomainTestCase): self.mock_no_metricity_user() # When - url = reverse('bot:user-metricity-data', args=[0], host='api') + url = reverse('api:bot:user-metricity-data', args=[0]) response = self.client.get(url) # Then @@ -441,7 +444,7 @@ class UserMetricityTests(APISubdomainTestCase): self.mock_no_metricity_user() # When - url = reverse('bot:user-metricity-review-data', args=[0], host='api') + url = reverse('api:bot:user-metricity-review-data', args=[0]) response = self.client.get(url) # Then @@ -460,7 +463,7 @@ class UserMetricityTests(APISubdomainTestCase): with patch("pydis_site.apps.api.viewsets.bot.user.Infraction.objects.get") as p: p.side_effect = case['exception'] - url = reverse('bot:user-metricity-data', args=[0], host='api') + url = reverse('api:bot:user-metricity-data', args=[0]) response = self.client.get(url) self.assertEqual(response.status_code, 200) @@ -475,7 +478,7 @@ class UserMetricityTests(APISubdomainTestCase): self.mock_metricity_user(joined_at, total_messages, total_blocks, channel_activity) # When - url = reverse('bot:user-metricity-review-data', args=[0], host='api') + url = reverse('api:bot:user-metricity-review-data', args=[0]) response = self.client.get(url) # Then @@ -501,7 +504,7 @@ class UserMetricityTests(APISubdomainTestCase): self.metricity = patcher.start() self.addCleanup(patcher.stop) self.metricity = self.metricity.return_value.__enter__.return_value - self.metricity.user.side_effect = NotFound() - self.metricity.total_messages.side_effect = NotFound() - self.metricity.total_message_blocks.side_effect = NotFound() - self.metricity.top_channel_activity.side_effect = NotFound() + self.metricity.user.side_effect = NotFoundError() + self.metricity.total_messages.side_effect = NotFoundError() + self.metricity.total_message_blocks.side_effect = NotFoundError() + self.metricity.top_channel_activity.side_effect = NotFoundError() diff --git a/pydis_site/apps/api/urls.py b/pydis_site/apps/api/urls.py index 2e1ef0b4..b0ab545b 100644 --- a/pydis_site/apps/api/urls.py +++ b/pydis_site/apps/api/urls.py @@ -16,7 +16,7 @@ from .viewsets import ( UserViewSet ) -# http://www.django-rest-framework.org/api-guide/routers/#defaultrouter +# https://www.django-rest-framework.org/api-guide/routers/#defaultrouter bot_router = DefaultRouter(trailing_slash=False) bot_router.register( 'filter-lists', diff --git a/pydis_site/apps/api/viewsets/bot/filter_list.py b/pydis_site/apps/api/viewsets/bot/filter_list.py index 2cb21ab9..4b05acee 100644 --- a/pydis_site/apps/api/viewsets/bot/filter_list.py +++ b/pydis_site/apps/api/viewsets/bot/filter_list.py @@ -59,7 +59,8 @@ class FilterListViewSet(ModelViewSet): ... ["GUILD_INVITE","Guild Invite"], ... ["FILE_FORMAT","File Format"], ... ["DOMAIN_NAME","Domain Name"], - ... ["FILTER_TOKEN","Filter Token"] + ... ["FILTER_TOKEN","Filter Token"], + ... ["REDIRECT", "Redirect"] ... ] #### Status codes diff --git a/pydis_site/apps/api/viewsets/bot/infraction.py b/pydis_site/apps/api/viewsets/bot/infraction.py index f8b0cb9d..8a48ed1f 100644 --- a/pydis_site/apps/api/viewsets/bot/infraction.py +++ b/pydis_site/apps/api/viewsets/bot/infraction.py @@ -70,7 +70,8 @@ class InfractionViewSet( ... 'actor': 125435062127820800, ... 'type': 'ban', ... 'reason': 'He terk my jerb!', - ... 'hidden': True + ... 'hidden': True, + ... 'dm_sent': True ... } ... ] @@ -100,7 +101,8 @@ class InfractionViewSet( ... 'hidden': True, ... 'type': 'ban', ... 'reason': 'He terk my jerb!', - ... 'user': 172395097705414656 + ... 'user': 172395097705414656, + ... 'dm_sent': False ... } #### Response format @@ -118,7 +120,8 @@ class InfractionViewSet( >>> { ... 'active': True, ... 'expires_at': '4143-02-15T21:04:31+00:00', - ... 'reason': 'durka derr' + ... 'reason': 'durka derr', + ... 'dm_sent': True ... } #### Response format diff --git a/pydis_site/apps/api/viewsets/bot/off_topic_channel_name.py b/pydis_site/apps/api/viewsets/bot/off_topic_channel_name.py index 826ad25e..78f8c340 100644 --- a/pydis_site/apps/api/viewsets/bot/off_topic_channel_name.py +++ b/pydis_site/apps/api/viewsets/bot/off_topic_channel_name.py @@ -1,18 +1,17 @@ from django.db.models import Case, Value, When from django.db.models.query import QuerySet -from django.http.request import HttpRequest from django.shortcuts import get_object_or_404 from rest_framework.exceptions import ParseError -from rest_framework.mixins import DestroyModelMixin +from rest_framework.request import Request from rest_framework.response import Response from rest_framework.status import HTTP_201_CREATED -from rest_framework.viewsets import ViewSet +from rest_framework.viewsets import ModelViewSet from pydis_site.apps.api.models.bot.off_topic_channel_name import OffTopicChannelName from pydis_site.apps.api.serializers import OffTopicChannelNameSerializer -class OffTopicChannelNameViewSet(DestroyModelMixin, ViewSet): +class OffTopicChannelNameViewSet(ModelViewSet): """ View of off-topic channel names used by the bot to rotate our off-topic names on a daily basis. @@ -20,7 +19,7 @@ class OffTopicChannelNameViewSet(DestroyModelMixin, ViewSet): ### GET /bot/off-topic-channel-names Return all known off-topic channel names from the database. If the `random_items` query parameter is given, for example using... - $ curl api.pythondiscord.local:8000/bot/off-topic-channel-names?random_items=5 + $ curl 127.0.0.1:8000/api/bot/off-topic-channel-names?random_items=5 ... then the API will return `5` random items from the database that is not used in current rotation. When running out of names, API will mark all names to not used and start new rotation. @@ -39,7 +38,7 @@ class OffTopicChannelNameViewSet(DestroyModelMixin, ViewSet): ### POST /bot/off-topic-channel-names Create a new off-topic-channel name in the database. The name must be given as a query parameter, for example: - $ curl api.pythondiscord.local:8000/bot/off-topic-channel-names?name=lemons-lemonade-shop + $ curl 127.0.0.1:8000/api/bot/off-topic-channel-names?name=lemons-lemonade-shop #### Status codes - 201: returned on success @@ -58,6 +57,7 @@ class OffTopicChannelNameViewSet(DestroyModelMixin, ViewSet): lookup_field = 'name' serializer_class = OffTopicChannelNameSerializer + queryset = OffTopicChannelName.objects.all() def get_object(self) -> OffTopicChannelName: """ @@ -65,15 +65,14 @@ class OffTopicChannelNameViewSet(DestroyModelMixin, ViewSet): If it doesn't, a HTTP 404 is returned by way of throwing an exception. """ - queryset = self.get_queryset() name = self.kwargs[self.lookup_field] - return get_object_or_404(queryset, name=name) + return get_object_or_404(self.queryset, name=name) def get_queryset(self) -> QuerySet: """Returns a queryset that covers the entire OffTopicChannelName table.""" return OffTopicChannelName.objects.all() - def create(self, request: HttpRequest) -> Response: + def create(self, request: Request, *args, **kwargs) -> Response: """ DRF method for creating a new OffTopicChannelName. @@ -91,7 +90,7 @@ class OffTopicChannelNameViewSet(DestroyModelMixin, ViewSet): 'name': ["This query parameter is required."] }) - def list(self, request: HttpRequest) -> Response: + def list(self, request: Request, *args, **kwargs) -> Response: """ DRF method for listing OffTopicChannelName entries. @@ -109,13 +108,13 @@ class OffTopicChannelNameViewSet(DestroyModelMixin, ViewSet): 'random_items': ["Must be a positive integer."] }) - queryset = self.get_queryset().order_by('used', '?')[:random_count] + queryset = self.queryset.order_by('used', '?')[:random_count] # When any name is used in our listing then this means we reached end of round # and we need to reset all other names `used` to False if any(offtopic_name.used for offtopic_name in queryset): # These names that we just got have to be excluded from updating used to False - self.get_queryset().update( + self.queryset.update( used=Case( When( name__in=(offtopic_name.name for offtopic_name in queryset), @@ -126,13 +125,18 @@ class OffTopicChannelNameViewSet(DestroyModelMixin, ViewSet): ) else: # Otherwise mark selected names `used` to True - self.get_queryset().filter( + self.queryset.filter( name__in=(offtopic_name.name for offtopic_name in queryset) ).update(used=True) serialized = self.serializer_class(queryset, many=True) return Response(serialized.data) - queryset = self.get_queryset() + params = {} + + if active_param := request.query_params.get("active"): + params["active"] = active_param.lower() == "true" + + queryset = self.queryset.filter(**params) serialized = self.serializer_class(queryset, many=True) return Response(serialized.data) diff --git a/pydis_site/apps/api/viewsets/bot/reminder.py b/pydis_site/apps/api/viewsets/bot/reminder.py index 111660d9..78d7cb3b 100644 --- a/pydis_site/apps/api/viewsets/bot/reminder.py +++ b/pydis_site/apps/api/viewsets/bot/reminder.py @@ -42,7 +42,8 @@ class ReminderViewSet( ... 'expiration': '5018-11-20T15:52:00Z', ... 'id': 11, ... 'channel_id': 634547009956872193, - ... 'jump_url': "https://discord.com/channels/<guild_id>/<channel_id>/<message_id>" + ... 'jump_url': "https://discord.com/channels/<guild_id>/<channel_id>/<message_id>", + ... 'failures': 3 ... }, ... ... ... ] @@ -67,7 +68,8 @@ class ReminderViewSet( ... 'expiration': '5018-11-20T15:52:00Z', ... 'id': 11, ... 'channel_id': 634547009956872193, - ... 'jump_url': "https://discord.com/channels/<guild_id>/<channel_id>/<message_id>" + ... 'jump_url': "https://discord.com/channels/<guild_id>/<channel_id>/<message_id>", + ... 'failures': 3 ... } #### Status codes @@ -80,7 +82,7 @@ class ReminderViewSet( #### Request body >>> { ... 'author': int, - ... 'mentions': List[int], + ... 'mentions': list[int], ... 'content': str, ... 'expiration': str, # ISO-formatted datetime ... 'channel_id': int, @@ -98,9 +100,10 @@ class ReminderViewSet( #### Request body >>> { - ... 'mentions': List[int], + ... 'mentions': list[int], ... 'content': str, - ... 'expiration': str # ISO-formatted datetime + ... 'expiration': str, # ISO-formatted datetime + ... 'failures': int ... } #### Status codes diff --git a/pydis_site/apps/api/viewsets/bot/user.py b/pydis_site/apps/api/viewsets/bot/user.py index 25722f5a..1a5e79f8 100644 --- a/pydis_site/apps/api/viewsets/bot/user.py +++ b/pydis_site/apps/api/viewsets/bot/user.py @@ -11,7 +11,7 @@ from rest_framework.serializers import ModelSerializer from rest_framework.viewsets import ModelViewSet from pydis_site.apps.api.models.bot.infraction import Infraction -from pydis_site.apps.api.models.bot.metricity import Metricity, NotFound +from pydis_site.apps.api.models.bot.metricity import Metricity, NotFoundError from pydis_site.apps.api.models.bot.user import User from pydis_site.apps.api.serializers import UserSerializer @@ -19,7 +19,7 @@ from pydis_site.apps.api.serializers import UserSerializer class UserListPagination(PageNumberPagination): """Custom pagination class for the User Model.""" - page_size = 10000 + page_size = 2500 page_size_query_param = "page_size" def get_next_page_number(self) -> typing.Optional[int]: @@ -271,11 +271,13 @@ class UserViewSet(ModelViewSet): with Metricity() as metricity: try: data = metricity.user(user.id) + data["total_messages"] = metricity.total_messages(user.id) - data["voice_banned"] = voice_banned data["activity_blocks"] = metricity.total_message_blocks(user.id) + + data["voice_banned"] = voice_banned return Response(data, status=status.HTTP_200_OK) - except NotFound: + except NotFoundError: return Response(dict(detail="User not found in metricity"), status=status.HTTP_404_NOT_FOUND) @@ -290,6 +292,6 @@ class UserViewSet(ModelViewSet): data["total_messages"] = metricity.total_messages(user.id) data["top_channel_activity"] = metricity.top_channel_activity(user.id) return Response(data, status=status.HTTP_200_OK) - except NotFound: + except NotFoundError: return Response(dict(detail="User not found in metricity"), status=status.HTTP_404_NOT_FOUND) diff --git a/pydis_site/apps/content/resources/code-of-conduct.md b/pydis_site/apps/content/resources/code-of-conduct.md index 6302438e..56050230 100644 --- a/pydis_site/apps/content/resources/code-of-conduct.md +++ b/pydis_site/apps/content/resources/code-of-conduct.md @@ -77,8 +77,8 @@ You may report in the following ways: If you wish to appeal a decision or action taken by the moderation team, you can do so in one of the following ways: -* By sending an email to [[email protected]](mailto:[email protected]) * By sending a direct message (DM) to ModMail from our Discord server. +* Joining our [ban appeals server](https://discord.gg/WXrCJxWBnm) and sending a direct message (DM) to the Ban Appeals bot. Please provide all relevant information in your appeal, including: diff --git a/pydis_site/apps/content/resources/frequently-asked-questions.md b/pydis_site/apps/content/resources/frequently-asked-questions.md index 212ea5f8..1c9c3f6d 100644 --- a/pydis_site/apps/content/resources/frequently-asked-questions.md +++ b/pydis_site/apps/content/resources/frequently-asked-questions.md @@ -89,7 +89,7 @@ It's also to ease the burden on our moderators, otherwise they would have to dow Even though Discord does support previewing of files like `.txt` and `.py`, that support is only available on Desktop, not mobile. Additionally, we prefer people to use hastebin as it encourages them to only copy over the relevant code snippets instead of their whole code; this makes helping much easier for all involved. -If you want to share code please use our hosted hastebin, [paste.pythondiscord.com](http://paste.pythondiscord.com). +If you want to share code please use our hosted hastebin, [paste.pythondiscord.com](https://paste.pythondiscord.com). #### **Q: Why is this permission not allowed in that channel?** @@ -119,7 +119,7 @@ You can also open an issue on our meta repo on GitHub, which can be found [here] While we love our blurple Python logo, we also enjoy celebrating other events throughout the year, like Advent of Code, Pride Month, Black History Month, Valentine's Day, Diwali, and more! In the spirit of those celebrations, we like to have some fun and change our icon instead. If you're wondering why it's changed this time, check out `#changelog` on the server, as the reasoning for the recent change will be there. -If you'd like to contribute and create a Python Discord server icon for us to use, check out [our branding repo](https://github.com/python-discord/branding) for what we currently have and talk to us in the `#media-branding` channel in the server. +If you'd like to contribute and create a Python Discord server icon for us to use, check out [our branding repo](https://github.com/python-discord/branding) for what we currently have and talk to us in the `#dev-branding` channel in the server. ## Misc diff --git a/pydis_site/apps/content/resources/guides/pydis-guides/contributing/bot.md b/pydis_site/apps/content/resources/guides/pydis-guides/contributing/bot.md index 741bf28e..b9589def 100644 --- a/pydis_site/apps/content/resources/guides/pydis-guides/contributing/bot.md +++ b/pydis_site/apps/content/resources/guides/pydis-guides/contributing/bot.md @@ -2,207 +2,661 @@ title: Contributing to Bot description: A guide to setting up and configuring Bot. icon: fab fa-github -toc: 1 +toc: 3 --- - -# Requirements -* [Python 3.9](https://www.python.org/downloads/) -* [Poetry](https://github.com/python-poetry/poetry#installation) - * `pip install poetry` -* [Git](https://git-scm.com/downloads) - * [Windows](https://git-scm.com/download/win) - * [MacOS](https://git-scm.com/download/mac) or `brew install git` - * [Linux](https://git-scm.com/download/linux) -* A running webserver for the [site](../site) - * Follow the linked guide only if you don't want to use Docker or if you plan to do development on the site project too. - -## Using Docker - -Both the site and the bot can be started using Docker. -Using Docker is generally recommended (but not strictly required) because it abstracts away some additional set up work, especially for the site. -However, if you plan to attach a debugger to either the site or the bot, run the respective project directly on your system (AKA the _host_) instead. - -The requirements for Docker are: - -* [Docker CE](https://docs.docker.com/install/) -* [Docker Compose](https://docs.docker.com/compose/install/) (This already comes bundled on macOS and Windows, so you shouldn't need to install it) - * `pip install docker-compose` +The purpose of this guide is to get you a running local version of [the Python bot](https://github.com/python-discord/bot). +This page will focus on the quickest steps one can take, with mentions of alternatives afterwards. + +### Clone The Repository +First things first, to run the bot's code and make changes to it, you need a local version of it (on your computer). + +<div class="card"> + <button type="button" class="card-header collapsible"> + <span class="card-header-title subtitle is-6 my-2 ml-2">Getting started with Git and GitHub</span> + <span class="card-header-icon"> + <i class="fas fa-angle-down title is-5" aria-hidden="true"></i> + </span> + </button> + <div class="collapsible-content"> + <div class="card-content"> + <p>If you don't have Git on your computer already, <a href="https://git-scm.com/downloads">install it</a>. You can additionally install a Git GUI such as <a href="https://www.gitkraken.com/download">GitKraken</a>, or the <a href="https://cli.github.com/manual/installation">GitHub CLI</a>.</p> + <p>To learn more about Git, you can look into <a href="../working-with-git">our guides</a>, as well as <a href="https://education.github.com/git-cheat-sheet-education.pdf">this cheatsheet</a>, <a href="https://learngitbranching.js.org">Learn Git Branching</a>, and otherwise any guide you can find on the internet. Once you got the basic idea though, the best way to learn Git is to use it.</p> + <p>Creating a copy of a repository under your own account is called a <em>fork</em>. This is where all your changes and commits will be pushed to, and from where your pull requests will originate from.</p> + <p><strong><a href="../forking-repository">Learn about forking a project</a></strong>.</p> + </div> + </div> +</div> +<br> + +You will need to create a fork of [the project](https://github.com/python-discord/bot), and clone the fork. +Once this is done, you will have completed the first step towards having a running version of the bot. + +#### Working on the Repository Directly +If you are a member of the organisation (a member of [this list](https://github.com/orgs/python-discord/people), or in our particular case, server staff), you can clone the project repository without creating a fork, and work on a feature branch instead. --- -# Fork the project -You will need access to a copy of the git repository of your own that will allow you to edit the code and push your commits to. -Creating a copy of a repository under your own account is called a _fork_. - -* [Learn how to create a fork of the repository here.](../forking-repository) -This is where all your changes and commits will be pushed to, and from where your PRs will originate from. +### Set Up a Test Server +The Python bot is tightly coupled with the Python Discord server, so to have a functional version of the bot you need a server with channels it can use. +It's possible to set the bot to use a single channel for all cogs, but that will cause extreme spam and will be difficult to work with. -For any staff member, since you have write permissions already to the original repository, you can just create a feature branch to push your commits to instead. - ---- -# Development environment -1. [Clone your fork to a local project directory](../cloning-repository/) -2. [Install the project's dependencies](../installing-project-dependencies/) -3. [Prepare your hosts file (Optional)](../hosts-file/) +You can start your own server and set up channels as you see fit, but for your convenience we have a template for a development server you can use: [https://discord.new/zmHtscpYN9E3](https://discord.new/zmHtscpYN9E3). +Keep in mind that this is not a mirror of the Python server, but a reduced version for testing purposes. A lot of the channels in the Python server were merged. --- -# Test server and bot account -You will need your own test server and bot account on Discord to test your changes to the bot. -* [**Create a test server**](../setting-test-server-and-bot-account#setting-up-a-test-server) -* [**Create a bot account**](../setting-test-server-and-bot-account#setting-up-a-bot-account) -* Invite it to the server you just created. +### Set Up a Bot Account +You will need your own bot account on Discord to test your changes to the bot. +See [here](../creating-bot-account) for help with setting up a bot account. Once you have a bot account, invite it to the test server you created in the previous section. -### Privileged Intents +#### Privileged Intents -With `discord.py` 1.5 and later, it is now necessary to explicitly request that your Discord bot receives certain gateway events. +It is necessary to explicitly request that your Discord bot receives certain gateway events. The Python bot requires the `Server Member Intent` to function. In order to enable it, visit the [Developer Portal](https://discord.com/developers/applications/) (from where you copied your bot's login token) and scroll down to the `Privileged Gateway Intents` section. The `Presence Intent` is not necessary and can be left disabled. If your bot fails to start with a `PrivilegedIntentsRequired` exception, this indicates that the required intent was not enabled. -### Server Setup - -Setup categories, channels, emojis, roles, and webhooks in your server. To see what needs to be added, please refer to the following sections in the `config-default.yml` file: +--- -* `style.emojis` +### Configure the Bot +You now have both the bot's code and a server to run it on. It's time you to connect the two by changing the bot's configurations. + +#### config.yml +Entering the directory of the cloned code, you will find a file named `config-default.yml`. +This file contains the various configurations we use to make the bot run on the Python Discord server, such as channel and role IDs, and the emojis it works with. +It also contains configurations such as how long it takes for a help channel to time out, and how many messages a user needs to voice-verify. + +To run the bot in your test server, you will need to override some of those configurations. +Create and open a new file in the directory called `config.yml`. Alternatively, copy the `config-default.yml` file and rename the copy to `config.yml`. +The bot will first look at the items in `config.yml`, and will fall back to `config-default.yml` only if necessary. Note that you don't have to specify all items in `config.yml`, just the ones you want to override such as channel IDs. + +See [here](../obtaining-discord-ids) for help with obtaining Discord IDs. + +<div class="card"> + <button type="button" class="card-header collapsible"> + <span class="card-header-title subtitle is-6 my-2 ml-2">Optional config.yml</span> + <span class="card-header-icon"> + <i class="fas fa-angle-down title is-5" aria-hidden="true"></i> + </span> + </button> + <div class="collapsible-content"> + <div class="card-content"> + <p>If you used the provided server template, and you're not sure which channels belong where in the config file, you can use the config below. Pay attention to the comments with several <code>#</code> symbols, and replace the <code>�</code> characters with the right IDs.</p> + <pre> + <code class="language-yaml"> +bot: + prefix: "!" + + redis: + host: "redis" + password: null + port: 6379 + use_fakeredis: true + + stats: + presence_update_timeout: 300 + statsd_host: "graphite.default.svc.cluster.local" + +urls: + # PyDis site vars + site: &DOMAIN "web:8000" + site_api: &API !JOIN [*DOMAIN, "/api"] + site_api_schema: "http://" + site_paste: &PASTE !JOIN ["paste.", "pythondiscord.com"] + site_schema: &SCHEMA "http://" + site_staff: &STAFF !JOIN [*DOMAIN, "/staff"] + + paste_service: !JOIN ["https://", *PASTE, "/{key}"] + site_logs_view: !JOIN [*SCHEMA, *STAFF, "/bot/logs"] + + # Snekbox + snekbox_eval_api: "http://localhost:8060/eval" + +##### << Replace the following � characters with the channel IDs in your test server >> ##### +# This assumes the template was used: https://discord.new/zmHtscpYN9E3 +dev_guild: + id: &DEV_GUILD_ID � + + categories: + logs: &DEV_LOGS � + help_available: &DEV_HELP_AVAILABLE � + help_occupied: &DEV_HELP_OCCUPIED � + help_dormant: &DEV_HELP_DORMANT � + voice: &DEV_VOICE � + + channels: + # Staff + admins_mods: &DEV_ADMINS_MODS � + lounge_helpers_org: &DEV_LOUNGE_HELPERS_ORG � + defcon: &DEV_DEFCON � + incidents: &DEV_INCIDENTS � + incidents_archive: &DEV_INCIDENTS_ARCHIVE � + staff_announcements: &DEV_STAFF_ANNOUNCEMENTS � + dev_logs: &DEV_DEV_LOGS � + + # Logs + all_logs: &DEV_ALL_LOGS � + bb_logs: &DEV_BB_LOGS � + duck_pond: &DEV_DUCK_POND � + + # Available Help Channels + how_to_get_help: &DEV_HTGH � + + # Miscellaneous + bot_commands: &DEV_BOT_CMD � + general_meta_voice: &DEV_GMV � + dev_core_contrib: &DEV_DEV � + + # Voice + voice-verification: &DEV_VOICE_VER � + vc: &DEV_VC � + staff_voice: &DEV_STAFF_VOICE � + + # News + announcements: &DEV_ANNOUNCEMENTS � + py_news: &DEV_PY_NEWS � + + # Off-topic + off_topic_0: &DEV_OT_0 � + off_topic_1: &DEV_OT_1 � + off_topic_2: &DEV_OT_2 � + +guild: + ##### << Replace the following � characters with the role and webhook IDs in your test server >> ##### + roles: + announcements: � + contributors: � + help_cooldown: � + muted: &MUTED_ROLE � + partners: &PY_PARTNER_ROLE � + python_community: &PY_COMMUNITY_ROLE � + voice_verified: � + + # Staff + admins: &ADMINS_ROLE � + core_developers: � + devops: � + domain_leads: � + helpers: &HELPERS_ROLE � + moderators: &MODS_ROLE � + mod_team: &MOD_TEAM_ROLE � + owners: &OWNERS_ROLE � + code_jam_event_team: � + project_leads: � + + # Code Jam + team_leaders: � + + # Streaming + video: � + + webhooks: + big_brother: � + dev_log: � + duck_pond: � + incidents_archive: � + python_news: &PYNEWS_WEBHOOK � + talent_pool: � + + ##### << At this point your test bot should be able to mostly work with your test server >> ##### + # The following is the actual configs the bot uses, don't delete these. + id: *DEV_GUILD_ID + invite: "https://discord.gg/python" + + categories: + help_available: *DEV_HELP_AVAILABLE + help_dormant: *DEV_HELP_DORMANT + help_in_use: *DEV_HELP_OCCUPIED + logs: *DEV_LOGS + voice: *DEV_VOICE + + channels: + # Public announcement and news channels + announcements: *DEV_ANNOUNCEMENTS + change_log: *DEV_ANNOUNCEMENTS + mailing_lists: *DEV_ANNOUNCEMENTS + python_events: *DEV_ANNOUNCEMENTS + python_news: *DEV_PY_NEWS + + # Development + dev_contrib: *DEV_DEV + dev_core: *DEV_DEV + dev_log: *DEV_DEV_LOGS + + # Discussion + meta: *DEV_GMV + python_general: *DEV_GMV + + # Python Help: Available + cooldown: *DEV_HTGH + how_to_get_help: *DEV_HTGH + + # Topical + discord_py: *DEV_GMV + + # Logs + attachment_log: *DEV_ALL_LOGS + message_log: *DEV_ALL_LOGS + mod_log: *DEV_ALL_LOGS + user_log: *DEV_ALL_LOGS + voice_log: *DEV_ALL_LOGS + + # Off-topic + off_topic_0: *DEV_OT_0 + off_topic_1: *DEV_OT_1 + off_topic_2: *DEV_OT_2 + + # Special + bot_commands: *DEV_BOT_CMD + voice_gate: *DEV_VOICE_VER + code_jam_planning: *DEV_ADMINS_MODS + + # Staff + admins: *DEV_ADMINS_MODS + admin_spam: *DEV_ADMINS_MODS + defcon: *DEV_DEFCON + duck_pond: *DEV_DUCK_POND + helpers: *DEV_LOUNGE_HELPERS_ORG + incidents: *DEV_INCIDENTS + incidents_archive: *DEV_INCIDENTS_ARCHIVE + mods: *DEV_ADMINS_MODS + mod_alerts: *DEV_ADMINS_MODS + mod_meta: *DEV_ADMINS_MODS + mod_spam: *DEV_ADMINS_MODS + mod_tools: *DEV_ADMINS_MODS + organisation: *DEV_LOUNGE_HELPERS_ORG + staff_lounge: *DEV_LOUNGE_HELPERS_ORG + + # Staff announcement channels + admin_announcements: *DEV_STAFF_ANNOUNCEMENTS + mod_announcements: *DEV_STAFF_ANNOUNCEMENTS + staff_announcements: *DEV_STAFF_ANNOUNCEMENTS + + # Voice Channels + admins_voice: *DEV_STAFF_VOICE + code_help_voice_1: *DEV_VC + code_help_voice_2: *DEV_VC + general_voice: *DEV_VC + staff_voice: *DEV_STAFF_VOICE + + # Voice Chat + code_help_chat_1: *DEV_GMV + code_help_chat_2: *DEV_GMV + staff_voice_chat: *DEV_ADMINS_MODS + voice_chat: *DEV_GMV + + # Watch + big_brother_logs: *DEV_BB_LOGS + + moderation_categories: + - *DEV_LOGS + + moderation_channels: + - *DEV_ADMINS_MODS + + # Modlog cog ignores events which occur in these channels + modlog_blacklist: + - *DEV_ADMINS_MODS + - *DEV_ALL_LOGS + - *DEV_STAFF_VOICE + + reminder_whitelist: + - *DEV_BOT_CMD + - *DEV_DEV + + moderation_roles: + - *ADMINS_ROLE + - *MODS_ROLE + - *MOD_TEAM_ROLE + - *OWNERS_ROLE + + staff_roles: + - *ADMINS_ROLE + - *HELPERS_ROLE + - *MODS_ROLE + - *OWNERS_ROLE + +##### << The bot shouldn't fail without these, but commands adding specific emojis won't work. >> ##### +# You should at least set the trashcan. Set the incidents emojis if relevant. +style: + emojis: + badge_bug_hunter: "<:bug_hunter_lvl1:�>" + badge_bug_hunter_level_2: "<:bug_hunter_lvl2:�>" + badge_early_supporter: "<:early_supporter:�>" + badge_hypesquad: "<:hypesquad_events:�>" + badge_hypesquad_balance: "<:hypesquad_balance:�>" + badge_hypesquad_bravery: "<:hypesquad_bravery:�>" + badge_hypesquad_brilliance: "<:hypesquad_brilliance:�>" + badge_partner: "<:partner:�>" + badge_staff: "<:discord_staff:�>" + badge_verified_bot_developer: "<:verified_bot_dev:�>" + + defcon_shutdown: "<:defcondisabled:�>" + defcon_unshutdown: "<:defconenabled:�>" + defcon_update: "<:defconsettingsupdated:�>" + + failmail: "<:failmail:�>" + + #incident_actioned: "<:incident_actioned:�>" + incident_investigating: "<:incident_investigating:�>" + incident_unactioned: "<:incident_unactioned:�>" + + status_dnd: "<:status_dnd:�>" + status_idle: "<:status_idle:�>" + status_offline: "<:status_offline:�>" + status_online: "<:status_online:�>" + + trashcan: "<:trashcan:�>" + +##### << Optional - If you don't care about the filtering and help channel cogs, ignore the rest of this file >> ##### +filter: + # What do we filter? + filter_domains: true + filter_everyone_ping: true + filter_invites: true + filter_zalgo: false + watch_regex: true + watch_rich_embeds: true + + # Notify user on filter? + # Notifications are not expected for "watchlist" type filters + notify_user_domains: false + notify_user_everyone_ping: true + notify_user_invites: true + notify_user_zalgo: false + + # Filter configuration + offensive_msg_delete_days: 7 # How many days before deleting an offensive message? + ping_everyone: true + + # Censor doesn't apply to these + channel_whitelist: + - *DEV_ADMINS_MODS + - *DEV_BB_LOGS + - *DEV_ALL_LOGS + - *DEV_LOUNGE_HELPERS_ORG + + role_whitelist: + - *ADMINS_ROLE + - *HELPERS_ROLE + - *MODS_ROLE + - *OWNERS_ROLE + - *PY_COMMUNITY_ROLE + - *PY_PARTNER_ROLE + +help_channels: + enable: true + + # Minimum interval before allowing a certain user to claim a new help channel + claim_minutes: 1 + + # Roles which are allowed to use the command which makes channels dormant + cmd_whitelist: + - *HELPERS_ROLE + + # Allowed duration of inactivity before making a channel dormant + idle_minutes: 1 + + # Allowed duration of inactivity when channel is empty (due to deleted messages) + # before message making a channel dormant + deleted_idle_minutes: 1 + + # Maximum number of channels to put in the available category + max_available: 2 + + # Maximum number of channels across all 3 categories + # Note Discord has a hard limit of 50 channels per category, so this shouldn't be > 50 + max_total_channels: 20 + + # Prefix for help channel names + name_prefix: 'help-' + + # Notify if more available channels are needed but there are no more dormant ones + notify: true + + # Channel in which to send notifications + notify_channel: *DEV_LOUNGE_HELPERS_ORG + + # Minimum interval between helper notifications + notify_minutes: 5 + + # Mention these roles in notifications + notify_roles: + - *HELPERS_ROLE + +##### << Add any additional sections you need to override from config-default.yml >> ##### + </code> + </pre> +</div></div></div> +<br> + +If you don't wish to use the provided `config.yml` above, these are the main sections in `config-default.yml` that need overriding: + +* `guild.id` * `guild.categories` * `guild.channels` * `guild.roles` * `guild.webhooks` +* `style.emojis` -We understand this is tedious and are working on a better solution for setting up test servers. -In the meantime, [here](https://discord.new/zmHtscpYN9E3) is a template for you to use.<br> +Additionally: ---- -# Configure the bot -You will need to copy IDs of the test Discord server, as well as the created channels and roles to paste in the config file. -If you're not sure how to do this, [check out the information over here.](../setting-test-server-and-bot-account#obtain-the-ids) - -1. Create a copy of `config-default.yml` named `config.yml` in the same directory. -2. Set `guild.id` to your test servers's ID. -3. Change the IDs in the [sections](#server-setup) mentioned earlier to match the ones in your test server. -4. Set `urls.site_schema` and `urls.site_api_schema` to `"http://"`. -5. Set `urls.site`: - - If running the webserver in Docker, set it to `"web:8000"`. - - If the site container is running separately (i.e. started from a clone of the site repository), then [COMPOSE_PROJECT_NAME](../docker/#compose-project-names) has to be set to use this domain. If you choose not to set it, the domain in the following step can be used instead. - - If running the webserver locally and the hosts file has been configured, set it to `"pythondiscord.local:8000"`. - - Otherwise, use whatever domain corresponds to the server where the site is being hosted. -6. Set `urls.site_api` to whatever value you assigned to `urls.site` with `api` prefixed to it, for example if you set `urls.site` to `web:8000` then set `urls.site_api` to `api.web:8000`. -7. Setup the environment variables listed in the section below. - -### Environment variables - -These contain various settings used by the bot. -To learn how to set environment variables, read [this page](../configure-environment-variables) first. +* At this stage, set `bot.redis.use_fakeredis` to `true`. If you're looking for instructions for working with Redis, see [Working with Redis](#optional-working-with-redis). +* Set `urls.site_api` to `!JOIN [*DOMAIN, "/api"]`. +* Set `urls.site_schema` and `urls.site_api_schema` to `"http://"`. -The following is a list of all available environment variables used by the bot: +We understand this is tedious and are working on a better solution for setting up test servers. -| Variable | Required | Description | -| -------- | -------- | -------- | -| `BOT_TOKEN` | Always | Your Discord bot account's token (see [Test server and bot account](#test-server-and-bot-account)). | -| `BOT_API_KEY` | When running bot without Docker | Used to authenticate with the site's API. When using Docker to run the bot, this is automatically set. By default, the site will always have the API key shown in the example below. | -| `BOT_SENTRY_DSN` | When connecting the bot to sentry | The DSN of the sentry monitor. | -| `BOT_TRACE_LOGGERS ` | When you wish to see specific or all trace logs | Comma separated list that specifies which loggers emit trace logs through the listed names. If the ! prefix is used, all of the loggers except the listed ones are set to the trace level. If * is used, the root logger is set to the trace level. | -| `REDIS_PASSWORD` | When not using FakeRedis | The password to connect to the redis database. *Leave empty if you're not using REDIS.* | +<div class="card"> + <button type="button" class="card-header collapsible"> + <span class="card-header-title subtitle is-6 my-2 ml-2">Why do you need a separate config file?</span> + <span class="card-header-icon"> + <i class="fas fa-angle-down title is-5" aria-hidden="true"></i> + </span> + </button> + <div class="collapsible-content"> + <div class="card-content"> + While it's technically possible to edit <code>config-default.yml</code> to match your server, it is heavily discouraged. + This file's purpose is to provide the configurations the Python bot needs to run in the Python server in production, and should remain as such. + In contrast, the <code>config.yml</code> file can remain in your local copy of the code, and will be ignored by commits via the project's <code>.gitignore</code>. + </div> + </div> +</div> +<br> + +#### .env +The second file you need to create is the one containing the environment variables, and needs to be named `.env`. +Inside, add the line `BOT_TOKEN=YourDiscordBotTokenHere`. See [here](../creating-bot-account) for help with obtaining the bot token. + +The `.env` file will be ignored by commits. --- -If you are running on the host, while not required, we advise you set `use_fakeredis` to `true` in your `config.yml` file during development to avoid the need of setting up a Redis server. -It does mean you may lose persistent data on restart but this is non-critical. -Otherwise, you should set up a Redis instance and fill in the necessary config. -{: .notification .is-warning } +### Run it! +#### With Docker +You are now almost ready to run the Python bot. The simplest way to do so is with Docker. + +<div class="card"> + <button type="button" class="card-header collapsible"> + <span class="card-header-title subtitle is-6 my-2 ml-2">Getting started with Docker</span> + <span class="card-header-icon"> + <i class="fas fa-angle-down title is-5" aria-hidden="true"></i> + </span> + </button> + <div class="collapsible-content"> + <div class="card-content"> + The requirements for Docker are: + <ul> + <li><a href="https://docs.docker.com/install">Docker CE</a></li> + <li>Docker Compose. If you're using macOS and Windows, this already comes bundled with the previous installation. Otherwise, you can download it either from the <a href="https://docs.docker.com/compose/install">website</a>, or by running <code>pip install docker-compose</code>.</li> + </ul> + <p class="notification is-warning">If you get any Docker related errors, reference the <a href="../docker#possible-issues">Possible Issue</a> section of the Docker page.</p> + </div> + </div> +</div> +<br> + +In your `config.yml` file: + +* Set `urls.site` to `"web:8000"`. +* If you wish to work with snekbox set `urls.snekbox_eval_api` to `"http://snekbox:8060/eval"`. + +Assuming you have Docker installed **and running**, enter the cloned repo in the command line and type `docker-compose up`. + +After pulling the images and building the containers, your bot will start. Enter your server and type `!help` (or whatever prefix you chose instead of `!`). + +Your bot is now running, but this method makes debugging with an IDE a fairly involved process. For additional running methods, continue reading the following sections. + +#### With the Bot Running Locally +The advantage of this method is that you can run the bot's code in your preferred editor, with debugger and all, while keeping all the setup of the bot's various dependencies inside Docker. + +* Append the following line to your `.env` file: `BOT_API_KEY=badbot13m0n8f570f942013fc818f234916ca531`. +* In your `config.yml` file, set `urls.site` to `"localhost:8000"`. If you wish to keep using `web:8000`, then [COMPOSE_PROJECT_NAME](../docker/#compose-project-names) has to be set. +* To work with snekbox, set `urls.snekbox_eval_api` to `"http://localhost:8060/eval"` + +You will need to start the services separately, but if you got the previous section with Docker working, that's pretty simple: + +* `docker-compose up web` to start the site container. This is required. +* `docker-compose up snekbox` to start the snekbox container. You only need this if you're planning on working on the snekbox cog. +* `docker-compose up redis` to start the Redis container. You only need this if you're not using fakeredis. For more info refer to [Working with Redis](#optional-working-with-redis). + +You can start several services together: `docker-compose up web snekbox redis`. + +##### Setting Up a Development Environment +The bot's code is Python code like any other. To run it locally, you will need the right version of Python with the necessary packages installed: + +1. Make sure you have [Python 3.9](https://www.python.org/downloads/) installed. It helps if it is your system's default Python version. +2. [Install Poetry](https://github.com/python-poetry/poetry#installation). +3. [Install the dependencies](../installing-project-dependencies). + +With at least the site running in Docker already (see the previous section on how to start services separately), you can now start the bot locally through the command line, or through your preferred IDE. +<div class="card"> + <button type="button" class="card-header collapsible"> + <span class="card-header-title subtitle is-6 my-2 ml-2">Ways to run code</span> + <span class="card-header-icon"> + <i class="fas fa-angle-down title is-5" aria-hidden="true"></i> + </span> + </button> + <div class="collapsible-content"> + <div class="card-content"> + Notice that the bot is started as a module. There are several ways to do so: + <ul> + <li>Through the command line, inside the bot directory, with either <code>poetry run task start</code>, or directly <code>python -m bot</code>.</li> + <li>If using PyCharm, enter <code>Edit Configurations</code> and set everything according to this image: <img src="/static/images/content/contributing/pycharm_run_module.png"></li> + <li>If using Visual Studio Code, set the interpreter to the poetry environment you created. In <code>launch.json</code> create a new Python configuration, and set the name of the program to be run to <code>bot</code>. VSC will correctly run it as a module.</li> + </ul> + </div> + </div> +</div> +<br> + +#### With More Things Running Locally +You can run additional services on the host, but this guide won't go over how to install and start them in this way. +If possible, prefer to start the services through Docker to replicate the production environment as much as possible. + +The site, however, is a mandatory service for the bot. +Refer to the [previous section](#with-the-bot-running-locally) and the [site contributing guide](../site) to learn how to start it on the host, in which case you will need to change `urls.site` in `config.yml` to wherever the site is being hosted. --- +### Development Tips +Now that you have everything setup, it is finally time to make changes to the bot! -Example `.env` file: +#### Working with Git -```shell -BOT_TOKEN=YourDiscordBotTokenHere -BOT_API_KEY=badbot13m0n8f570f942013fc818f234916ca531 -REDDIT_CLIENT_ID=YourRedditClientIDHere -REDDIT_SECRET=YourRedditSecretHere -``` +If you have not yet [read the contributing guidelines](../contributing-guidelines), now is a good time. +Contributions that do not adhere to the guidelines may be rejected. ---- -# Run the project +Notably, version control of our projects is done using Git and Github. +It can be intimidating at first, so feel free to ask for any help in the server. -The bot can run with or without Docker. -When using Docker, the site, which is a prerequisite, can be automatically set up too. -If you don't use Docker, you have to first follow [the site guide](../site/) to set it up yourself. -The bot and site can be started using independent methods. -For example, the site could run with Docker and the bot could run directly on your system (AKA the _host_) or vice versa. +[**Click here to see the basic Git workflow when contributing to one of our projects.**](../working-with-git/) -## Run with Docker +#### Running tests -The following sections describe how to start either the site, bot, or both using Docker. -If you are not interested in using Docker, see [this page](../site/) for setting up the site and [this section](#run-on-the-host) for running the bot. +[This section](https://github.com/python-discord/bot/blob/main/tests/README.md#tools) of the README in the `tests` repository will explain how to run tests. +The whole document explains how unittesting works, and how it fits in the context of our project. -If you get any Docker related errors, reference the [Possible Issues](../docker#possible-issues) section of the Docker page. +Make sure to run tests *before* pushing code. -### Site and bot +Even if you run the bot through Docker, you might want to [setup a development environment](#setting-up-a-development-environment) in order to run the tests locally. -This method will start both the site and the bot using Docker. +#### Lint before you push +As mentioned in the [contributing guidelines](../contributing-guidelines), you should make sure your code passes linting for each commit you make. -Start the containers using Docker Compose while inside the root of the project directory: +For ease of development, you can install the pre-commit hook with `poetry run task precommit`, which will check your code every time you try to commit it. +For that purpose, even if you run the bot through Docker, you might want to [setup a development environment](#setting-up-a-development-environment), as otherwise the hook installation will fail. -```shell -docker-compose up -``` +#### Reloading parts of the bot +If you make changes to an extension, you might not need to restart the entire bot for the changes to take effect. The command `!ext reload <extension_name>` re-imports the files associated with the extension. +Invoke `!ext list` for a full list of the available extensions. In this bot in particular, cogs are defined inside extensions. -The `-d` option can be appended to the command to run in detached mode. -This runs the containers in the background so the current terminal session is available for use with other things. +Note that if you changed code that is not associated with a particular extension, such as utilities, converters, and constants, you will need to restart the bot. -### Site only +#### Adding new statistics -This method will start only the site using Docker. +Details on how to add new statistics can be found on the [statistic infrastructure page](https://blog.pythondiscord.com/statistics-infrastructure). +We are always open to more statistics so add as many as you can! -```shell -docker-compose up site -``` +--- -See [this section](#run-on-the-host) for how to start the bot on the host. +### Optional: Working with Redis +In [Configure the Bot](#configyml) you were asked to set `bot.redis.use_fakeredis` to `true`. If you do not need to work on features that rely on Redis, this is enough. Fakeredis will give the illusion that features relying on Redis are saving information properly, but restarting the bot or the specific cog will wipe that information. -### Bot only +If you are working on a feature that relies on Redis, you will need to enable Redis to make sure persistency is achieved for the feature across restarts. The first step towards that is going to `config.yml` and setting `bot.redis.use_fakeredis` to `false`. -This method will start only the bot using Docker. -The site has to have been started somehow beforehand. +#### Starting Redis in Docker (Recommended) +If you're using the Docker image provided in the project's Docker Compose, open your `config.yml` file. If you're running the bot in Docker, set `bot.redis.host` to `redis`, and if you're running it on the host set it to `localhost`. Set `bot.redis.password` to `null`. -Start the bot using Docker Compose while inside the root of the project directory: +#### Starting Redis Using Other Methods +You can run your own instance of Redis, but in that case you will need to correctly set `bot.redis.host` and `bot.redis.port`, and the `bot.redis.password` value in `config-default.yml` should not be overridden. Then, enter the `.env` file, and set `REDIS_PASSWORD` to whatever password you set. -```shell -docker-compose up --no-deps bot -``` +--- -## Run on the host +### Optional: Working with Metricity +[Metricity](https://github.com/python-discord/metricity) is our home-grown bot for collecting metrics on activity within the server, such as what users are present, and IDs of the messages they've sent. +Certain features in the Python bot rely on querying the Metricity database for information such as the number of messages a user has sent, most notably the voice verification system. -Running on the host is particularly useful if you wish to debug the bot. -The site has to have been started somehow beforehand. +If you wish to work on a feature that relies on Metricity, for your convenience we've made the process of using it relatively painless with Docker: Enter the `.env` file you've written for the Python bot, and append the line `USE_METRICITY=true`. +Note that if you don't need Metricity, there's no reason to have it enabled as it is just unnecessary overhead. -```shell -poetry run task start +To make the Metricity bot work with your test server, you will need to override its configurations similarly to the Python bot. +You can see the various configurations in [the Metricity repo](https://github.com/python-discord/metricity), but the bare minimum is the guild ID setting. +In your local version of the Python bot repo, create a file called `metricity-config.toml` and insert the following lines: +```yaml +[bot] +guild_id = replace_with_your_guild_id ``` +To properly replicate production behavior, set the `staff_role_id`, `staff_categories`, and `ignore_categories` fields as well. ---- -## Working with Git -Now that you have everything setup, it is finally time to make changes to the bot! -If you have not yet [read the contributing guidelines](../contributing-guidelines), now is a good time. -Contributions that do not adhere to the guidelines may be rejected. +Now, `docker-compose up` will also start Metricity. -Notably, version control of our projects is done using Git and Github. -It can be intimidating at first, so feel free to ask for any help in the server. +If you want to run the bot locally, you can run `docker-compose up metricity` instead. -[**Click here to see the basic Git workflow when contributing to one of our projects.**](../working-with-git/) +--- -## Adding new statistics +### Issues? +If you have any issues with setting up the bot, come discuss it with us on the [#dev-contrib](https://discord.gg/2h3qBv8Xaa) channel on our server. -Details on how to add new statistics can be found on the [statistic infrastructure page](https://blog.pythondiscord.com/statistics-infrastructure). -We are always open to more statistics so add as many as you can! +If you find any bugs in the bot or would like to request a feature, feel free to open an issue on the repository. -## Running tests +--- -[This section](https://github.com/python-discord/bot/blob/main/tests/README.md#tools) of the README in the `tests` repository will explain how to run tests. -The whole document explains how unittesting works, and how it fits in the context of our project. +### Appendix: Full ENV File Options +The following is a list of all available environment variables used by the bot: + +| Variable | Required | Description | +| -------- | -------- | -------- | +| `BOT_TOKEN` | Always | Your Discord bot account's token (see [Set Up a Bot Account](#set-up-a-bot-account)). | +| `BOT_API_KEY` | When running bot without Docker | Used to authenticate with the site's API. When using Docker to run the bot, this is automatically set. By default, the site will always have the API key shown in the example below. | +| `BOT_SENTRY_DSN` | When connecting the bot to sentry | The DSN of the sentry monitor. | +| `BOT_TRACE_LOGGERS ` | When you wish to see specific or all trace logs | Comma separated list that specifies which loggers emit trace logs through the listed names. If the ! prefix is used, all of the loggers except the listed ones are set to the trace level. If * is used, the root logger is set to the trace level. | +| `BOT_DEBUG` | In production | `true` or `false`, depending on whether to enable debug mode, affecting the behavior of certain features. `true` by default. +| `REDIS_PASSWORD` | When not using FakeRedis | The password to connect to the Redis database (see [Optional: Working with Redis](#optional-working-with-redis)). | +| `USE_METRICITY` | When using Metricity | `true` or `false`, depending on whether to enable metrics collection using Metricity (see [Optional: Working with Metricity](#optional-working-with-metricity)). `false` by default. | +| `GITHUB_API_KEY` | When you wish to interact with GitHub | The API key to interact with GitHub, for example to download files for the branding manager. +| `METABASE_USERNAME` | When you wish to interact with Metabase | The username for a Metabase admin account. +| `METABASE_PASSWORD` | When you wish to interact with Metabase | The password for a Metabase admin account. Have fun! diff --git a/pydis_site/apps/content/resources/guides/pydis-guides/contributing/contributing-guidelines/supplemental-information.md b/pydis_site/apps/content/resources/guides/pydis-guides/contributing/contributing-guidelines/supplemental-information.md index 70d47563..e64e4fc6 100644 --- a/pydis_site/apps/content/resources/guides/pydis-guides/contributing/contributing-guidelines/supplemental-information.md +++ b/pydis_site/apps/content/resources/guides/pydis-guides/contributing/contributing-guidelines/supplemental-information.md @@ -90,7 +90,7 @@ Our projects currently defines logging levels as follows, from lowest to highest - **ERROR:** These events can cause a failure in a specific part of the application and require urgent attention. - **CRITICAL:** These events can cause the whole application to fail and require immediate intervention. -Any logging above the **INFO** level will trigger a [Sentry](http://sentry.io) issue and alert the Core Developer team. +Any logging above the **INFO** level will trigger a [Sentry](https://sentry.io) issue and alert the Core Developer team. ## Draft Pull Requests diff --git a/pydis_site/apps/content/resources/guides/pydis-guides/contributing/creating-bot-account.md b/pydis_site/apps/content/resources/guides/pydis-guides/contributing/creating-bot-account.md new file mode 100644 index 00000000..ee38baa3 --- /dev/null +++ b/pydis_site/apps/content/resources/guides/pydis-guides/contributing/creating-bot-account.md @@ -0,0 +1,17 @@ +--- +title: Setting up a Bot Account +description: How to set up a bot account. +icon: fab fa-discord +--- +1. Go to the [Discord Developers Portal](https://discordapp.com/developers/applications/). +2. Click on the `New Application` button, enter your desired bot name, and click `Create`. +3. In your new application, go to the `Bot` tab, click `Add Bot`, and confirm `Yes, do it!` +4. Change your bot's `Public Bot` setting off so only you can invite it, save, and then get your **Bot Token** with the `Copy` button. +> **Note:** **DO NOT** post your bot token anywhere public. If you do it can and will be compromised. +5. Save your **Bot Token** somewhere safe to use in the project settings later. +6. In the `General Information` tab, grab the **Client ID**. +7. Replace `<CLIENT_ID_HERE>` in the following URL and visit it in the browser to invite your bot to your new test server. +```plaintext +https://discordapp.com/api/oauth2/authorize?client_id=<CLIENT_ID_HERE>&permissions=8&scope=bot +``` +Optionally, you can generate your own invite url in the `OAuth` tab, after selecting `bot` as the scope. diff --git a/pydis_site/apps/content/resources/guides/pydis-guides/contributing/hosts-file.md b/pydis_site/apps/content/resources/guides/pydis-guides/contributing/hosts-file.md index 5d55a7f3..bba5722d 100644 --- a/pydis_site/apps/content/resources/guides/pydis-guides/contributing/hosts-file.md +++ b/pydis_site/apps/content/resources/guides/pydis-guides/contributing/hosts-file.md @@ -8,16 +8,13 @@ toc: 3 # What's a hosts file? The hosts file maps a hostname/domain to an IP address, allowing you to visit a given domain on your browser and have it resolve by your system to the given IP address, even if it's pointed back to your own system or network. -When staging a local [Site](https://pythondiscord.com/pages/contributing/site/) project, you will need to add some entries to your hosts file so you can visit the site with the domain `http://pythondiscord.local` +When staging a local [Site](https://pythondiscord.com/pages/contributing/site/) project, you may want to add an entries to your hosts file so you can visit the site with the domain `http://pythondiscord.local`. This is purely for convenience, and you can use `localhost` or `127.0.0.1` instead if you prefer. # What to add -You would add the following entries to your hosts file. +You would add the following entry to your hosts file. ```plaintext 127.0.0.1 pythondiscord.local -127.0.0.1 api.pythondiscord.local -127.0.0.1 staff.pythondiscord.local -127.0.0.1 admin.pythondiscord.local ``` # How to add it diff --git a/pydis_site/apps/content/resources/guides/pydis-guides/contributing/issues.md b/pydis_site/apps/content/resources/guides/pydis-guides/contributing/issues.md index 9151e5e3..0c6d3513 100644 --- a/pydis_site/apps/content/resources/guides/pydis-guides/contributing/issues.md +++ b/pydis_site/apps/content/resources/guides/pydis-guides/contributing/issues.md @@ -56,7 +56,7 @@ Definitely try to: Labels allow us to better organise Issues by letting us view what type of Issue it is, how it might impact the codebase and at what stage it's at. -In our repositories, we try to prefix labels belonging to the same group, for example the label groups `status` or `type`. We will be trying to keep to the same general structure across our project repositories, but just have a look at the full lables list in the respective repository to get a clear idea what's available. +In our repositories, we try to prefix labels belonging to the same group, for example the label groups `status` or `type`. We will be trying to keep to the same general structure across our project repositories, but just have a look at the full labels list in the respective repository to get a clear idea what's available. If you're a contributor, you can add relevant labels yourself to any new Issue ticket you create. diff --git a/pydis_site/apps/content/resources/guides/pydis-guides/contributing/obtaining-discord-ids.md b/pydis_site/apps/content/resources/guides/pydis-guides/contributing/obtaining-discord-ids.md new file mode 100644 index 00000000..afa07b5a --- /dev/null +++ b/pydis_site/apps/content/resources/guides/pydis-guides/contributing/obtaining-discord-ids.md @@ -0,0 +1,42 @@ +--- +title: Obtaining Discord IDs +description: How to obtain Discord IDs to set up the bots. +icon: fab fa-discord +--- +First, enable developer mode in your client so you can easily copy IDs. + +1. Go to your `User Settings` and click on the `Appearance` tab. +2. Under `Advanced`, enable `Developer Mode`. + +#### Guild ID + +Right click the server icon and click `Copy ID`. + +#### Channel ID + +Right click a channel name and click `Copy ID`. + +#### Role ID + +Right click a role and click `Copy ID`. +The easiest way to do this is by going to the role list in the guild's settings. + +#### Emoji ID + +Insert the emoji into the Discord text box, then add a backslash (`\`) right before the emoji and send the message. +The result should be similar to the following + +```plaintext +<:bbmessage:511950877733552138> +``` + +The long number you see, in this case `511950877733552138`, is the emoji's ID. + +#### Webhook ID + +Once a [webhook](https://support.discordapp.com/hc/en-us/articles/228383668-Intro-to-Webhooks) is created, the ID is found in the penultimate part of the URL. +For example, in the following URL, `661995360146817053` is the ID of the webhook. + +```plaintext +https://discordapp.com/api/webhooks/661995360146817053/t-9mI2VehOGcPuPS_F8R-6mB258Ob6K7ifhtoxerCvWyM9VEQug-anUr4hCHzdbhzfbz +``` diff --git a/pydis_site/apps/content/resources/guides/pydis-guides/contributing/setting-test-server-and-bot-account.md b/pydis_site/apps/content/resources/guides/pydis-guides/contributing/setting-test-server-and-bot-account.md index c14fe50d..43d1c8f5 100644 --- a/pydis_site/apps/content/resources/guides/pydis-guides/contributing/setting-test-server-and-bot-account.md +++ b/pydis_site/apps/content/resources/guides/pydis-guides/contributing/setting-test-server-and-bot-account.md @@ -18,13 +18,11 @@ icon: fab fa-discord 4. Change your bot's `Public Bot` setting off so only you can invite it, save, and then get your **Bot Token** with the `Copy` button. > **Note:** **DO NOT** post your bot token anywhere public, or it can and will be compromised. 5. Save your **Bot Token** somewhere safe to use in the project settings later. -6. In the `General Information` tab, grab the **Client ID**. +6. In the `OAuth2` tab, grab the **Client ID**. 7. Replace `<CLIENT_ID_HERE>` in the following URL and visit it in the browser to invite your bot to your new test server. ```plaintext https://discordapp.com/api/oauth2/authorize?client_id=<CLIENT_ID_HERE>&permissions=8&scope=bot ``` -Optionally, you can generate your own invite url in the `OAuth` tab, after selecting `bot` as the scope. - --- ## Obtain the IDs diff --git a/pydis_site/apps/content/resources/guides/pydis-guides/contributing/sir-lancebot.md b/pydis_site/apps/content/resources/guides/pydis-guides/contributing/sir-lancebot.md index 068b08ae..e3cd8f0c 100644 --- a/pydis_site/apps/content/resources/guides/pydis-guides/contributing/sir-lancebot.md +++ b/pydis_site/apps/content/resources/guides/pydis-guides/contributing/sir-lancebot.md @@ -16,6 +16,20 @@ toc: 1 - [MacOS Installer](https://git-scm.com/download/mac) or `brew install git` - [Linux](https://git-scm.com/download/linux) +## Using Gitpod +Sir Lancebot can be edited and tested on Gitpod. Gitpod will automatically install the correct dependencies and Python version, so you can get straight to coding. + +To do this, you will need a Gitpod account, which you can get [here](https://www.gitpod.io/#get-started), and a fork of Sir Lancebot. This guide covers forking the repository [here](#fork-the-project). + +Afterwards, click on [this link](https://gitpod.io/#/github.com/python-discord/sir-lancebot) to spin up a new workspace for Sir Lancebot. Then run the following commands in the terminal after the existing tasks have finished running: +```sh +git remote rename origin upstream +git add remote origin https://github.com/{your_username}/sir-lancebot +``` +Make sure you replace `{your_username}` with your Github username. These commands will set the Sir Lancebot repository as the secondary remote, and your fork as the primary remote. This means you can easily grab new changes from the main Sir Lancebot repository. + +Once you've set up [a test server and bot account](#test-server-and-bot-account) and your [environment variables](#environment-variables), you are ready to begin contributing to Sir Lancebot! + ## Using Docker Sir Lancebot can be started using Docker. Using Docker is generally recommended (but not strictly required) because it abstracts away some additional set up work. @@ -87,7 +101,7 @@ Otherwise, please see the below linked guide for Redis related variables. --- # Run the project -The sections below describe the two ways you can run this project. We recomend Docker as it requires less setup. +The sections below describe the two ways you can run this project. We recommend Docker as it requires less setup. ## Run with Docker Make sure to have Docker running, then use the Docker command `docker-compose up` in the project root. diff --git a/pydis_site/apps/content/resources/guides/pydis-guides/contributing/sir-lancebot/env-var-reference.md b/pydis_site/apps/content/resources/guides/pydis-guides/contributing/sir-lancebot/env-var-reference.md index 2a7ef0d6..51587aac 100644 --- a/pydis_site/apps/content/resources/guides/pydis-guides/contributing/sir-lancebot/env-var-reference.md +++ b/pydis_site/apps/content/resources/guides/pydis-guides/contributing/sir-lancebot/env-var-reference.md @@ -26,7 +26,7 @@ Additionally, you may find the following environment variables useful during dev | `BOT_DEBUG` | Debug mode of the bot | False | | `PREFIX` | The bot's invocation prefix | `.` | | `CYCLE_FREQUENCY` | Amount of days between cycling server icon | 3 | -| `MONTH_OVERRIDE` | Interger in range `[0, 12]`, overrides current month w.r.t. seasonal decorators | +| `MONTH_OVERRIDE` | Integer in range `[0, 12]`, overrides current month w.r.t. seasonal decorators | | `REDIS_HOST` | The address to connect to for the Redis database. | | `REDIS_PORT` | | | `REDIS_PASSWORD` | | @@ -43,7 +43,7 @@ If you will be working with an external service, you might have to set one of th | -------- | -------- | | `GITHUB_TOKEN` | Personal access token for GitHub, raises rate limits from 60 to 5000 requests per hour. | | `GIPHY_TOKEN` | Required for API access. [Docs](https://developers.giphy.com/docs/api) | -| `OMDB_API_KEY` | Required for API access. [Docs](http://www.omdbapi.com/) | +| `OMDB_API_KEY` | Required for API access. [Docs](https://www.omdbapi.com/) | | `REDDIT_CLIENT_ID` | OAuth2 client ID for authenticating with the [reddit API](https://github.com/reddit-archive/reddit/wiki/OAuth2). | | `REDDIT_SECRET` | OAuth2 secret for authenticating with the reddit API. *Leave empty if you're not using the reddit API.* | | `REDDIT_WEBHOOK` | Webhook ID for Reddit channel | @@ -61,10 +61,10 @@ These variables might come in handy while working on certain cogs: | Cog | Environment Variable | Description | | -------- | -------- | -------- | -| Advent of Code | `AOC_LEADERBOARDS` | List of leaderboards seperated by `::`. Each entry should have an `id,session cookie,join code` seperated by commas in that order. | +| Advent of Code | `AOC_LEADERBOARDS` | List of leaderboards separated by `::`. Each entry should have an `id,session cookie,join code` separated by commas in that order. | | Advent of Code | `AOC_STAFF_LEADERBOARD_ID` | Integer ID of the staff leaderboard. | | Advent of Code | `AOC_ROLE_ID` | ID of the advent of code role. -| Advent of Code | `AOC_IGNORED_DAYS` | Comma seperated list of days to ignore while calulating score. | +| Advent of Code | `AOC_IGNORED_DAYS` | Comma separated list of days to ignore while calculating score. | | Advent of Code | `AOC_YEAR` | Debug variable to change the year used for AoC. | | Advent of Code | `AOC_CHANNEL_ID` | The ID of the #advent-of-code channel | | Advent of Code | `AOC_COMMANDS_CHANNEL_ID` | The ID of the #advent-of-code-commands channel | diff --git a/pydis_site/apps/content/resources/guides/pydis-guides/contributing/site.md b/pydis_site/apps/content/resources/guides/pydis-guides/contributing/site.md index 24227f24..f2c3bd95 100644 --- a/pydis_site/apps/content/resources/guides/pydis-guides/contributing/site.md +++ b/pydis_site/apps/content/resources/guides/pydis-guides/contributing/site.md @@ -43,7 +43,6 @@ For any Core Developers, since you have write permissions already to the origina 1. [Clone your fork to a local project directory](../cloning-repository/) 2. [Install the project's dependencies](../installing-project-dependencies/) -3. [Prepare your hosts file](../hosts-file/) ## Without Docker @@ -84,7 +83,7 @@ detailed information about these settings. #### Notes regarding `DATABASE_URL` -- If the database is hosted locally i.e. on the same machine as the webserver, then use `localhost` for the host. Windows and macOS users may need to use the [Docker host IP](../hosts-file/#windows) instead. +- If the database is hosted locally i.e. on the same machine as the webserver, then use `localhost` for the host. Windows and macOS users may need to use the [Docker host IP](https://stackoverflow.com/questions/22944631/how-to-get-the-ip-address-of-the-docker-host-from-inside-a-docker-container) instead. - If the database is running in Docker, use port `7777`. Otherwise, use `5432` as that is the default port used by PostegreSQL. - If you configured PostgreSQL in a different manner or you are not hosting it locally, then you will need to determine the correct host and port yourself. The user, password, and database name should all still be `pysite` unless you deviated from the setup instructions in the previous section. @@ -109,7 +108,7 @@ If you get any Docker related errors, reference the [Possible Issues](https://py ## Run on the host -Running on the host is particularily useful if you wish to debug the site. The [environment variables](#2-environment-variables) shown in a previous section need to have been configured. +Running on the host is particularly useful if you wish to debug the site. The [environment variables](#2-environment-variables) shown in a previous section need to have been configured. ### Database @@ -145,7 +144,7 @@ Unless you are editing the Dockerfile or docker-compose.yml, you shouldn't need Django provides an interface for administration with which you can view and edit the models among other things. -It can be found at [http://admin.pythondiscord.local:8000](http://admin.pythondiscord.local:8000). The default credentials are `admin` for the username and `admin` for the password. +It can be found at [http://127.0.0.1:8000/admin/](http://127.0.0.1:8000/admin/). The default credentials are `admin` for the username and `admin` for the password. --- diff --git a/pydis_site/apps/content/resources/guides/pydis-guides/contributing/working-with-git/cli.md b/pydis_site/apps/content/resources/guides/pydis-guides/contributing/working-with-git/cli.md index 5f196837..94f94d57 100644 --- a/pydis_site/apps/content/resources/guides/pydis-guides/contributing/working-with-git/cli.md +++ b/pydis_site/apps/content/resources/guides/pydis-guides/contributing/working-with-git/cli.md @@ -27,7 +27,7 @@ If you use SSH, use `[email protected]:python-discord/sir-lancebot.git` for the ups You will be committing your changes to a new branch rather than to `main`. Using branches allows you to work on muiltiple pull requests without conflicts. -You can name your branch whatever you want, but it's recommended to name it something succint and relevant to the changes you will be making. +You can name your branch whatever you want, but it's recommended to name it something succinct and relevant to the changes you will be making. Run the following commands to create a new branch. Replace `branch_name` with the name you wish to give your branch. ```sh diff --git a/pydis_site/apps/content/resources/guides/pydis-guides/contributing/working-with-git/pycharm.md b/pydis_site/apps/content/resources/guides/pydis-guides/contributing/working-with-git/pycharm.md index 3f7fefa0..e0b2e33c 100644 --- a/pydis_site/apps/content/resources/guides/pydis-guides/contributing/working-with-git/pycharm.md +++ b/pydis_site/apps/content/resources/guides/pydis-guides/contributing/working-with-git/pycharm.md @@ -27,7 +27,7 @@ The following will use the [Sir-Lancebot](https://github.com/python-discord/sir- ## Creating a New Branch > You will be committing your changes to a new branch rather than to `main`. Using branches allows you to work on multiple pull requests at the same time without conflicts. -> You can name your branch whatever you want, but it's recommended to name it something succint and relevant to the changes you will be making. +> You can name your branch whatever you want, but it's recommended to name it something succinct and relevant to the changes you will be making. > Before making new branches, be sure to checkout the `main` branch and ensure it's up to date. diff --git a/pydis_site/apps/content/resources/guides/pydis-guides/helping-others.md b/pydis_site/apps/content/resources/guides/pydis-guides/helping-others.md index d126707d..a7f1ce1d 100644 --- a/pydis_site/apps/content/resources/guides/pydis-guides/helping-others.md +++ b/pydis_site/apps/content/resources/guides/pydis-guides/helping-others.md @@ -112,7 +112,7 @@ Presenting a solution that is considered a bad practice might be useful in certa > for i in range(len(your_list)): > print(your_list[i]) > -> The second replier gave a valid solution, but it's important that he clarifies that it is concidered a bad practice in Python, and that the first solution should usually be used in this case. +> The second replier gave a valid solution, but it's important that he clarifies that it is considered a bad practice in Python, and that the first solution should usually be used in this case. ## It's OK to Step Away diff --git a/pydis_site/apps/content/resources/rules.md b/pydis_site/apps/content/resources/rules.md index 05d0e93a..ef6cc4d1 100644 --- a/pydis_site/apps/content/resources/rules.md +++ b/pydis_site/apps/content/resources/rules.md @@ -43,4 +43,7 @@ The possible actions we take based on infractions can include the following: While we do discuss more serious matters internally before handing out a punishment, simpler infractions are dealt with directly by individual staffers and the punishment they hand out is left to their own discretion. -If you receive an infraction and would like to appeal it, send an e-mail to [[email protected]](mailto:[email protected]). +If you wish to appeal a decision or action taken by the moderation team, you can do so in one of the following ways: + +* By sending a direct message (DM) to ModMail from our Discord server. +* Joining our [ban appeals server](https://discord.gg/WXrCJxWBnm) and sending a direct message (DM) to the Ban Appeals bot. diff --git a/pydis_site/apps/content/tests/test_views.py b/pydis_site/apps/content/tests/test_views.py index b6e752d6..eadad7e3 100644 --- a/pydis_site/apps/content/tests/test_views.py +++ b/pydis_site/apps/content/tests/test_views.py @@ -167,11 +167,16 @@ class PageOrCategoryViewTests(MockPagesTestCase, SimpleTestCase, TestCase): self.ViewClass.dispatch(request, location="category/subcategory/with_metadata") context = self.ViewClass.get_context_data() + + # Convert to paths to avoid dealing with non-standard path separators + for item in context["breadcrumb_items"]: + item["path"] = Path(item["path"]) + self.assertEquals( context["breadcrumb_items"], [ - {"name": PARSED_CATEGORY_INFO["title"], "path": "."}, - {"name": PARSED_CATEGORY_INFO["title"], "path": "category"}, - {"name": PARSED_CATEGORY_INFO["title"], "path": "category/subcategory"}, + {"name": PARSED_CATEGORY_INFO["title"], "path": Path(".")}, + {"name": PARSED_CATEGORY_INFO["title"], "path": Path("category")}, + {"name": PARSED_CATEGORY_INFO["title"], "path": Path("category/subcategory")}, ] ) diff --git a/pydis_site/apps/content/urls.py b/pydis_site/apps/content/urls.py index c11b222a..fe7c2852 100644 --- a/pydis_site/apps/content/urls.py +++ b/pydis_site/apps/content/urls.py @@ -1,9 +1,46 @@ -from django.urls import path +import typing +from pathlib import Path + +from django_distill import distill_path from . import views app_name = "content" + + +def __get_all_files(root: Path, folder: typing.Optional[Path] = None) -> list[str]: + """Find all folders and markdown files recursively starting from `root`.""" + if not folder: + folder = root + + results = [] + + for item in folder.iterdir(): + name = item.relative_to(root).__str__().replace("\\", "/") + + if item.is_dir(): + results.append(name) + results.extend(__get_all_files(root, item)) + else: + path, extension = name.rsplit(".", maxsplit=1) + if extension == "md": + results.append(path) + + return results + + +def get_all_pages() -> typing.Iterator[dict[str, str]]: + """Yield a dict of all pag categories.""" + for location in __get_all_files(Path("pydis_site", "apps", "content", "resources")): + yield {"location": location} + + urlpatterns = [ - path("", views.PageOrCategoryView.as_view(), name='pages'), - path("<path:location>/", views.PageOrCategoryView.as_view(), name='page_category'), + distill_path("", views.PageOrCategoryView.as_view(), name='pages'), + distill_path( + "<path:location>/", + views.PageOrCategoryView.as_view(), + name='page_category', + distill_func=get_all_pages + ), ] diff --git a/pydis_site/apps/events/tests/test_views.py b/pydis_site/apps/events/tests/test_views.py index 23c9e596..669fbf82 100644 --- a/pydis_site/apps/events/tests/test_views.py +++ b/pydis_site/apps/events/tests/test_views.py @@ -2,7 +2,7 @@ from pathlib import Path from django.conf import settings from django.test import TestCase, override_settings -from django_hosts.resolvers import reverse +from django.urls import reverse PAGES_PATH = Path(settings.BASE_DIR, "pydis_site", "templates", "events", "test-pages") @@ -21,8 +21,8 @@ class PageTests(TestCase): def test_valid_event_page_reponse_200(self): """Should return response code 200 when visiting valid event page.""" pages = ( - reverse("events:page", ("my-event",)), - reverse("events:page", ("my-event/subpage",)), + reverse("events:page", args=("my-event",)), + reverse("events:page", args=("my-event/subpage",)), ) for page in pages: with self.subTest(page=page): @@ -33,8 +33,8 @@ class PageTests(TestCase): def test_invalid_event_page_404(self): """Should return response code 404 when visiting invalid event page.""" pages = ( - reverse("events:page", ("invalid",)), - reverse("events:page", ("invalid/invalid",)) + reverse("events:page", args=("invalid",)), + reverse("events:page", args=("invalid/invalid",)) ) for page in pages: with self.subTest(page=page): diff --git a/pydis_site/apps/events/urls.py b/pydis_site/apps/events/urls.py index 9a65cf1f..7ea65a31 100644 --- a/pydis_site/apps/events/urls.py +++ b/pydis_site/apps/events/urls.py @@ -1,9 +1,38 @@ -from django.urls import path +import typing +from pathlib import Path + +from django_distill import distill_path from pydis_site.apps.events.views import IndexView, PageView app_name = "events" + + +def __get_all_files(root: Path, folder: typing.Optional[Path] = None) -> list[str]: + """Find all folders and HTML files recursively starting from `root`.""" + if not folder: + folder = root + + results = [] + + for sub_folder in folder.iterdir(): + results.append( + sub_folder.relative_to(root).__str__().replace("\\", "/").replace(".html", "") + ) + + if sub_folder.is_dir(): + results.extend(__get_all_files(root, sub_folder)) + + return results + + +def get_all_events() -> typing.Iterator[dict[str, str]]: + """Yield a dict of all event pages.""" + for file in __get_all_files(Path("pydis_site", "templates", "events", "pages")): + yield {"path": file} + + urlpatterns = [ - path("", IndexView.as_view(), name="index"), - path("<path:path>/", PageView.as_view(), name="page"), + distill_path("", IndexView.as_view(), name="index"), + distill_path("<path:path>/", PageView.as_view(), name="page", distill_func=get_all_events), ] diff --git a/pydis_site/apps/home/tests/test_views.py b/pydis_site/apps/home/tests/test_views.py index bd1671b1..b1215df4 100644 --- a/pydis_site/apps/home/tests/test_views.py +++ b/pydis_site/apps/home/tests/test_views.py @@ -1,10 +1,10 @@ from django.test import TestCase -from django_hosts.resolvers import reverse +from django.urls import reverse class TestIndexReturns200(TestCase): def test_index_returns_200(self): """Check that the index page returns a HTTP 200 response.""" - url = reverse('home') + url = reverse('home:home') resp = self.client.get(url) self.assertEqual(resp.status_code, 200) diff --git a/pydis_site/apps/home/urls.py b/pydis_site/apps/home/urls.py index bb77220b..30321ece 100644 --- a/pydis_site/apps/home/urls.py +++ b/pydis_site/apps/home/urls.py @@ -1,16 +1,9 @@ -from django.contrib import admin -from django.urls import include, path +from django_distill import distill_path from .views import HomeView, timeline app_name = 'home' urlpatterns = [ - path('', HomeView.as_view(), name='home'), - path('', include('pydis_site.apps.redirect.urls')), - path('', include('django_prometheus.urls')), - path('admin/', admin.site.urls), - path('resources/', include('pydis_site.apps.resources.urls')), - path('pages/', include('pydis_site.apps.content.urls')), - path('events/', include('pydis_site.apps.events.urls', namespace='events')), - path('timeline/', timeline, name="timeline"), + distill_path('', HomeView.as_view(), name='home'), + distill_path('timeline/', timeline, name="timeline"), ] diff --git a/pydis_site/apps/home/views/home.py b/pydis_site/apps/home/views/home.py index 401c768f..e28a3a00 100644 --- a/pydis_site/apps/home/views/home.py +++ b/pydis_site/apps/home/views/home.py @@ -8,6 +8,7 @@ from django.shortcuts import render from django.utils import timezone from django.views import View +from pydis_site import settings from pydis_site.apps.home.models import RepositoryMetadata from pydis_site.constants import GITHUB_TOKEN, TIMEOUT_PERIOD @@ -32,7 +33,10 @@ class HomeView(View): def __init__(self): """Clean up stale RepositoryMetadata.""" - RepositoryMetadata.objects.exclude(repo_name__in=self.repos).delete() + self._static_build = settings.env("STATIC_BUILD") + + if not self._static_build: + RepositoryMetadata.objects.exclude(repo_name__in=self.repos).delete() # If no token is defined (for example in local development), then # it does not make sense to pass the Authorization header. More @@ -91,10 +95,13 @@ class HomeView(View): def _get_repo_data(self) -> List[RepositoryMetadata]: """Build a list of RepositoryMetadata objects that we can use to populate the front page.""" # First off, load the timestamp of the least recently updated entry. - last_update = ( - RepositoryMetadata.objects.values_list("last_updated", flat=True) - .order_by("last_updated").first() - ) + if self._static_build: + last_update = None + else: + last_update = ( + RepositoryMetadata.objects.values_list("last_updated", flat=True) + .order_by("last_updated").first() + ) # If we did not retrieve any results here, we should import them! if last_update is None: @@ -104,7 +111,7 @@ class HomeView(View): api_repositories = self._get_api_data() # Create all the repodata records in the database. - return RepositoryMetadata.objects.bulk_create( + data = [ RepositoryMetadata( repo_name=api_data["full_name"], description=api_data["description"], @@ -113,7 +120,12 @@ class HomeView(View): language=api_data["language"], ) for api_data in api_repositories.values() - ) + ] + + if settings.env("STATIC_BUILD"): + return data + else: + return RepositoryMetadata.objects.bulk_create(data) # If the data is stale, we should refresh it. if (timezone.now() - last_update).seconds > self.repository_cache_ttl: diff --git a/pydis_site/apps/redirect/redirects.yaml b/pydis_site/apps/redirect/redirects.yaml index ea91ce03..8022e7bf 100644 --- a/pydis_site/apps/redirect/redirects.yaml +++ b/pydis_site/apps/redirect/redirects.yaml @@ -178,7 +178,7 @@ events_game_jams_twenty_twenty_rules_redirect: redirect_arguments: ["game-jams/2020/rules"] events_game_jams_twenty_twenty_technical_requirements_redirect: - original_path: pages/events/game-jam-2020/technical-requirements + original_path: pages/events/game-jam-2020/technical-requirements/ redirect_route: "events:page" redirect_arguments: ["game-jams/2020/technical-requirements"] diff --git a/pydis_site/apps/redirect/tests.py b/pydis_site/apps/redirect/tests.py index 2cfa3478..c181d6e5 100644 --- a/pydis_site/apps/redirect/tests.py +++ b/pydis_site/apps/redirect/tests.py @@ -31,7 +31,7 @@ class RedirectTests(TestCase): ): resp = self.client.get( reverse( - f"home:redirect:{name}", + f"redirect:{name}", args=TESTING_ARGUMENTS.get(name, ()) ), follow=True @@ -53,7 +53,7 @@ class RedirectTests(TestCase): self.assertRedirects( resp, reverse( - f"home:{data['redirect_route']}", + f"{data['redirect_route']}", args=expected_args ), status_code=301 diff --git a/pydis_site/apps/redirect/urls.py b/pydis_site/apps/redirect/urls.py index 6187af17..f7ddf45b 100644 --- a/pydis_site/apps/redirect/urls.py +++ b/pydis_site/apps/redirect/urls.py @@ -1,19 +1,105 @@ +import dataclasses +import re + import yaml -from django.conf import settings -from django.urls import path +from django import conf +from django.urls import URLPattern, path +from django_distill import distill_path +from pydis_site import settings +from pydis_site.apps.content import urls as pages_urls from pydis_site.apps.redirect.views import CustomRedirectView +from pydis_site.apps.resources import urls as resources_urls app_name = "redirect" -urlpatterns = [ - path( - data["original_path"], - CustomRedirectView.as_view( - pattern_name=data["redirect_route"], - static_args=tuple(data.get("redirect_arguments", ())), - prefix_redirect=data.get("prefix_redirect", False) - ), - name=name - ) - for name, data in yaml.safe_load(settings.REDIRECTIONS_PATH.read_text()).items() -] + + +__PARAMETER_REGEX = re.compile(r"<\w+:\w+>") +REDIRECT_TEMPLATE = "<meta http-equiv=\"refresh\" content=\"0; URL={url}\"/>" + + [email protected](frozen=True) +class Redirect: + """Metadata about a redirect route.""" + + original_path: str + redirect_route: str + redirect_arguments: tuple[str] = tuple() + + prefix_redirect: bool = False + + +def map_redirect(name: str, data: Redirect) -> list[URLPattern]: + """Return a pattern using the Redirects app, or a static HTML redirect for static builds.""" + if not settings.env("STATIC_BUILD"): + # Normal dynamic redirect + return [path( + data.original_path, + CustomRedirectView.as_view( + pattern_name=data.redirect_route, + static_args=tuple(data.redirect_arguments), + prefix_redirect=data.prefix_redirect + ), + name=name + )] + + # Create static HTML redirects for static builds + new_app_name = data.redirect_route.split(":")[0] + + if __PARAMETER_REGEX.search(data.original_path): + # Redirects for paths which accept parameters + # We generate an HTML redirect file for all possible entries + paths = [] + + class RedirectFunc: + def __init__(self, new_url: str, _name: str): + self.result = REDIRECT_TEMPLATE.format(url=new_url) + self.__qualname__ = _name + + def __call__(self, *args, **kwargs): + return self.result + + if new_app_name == resources_urls.app_name: + items = resources_urls.get_all_resources() + elif new_app_name == pages_urls.app_name: + items = pages_urls.get_all_pages() + else: + raise ValueError(f"Unknown app in redirect: {new_app_name}") + + for item in items: + entry = list(item.values())[0] + + # Replace dynamic redirect with concrete path + concrete_path = __PARAMETER_REGEX.sub(entry, data.original_path) + new_redirect = f"/{new_app_name}/{entry}" + pattern_name = f"{name}_{entry}" + + paths.append(distill_path( + concrete_path, + RedirectFunc(new_redirect, pattern_name), + name=pattern_name + )) + + return paths + + else: + redirect_path_name = "pages" if new_app_name == "content" else new_app_name + if len(data.redirect_arguments) > 0: + redirect_arg = data.redirect_arguments[0] + else: + redirect_arg = "resources/" + new_redirect = f"/{redirect_path_name}/{redirect_arg}" + + if new_redirect == "/resources/resources/": + new_redirect = "/resources/" + + return [distill_path( + data.original_path, + lambda *args: REDIRECT_TEMPLATE.format(url=new_redirect), + name=name, + )] + + +urlpatterns = [] +for _name, _data in yaml.safe_load(conf.settings.REDIRECTIONS_PATH.read_text()).items(): + urlpatterns.extend(map_redirect(_name, Redirect(**_data))) diff --git a/pydis_site/apps/resources/resources/byte_of_python.yaml b/pydis_site/apps/resources/resources/byte_of_python.yaml index 5f6788e7..d2b8fa35 100644 --- a/pydis_site/apps/resources/resources/byte_of_python.yaml +++ b/pydis_site/apps/resources/resources/byte_of_python.yaml @@ -5,7 +5,7 @@ name: A Byte of Python title_url: https://python.swaroopch.com/ urls: - icon: regular/book - url: http://www.lulu.com/shop/swaroop-c-h/a-byte-of-python/paperback/product-21142968.html + url: https://www.lulu.com/shop/swaroop-c-h/a-byte-of-python/paperback/product-21142968.html color: black - icon: branding/goodreads url: https://www.goodreads.com/book/show/6762544-a-byte-of-python diff --git a/pydis_site/apps/resources/resources/panda3d.yaml b/pydis_site/apps/resources/resources/panda3d.yaml index 27c35244..0d488565 100644 --- a/pydis_site/apps/resources/resources/panda3d.yaml +++ b/pydis_site/apps/resources/resources/panda3d.yaml @@ -1,6 +1,6 @@ description: Panda3D is a Python-focused 3-D framework for rapid development of games, visualizations, and simulations, written in C++ with an emphasis on performance and flexibility. -title_image: http://www.panda3d.org/wp-content/uploads/2019/01/panda3d_logo.png +title_image: https://www.panda3d.org/wp-content/uploads/2019/01/panda3d_logo.png title_url: https://discord.gg/9XsucTT position: 9 urls: diff --git a/pydis_site/apps/resources/resources/simple_guide_to_git.yaml b/pydis_site/apps/resources/resources/simple_guide_to_git.yaml index 144b6c70..6dacdf5c 100644 --- a/pydis_site/apps/resources/resources/simple_guide_to_git.yaml +++ b/pydis_site/apps/resources/resources/simple_guide_to_git.yaml @@ -1,6 +1,6 @@ description: A simple, no-nonsense guide to the basics of using Git. name: A Simple Guide to Git -title_url: http://rogerdudler.github.io/git-guide/ +title_url: https://rogerdudler.github.io/git-guide/ title_icon: branding/github title_icon_color: black tags: diff --git a/pydis_site/apps/resources/resources/thonny.yaml b/pydis_site/apps/resources/resources/thonny.yaml index 59aba96d..a60e4d1b 100644 --- a/pydis_site/apps/resources/resources/thonny.yaml +++ b/pydis_site/apps/resources/resources/thonny.yaml @@ -1,4 +1,4 @@ -description: A Python IDE specifially aimed at learning programming. Has a lot of +description: A Python IDE specifically aimed at learning programming. Has a lot of helpful features to help you understand your code. name: Thonny title_url: https://thonny.org/ diff --git a/pydis_site/apps/staff/tests/test_logs_view.py b/pydis_site/apps/staff/tests/test_logs_view.py index 00e0ab2f..45e9ce8f 100644 --- a/pydis_site/apps/staff/tests/test_logs_view.py +++ b/pydis_site/apps/staff/tests/test_logs_view.py @@ -1,6 +1,6 @@ -from django.test import Client, TestCase +from django.test import TestCase +from django.urls import reverse from django.utils import timezone -from django_hosts.resolvers import reverse, reverse_host from pydis_site.apps.api.models.bot import DeletedMessage, MessageDeletionContext, Role, User from pydis_site.apps.staff.templatetags.deletedmessage_filters import hex_colour @@ -105,22 +105,18 @@ class TestLogsView(TestCase): deletion_context=cls.deletion_context, ) - def setUp(self): - """Sets up a test client that automatically sets the correct HOST header.""" - self.client = Client(HTTP_HOST=reverse_host(host="staff")) - def test_logs_returns_200_for_existing_logs_pk(self): - url = reverse('logs', host="staff", args=(self.deletion_context.id,)) + url = reverse('staff:logs', args=(self.deletion_context.id,)) response = self.client.get(url) self.assertEqual(response.status_code, 200) def test_logs_returns_404_for_nonexisting_logs_pk(self): - url = reverse('logs', host="staff", args=(self.deletion_context.id + 100,)) + url = reverse('staff:logs', args=(self.deletion_context.id + 100,)) response = self.client.get(url) self.assertEqual(response.status_code, 404) def test_author_color_is_set_in_response(self): - url = reverse('logs', host="staff", args=(self.deletion_context.id,)) + url = reverse('staff:logs', args=(self.deletion_context.id,)) response = self.client.get(url) role_colour = hex_colour(self.developers_role.colour) html_needle = ( @@ -129,7 +125,7 @@ class TestLogsView(TestCase): self.assertInHTML(html_needle, response.content.decode()) def test_correct_messages_have_been_passed_to_template(self): - url = reverse('logs', host="staff", args=(self.deletion_context.id,)) + url = reverse('staff:logs', args=(self.deletion_context.id,)) response = self.client.get(url) self.assertIn("messages", response.context) self.assertListEqual( @@ -138,7 +134,7 @@ class TestLogsView(TestCase): ) def test_if_both_embeds_are_included_html_response(self): - url = reverse('logs', host="staff", args=(self.deletion_context.id,)) + url = reverse('staff:logs', args=(self.deletion_context.id,)) response = self.client.get(url) html_response = response.content.decode() @@ -151,7 +147,7 @@ class TestLogsView(TestCase): self.assertInHTML(embed_colour_needle.format(colour=embed_two_colour), html_response) def test_if_both_attachments_are_included_html_response(self): - url = reverse('logs', host="staff", args=(self.deletion_context.id,)) + url = reverse('staff:logs', args=(self.deletion_context.id,)) response = self.client.get(url) html_response = response.content.decode() @@ -166,7 +162,7 @@ class TestLogsView(TestCase): ) def test_if_html_in_content_is_properly_escaped(self): - url = reverse('logs', host="staff", args=(self.deletion_context.id,)) + url = reverse('staff:logs', args=(self.deletion_context.id,)) response = self.client.get(url) html_response = response.content.decode() diff --git a/pydis_site/hosts.py b/pydis_site/hosts.py deleted file mode 100644 index 5a837a8b..00000000 --- a/pydis_site/hosts.py +++ /dev/null @@ -1,13 +0,0 @@ -from django.conf import settings -from django_hosts import host, patterns - -host_patterns = patterns( - '', - host(r'admin', 'pydis_site.apps.admin.urls', name="admin"), - # External API ingress (over the net) - host(r'api', 'pydis_site.apps.api.urls', name='api'), - # Internal API ingress (cluster local) - host(r'pydis-api', 'pydis_site.apps.api.urls', name='internal_api'), - host(r'staff', 'pydis_site.apps.staff.urls', name='staff'), - host(r'.*', 'pydis_site.apps.home.urls', name=settings.DEFAULT_HOST) -) diff --git a/pydis_site/settings.py b/pydis_site/settings.py index da582517..5a8e9be7 100644 --- a/pydis_site/settings.py +++ b/pydis_site/settings.py @@ -25,7 +25,8 @@ from pydis_site.constants import GIT_SHA env = environ.Env( DEBUG=(bool, False), SITE_DSN=(str, ""), - BUILDING_DOCKER=(bool, False) + BUILDING_DOCKER=(bool, False), + STATIC_BUILD=(bool, False), ) sentry_sdk.init( @@ -55,22 +56,24 @@ else: ALLOWED_HOSTS = env.list( 'ALLOWED_HOSTS', default=[ + 'www.pythondiscord.com', 'pythondiscord.com', - 'admin.pythondiscord.com', - 'api.pythondiscord.com', - 'staff.pythondiscord.com', - 'pydis-api.default.svc.cluster.local', gethostname(), - gethostbyname(gethostname()) - ] + gethostbyname(gethostname()), + 'site.default.svc.cluster.local', + ], ) SECRET_KEY = env('SECRET_KEY') # Application definition -INSTALLED_APPS = [ +NON_STATIC_APPS = [ 'pydis_site.apps.api', - 'pydis_site.apps.home', 'pydis_site.apps.staff', +] if not env("STATIC_BUILD") else [] + +INSTALLED_APPS = [ + *NON_STATIC_APPS, + 'pydis_site.apps.home', 'pydis_site.apps.resources', 'pydis_site.apps.content', 'pydis_site.apps.events', @@ -84,20 +87,24 @@ INSTALLED_APPS = [ 'django.contrib.sites', 'django.contrib.staticfiles', - 'django_hosts', 'django_filters', 'django_simple_bulma', 'rest_framework', 'rest_framework.authtoken', + + 'django_distill', ] if not env("BUILDING_DOCKER"): INSTALLED_APPS.append("django_prometheus") +NON_STATIC_MIDDLEWARE = [ + 'django_prometheus.middleware.PrometheusBeforeMiddleware', +] if not env("STATIC_BUILD") else [] + # Ensure that Prometheus middlewares are first and last here. MIDDLEWARE = [ - 'django_prometheus.middleware.PrometheusBeforeMiddleware', - 'django_hosts.middleware.HostsRequestMiddleware', + *NON_STATIC_MIDDLEWARE, 'django.middleware.security.SecurityMiddleware', 'whitenoise.middleware.WhiteNoiseMiddleware', @@ -108,7 +115,6 @@ MIDDLEWARE = [ 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', - 'django_hosts.middleware.HostsResponseMiddleware', 'django_prometheus.middleware.PrometheusAfterMiddleware' ] @@ -120,10 +126,6 @@ TEMPLATES = [ 'DIRS': [os.path.join(BASE_DIR, 'pydis_site', 'templates')], 'APP_DIRS': True, 'OPTIONS': { - 'builtins': [ - 'django_hosts.templatetags.hosts_override', - ], - 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', @@ -143,7 +145,7 @@ WSGI_APPLICATION = 'pydis_site.wsgi.application' DATABASES = { 'default': env.db(), 'metricity': env.db('METRICITY_DB_URL'), -} +} if not env("STATIC_BUILD") else {} # Password validation # https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators @@ -185,11 +187,6 @@ STATICFILES_FINDERS = [ 'django_simple_bulma.finders.SimpleBulmaFinder', ] -# django-hosts -# https://django-hosts.readthedocs.io/en/latest/ -ROOT_HOSTCONF = 'pydis_site.hosts' -DEFAULT_HOST = 'home' - if DEBUG: PARENT_HOST = env('PARENT_HOST', default='pythondiscord.local:8000') @@ -201,7 +198,7 @@ else: PARENT_HOST = env('PARENT_HOST', default='pythondiscord.com') # Django REST framework -# http://www.django-rest-framework.org +# https://www.django-rest-framework.org REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework.authentication.TokenAuthentication', diff --git a/pydis_site/static/css/base/base.css b/pydis_site/static/css/base/base.css index f3fe1e44..4b36b7ce 100644 --- a/pydis_site/static/css/base/base.css +++ b/pydis_site/static/css/base/base.css @@ -78,12 +78,20 @@ main.site-content { color: #00000000; } +#netcup-logo { + padding-left: 15px; + background: url(https://www.netcup-wiki.de/static/assets/images/netcup_logo_white.svg) no-repeat center; + background-size: 60px; + background-position: 0px 3px; + color: #00000000; +} + #django-logo { padding-bottom: 2px; - background: url(https://static.djangoproject.com/img/logos/django-logo-negative.png) no-repeat center; - filter: grayscale(1) invert(0.02); + background: url(https://static.djangoproject.com/img/logos/django-logo-negative.svg) no-repeat center; + filter: grayscale(1) invert(0.09); background-size: 52px 25.5px; - background-position: -1px -2px; + background-position: -2px -1px; color: #00000000; } @@ -92,6 +100,7 @@ main.site-content { height: 20px; background: url(https://bulma.io/images/bulma-logo-white.png) no-repeat center; background-size: 60px; + background-position: 0px 3px; color: #00000000; } diff --git a/pydis_site/static/css/content/page.css b/pydis_site/static/css/content/page.css index d831f86d..2d4bd325 100644 --- a/pydis_site/static/css/content/page.css +++ b/pydis_site/static/css/content/page.css @@ -77,3 +77,16 @@ ul.menu-list.toc { li img { margin-top: 0.5em; } + +.collapsible { + cursor: pointer; + width: 100%; + border: none; + outline: none; +} + +.collapsible-content { + overflow: hidden; + max-height: 0; + transition: max-height 0.2s ease-out; +} diff --git a/pydis_site/static/css/home/index.css b/pydis_site/static/css/home/index.css index ee6f6e4c..7ec8af74 100644 --- a/pydis_site/static/css/home/index.css +++ b/pydis_site/static/css/home/index.css @@ -215,12 +215,20 @@ h1 { } #sponsors .columns { + display: block; justify-content: center; margin: auto; max-width: 80%; } +#sponsors a { + margin: auto; + display: inline-block; +} + #sponsors img { - height: 5rem; - margin: auto 1rem; + width: auto; + height: auto; + + max-height: 5rem; } diff --git a/pydis_site/static/images/content/contributing/pycharm_run_module.png b/pydis_site/static/images/content/contributing/pycharm_run_module.png Binary files differnew file mode 100644 index 00000000..c5030519 --- /dev/null +++ b/pydis_site/static/images/content/contributing/pycharm_run_module.png diff --git a/pydis_site/static/images/sponsors/netcup.png b/pydis_site/static/images/sponsors/netcup.png Binary files differnew file mode 100644 index 00000000..e5dff196 --- /dev/null +++ b/pydis_site/static/images/sponsors/netcup.png diff --git a/pydis_site/static/images/sponsors/netlify.png b/pydis_site/static/images/sponsors/netlify.png Binary files differnew file mode 100644 index 00000000..0f14f385 --- /dev/null +++ b/pydis_site/static/images/sponsors/netlify.png diff --git a/pydis_site/static/js/content/page.js b/pydis_site/static/js/content/page.js new file mode 100644 index 00000000..366a033c --- /dev/null +++ b/pydis_site/static/js/content/page.js @@ -0,0 +1,13 @@ +document.addEventListener("DOMContentLoaded", () => { + const headers = document.getElementsByClassName("collapsible"); + for (const header of headers) { + header.addEventListener("click", () => { + var content = header.nextElementSibling; + if (content.style.maxHeight){ + content.style.maxHeight = null; + } else { + content.style.maxHeight = content.scrollHeight + "px"; + } + }); + } +}); diff --git a/pydis_site/templates/base/footer.html b/pydis_site/templates/base/footer.html index bca43b5d..0bc93578 100644 --- a/pydis_site/templates/base/footer.html +++ b/pydis_site/templates/base/footer.html @@ -1,7 +1,7 @@ <footer class="footer has-background-dark has-text-light"> <div class="content has-text-centered"> <p> - Powered by <a href="https://www.linode.com/?r=3bc18ce876ff43ea31f201b91e8e119c9753f085"><span id="linode-logo">Linode</span></a><br>Built with <a href="https://www.djangoproject.com/"><span id="django-logo">django</span></a> and <a href="https://bulma.io"><span id="bulma-logo">Bulma</span></a> <br/> © {% now "Y" %} <span id="pydis-text">Python Discord</span> + Powered by <a href="https://www.linode.com/?r=3bc18ce876ff43ea31f201b91e8e119c9753f085"><span id="linode-logo">Linode</span></a> and <a href="https://www.netcup.eu/"><span id="netcup-logo">netcup</span></a><br>Built with <a href="https://www.djangoproject.com/"><span id="django-logo">django</span></a> and <a href="https://bulma.io"><span id="bulma-logo">Bulma</span></a> <br/> © {% now "Y" %} <span id="pydis-text">Python Discord</span> </p> </div> </footer> diff --git a/pydis_site/templates/base/navbar.html b/pydis_site/templates/base/navbar.html index 11a11e10..18ff7efa 100644 --- a/pydis_site/templates/base/navbar.html +++ b/pydis_site/templates/base/navbar.html @@ -44,7 +44,7 @@ </a> {# Patreon #} - <a class="navbar-item" href="http://patreon.com/python_discord"> + <a class="navbar-item" href="https://patreon.com/python_discord"> <span class="icon is-size-4 is-medium"><i class="fab fa-patreon"></i></span> <span> Patreon</span> </a> @@ -79,7 +79,7 @@ <a class="navbar-item" href="{% url "content:page_category" location="frequently-asked-questions" %}"> FAQ </a> - <a class="navbar-item" href="{% url 'timeline' %}"> + <a class="navbar-item" href="{% url 'home:timeline' %}"> Timeline </a> <a class="navbar-item" href="{% url "content:page_category" location="rules" %}"> diff --git a/pydis_site/templates/content/base.html b/pydis_site/templates/content/base.html index 21895479..00f4fce4 100644 --- a/pydis_site/templates/content/base.html +++ b/pydis_site/templates/content/base.html @@ -7,6 +7,7 @@ <meta property="og:type" content="website" /> <meta property="og:description" content="{{ page_description }}" /> <link rel="stylesheet" href="{% static "css/content/page.css" %}"> + <script src="{% static "js/content/page.js" %}"></script> {% endblock %} {% block content %} diff --git a/pydis_site/templates/events/index.html b/pydis_site/templates/events/index.html index daad1c9c..158ec56b 100644 --- a/pydis_site/templates/events/index.html +++ b/pydis_site/templates/events/index.html @@ -9,58 +9,34 @@ {% block event_content %} <div class="box"> <h2 class="title is-4">Code Jams</h2> - <div class="notification is-success"> - The 2021 Summer Code Jam qualifier will open June 21st. Check out the details <a href="{% url "events:page" path="code-jams/8" %}">here</a>. - </div> - <p>Each year, we organize a Winter Code Jam and a Summer Code Jam. During these events, members of our community will work together in teams to create something amazing using a technology we picked for them. One such technology that was picked for the Winter Code Jam 2020 was Kivy, a cross-platform GUI framework.</p> - <p>To help fuel the creative process, we provide a specific theme, like <strong>Ancient Technology</strong> or <strong>This App Hates You</strong>. At the end of the Code Jam, the projects are judged by Python Discord server staff members and guest judges from the larger Python community. The judges will consider creativity, code quality, teamwork, and adherence to the theme.</p> + <p>Each year, we organize at least one code jam, one during the summer and sometimes one during the winter. During these events, members of our community will work together in teams to create something amazing using a technology we picked for them. One such technology that was picked for the Summer 2021 Code Jam was text user interfaces (TUIS), where teams could pick from a pre-approved list of frameworks.</p> + <p>To help fuel the creative process, we provide a specific theme, like <strong>Think Inside the Box</strong> or <strong>Early Internet</strong>. At the end of the Code Jam, the projects are judged by Python Discord server staff members and guest judges from the larger Python community. The judges will consider creativity, code quality, teamwork, and adherence to the theme.</p> <p>If you want to read more about Code Jams, visit our <a href="{% url "events:page" path="code-jams" %}">Code Jam info page</a> or watch this video showcasing the best projects created during the <strong>Winter Code Jam 2020: Ancient Technology</strong>:</p> <iframe width="560" height="315" src="https://www.youtube.com/embed/8fbZsGrqBzo" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> </div> <div class="box"> - <h2 class="title is-4">Game Jam</h2> + <h2 class="title is-4">PyWeek</h2> <div class="columns is-3" style="--columnGap: 0.75rem;"> <div class="column"> <p> - The Game Jam is similar to our Code Jams, but smaller in scope. Instead of having to complete a qualifier - and being teamed up with random strangers, members of our community can just sign-up individually or pair up - with whoever they like. - </p> - <p> - The participants will have ten days to create a game using the technology we've selected, and drawing - inspiration from a provided theme. After the event, a panel of judges will play all the games and select a - winner. The top 5 will featured in a special video on our <a href="https://www.youtube.com/channel/UCQsrA4xo6jvdgsJZhKaBL6w">YouTube channel</a>. - </p> - <p> - The <a class="has-text-link" href="{% url "events:page" path="game-jams/2020" %}">first edition of the Game Jam</a> ran from - <strong>April 17, 2020 to April 26, 2020</strong>. + For the past 15 years, <a href="https://pyweek.org" target="_blank" rel="noopener">PyWeek</a> has been running a bi-annual game jam for the + Python language. As of 2020, we are excited to say we are officially partnered with PyWeek to co-run these + events. </p> - </div> - <div class="column is-3"> - <img src="https://user-images.githubusercontent.com/33516116/77593036-5fb09780-6eeb-11ea-9feb-336b2e5e23de.png" style="border-radius: 10px;" alt=""> - </div> - </div> - </div> - - <div class="box"> - <h2 class="title is-4">Hacktoberfest</h2> - <div class="columns is-3" style="--columnGap: 0.75rem;"> - <div class="column"> <p> - This event revolves around the annual <a href="https://hacktoberfest.digitalocean.com/">Hacktoberfest - event</a> organized by Digital Ocean. In addition to promoting Hacktoberfest in our community and supporting - those who choose to take their first steps into the world of open source, we will also ease our members into - contributing to open source by starting a low-entry, beginner-friendly open source project where we will - guide our members through the open source process in a safe environment. + During each PyWeek event, we open a special discussion channel in which our members can discuss their + submissions, meet other participants, and talk to PyWeek staff. The PyWeek organizer, + Daniel Pope (<a href="https://twitter.com/lordmauve" target="_blank" rel="noopener">@lordmauve</a>) will be present during the entire event to answer + questions and post announcements and information in our community. </p> <p> - The exact form this event will take has not been decided yet, but we'll make sure to keep you updated in - our community announcements! + Unlike our other events, the <strong>community</strong> will select the winner from all the submissions + during PyWeek. We may release YouTube content showcasing the best submissions after the events are finished. </p> </div> <div class="column is-3"> - <img src="https://raw.githubusercontent.com/python-discord/branding/master/seasonal/halloween/hacktoberfest/2020/animated_server_icon.gif" style="border-radius: 10px;" alt=""> + <img src="https://pyweek.readthedocs.io/en/latest/_static/pyweek.svg" style="border-radius: 10px;" alt=""> </div> </div> </div> @@ -71,7 +47,7 @@ <div class="column"> <p> Each year, many of our members take part of an online coding competition called - <a href="https://adventofcode.com/">Advent of Code</a> that takes place in December. Advent of Code is an + <a href="https://adventofcode.com/" target="_blank" rel="noopener">Advent of Code</a> that takes place in December. Advent of Code is an Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like, including Python. </p> @@ -88,39 +64,37 @@ </p> </div> <div class="column is-3"> - <img src="https://raw.githubusercontent.com/python-discord/branding/master/seasonal/christmas/2019/festive_256.gif" style="border-radius: 10px;" alt=""> + <img src="https://raw.githubusercontent.com/python-discord/branding/main/events/christmas/server_icons/festive_256.gif" style="border-radius: 10px;" alt=""> </div> </div> </div> <div class="box"> - <h2 class="title is-4">PyWeek</h2> + <h2 class="title is-4">Game Jam</h2> <div class="columns is-3" style="--columnGap: 0.75rem;"> <div class="column"> <p> - For the past 15 years, <a href="https://pyweek.org">PyWeek</a> has been running a bi-annual game jam for the - Python language. As of 2020, we are excited to say we are officially partnered with PyWeek to co-run these - events. + The Game Jam is similar to our Code Jams, but smaller in scope. Instead of having to complete a qualifier + and being teamed up with random strangers, members of our community can just sign-up individually or pair up + with whoever they like. </p> <p> - During each PyWeek event, we open a special discussion channel in which our members can discuss their - submissions, meet other participants, and talk to PyWeek staff. The PyWeek organizer, - Daniel Pope (<a href="https://twitter.com/lordmauve">@lordmauve</a>) will be present during the entire event to answer - questions and post announcements and information in our community. + The participants will have ten days to create a game using the technology we've selected, and drawing + inspiration from a provided theme. After the event, a panel of judges will play all the games and select a + winner. The top 5 will featured in a special video on our <a href="https://www.youtube.com/channel/UCQsrA4xo6jvdgsJZhKaBL6w" target="_blank" rel="noopener">YouTube channel</a>. </p> <p> - Unlike our other events, the <strong>community</strong> will select the winner from all the submissions - during PyWeek. We may release YouTube content showcasing the best submissions after the events are finished. + The <a class="has-text-link" href="{% url "events:page" path="game-jams/2020" %}">first edition of the Game Jam</a> ran from + <strong>April 17, 2020 to April 26, 2020</strong>. </p> </div> <div class="column is-3"> - <img src="https://pyweek.readthedocs.io/en/latest/_static/pyweek.svg" style="border-radius: 10px;" alt=""> + <img src="https://user-images.githubusercontent.com/33516116/77593036-5fb09780-6eeb-11ea-9feb-336b2e5e23de.png" style="border-radius: 10px;" alt=""> </div> </div> </div> {% endblock %} {% block sidebar %} - {% include "events/sidebar/ongoing-event.html" %} {% include "events/sidebar/events-list.html" %} {% endblock %} diff --git a/pydis_site/templates/events/pages/code-jams/8/_index.html b/pydis_site/templates/events/pages/code-jams/8/_index.html index 55bdc95b..628a2c22 100644 --- a/pydis_site/templates/events/pages/code-jams/8/_index.html +++ b/pydis_site/templates/events/pages/code-jams/8/_index.html @@ -20,10 +20,58 @@ and walking through the program that your team has created. </p> + <h3 id="winners"><a href="#winners">Code Jam Winners</a></h3> + <p>Congratulations to our winners and the two runner ups! Check out their projects below.</p> + + <h4 class="mt-5 mb-2"><i class="fa fa-trophy"></i> Perceptive Porcupines: WTPython!?</h4> + <p class="my-1"><em>VV, Poppinawhile, ethansocal, Jeff Z, Cohan, ¯\_(ツ)_/¯</em></p> + <p class="my-1"> + What the Python (wtpython) is a simple terminal user interface that allows you to explore relevant answers on Stackoverflow without leaving your terminal or IDE. When you get an error, all you have to do is swap python for wtpython. When your code hits an error, you'll see a textual interface for exploring relevant answers allowing you to stay focused and ship faster! + </p> + <p> + <a href="https://www.youtube.com/watch?v=DV3uMdsw9KE" title="Perceptive Porcupines Demo Video" target="_blank" rel="noopener"><i class="fa fa-video"> </i> Demo video</a> + <br/> + <a href="https://github.com/what-the-python/wtpython" title="Perceptive Porcupines GitHub Repository" target="_blank" rel="noopener"><i class="fa fa-github"></i> GitHub Repository</a> + <br/> + + </p> + + <h4 class="mt-5 mb-2"><i class="fa fa-medal"></i> Lovable Lobsters: Ultimate Tic Tac Toe</h4> + <p class="my-1"><em>A5Rocks, Bast, Dacheat, mega_hirtz, CopOnTheRun, richphi</em></p> + <p class="my-1"> + Thinking inside a box, that is inside a box, that is inside yet another box. + + The terminal program created by the Lovable Lobsters allows you to play Ultimate Tic Tac Toe right form your terminal. The really impressive part though? You can play with your friends and family over your network! Their program has a server-client set-up that lets you play with your friends and family from different computers. + </p> + <p> + <a href="https://www.youtube.com/watch?v=WI9tgQeAfXw" title="Lovable Lobsters Demo Video" target="_blank" rel="noopener"><i class="fa fa-video"> </i> Demo video</a> + <br/> + <a href="https://github.com/A5rocks/code-jam-8" title="Lovable Lobsters GitHub Repository" target="_blank" rel="noopener"><i class="fa fa-github"></i> GitHub Repository</a> + <br/> + </p> + + <h4 class="mt-5 mb-2"><i class="fa fa-medal"></i> Robust Reindeer: Rubik's Cube</h4> + <p class="my-1"><em>Björn, aaronshenhao, mathsman, Dude Saber, 詭異, Keppo</em></p> + <p class="my-1"> + This submission is a Rubik's cube, rendered in a text user interface (that was a constraint) using the asciimatics package, and addressing the theme "thinking inside the box". + + Just like a real world Rubik's cube, you can move this cube around to look at it from all sides. And, of course, you can rotate the individual discs it is made up of to first scramble up the order and then to try and solve it into perfect ordering again. + </p> + <p> + <a href="https://github.com/bjoseru/pdcj8-robust-reindeer" title="Robust Reindeer GitHub Repository" target="_blank" rel="noopener"><i class="fa fa-github"></i> GitHub Repository</a> + <br/> + </p> + + <h3 id="submissions"><a href="#submissions">Submissions</a></h3> + <p> + 63 teams started out on July 9th 2021. By the end of the jam, 51 teams made project submissions. Check them all out here: + <div class="has-text-centered"><a class="button is-link" href="submissions">View Submissions</a></div> + </p> + <h3 id="important-dates"><a href="#important-dates">Important Dates</a></h3> <ul> <li>Tuesday, June 15 - Form to submit theme suggestions opens</li> - <li>Monday, June 21 - <a href="https://github.com/python-discord/cj8-qualifier">The Qualifier</a> is released</li> + <li>Monday, June 21 - <a href="https://github.com/python-discord/cj8-qualifier" target="_blank" rel="noopener">The Qualifier</a> is released</li> <li>Friday, June 25 - Voting for the theme opens</li> <li>Saturday, June 26 @ 4PM UTC- <a class="has-text-link" href="{% url "events:page" path="code-jams/8/github-bootcamp" %}">GitHub Bootcamp</a></li> <li>Wednesday, July 1 - The Qualifier closes</li> @@ -36,14 +84,14 @@ <p> The chosen technology/tech stack for this year is <strong>Text User Interfaces</strong> (TUIs). Each team must create a program with one of <a href="{% url "events:page" path="code-jams/8/frameworks" %}">the approved frameworks</a> that creates a user interface that is text based. - For more information of TUIs and what's involved with such an interface, check out <a href="https://en.wikipedia.org/wiki/Text-based_user_interface">this wikipedia article</a>. + For more information of TUIs and what's involved with such an interface, check out <a href="https://en.wikipedia.org/wiki/Text-based_user_interface" target="_blank" rel="noopener">this wikipedia article</a>. </p> - <h3 if="qualifier"><a href="#qualifier">The Qualifier</a></h3> + <h3 id="qualifier"><a href="#qualifier">The Qualifier</a></h3> <p> The qualifier is a coding challenge that you are required to complete before registering for the code jam. This is meant as a basic assessment of your skills to ensure you have enough python knowledge to effectively contribute in a team environment. </p> - <p class="has-text-centered"><a class="button is-link" href="https://github.com/python-discord/cj8-qualifier" target="_blank">View the Qualifier</a></p + <p class="has-text-centered"><a class="button is-link" href="https://github.com/python-discord/cj8-qualifier" target="_blank" rel="noopener">View the Qualifier</a></p> <p> Please note the requirements for the qualifier. <ul> @@ -52,11 +100,7 @@ <li>The Qualifier must be submitted through the Code Jam sign-up form.</li> </ul> </p> - <h3 id="submissions"><a href="#submissions">Submissions</a></h3> - <p> - 63 teams started out on July 9th 2021. By the end of the jam, 51 teams made project submissions. Check them all out here: - <div class="has-text-centered"><a class="button is-link" href="submissions">View Submissions</a></div> - </p> + <h3 id="prizes"><a href="#prizes">Prizes</a></h3> <p> Our Code Jam Sponsors have provided prizes for the winners of the code jam. @@ -71,7 +115,7 @@ <img src="{% static "images/events/DO_Logo_Vertical_Blue.png" %}" alt="Digital Ocean"> </div> <div class="media-content"> - <p class="subtitle has-link"><a href="https://www.digitalocean.com/">DigitalOcean</a></p> + <p class="subtitle has-link"><a href="https://www.digitalocean.com/" target="_blank" rel="noopener">DigitalOcean</a></p> <p class="is-italic"> Scalable compute platform with add-on storage, security, and monitoring capabilities. We make it simple to launch in the cloud and scale up as you grow—whether you’re running one virtual machine or ten thousand. @@ -90,7 +134,7 @@ <img src="{% static "images/sponsors/jetbrains.png" %}" alt="JetBrains"> </div> <div class="media-content"> - <p class="subtitle has-link"><a href="https://www.jetbrains.com/">JetBrains</a></p> + <p class="subtitle has-link"><a href="https://www.jetbrains.com/" target="_blank" rel="noopener">JetBrains</a></p> <p class="is-italic"> Whatever platform or language you work with, JetBrains has a development tool for you. We help developers work faster by automating common, repetitive tasks to enable them to stay focused on code design and the big picture. @@ -109,7 +153,7 @@ <img src="{% static "images/events/Tabnine.png" %}" alt="Tabnine"> </div> <div class="media-content"> - <p class="subtitle has-link"><a href="https://www.tabnine.com/now?utm_source=discord&utm_medium=Ins&utm_campaign=PythonDis">Tabnine</a></p> + <p class="subtitle has-link"><a href="https://www.tabnine.com/now?utm_source=discord&utm_medium=Ins&utm_campaign=PythonDis" target="_blank" rel="noopener">Tabnine</a></p> <p class="is-italic">Tabnine is an AI-powered code completion tool used by millions of devs around the world every day - Tabnine supports dozens of programming languages, in all of your favorite IDEs, saving you tons of time - so that you can type less and code more. Tabnine comes as a plugin and has a free-forever basic plan, so you can get started with it right away! diff --git a/pydis_site/templates/events/pages/code-jams/8/frameworks.html b/pydis_site/templates/events/pages/code-jams/8/frameworks.html index 34ac4f0a..1c02e38a 100644 --- a/pydis_site/templates/events/pages/code-jams/8/frameworks.html +++ b/pydis_site/templates/events/pages/code-jams/8/frameworks.html @@ -19,7 +19,7 @@ <div class="columns"> <div class="column"> <ul> - <li><a href="http://urwid.org/" target="_blank">Documentation Link</a></li> + <li><a href="https://urwid.org/" target="_blank">Documentation Link</a></li> <li><strong>Supports:</strong> Linux, Mac, other unix-like OS</li> <li>Somewhat in-depth tutorial</li> <li>Uses widgets in a fairly straight forward design</li> diff --git a/pydis_site/templates/events/pages/code-jams/_index.html b/pydis_site/templates/events/pages/code-jams/_index.html index 22a86db3..207d4b9a 100644 --- a/pydis_site/templates/events/pages/code-jams/_index.html +++ b/pydis_site/templates/events/pages/code-jams/_index.html @@ -66,7 +66,6 @@ {% endblock %} {% block sidebar %} - {% include "events/sidebar/code-jams/ongoing-code-jam.html" %} {% include "events/sidebar/code-jams/previous-code-jams.html" %} {% include "events/sidebar/code-jams/useful-information.html" %} {% endblock %} diff --git a/pydis_site/templates/events/sidebar/code-jams/previous-code-jams.html b/pydis_site/templates/events/sidebar/code-jams/previous-code-jams.html index 9f9ecd1a..21b2ccb4 100644 --- a/pydis_site/templates/events/sidebar/code-jams/previous-code-jams.html +++ b/pydis_site/templates/events/sidebar/code-jams/previous-code-jams.html @@ -1,6 +1,7 @@ <div class="box"> <p class="menu-label">Previous Code Jams</p> <ul class="menu-list"> + <li><a class="has-text-link" href="{% url "events:page" path="code-jams/8" %}">Code Jam 8: Think Inside the Box</a></li> <li><a class="has-text-link" href="{% url "events:page" path="code-jams/7" %}">Code Jam 7: Early Internet</a></li> <li><a class="has-text-link" href="{% url "events:page" path="code-jams/6" %}">Code Jam 6: Ancient Technology</a></li> <li><a class="has-text-link" href="{% url "events:page" path="code-jams/5" %}">Code Jam 5: Climate Change</a></li> diff --git a/pydis_site/templates/events/sidebar/events-list.html b/pydis_site/templates/events/sidebar/events-list.html index 327b0e77..5dfe5dc2 100644 --- a/pydis_site/templates/events/sidebar/events-list.html +++ b/pydis_site/templates/events/sidebar/events-list.html @@ -1,10 +1,10 @@ <div class="box"> - <p class="menu-label">Event Calendar 2020</p> + <p class="menu-label">Event Calendar 2021</p> <ul class="menu-list"> - <li><a class="has-text-link" href="{% url "events:page" path="code-jams/6" %}">January 17-January 26: Winter Code Jam</a></li> - <li><a class="has-text-link" href="{% url "events:page" path="game-jams/2020" %}">April 17-April 26: Game Jam</a></li> - <li><a class="has-text-link" href="{% url "events:page" path="code-jams/7" %}">July 31-August 9: Summer Code Jam</a></li> - <li><a class="has-text-black" style="cursor: default;">October: Hacktoberfest</a></li> + <li><a class="has-text-link" href="https://pyweek.org/31/" target="_blank" rel="noopener">March: PyWeek 31</a></li> + <li><a class="has-text-black" style="cursor: default;">May: Pixels</a></li> + <li><a class="has-text-link" href="{% url "events:page" path="code-jams/8" %}">July: Summer Code Jam</a></li> + <li><a class="has-text-link" href="https://pyweek.org/32/" target="_blank" rel="noopener">September: PyWeek 32</a></li> <li><a class="has-text-black" style="cursor: default;">December: Advent of Code</a></li> </ul> </div> diff --git a/pydis_site/templates/home/index.html b/pydis_site/templates/home/index.html index 072e3817..c7350cac 100644 --- a/pydis_site/templates/home/index.html +++ b/pydis_site/templates/home/index.html @@ -9,21 +9,14 @@ {% block content %} {% include "base/navbar.html" %} - <!-- Mobile-only Notice --> - <section id="mobile-notice" class="message is-primary is-hidden-tablet"> - <a href="/events/code-jams/8/"> - <img src="{% static "images/events/summer_code_jam_2021/front_page_banners/currently_live.png" %}" alt="Summer Code Jam 2021"> - </a> - </section> - <!-- Wave Hero --> <section id="wave-hero" class="section is-hidden-mobile"> <div class="container"> - <div class="columns is-variable is-8"> + <div class="columns is-variable is-8 is-centered"> {# Embedded Welcome video #} - <div id="wave-hero-left" class="column is-half"> + <div id="wave-hero-left" class="column is-half "> <div class="force-aspect-container"> <iframe class="force-aspect-content" @@ -44,13 +37,6 @@ ></iframe> </div> </div> - - {# Code Jam banner #} - <div id="wave-hero-right" class="column is-half"> - <a href="/events/code-jams/8/"> - <img src="{% static "images/events/summer_code_jam_2021/front_page_banners/currently_live.png" %}" alt="Summer Code Jam 2021"> - </a> - </div> </div> </div> @@ -109,7 +95,7 @@ </p> <div class="buttons are-large is-centered"> - <a href="{% url 'timeline' %}" class="button is-primary"> + <a href="{% url 'home:timeline' %}" class="button is-primary"> <span>Check it out!</span> <span class="icon"> <i class="fas fa-arrow-right"></i> @@ -187,6 +173,9 @@ Sponsors </h1> <div class="columns is-mobile is-multiline"> + <a href="https://www.netcup.eu/" class="column is-narrow"> + <img src="{% static "images/sponsors/netcup.png" %}" alt="netcup"/> + </a> <a href="https://www.linode.com/?r=3bc18ce876ff43ea31f201b91e8e119c9753f085" class="column is-narrow"> <img src="{% static "images/sponsors/linode.png" %}" alt="Linode"/> </a> @@ -201,6 +190,10 @@ </a> <a href="https://streamyard.com" class="column is-narrow"> <img src="{% static "images/sponsors/streamyard.png" %}" alt="StreamYard"/> + </a> + <a href="https://www.netlify.com/" class="column is-narrow"> + <img src="{% static "images/sponsors/netlify.png" %}" alt="Netlify"/> + </a> <a href="https://www.cloudflare.com/" class="column is-narrow"> <img src="{% static "images/sponsors/cloudflare.png" %}" alt="Cloudflare"/> </a> diff --git a/pydis_site/urls.py b/pydis_site/urls.py index 47cf0ba1..6cd31f26 100644 --- a/pydis_site/urls.py +++ b/pydis_site/urls.py @@ -1,7 +1,35 @@ +from django.contrib import admin from django.urls import include, path +from pydis_site import settings + +NON_STATIC_PATTERNS = [ + path('admin/', admin.site.urls), + + # External API ingress (over the net) + path('api/', include('pydis_site.apps.api.urls', namespace='api')), + # Internal API ingress (cluster local) + path('pydis-api/', include('pydis_site.apps.api.urls', namespace='internal_api')), + + path('', include('django_prometheus.urls')), +] if not settings.env("STATIC_BUILD") else [] + urlpatterns = ( + *NON_STATIC_PATTERNS, + + # This must be mounted before the `content` app to prevent Django + # from wildcard matching all requests to `pages/...`. + path('', include('pydis_site.apps.redirect.urls')), + + path('pages/', include('pydis_site.apps.content.urls', namespace='content')), + path('resources/', include('pydis_site.apps.resources.urls')), + path('events/', include('pydis_site.apps.events.urls', namespace='events')), path('', include('pydis_site.apps.home.urls', namespace='home')), - path('staff/', include('pydis_site.apps.staff.urls', namespace='staff')), ) + + +if not settings.env("STATIC_BUILD"): + urlpatterns += ( + path('staff/', include('pydis_site.apps.staff.urls', namespace='staff')), + ) diff --git a/pyproject.toml b/pyproject.toml index 8c0dc937..289d825c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,11 +7,10 @@ license = "MIT" [tool.poetry.dependencies] python = "3.9.*" -django = "~=3.0.4" +django = "~=3.1.14" django-environ = "~=0.4.5" -django-filter = "~=2.1.0" -django-hosts = "~=4.0" -djangorestframework = "~=3.11.0" +django-filter = "~=21.1" +djangorestframework = "~=3.12.0" psycopg2-binary = "~=2.8.0" django-simple-bulma = "~=2.4" whitenoise = "~=5.0" @@ -22,6 +21,7 @@ sentry-sdk = "~=0.19" markdown = "~=3.3.4" python-frontmatter = "~=1.0" django-prometheus = "~=2.1" +django-distill = "~=2.9.0" [tool.poetry.dev-dependencies] coverage = "~=5.0" diff --git a/static-builds/README.md b/static-builds/README.md new file mode 100644 index 00000000..9b86ed08 --- /dev/null +++ b/static-builds/README.md @@ -0,0 +1,50 @@ +# Static Builds +This directory includes all the needed information to build and deploy static previews of the site. + +Static deployments use [django-distill](https://github.com/meeb/django-distill) to build the static content. +The content is built in GitHub Actions, and is fetched and deployed by Netlify. + + +## Instructions +These are the configuration instructions to get started with static deployments. +They are split into two parts: + +- [Building The Site](#building-the-site) +- [Deploying To Netlify](#deploying-to-netlify) + + +### Building The Site +To get started with building, you can use the following command: + +```shell +poetry install +python -m pip install httpx==0.19.0 +poetry run task static +``` + +Alternatively, you can use the [Dockerfile](/Dockerfile) and extract the build. + +Both output their builds to a `build/` directory. + +### Deploying To Netlify +To deploy to netlify, link your site GitHub repository to a netlify site, and use the following settings: + +Build Command: +`python -m pip install httpx==0.19.0 && python static-builds/netlify_build.py` + +Publish Directory: +`build` + +Environment Variables: +- PYTHON_VERSION: 3.8 + + +Note that at this time, if you are deploying to netlify yourself, you won't have access to the +fa-icons pack we are using, which will lead to many missing icons on your preview. +You can either update the pack to one which will work on your domain, or you'll have to live with the missing icons. + + +> Warning: If you are modifying the [build script](./netlify_build.py), make sure it is compatible with Python 3.8. + +Note: The build script uses [nightly.link](https://github.com/oprypin/nightly.link) +to fetch the artifact with no authentication. diff --git a/static-builds/netlify_build.py b/static-builds/netlify_build.py new file mode 100644 index 00000000..4e1e6106 --- /dev/null +++ b/static-builds/netlify_build.py @@ -0,0 +1,122 @@ +"""Build script to deploy project on netlify.""" + +# WARNING: This file must remain compatible with python 3.8 + +# This script performs all the actions required to build and deploy our project on netlify +# It depends on the following packages, which are set in the netlify UI: +# httpx == 0.19.0 + +import os +import time +import typing +import zipfile +from pathlib import Path +from urllib import parse + +import httpx + +API_URL = "https://api.github.com" +NIGHTLY_URL = "https://nightly.link" +OWNER, REPO = parse.urlparse(os.getenv("REPOSITORY_URL")).path.lstrip("/").split("/")[0:2] + + +def get_build_artifact() -> typing.Tuple[int, str]: + """ + Search for a build artifact, and return the result. + + The return is a tuple of the check suite ID, and the URL to the artifacts. + """ + print("Fetching build URL.") + + if os.getenv("PULL_REQUEST").lower() == "true": + print(f"Fetching data for PR #{os.getenv('REVIEW_ID')}") + + pull_url = f"{API_URL}/repos/{OWNER}/{REPO}/pulls/{os.getenv('REVIEW_ID')}" + pull_request = httpx.get(pull_url) + pull_request.raise_for_status() + + commit_sha = pull_request.json()["head"]["sha"] + + workflows_params = parse.urlencode({ + "event": "pull_request", + "per_page": 100 + }) + + else: + commit_sha = os.getenv("COMMIT_REF") + + workflows_params = parse.urlencode({ + "event": "push", + "per_page": 100 + }) + + print(f"Fetching action data for commit {commit_sha}") + + workflows = httpx.get(f"{API_URL}/repos/{OWNER}/{REPO}/actions/runs?{workflows_params}") + workflows.raise_for_status() + + for run in workflows.json()["workflow_runs"]: + if run["name"] == "Build & Publish Static Preview" and commit_sha == run["head_sha"]: + print(f"Found action for this commit: {run['id']}\n{run['html_url']}") + break + else: + raise Exception("Could not find the workflow run for this event.") + + polls = 0 + while polls <= 20: + if run["status"] != "completed": + print("Action isn't ready, sleeping for 10 seconds.") + polls += 1 + time.sleep(10) + + elif run["conclusion"] != "success": + print("Aborting build due to a failure in a previous CI step.") + exit(0) + + else: + print(f"Found artifact URL:\n{run['artifacts_url']}") + return run["check_suite_id"], run["artifacts_url"] + + _run = httpx.get(run["url"]) + _run.raise_for_status() + run = _run.json() + + raise Exception("Polled for the artifact workflow, but it was not ready in time.") + + +def download_artifact(suite_id: int, url: str) -> None: + """Download a build artifact from `url`, and unzip the content.""" + print("Fetching artifact data.") + + artifacts = httpx.get(url) + artifacts.raise_for_status() + artifacts = artifacts.json() + + if artifacts["total_count"] == "0": + raise Exception(f"No artifacts were found for this build, aborting.\n{url}") + + for artifact in artifacts["artifacts"]: + if artifact["name"] == "static-build": + print("Found artifact with build.") + break + else: + raise Exception("Could not find an artifact with the expected name.") + + artifact_url = f"{NIGHTLY_URL}/{OWNER}/{REPO}/suites/{suite_id}/artifacts/{artifact['id']}" + zipped_content = httpx.get(artifact_url) + zipped_content.raise_for_status() + + zip_file = Path("temp.zip") + zip_file.write_bytes(zipped_content.read()) + + with zipfile.ZipFile(zip_file, "r") as zip_ref: + zip_ref.extractall("build") + + zip_file.unlink(missing_ok=True) + + print("Wrote artifact content to target directory.") + + +if __name__ == "__main__": + print("Build started") + download_artifact(*get_build_artifact()) |