diff options
author | 2022-08-09 23:40:23 +0200 | |
---|---|---|
committer | 2022-08-09 23:40:23 +0200 | |
commit | 82d92779bd8b2d0adfa93ab4c0b7294cfa1c1045 (patch) | |
tree | 6ce922b3e1aff19fbcaa325bc5144abed2122c2e | |
parent | Use UTC Time For GitHub API (diff) | |
parent | Merge pull request #758 from python-discord/api-last_applied (diff) |
Merge branch 'main' into github-artifacts
-rw-r--r-- | .github/workflows/lint-test.yaml | 1 | ||||
-rw-r--r-- | docker-compose.yml | 6 | ||||
-rwxr-xr-x | manage.py | 20 | ||||
-rw-r--r-- | pydis_site/apps/api/migrations/0084_infraction_last_applied.py | 26 | ||||
-rw-r--r-- | pydis_site/apps/api/models/bot/infraction.py | 6 | ||||
-rw-r--r-- | pydis_site/apps/api/serializers.py | 1 | ||||
-rw-r--r-- | pyproject.toml | 2 |
7 files changed, 53 insertions, 9 deletions
diff --git a/.github/workflows/lint-test.yaml b/.github/workflows/lint-test.yaml index f97cd758..a167ce32 100644 --- a/.github/workflows/lint-test.yaml +++ b/.github/workflows/lint-test.yaml @@ -97,7 +97,6 @@ jobs: - name: Migrations and run tests with coverage.py run: | python manage.py makemigrations --check - python manage.py migrate coverage run manage.py test --no-input coverage report -m env: diff --git a/docker-compose.yml b/docker-compose.yml index eb987624..a6f4fd18 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,12 +8,12 @@ # and additionally use the Django development server which is # unsuitable for production. -version: "3.6" +version: "3.8" services: postgres: image: postgres:13-alpine ports: - - "127.0.0.1:7777:5432" + - "7777:5432" environment: POSTGRES_DB: pysite POSTGRES_PASSWORD: pysite @@ -38,7 +38,7 @@ services: - admin.web - staff.web ports: - - "127.0.0.1:8000:8000" + - "8000:8000" depends_on: postgres: condition: service_healthy @@ -95,13 +95,15 @@ class SiteManager: name="pythondiscord.local:8000" ) - def prepare_server(self) -> None: - """Perform preparation tasks before running the server.""" + def prepare_environment(self) -> None: + """Perform common preparation tasks.""" django.setup() print("Applying migrations.") call_command("migrate", verbosity=self.verbosity) + def prepare_server(self) -> None: + """Preform runserver-specific preparation tasks.""" if self.debug: # In Production, collectstatic is ran in the Docker image print("Collecting static files.") @@ -121,6 +123,7 @@ class SiteManager: # Prevent preparing twice when in dev mode due to reloader if not self.debug or in_reloader: + self.prepare_environment() self.prepare_server() print("Starting server.") @@ -148,6 +151,11 @@ class SiteManager: # Run gunicorn for the production server. gunicorn.app.wsgiapp.run() + def run_tests(self) -> None: + """Prepare and run the test suite.""" + self.prepare_environment() + call_command(*sys.argv[1:]) + def clean_up_static_files(build_folder: Path) -> None: """Recursively loop over the build directory and fix links.""" @@ -168,8 +176,12 @@ def clean_up_static_files(build_folder: Path) -> None: def main() -> None: """Entry point for Django management script.""" # Use the custom site manager for launching the server - if len(sys.argv) > 1 and sys.argv[1] == "run": - SiteManager(sys.argv).run_server() + if len(sys.argv) > 1 and sys.argv[1] in ("run", "test"): + manager = SiteManager(sys.argv) + if sys.argv[1] == "run": + manager.run_server() + elif sys.argv[1] == "test": + manager.run_tests() # Pass any others directly to standard management commands else: diff --git a/pydis_site/apps/api/migrations/0084_infraction_last_applied.py b/pydis_site/apps/api/migrations/0084_infraction_last_applied.py new file mode 100644 index 00000000..7704ddb8 --- /dev/null +++ b/pydis_site/apps/api/migrations/0084_infraction_last_applied.py @@ -0,0 +1,26 @@ +# Generated by Django 4.0.6 on 2022-07-27 20:32 + +import django.utils.timezone +from django.db import migrations, models +from django.apps.registry import Apps + + +def set_last_applied_to_inserted_at(apps: Apps, schema_editor): + Infractions = apps.get_model("api", "infraction") + Infractions.objects.all().update(last_applied=models.F("inserted_at")) + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0083_remove_embed_validation'), + ] + + operations = [ + migrations.AddField( + model_name='infraction', + name='last_applied', + field=models.DateTimeField(default=django.utils.timezone.now, help_text='The date and time of when this infraction was last applied.'), + ), + migrations.RunPython(set_last_applied_to_inserted_at) + ] diff --git a/pydis_site/apps/api/models/bot/infraction.py b/pydis_site/apps/api/models/bot/infraction.py index c9303024..218ee5ec 100644 --- a/pydis_site/apps/api/models/bot/infraction.py +++ b/pydis_site/apps/api/models/bot/infraction.py @@ -23,6 +23,12 @@ class Infraction(ModelReprMixin, models.Model): default=timezone.now, help_text="The date and time of the creation of this infraction." ) + last_applied = models.DateTimeField( + # This default is for backwards compatibility with bot versions + # that don't explicitly give a value. + default=timezone.now, + help_text="The date and time of when this infraction was last applied." + ) expires_at = models.DateTimeField( null=True, help_text=( diff --git a/pydis_site/apps/api/serializers.py b/pydis_site/apps/api/serializers.py index e53ccffa..9228c1f4 100644 --- a/pydis_site/apps/api/serializers.py +++ b/pydis_site/apps/api/serializers.py @@ -176,6 +176,7 @@ class InfractionSerializer(ModelSerializer): fields = ( 'id', 'inserted_at', + 'last_applied', 'expires_at', 'active', 'user', diff --git a/pyproject.toml b/pyproject.toml index 1c24d308..037f837c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -52,7 +52,7 @@ start = "python manage.py run --debug" makemigrations = "python manage.py makemigrations" django_shell = "python manage.py shell" test = "coverage run manage.py test" -coverage = "coverage run manage.py test --no-input; coverage report -m" +coverage = "coverage run manage.py test --no-input && coverage report -m" report = "coverage report -m" lint = "pre-commit run --all-files" precommit = "pre-commit install" |