Skip to content
This repository has been archived by the owner on Dec 7, 2022. It is now read-only.

Commit

Permalink
Prepend pulpcore Content model fields with _
Browse files Browse the repository at this point in the history
Prepend all pulpcore model fields in the Content model hierarchy with _
(e.g. '_type', '_id', '_notes')

Required PR: pulp/pulp_file#148

ref #4206
https://pulp.plan.io/issues/4206
  • Loading branch information
CodeHeeler authored and daviddavis committed Dec 19, 2018
1 parent 6e4e1bd commit cf30f2e
Show file tree
Hide file tree
Showing 17 changed files with 90 additions and 89 deletions.
27 changes: 14 additions & 13 deletions pulpcore/app/models/base.py
Expand Up @@ -6,16 +6,17 @@ class Model(models.Model):
"""Base model class for all Pulp models.
Fields:
created (models.DateTimeField): Created timestamp UTC.
last_updated (models.DateTimeField): Last updated timestamp UTC.
_created (models.DateTimeField): Created timestamp UTC.
_last_updated (models.DateTimeField): Last updated timestamp UTC.
References:
* https://docs.djangoproject.com/en/1.8/topics/db/models/#automatic-primary-key-fields
"""
created = models.DateTimeField(auto_now_add=True)
last_updated = models.DateTimeField(auto_now=True, null=True)
_id = models.AutoField(primary_key=True)
_created = models.DateTimeField(auto_now_add=True)
_last_updated = models.DateTimeField(auto_now=True, null=True)

class Meta:
abstract = True
Expand All @@ -40,12 +41,12 @@ class MasterModel(Model):
Attributes:
TYPE (str): Default constant value saved into the ``type``
TYPE (str): Default constant value saved into the ``_type``
field of Model instances
Fields:
type: The user-facing string identifying the detail type of this model
_type: The user-facing string identifying the detail type of this model
Warning:
Subclasses of this class rely on there being no other parent/child Model
Expand All @@ -61,25 +62,25 @@ class MasterModel(Model):
# It can also be used for filtering across a relation where a model is related to a Master
# model. Set this to something reasonable in Master and Detail model classes, e.g. when
# create a master model, like "Remote", its TYPE value could be "remote". Then, when
# creating a Remote Detail class like PackageRemote, its type value could be "package",
# creating a Remote Detail class like PackageRemote, its _type value could be "package",
# not "package_remote", since "package_remote" would be redundant in the context of
# a remote Master model.
TYPE = None

# This field must have a value when models are saved, and defaults to the value of
# the TYPE attribute on the Model being saved (seen above).
type = models.TextField(null=False, default=None)
_type = models.TextField(null=False, default=None)

class Meta:
abstract = True

def save(self, *args, **kwargs):
# instances of "detail" models that subclass MasterModel are exposed
# on instances of MasterModel by the string stored in that model's TYPE attr.
# Storing this type in a column on the MasterModel next to makes it trivial
# Storing this _type in a column on the MasterModel next to makes it trivial
# to filter for specific detail model types across master's relations.
if not self.type:
self.type = self.TYPE
if not self._type:
self._type = self.TYPE
return super().save(*args, **kwargs)

def cast(self):
Expand Down Expand Up @@ -122,9 +123,9 @@ def __str__(self):
return super().__str__()

try:
return '<{} (type={}): {}>'.format(self._meta.object_name, cast.TYPE, cast.name)
return '<{} (_type={}): {}>'.format(self._meta.object_name, cast.TYPE, cast.name)
except AttributeError:
return '<{} (type={}): pk={}>'.format(self._meta.object_name, cast.TYPE, cast.pk)
return '<{} (_type={}): pk={}>'.format(self._meta.object_name, cast.TYPE, cast.pk)


