Skip to content

Commit 7fcd568

Browse files
authored
Integrations: add created and updated fields to model (#11067)
* Integrations: add created and updated fields to model * Set fields as nullable * Add data migration * Format
1 parent ae09228 commit 7fcd568

File tree

3 files changed

+75
-6
lines changed

3 files changed

+75
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Generated by Django 4.2.9 on 2024-01-25 17:46
2+
3+
import django.utils.timezone
4+
import django_extensions.db.fields
5+
from django.db import migrations
6+
7+
8+
class Migration(migrations.Migration):
9+
dependencies = [
10+
("integrations", "0010_remove_old_jsonfields"),
11+
]
12+
13+
operations = [
14+
migrations.AlterModelOptions(
15+
name="integration",
16+
options={"get_latest_by": "modified"},
17+
),
18+
migrations.AddField(
19+
model_name="integration",
20+
name="created",
21+
field=django_extensions.db.fields.CreationDateTimeField(
22+
auto_now_add=True,
23+
default=django.utils.timezone.now,
24+
verbose_name="created",
25+
null=True,
26+
blank=True,
27+
),
28+
preserve_default=False,
29+
),
30+
migrations.AddField(
31+
model_name="integration",
32+
name="modified",
33+
field=django_extensions.db.fields.ModificationDateTimeField(
34+
auto_now=True,
35+
verbose_name="modified",
36+
null=True,
37+
blank=True,
38+
),
39+
),
40+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 4.2.9 on 2024-01-29 19:16
2+
3+
from django.db import migrations
4+
from django.utils import timezone
5+
6+
7+
def forwards_func(apps, schema_editor):
8+
Integration = apps.get_model("integrations", "Integration")
9+
Integration.objects.filter(created=None).update(created=timezone.now())
10+
Integration.objects.filter(modified=None).update(modified=timezone.now())
11+
12+
13+
class Migration(migrations.Migration):
14+
dependencies = [
15+
("integrations", "0011_add_created_and_updated_fields"),
16+
]
17+
18+
operations = [migrations.RunPython(forwards_func)]

readthedocs/integrations/models.py

+17-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
"""Integration models for external services."""
2-
32
import json
43
import re
54
import uuid
65

7-
from django.contrib.contenttypes.fields import (
8-
GenericForeignKey,
9-
GenericRelation,
10-
)
6+
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
117
from django.contrib.contenttypes.models import ContentType
128
from django.db import models, transaction
139
from django.utils.crypto import get_random_string
1410
from django.utils.safestring import mark_safe
1511
from django.utils.translation import gettext_lazy as _
12+
from django_extensions.db.fields import CreationDateTimeField, ModificationDateTimeField
13+
from django_extensions.db.models import TimeStampedModel
1614
from pygments import highlight
1715
from pygments.formatters import HtmlFormatter
1816
from pygments.lexers import JsonLexer
@@ -262,7 +260,7 @@ def create(self, **kwargs):
262260
return obj
263261

264262

265-
class Integration(models.Model):
263+
class Integration(TimeStampedModel):
266264

267265
"""Inbound webhook integration for projects."""
268266

@@ -280,6 +278,19 @@ class Integration(models.Model):
280278

281279
INTEGRATIONS = WEBHOOK_INTEGRATIONS
282280

281+
# Overridden from TimeStampedModel just to allow null values.
282+
# TODO: remove after deploy.
283+
created = CreationDateTimeField(
284+
_("created"),
285+
null=True,
286+
blank=True,
287+
)
288+
modified = ModificationDateTimeField(
289+
_("modified"),
290+
null=True,
291+
blank=True,
292+
)
293+
283294
project = models.ForeignKey(
284295
Project,
285296
related_name="integrations",

0 commit comments

Comments
 (0)