mirror of
https://github.com/daniviga/django-ram.git
synced 2025-08-05 05:37:50 +02:00
Add documents to decoders (#22)
* Add decoder documents support * Use abstract model for Documents * Increase version * Code cleanup
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
from ram.utils import git_suffix
|
||||
|
||||
__version__ = "0.3.3"
|
||||
__version__ = "0.4.0"
|
||||
__version__ += git_suffix(__file__)
|
||||
|
30
ram/ram/models.py
Normal file
30
ram/ram/models.py
Normal file
@@ -0,0 +1,30 @@
|
||||
import os
|
||||
|
||||
from django.db import models
|
||||
from django.utils.safestring import mark_safe
|
||||
|
||||
from ram.utils import DeduplicatedStorage
|
||||
|
||||
|
||||
class Document(models.Model):
|
||||
description = models.CharField(max_length=128, blank=True)
|
||||
file = models.FileField(
|
||||
upload_to="files/",
|
||||
storage=DeduplicatedStorage(),
|
||||
null=True,
|
||||
blank=True,
|
||||
)
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
def __str__(self):
|
||||
return "{0}".format(os.path.basename(self.file.name))
|
||||
|
||||
def filename(self):
|
||||
return self.__str__()
|
||||
|
||||
def download(self):
|
||||
return mark_safe(
|
||||
'<a href="{0}" target="_blank">Link</a>'.format(self.file.url)
|
||||
)
|
@@ -16,7 +16,7 @@ class DeduplicatedStorage(FileSystemStorage):
|
||||
|
||||
def save(self, name, content, max_length=None):
|
||||
if super().exists(name):
|
||||
new = hashlib.sha256(content.file.getbuffer()).hexdigest()
|
||||
new = hashlib.sha256(content.read()).hexdigest()
|
||||
with open(super().path(name), "rb") as file:
|
||||
file_binary = file.read()
|
||||
old = hashlib.sha256(file_binary).hexdigest()
|
||||
|
Reference in New Issue
Block a user