-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
176 additions
and
19 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
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
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,30 @@ | ||
from django.db import models | ||
|
||
from anchor.models.base import BaseModel | ||
from anchor.models.fields import SingleAttachmentField | ||
|
||
|
||
class VariantRecord(BaseModel): | ||
class Meta: | ||
verbose_name = "variant record" | ||
verbose_name_plural = "variant records" | ||
indexes = ( | ||
models.Index( | ||
fields=["blob", "variation_digest"], | ||
name="ix_anchor_records_blob_digest", | ||
), | ||
) | ||
|
||
blob = models.ForeignKey( | ||
"anchor.Blob", | ||
on_delete=models.CASCADE, | ||
related_name="variant_records", | ||
db_index=False, | ||
verbose_name="blob", | ||
) | ||
variation_digest = models.CharField(max_length=32, verbose_name="variation digest") | ||
image = SingleAttachmentField() | ||
|
||
def delete(self): | ||
self.image.delete() | ||
super().delete() |
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,41 @@ | ||
from contextlib import contextmanager | ||
|
||
from django.core.files import File | ||
|
||
from anchor.models.variant import Variant | ||
from anchor.models.variant_record import VariantRecord | ||
|
||
|
||
class VariantWithRecord(Variant): | ||
@property | ||
def is_processed(self) -> bool: | ||
return self.record is not None | ||
|
||
def image(self): | ||
return self.record.image if self.record else None | ||
|
||
def delete(self): | ||
if self.record: | ||
self.record.delete() | ||
|
||
super().delete() | ||
|
||
@property | ||
def record(self) -> VariantRecord: | ||
return VariantRecord.objects.filter( | ||
blob=self.blob, variation_digest=self.variation.digest | ||
).first() | ||
|
||
def get_or_create_record(self, image: File) -> VariantRecord: | ||
record = VariantRecord.objects.get_or_create( | ||
blob=self.blob, variation_digest=self.variation.digest | ||
)[0] | ||
record.image = image | ||
return record | ||
|
||
@contextmanager | ||
def process(self): | ||
with super().process() as image: | ||
self.get_or_create_record(image) | ||
image.seek(0) | ||
yield image |
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
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