Browse Source

set Check.user to not null, add uniqueness constraint to Check.code

pull/211/head
Pēteris Caune 6 years ago
parent
commit
be4c4f7a26
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
8 changed files with 64 additions and 15 deletions
  1. +7
    -0
      CHANGELOG.md
  2. +1
    -1
      hc/api/management/commands/prunepingsslow.py
  3. +19
    -0
      hc/api/migrations/0048_auto_20190102_0737.py
  4. +26
    -0
      hc/api/migrations/0049_auto_20190102_0743.py
  5. +2
    -2
      hc/api/models.py
  6. +4
    -3
      hc/api/tests/test_check_model.py
  7. +5
    -4
      hc/api/tests/test_ping.py
  8. +0
    -5
      hc/front/tests/test_ping_details.py

+ 7
- 0
CHANGELOG.md View File

@ -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


+ 1
- 1
hc/api/management/commands/prunepingsslow.py View File

@ -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:


+ 19
- 0
hc/api/migrations/0048_auto_20190102_0737.py View File

@ -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),
]

+ 26
- 0
hc/api/migrations/0049_auto_20190102_0743.py View File

@ -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),
),
]

+ 2
- 2
hc/api/models.py View File

@ -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)


+ 4
- 3
hc/api/tests/test_check_model.py View File

@ -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()


+ 5
- 4
hc/api/tests/test_ping.py View File

@ -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)


+ 0
- 5
hc/front/tests/test_ping_details.py View File

@ -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()


Loading…
Cancel
Save