# Add properties to model _meta info to support master/detail models
Expand Down
6 changes: 3 additions & 3 deletions pulpcore/app/models/content.py
Expand Up @@ -240,11 +240,11 @@ class Content(MasterModel, QueryMixin):
Relations:
artifacts (models.ManyToManyField): Artifacts related to Content through ContentArtifact
_artifacts (models.ManyToManyField): Artifacts related to Content through ContentArtifact
"""
TYPE = 'content'

artifacts = models.ManyToManyField(Artifact, through='ContentArtifact')
_artifacts = models.ManyToManyField(Artifact, through='ContentArtifact')

objects = BulkCreateManager()

Expand Down Expand Up @@ -282,7 +282,7 @@ class ContentArtifact(Model, QueryMixin):
"""
A relationship between a Content and an Artifact.
Serves as a through model for the 'artifacts' ManyToManyField in Content.
Serves as a through model for the '_artifacts' ManyToManyField in Content.
Artifact is protected from deletion if it's present in a ContentArtifact relationship.
"""
artifact = models.ForeignKey(Artifact, on_delete=models.PROTECT, null=True)
Expand Down
6 changes: 3 additions & 3 deletions pulpcore/app/models/task.py
Expand Up @@ -301,7 +301,7 @@ def set_running(self):
This updates the :attr:`started_at` and sets the :attr:`state` to :attr:`RUNNING`.
"""
if self.state != TASK_STATES.WAITING:
_logger.warning(_('Task __call__() occurred but Task %s is not at WAITING') % self.id)
_logger.warning(_('Task __call__() occurred but Task %s is not at WAITING') % self.pk)
self.state = TASK_STATES.RUNNING
self.started_at = timezone.now()
self.save()
Expand All @@ -321,7 +321,7 @@ def set_completed(self):
self.state = TASK_STATES.COMPLETED
else:
msg = _('Task set_completed() occurred but Task %s is already in final state')
_logger.warning(msg % self.id)
_logger.warning(msg % self.pk)

self.save()

Expand All @@ -348,7 +348,7 @@ def release_resources(self):
longer has any tasks reserving it, delete it.
"""
for reservation in self.reserved_resources.all():
TaskReservedResource.objects.filter(task=self.id).delete()
TaskReservedResource.objects.filter(task=self.pk).delete()
if not reservation.tasks.exists():
reservation.delete()

Expand Down
8 changes: 4 additions & 4 deletions pulpcore/app/serializers/base.py
Expand Up @@ -33,9 +33,9 @@ class ModelSerializer(QueryFieldsMixin, serializers.HyperlinkedModelSerializer):
"""

class Meta:
fields = ('_href', 'created')
fields = ('_href', '_created')

