Skip to content

Commit

Permalink
Add store additional package-index fields
Browse files Browse the repository at this point in the history
  • Loading branch information
m-bucher committed Jul 29, 2021
1 parent bac2a41 commit 0cad446
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGES/8232.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add custom_fields to hold non-standard PackageIndex fields
24 changes: 24 additions & 0 deletions pulp_deb/app/migrations/0015_add_custom_fields_to_packages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 2.2.20 on 2021-06-07 14:53

import django.contrib.postgres.fields.jsonb
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('deb', '0014_swap_distribution_model'),
]

operations = [
migrations.AddField(
model_name='installerpackage',
name='custom_fields',
field=django.contrib.postgres.fields.jsonb.JSONField(null=True),
),
migrations.AddField(
model_name='package',
name='custom_fields',
field=django.contrib.postgres.fields.jsonb.JSONField(null=True),
),
]
5 changes: 5 additions & 0 deletions pulp_deb/app/models/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

from django.db import models

# TODO: use django.db.models.JSONField after Django 3.1
from django.contrib.postgres.fields import JSONField

from pulpcore.plugin.models import Content


Expand Down Expand Up @@ -179,6 +182,8 @@ class BasePackage(Content):
# this digest is transferred to the content as a natural_key
sha256 = models.TextField(null=False)

custom_fields = JSONField(null=True)

@property
def name(self):
"""Print a nice name for Packages."""
Expand Down
24 changes: 20 additions & 4 deletions pulp_deb/app/serializers/content_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from debian import deb822, debfile

from rest_framework.serializers import CharField, Field, ValidationError
from rest_framework.serializers import CharField, DictField, Field, ValidationError
from pulpcore.plugin.models import Artifact, RemoteArtifact
from pulpcore.plugin.serializers import (
ContentChecksumSerializer,
Expand Down Expand Up @@ -218,6 +218,7 @@ class BasePackage822Serializer(SingleArtifactContentSerializer):
"provides": "Provides",
"replaces": "Replaces",
}
TRANSLATION_DICT_INV = {v: k for k, v in TRANSLATION_DICT.items()}

package = CharField()
source = CharField(required=False)
Expand Down Expand Up @@ -248,6 +249,7 @@ class BasePackage822Serializer(SingleArtifactContentSerializer):
pre_depends = CharField(required=False)
provides = CharField(required=False)
replaces = CharField(required=False)
custom_fields = DictField(child=CharField(), allow_empty=True, required=False)

def __init__(self, *args, **kwargs):
"""Initializer for BasePackage822Serializer."""
Expand All @@ -261,9 +263,17 @@ def from822(cls, data, **kwargs):
"""
Translate deb822.Package to a dictionary for class instatiation.
"""
return cls(
data={k: data[v] for k, v in cls.TRANSLATION_DICT.items() if v in data}, **kwargs
)
skip = ["Filename", "MD5sum", "Size", "SHA1", "SHA256", "SHA512"]
args = {"custom_fields": {}}
for k, v in data.items():
if k in cls.TRANSLATION_DICT_INV:
key = cls.TRANSLATION_DICT_INV[k]
args[key] = v
elif k not in skip:
# also save the fields not in TRANSLATION_DICT
args["custom_fields"][k] = v

return cls(data=args, **kwargs)

def to822(self, component=""):
"""Create deb822.Package object from model."""
Expand All @@ -274,6 +284,10 @@ def to822(self, component=""):
if value is not None:
ret[v] = value

custom_fields = self.data.get("custom_fields")
if custom_fields:
ret.update(custom_fields)

try:
artifact = self.instance._artifacts.get()
artifact.touch() # Orphan cleanup protection until we are done!
Expand Down Expand Up @@ -327,6 +341,7 @@ class Meta(SingleArtifactContentSerializer.Meta):
"pre_depends",
"provides",
"replaces",
"custom_fields",
)
model = BasePackage

Expand Down Expand Up @@ -383,6 +398,7 @@ class BasePackageSerializer(SingleArtifactContentUploadSerializer, ContentChecks
pre_depends = CharField(read_only=True)
provides = CharField(read_only=True)
replaces = CharField(read_only=True)
custom_fields = DictField(child=CharField(), allow_empty=True, required=False)

def __init__(self, *args, **kwargs):
"""Initializer for BasePackageSerializer."""
Expand Down

0 comments on commit 0cad446

Please sign in to comment.