diff --git a/CHANGELOG.md b/CHANGELOG.md index c02a4bbf..981803c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,13 @@ # Changelog All notable changes to this project will be documented in this file. +## Unreleased + +### Improvements +- Database schema: set Check.user to not null +- Database schema: add uniqueness constraint to Check.code + + ## 1.4.0 - 2018-12-25 ### Improvements diff --git a/hc/api/management/commands/prunepingsslow.py b/hc/api/management/commands/prunepingsslow.py index 1ceabf85..c1700412 100644 --- a/hc/api/management/commands/prunepingsslow.py +++ b/hc/api/management/commands/prunepingsslow.py @@ -20,7 +20,7 @@ class Command(BaseCommand): for user in User.objects.filter(profile=None): Profile.objects.get_or_create(user_id=user.id) - checks = Check.objects.filter(user__isnull=False) + checks = Check.objects checks = checks.annotate(limit=F("user__profile__ping_log_limit")) for check in checks: diff --git a/hc/api/migrations/0048_auto_20190102_0737.py b/hc/api/migrations/0048_auto_20190102_0737.py new file mode 100644 index 00000000..d636f261 --- /dev/null +++ b/hc/api/migrations/0048_auto_20190102_0737.py @@ -0,0 +1,19 @@ +# Generated by Django 2.1.4 on 2019-01-02 07:37 + +from django.db import migrations + + +def remove_anon_checks(apps, schema_editor): + Check = apps.get_model("api", "Check") + Check.objects.filter(user=None).delete() + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0047_auto_20181225_2315'), + ] + + operations = [ + migrations.RunPython(remove_anon_checks, migrations.RunPython.noop), + ] diff --git a/hc/api/migrations/0049_auto_20190102_0743.py b/hc/api/migrations/0049_auto_20190102_0743.py new file mode 100644 index 00000000..e3db9b0a --- /dev/null +++ b/hc/api/migrations/0049_auto_20190102_0743.py @@ -0,0 +1,26 @@ +# Generated by Django 2.1.4 on 2019-01-02 07:43 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion +import uuid + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0048_auto_20190102_0737'), + ] + + operations = [ + migrations.AlterField( + model_name='check', + name='code', + field=models.UUIDField(default=uuid.uuid4, editable=False, unique=True), + ), + migrations.AlterField( + model_name='check', + name='user', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/hc/api/models.py b/hc/api/models.py index aa9ff5b7..6dab0c88 100644 --- a/hc/api/models.py +++ b/hc/api/models.py @@ -64,9 +64,9 @@ def isostring(dt): class Check(models.Model): name = models.CharField(max_length=100, blank=True) tags = models.CharField(max_length=500, blank=True) - code = models.UUIDField(default=uuid.uuid4, editable=False, db_index=True) + code = models.UUIDField(default=uuid.uuid4, editable=False, unique=True) desc = models.TextField(blank=True) - user = models.ForeignKey(User, models.CASCADE, blank=True, null=True) + user = models.ForeignKey(User, models.CASCADE) created = models.DateTimeField(auto_now_add=True) kind = models.CharField(max_length=10, default="simple", choices=CHECK_KINDS) diff --git a/hc/api/tests/test_check_model.py b/hc/api/tests/test_check_model.py index 858e3954..ea0ee595 100644 --- a/hc/api/tests/test_check_model.py +++ b/hc/api/tests/test_check_model.py @@ -1,11 +1,11 @@ from datetime import datetime, timedelta -from django.test import TestCase from django.utils import timezone from hc.api.models import Check +from hc.test import BaseTestCase -class CheckModelTestCase(TestCase): +class CheckModelTestCase(BaseTestCase): def test_it_strips_tags(self): check = Check() @@ -155,11 +155,12 @@ class CheckModelTestCase(TestCase): dt = timezone.make_aware(datetime(2000, 1, 1), timezone=timezone.utc) # Expect ping every round hour - check = Check() + check = Check(user=self.alice) check.kind = "cron" check.schedule = "0 * * * *" check.status = "up" check.last_ping = dt + # Need to save it for M2M relations to work: check.save() d = check.to_dict() diff --git a/hc/api/tests/test_ping.py b/hc/api/tests/test_ping.py index 193318ce..1d3223f1 100644 --- a/hc/api/tests/test_ping.py +++ b/hc/api/tests/test_ping.py @@ -1,15 +1,16 @@ from datetime import timedelta as td -from django.test import Client, TestCase +from django.test import Client from django.utils.timezone import now from hc.api.models import Check, Flip, Ping +from hc.test import BaseTestCase -class PingTestCase(TestCase): +class PingTestCase(BaseTestCase): def setUp(self): - super(PingTestCase, self).setUp() - self.check = Check.objects.create() + super().setUp() + self.check = Check.objects.create(user=self.alice) def test_it_works(self): r = self.client.get("/ping/%s/" % self.check.code) diff --git a/hc/front/tests/test_ping_details.py b/hc/front/tests/test_ping_details.py index 3c9108ad..1b26e8b1 100644 --- a/hc/front/tests/test_ping_details.py +++ b/hc/front/tests/test_ping_details.py @@ -34,11 +34,6 @@ class LastPingTestCase(BaseTestCase): r = self.client.get("/checks/%s/last_ping/" % check.code) self.assertContains(r, "/start", status_code=200) - def test_it_requires_user(self): - check = Check.objects.create() - r = self.client.get("/checks/%s/last_ping/" % check.code) - self.assertEqual(r.status_code, 404) - def test_it_accepts_n(self): check = Check(user=self.alice) check.save()