created = serializers.DateTimeField(
_created = serializers.DateTimeField(
help_text=_('Timestamp of creation.'),
read_only=True
)
Expand Down Expand Up @@ -102,10 +102,10 @@ class Meta:
as-needed.
"""
type = serializers.CharField(read_only=True)
_type = serializers.CharField(read_only=True)

class Meta:
fields = ModelSerializer.Meta.fields + ('type',)
fields = ModelSerializer.Meta.fields + ('_type',)


class MatchingNullViewName(object):
Expand Down
4 changes: 2 additions & 2 deletions pulpcore/app/serializers/content.py
Expand Up @@ -14,15 +14,15 @@
class ContentSerializer(base.MasterModelSerializer):
_href = base.DetailIdentityField()

artifacts = fields.ContentArtifactsField(
_artifacts = fields.ContentArtifactsField(
help_text=_("A dict mapping relative paths inside the Content to the corresponding"
"Artifact URLs. E.g.: {'relative/path': "
"'/artifacts/1/'"),
)

class Meta:
model = models.Content
fields = base.MasterModelSerializer.Meta.fields + ('artifacts',)
fields = base.MasterModelSerializer.Meta.fields + ('_artifacts',)


class ArtifactSerializer(base.ModelSerializer):
Expand Down
2 changes: 1 addition & 1 deletion pulpcore/app/serializers/fields.py
Expand Up @@ -19,7 +19,7 @@ class ContentRelatedField(DetailRelatedField):

class ContentArtifactsField(serializers.DictField):
"""
A serializer field for the 'artifacts' ManyToManyField on the Content model.
A serializer field for the '_artifacts' ManyToManyField on the Content model.
"""

def run_validation(self, data):
Expand Down
50 changes: 25 additions & 25 deletions pulpcore/app/serializers/repository.py
Expand Up @@ -105,7 +105,7 @@ class RemoteSerializer(MasterModelSerializer):
required=False,
allow_blank=True,
)
last_updated = serializers.DateTimeField(
_last_updated = serializers.DateTimeField(
help_text='Timestamp of the most recent update of the remote.',
read_only=True
)
Expand All @@ -126,8 +126,8 @@ class Meta:
model = models.Remote
fields = MasterModelSerializer.Meta.fields + (
'name', 'url', 'validate', 'ssl_ca_certificate', 'ssl_client_certificate',
'ssl_client_key', 'ssl_validation', 'proxy_url', 'username', 'password', 'last_updated',
'connection_limit', 'policy')
'ssl_client_key', 'ssl_validation', 'proxy_url', 'username', 'password',
'_last_updated', 'connection_limit', 'policy')


class RepositorySyncURLSerializer(serializers.Serializer):
Expand Down Expand Up @@ -159,7 +159,7 @@ class PublisherSerializer(MasterModelSerializer):
help_text=_('A unique name for this publisher.'),
validators=[UniqueValidator(queryset=models.Publisher.objects.all())]
)
last_updated = serializers.DateTimeField(
_last_updated = serializers.DateTimeField(
help_text=_('Timestamp of the most recent update of the publisher configuration.'),
read_only=True
)
Expand All @@ -173,7 +173,7 @@ class Meta:
abstract = True
model = models.Publisher
fields = MasterModelSerializer.Meta.fields + (
'name', 'last_updated', 'distributions',
'name', '_last_updated', 'distributions',
)


Expand Down Expand Up @@ -229,7 +229,7 @@ class ExporterSerializer(MasterModelSerializer):
help_text=_('The exporter unique name.'),
validators=[UniqueValidator(queryset=models.Exporter.objects.all())]
)
last_updated = serializers.DateTimeField(
_last_updated = serializers.DateTimeField(
help_text=_('Timestamp of the last update.'),
read_only=True
)
Expand All @@ -243,7 +243,7 @@ class Meta:
model = models.Exporter
fields = MasterModelSerializer.Meta.fields + (
'name',
'last_updated',
'_last_updated',
'last_export',
)

Expand Down Expand Up @@ -455,30 +455,30 @@ def get_content_summary(self, obj):
The summary of contained content.
Returns:
dict: of {<type>: <count>}
dict: of {<_type>: <count>}
"""
annotated = obj.content.values('type').annotate(count=Count('type'))
return {c['type']: c['count'] for c in annotated}
annotated = obj.content.values('_type').annotate(count=Count('_type'))
return {c['_type']: c['count'] for c in annotated}

def get_content_added_summary(self, obj):
"""
The summary of added content.
Returns:
dict: of {<type>: <count>}
dict: of {<_type>: <count>}
"""
annotated = obj.added().values('type').annotate(count=Count('type'))
return {c['type']: c['count'] for c in annotated}
annotated = obj.added().values('_type').annotate(count=Count('_type'))
return {c['_type']: c['count'] for c in annotated}

def get_content_removed_summary(self, obj):
"""
The summary of removed content.
Returns:
dict: of {<type>: <count>}
dict: of {<_type>: <count>}
"""
annotated = obj.removed().values('type').annotate(count=Count('type'))
return {c['type']: c['count'] for c in annotated}
annotated = obj.removed().values('_type').annotate(count=Count('_type'))
return {c['_type']: c['count'] for c in annotated}

def get_content_hrefs(self, obj):
"""
Expand All @@ -492,12 +492,12 @@ def get_content_hrefs(self, obj):
obj (pulpcore.app.models.RepositoryVersion): The RepositoryVersion being serialized.
Returns:
dict: {<type>: <url>}
dict: {<_type>: <url>}
"""
content_urls = {}

for ctype in obj.content.values_list('type', flat=True):
ctype_model = obj.content.filter(type=ctype).first().cast().__class__
for ctype in obj.content.values_list('_type', flat=True):
ctype_model = obj.content.filter(_type=ctype).first().cast().__class__
ctype_view = get_view_name_for_model(ctype_model, 'list')
try:
ctype_url = reverse(ctype_view)
Expand Down Expand Up @@ -525,12 +525,12 @@ def get_content_added_hrefs(self, obj):
obj (pulpcore.app.models.RepositoryVersion): The RepositoryVersion being serialized.
Returns:
dict: {<type>: <url>}
dict: {<_type>: <url>}
"""
content_urls = {}

for ctype in obj.added().values_list('type', flat=True):
ctype_model = obj.content.filter(type=ctype).first().cast().__class__
for ctype in obj.added().values_list('_type', flat=True):
ctype_model = obj.content.filter(_type=ctype).first().cast().__class__
ctype_view = get_view_name_for_model(ctype_model, 'list')
try:
ctype_url = reverse(ctype_view)
Expand Down Expand Up @@ -558,12 +558,12 @@ def get_content_removed_hrefs(self, obj):
obj (pulpcore.app.models.RepositoryVersion): The RepositoryVersion being serialized.
Returns:
dict: {<type>: <url>}
dict: {<_type>: <url>}
"""
content_urls = {}

for ctype in obj.removed().values_list('type', flat=True):
ctype_model = obj.content.filter(type=ctype).first().cast().__class__
for ctype in obj.removed().values_list('_type', flat=True):
ctype_model = obj.content.filter(_type=ctype).first().cast().__class__
ctype_view = get_view_name_for_model(ctype_model, 'list')
try:
ctype_url = reverse(ctype_view)
Expand Down
4 changes: 2 additions & 2 deletions pulpcore/app/tasks/base.py
Expand Up @@ -26,7 +26,7 @@ def general_update(instance_id, app_label, serializer_name, *args, **kwargs):
data = kwargs.pop('data', None)
partial = kwargs.pop('partial', False)
serializer_class = get_plugin_config(app_label).named_serializers[serializer_name]
instance = serializer_class.Meta.model.objects.get(id=instance_id).cast()
instance = serializer_class.Meta.model.objects.get(pk=instance_id).cast()
serializer = serializer_class(instance, data=data, partial=partial)
serializer.is_valid(raise_exception=True)
serializer.save()
Expand All @@ -44,5 +44,5 @@ def general_delete(instance_id, app_label, serializer_name):
serializer_name (str): name of the serializer class for the model
"""
serializer_class = get_plugin_config(app_label).named_serializers[serializer_name]
instance = serializer_class.Meta.model.objects.get(id=instance_id).cast()
instance = serializer_class.Meta.model.objects.get(pk=instance_id).cast()
instance.delete()
6 changes: 3 additions & 3 deletions pulpcore/app/viewsets/base.py
Expand Up @@ -27,8 +27,8 @@
# /?name__in=foo,bar
DATETIME_FILTER_OPTIONS = ['lt', 'lte', 'gt', 'gte', 'range']
# e.g.
# /?created__gte=2018-04-12T19:45:52
# /?created__range=2018-04-12T19:45:52,2018-04-13T19:45:52
# /?_created__gte=2018-04-12T19:45:52
# /?_created__range=2018-04-12T19:45:52,2018-04-13T19:45:52


class DefaultSchema(AutoSchema):
Expand Down Expand Up @@ -338,7 +338,7 @@ class BaseFilterSet(filterset.FilterSet):
filter. However, this can be overriden by setting a help_text dict with the the field name
mapped to some help text:
help_text = {'name__in': 'Lorem ipsum dolor', 'last_updated__lt': 'blah blah'}
help_text = {'name__in': 'Lorem ipsum dolor', '_last_updated__lt': 'blah blah'}
"""
help_text = {}
Expand Down
6 changes: 3 additions & 3 deletions pulpcore/app/viewsets/content.py
Expand Up @@ -80,7 +80,7 @@ class ContentFilter(BaseFilterSet):
class Meta:
model = Content
fields = {
'type': ['exact', 'in'],
'_type': ['exact', 'in'],
'repository_version': ['exact'],
'repository_version_added': ['exact'],
'repository_version_removed': ['exact'],
Expand All @@ -103,10 +103,10 @@ def create(self, request):
"""
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
artifacts = serializer.validated_data.pop('artifacts')
_artifacts = serializer.validated_data.pop('_artifacts')
content = serializer.save()

for relative_path, artifact in artifacts.items():
for relative_path, artifact in _artifacts.items():
ca = ContentArtifact(artifact=artifact, content=content, relative_path=relative_path)
ca.save()

Expand Down

0 comments on commit cf30f2e

Please sign in to comment.