Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes #2098
- Loading branch information
Showing
6 changed files
with
503 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
""" | ||
Content related Django models. | ||
""" | ||
import hashlib | ||
|
||
from django.contrib.contenttypes import fields | ||
from django.db import models | ||
|
||
|
||
from pulp.platform.models import Model, MasterModel, Notes | ||
from pulp.platform.models.storage import StoragePath | ||
|
||
|
||
class Content(MasterModel): | ||
""" | ||
A piece of managed content. | ||
:cvar natural_key_fields: Tuple of natural fields. Must be: models.Field. | ||
:type natural_key_fields: tuple | ||
Fields: | ||
:cvar type: The content type. | ||
:type type: models.TextField | ||
Relations: | ||
:cvar notes: Arbitrary information stored with the content. | ||
:type notes: fields.GenericRelation | ||
""" | ||
natural_key_fields = () | ||
|
||
type = models.TextField(blank=False, default=None) | ||
|
||
notes = fields.GenericRelation(Notes) | ||
|
||
def natural_key(self): | ||
""" | ||
Get the model's natural key based on natural_key_fields. | ||
:return: The natural key. | ||
:rtype: tuple | ||
""" | ||
return (getattr(self, f.name) for f in self.natural_key_fields) | ||
|
||
def natural_key_digest(self): | ||
""" | ||
Get the SHA-256 digest of the natural key. | ||
The digest is updated with each field name followed by its value. | ||
The field names are only necessary to preserve backward compatibility | ||
with digests generated in pulp 2. | ||
:return: The hex digest. | ||
:rtype: str | ||
""" | ||
h = hashlib.sha256() | ||
for name in sorted(f.name for f in self.natural_key_fields): | ||
h.update(name) | ||
value = getattr(self, name) | ||
if not isinstance(value, basestring): | ||
h.update(str(value)) | ||
else: | ||
h.update(value) | ||
return h.hexdigest() | ||
|
||
|
||
class Artifact(Model): | ||
""" | ||
A file associated with a piece of content. | ||
Fields: | ||
:cvar file: The stored file. | ||
:type file: models.FileField | ||
:cvar downloaded: The associated file has been successfully downloaded. | ||
:type downloaded: BooleanField | ||
:cvar published_path: The relative published path. | ||
:type published_path: models.FileField | ||
:cvar size: The size of the file in bytes. | ||
:type size: models.IntegerField | ||
:cvar md5: The MD5 checksum of the file. | ||
:type md5: models.CharField | ||
:cvar sha1: The SHA-1 checksum of the file. | ||
:type sha1: models.CharField | ||
:cvar sha224: The SHA-224 checksum of the file. | ||
:type sha224: models.CharField | ||
:cvar sha256: The SHA-256 checksum of the file. | ||
:type sha256: models.CharField | ||
:cvar sha384: The SHA-384 checksum of the file. | ||
:type sha384: models.CharField | ||
:cvar sha512: The SHA-512 checksum of the file. | ||
:type sha512: models.CharField | ||
Relations: | ||
:cvar content: The associated content. | ||
:type content: Content | ||
""" | ||
|
||
# Note: The FileField does not support unique indexes and has | ||
# other undesirable behavior and complexities. Using a custom | ||
# field should be investigated. | ||
|
||
file = models.FileField(db_index=True, upload_to=StoragePath(), max_length=255) | ||
downloaded = models.BooleanField(db_index=True, default=False) | ||
published_path = models.TextField(db_index=True, blank=False, default=None) | ||
|
||
size = models.IntegerField(blank=True, null=True) | ||
|
||
md5 = models.CharField(max_length=32, blank=True, null=True) | ||
sha1 = models.CharField(max_length=40, blank=True, null=True) | ||
sha224 = models.CharField(max_length=56, blank=True, null=True) | ||
sha256 = models.CharField(max_length=64, blank=True, null=True) | ||
sha384 = models.CharField(max_length=96, blank=True, null=True) | ||
sha512 = models.CharField(max_length=128, blank=True, null=True) | ||
|
||
content = models.ForeignKey(Content, related_name='artifacts', on_delete=models.CASCADE) | ||
|
||
class Meta: | ||
unique_together = ('content', 'published_path') | ||
|
||
def delete(self, *args, **kwargs): | ||
if self.downloaded: | ||
self.file.delete() | ||
super(Artifact, self).delete(*args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.