[Patchew-devel] [PATCH 15/17] models: add property fields to Message

Paolo Bonzini posted 17 patches 2 years, 5 months ago

[Patchew-devel] [PATCH 15/17] models: add property fields to Message

Posted by Paolo Bonzini 2 years, 5 months ago
This is the first step towards storing properties in a single JSON
dictionary, which is simpler now that properties are fewer and smaller
than they used to be.

The related name "properties" has to be freed up for the new field.
---
 api/migrations/0051_auto_20190418_1346.py | 33 +++++++++++++++++++++++
 api/models.py                             |  9 ++++---
 2 files changed, 38 insertions(+), 4 deletions(-)
 create mode 100644 api/migrations/0051_auto_20190418_1346.py

diff --git a/api/migrations/0051_auto_20190418_1346.py b/api/migrations/0051_auto_20190418_1346.py
new file mode 100644
index 0000000..623da0f
--- /dev/null
+++ b/api/migrations/0051_auto_20190418_1346.py
@@ -0,0 +1,33 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.20 on 2019-04-18 13:46
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+import django.db.models.deletion
+import jsonfield.encoder
+import jsonfield.fields
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('api', '0050_message_flags_postgres_fts'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='message',
+            name='properties',
+            field=jsonfield.fields.JSONField(default={}, dump_kwargs={'cls': jsonfield.encoder.JSONEncoder, 'separators': (',', ':')}, load_kwargs={}),
+        ),
+        migrations.AddField(
+            model_name='project',
+            name='properties',
+            field=jsonfield.fields.JSONField(default={}, dump_kwargs={'cls': jsonfield.encoder.JSONEncoder, 'separators': (',', ':')}, load_kwargs={}),
+        ),
+        migrations.AlterField(
+            model_name='messageproperty',
+            name='message',
+            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='api.Message'),
+        ),
+    ]
diff --git a/api/models.py b/api/models.py
index 67d1ed5..dd385ad 100644
--- a/api/models.py
+++ b/api/models.py
@@ -172,6 +172,7 @@ class Project(models.Model):
                                                   "parent_project=NULL"))
     maintainers = models.ManyToManyField(User, blank=True)
     config = jsonfield.JSONField(default={})
+    properties = jsonfield.JSONField(default={})
 
     def __str__(self):
         return self.name
@@ -358,7 +359,7 @@ class MessageManager(models.Manager):
                 return None
         else:
             q = super(MessageManager, self).get_queryset()
-        return q.filter(is_series_head=True).prefetch_related('properties', 'project')
+        return q.filter(is_series_head=True).prefetch_related('messageproperty_set', 'project')
 
     def find_series(self, message_id, project_name=None):
         heads = self.series_heads(project_name)
@@ -520,6 +521,7 @@ class Message(models.Model):
 
     maintainers = jsonfield.JSONField(blank=True, default=[])
     flags = models.CharField(max_length=64, blank=True, default="")
+    properties = jsonfield.JSONField(default={})
 
     def save_mbox(self, mbox_blob):
         save_blob(mbox_blob, self.message_id)
@@ -631,7 +633,7 @@ class Message(models.Model):
                 # The prefetch cache is invalidated, query again
                 all_props = MessageProperty.objects.filter(message=self)
         else:
-            all_props = self.properties.all()
+            all_props = self.messageproperty_set.all()
         r = {}
         for m in all_props:
             r[m.name] = m.value
@@ -793,8 +795,7 @@ class MessageResult(Result):
 
 
 class MessageProperty(models.Model):
-    message = models.ForeignKey('Message', on_delete=models.CASCADE,
-                                related_name='properties')
+    message = models.ForeignKey('Message', on_delete=models.CASCADE)
     name = models.CharField(max_length=256)
     value = jsonfield.JSONField()
 
-- 
2.21.0


_______________________________________________
Patchew-devel mailing list
Patchew-devel@redhat.com
https://www.redhat.com/mailman/listinfo/patchew-devel