diff mbox series

[layerindex-web,1/5] layerindex/models: add BBClassRecipe BBClassGlobal

Message ID c2a8e070204493d04d0bd7d4c8e53718a1b47379.1705982792.git.tim.orling@konsulko.com
State New
Headers show
Series [layerindex-web,1/5] layerindex/models: add BBClassRecipe BBClassGlobal | expand

Commit Message

Tim Orling Jan. 23, 2024, 4:15 a.m. UTC
Add support for classes-global and classes-recipe, but use
"proxy=True" to not create new tables for the new classes.
Rather, we use the bbclass_type field.

[YOCTO #15238]

Signed-off-by: Tim Orling <tim.orling@konsulko.com>
---
 ...obal_bbclassrecipe_bbclass_bbclass_type.py | 41 ++++++++++++++++++
 layerindex/models.py                          | 43 ++++++++++++++++++-
 2 files changed, 83 insertions(+), 1 deletion(-)
 create mode 100644 layerindex/migrations/0048_bbclassglobal_bbclassrecipe_bbclass_bbclass_type.py
diff mbox series

Patch

diff --git a/layerindex/migrations/0048_bbclassglobal_bbclassrecipe_bbclass_bbclass_type.py b/layerindex/migrations/0048_bbclassglobal_bbclassrecipe_bbclass_bbclass_type.py
new file mode 100644
index 0000000..dba7927
--- /dev/null
+++ b/layerindex/migrations/0048_bbclassglobal_bbclassrecipe_bbclass_bbclass_type.py
@@ -0,0 +1,41 @@ 
+# Generated by Django 4.2.9 on 2024-01-20 21:20
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+    dependencies = [
+        ("layerindex", "0047_layerbranch_updates_enabled"),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name="BBClassGlobal",
+            fields=[],
+            options={
+                "proxy": True,
+                "indexes": [],
+                "constraints": [],
+            },
+            bases=("layerindex.bbclass",),
+        ),
+        migrations.CreateModel(
+            name="BBClassRecipe",
+            fields=[],
+            options={
+                "proxy": True,
+                "indexes": [],
+                "constraints": [],
+            },
+            bases=("layerindex.bbclass",),
+        ),
+        migrations.AddField(
+            model_name="bbclass",
+            name="bbclass_type",
+            field=models.CharField(
+                choices=[("", "Class"), ("Global", "Global"), ("Recipe", "Recipe")],
+                default="",
+                max_length=25,
+            ),
+        ),
+    ]
diff --git a/layerindex/models.py b/layerindex/models.py
index 676f05f..c52927f 100644
--- a/layerindex/models.py
+++ b/layerindex/models.py
@@ -784,18 +784,59 @@  class BBAppend(models.Model):
 class BBClass(models.Model):
     layerbranch = models.ForeignKey(LayerBranch, on_delete=models.CASCADE)
     name = models.CharField(max_length=100)
+    # Type field
+    TYPE_CHOICES = [
+        ('', 'Class'),
+        ('Global', 'Global'),
+        ('Recipe', 'Recipe')
+        ]
+    bbclass_type = models.CharField(max_length=25, choices=TYPE_CHOICES, default='')
 
     class Meta:
         verbose_name = "Class"
         verbose_name_plural = "Classes"
 
     def vcs_web_url(self):
-        url = self.layerbranch.file_url(os.path.join('classes', "%s.bbclass" % self.name))
+        # We cannot override vcs_web_url in the sub-classes without defining self.url
+        # but we want url to be dynamic
+        if self.bbclass_type == 'Global':
+            url = self.layerbranch.file_url(os.path.join('classes-global', "%s.bbclass" % self.name))
+        elif self.bbclass_type == 'Recipe':
+            url = self.layerbranch.file_url(os.path.join('classes-recipe', "%s.bbclass" % self.name))
+        else:
+            url = self.layerbranch.file_url(os.path.join('classes', "%s.bbclass" % self.name))
         return url or ''
 
     def __str__(self):
         return '%s (%s)' % (self.name, self.layerbranch.layer.name)
 
+class BBClassGlobalManager(models.Manager):
+    def get_queryset(self):
+        return super().get_queryset().filter(bbclass_type='Global')
+
+class BBClassGlobal(BBClass):
+    objects = BBClassGlobalManager()
+
+    class Meta:
+        proxy = True
+
+    def save(self, *args, **kwargs):
+        self.bbclass_type = 'Global'
+        return super(BBClassGlobal, self).save(*args, **kwargs)
+
+class BBClassRecipeManager(models.Manager):
+    def get_queryset(self):
+        return super().get_queryset().filter(bbclass_type='Recipe')
+
+class BBClassRecipe(BBClass):
+    objects = BBClassRecipeManager()
+
+    class Meta:
+        proxy = True
+
+    def save(self, *args, **kwargs):
+        self.bbclass_type = 'Recipe'
+        return super(BBClassRecipe, self).save(*args, **kwargs)
 
 class IncFile(models.Model):
     layerbranch = models.ForeignKey(LayerBranch, on_delete=models.CASCADE)