parse and save email attachments
This commit is contained in:
parent
f9a95317a2
commit
734af10525
8 changed files with 357 additions and 76 deletions
|
@ -27,12 +27,16 @@ class FileManager(models.Manager):
|
|||
kwargs['file'] = ContentFile(content, content_hash)
|
||||
kwargs['hash'] = content_hash
|
||||
kwargs['mime_type'] = mime_type
|
||||
elif 'file' in kwargs and 'hash' in kwargs and type(kwargs['file']) == ContentFile and 'mime_type' in kwargs:
|
||||
pass
|
||||
else:
|
||||
raise ValueError('data must be a base64 encoded string or file and hash must be provided')
|
||||
try:
|
||||
return self.get(hash=kwargs['hash']), False
|
||||
except self.model.DoesNotExist:
|
||||
return self.create(**kwargs), True
|
||||
obj = super().create(**kwargs)
|
||||
obj.file.save(content=kwargs['file'], name=kwargs['hash'])
|
||||
return obj, True
|
||||
|
||||
def create(self, **kwargs):
|
||||
if 'data' in kwargs and type(kwargs['data']) == str:
|
||||
|
@ -51,23 +55,32 @@ class FileManager(models.Manager):
|
|||
kwargs['file'] = ContentFile(content, content_hash)
|
||||
kwargs['hash'] = content_hash
|
||||
kwargs['mime_type'] = mime_type
|
||||
elif 'file' in kwargs and 'hash' in kwargs and type(kwargs['file']) == ContentFile:
|
||||
elif 'file' in kwargs and 'hash' in kwargs and type(kwargs['file']) == ContentFile and 'mime_type' in kwargs:
|
||||
pass
|
||||
else:
|
||||
raise ValueError('data must be a base64 encoded string or file and hash must be provided')
|
||||
if not self.filter(hash=kwargs['hash']).exists():
|
||||
return super().create(**kwargs)
|
||||
obj = super().create(**kwargs)
|
||||
obj.file.save(content=kwargs['file'], name=kwargs['hash'])
|
||||
return obj
|
||||
else:
|
||||
raise IntegrityError('File with this hash already exists')
|
||||
|
||||
|
||||
class File(models.Model):
|
||||
item = models.ForeignKey(Item, models.CASCADE, db_column='iid', null=True, blank=True, related_name='files')
|
||||
class AbstractFile(models.Model):
|
||||
created_at = models.DateTimeField(blank=True, null=True)
|
||||
updated_at = models.DateTimeField(blank=True, null=True)
|
||||
deleted_at = models.DateTimeField(blank=True, null=True)
|
||||
file = models.ImageField(upload_to=hash_upload)
|
||||
file = models.FileField(upload_to=hash_upload)
|
||||
mime_type = models.CharField(max_length=255, null=False, blank=False)
|
||||
hash = models.CharField(max_length=64, null=False, blank=False, unique=True)
|
||||
|
||||
objects = FileManager()
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
|
||||
class File(AbstractFile):
|
||||
item = models.ForeignKey(Item, models.CASCADE, db_column='iid', null=True, blank=True, related_name='files')
|
||||
pass
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue