diff options
author | lxf <sng@luxagraf.net> | 2022-05-14 16:38:07 -0400 |
---|---|---|
committer | lxf <sng@luxagraf.net> | 2022-05-14 16:38:07 -0400 |
commit | bb3973ffb714c932e9ec6dd6a751228dc71fe1d3 (patch) | |
tree | 6fa32f9392ad2ec32271349b86a4c1388fd6ba95 /app/lib/upload/models.py |
initial commit
Diffstat (limited to 'app/lib/upload/models.py')
-rw-r--r-- | app/lib/upload/models.py | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/app/lib/upload/models.py b/app/lib/upload/models.py new file mode 100644 index 0000000..ae2dab4 --- /dev/null +++ b/app/lib/upload/models.py @@ -0,0 +1,48 @@ +import datetime +from django.db import models +from django.conf import settings +from django.template.defaultfilters import slugify +import mimetypes + +class FileUpload(models.Model): + upload_date = models.DateTimeField(auto_now_add=True) + upload = models.FileField(upload_to="images/%s" %(datetime.datetime.today().strftime("%Y"))) + title = models.CharField(max_length=100) + description = models.CharField(blank=True, max_length=200) + content_type = models.CharField(editable=False, max_length=100) + sub_type = models.CharField(editable=False, max_length=100) + + + + class Meta: + ordering = ['upload_date', 'title'] + + def __unicode__(self): + return self.title + + def mime_type(self): + return '%s/%s' % (self.content_type, self.sub_type) + + def type_slug(self): + return slugify(self.sub_type) + + def is_image(self): + if self.content_type == 'image': + return True + else: + return False + + def get_absolute_url(self): + return '%s%s' % (settings.IMAGES_URL, self.upload.url[33:]) + + def save(self, *args, **kwargs): + file_path = '%s%s' % (settings.MEDIA_ROOT, self.upload) + (mime_type, encoding) = mimetypes.guess_type(file_path) + try: + [self.content_type, self.sub_type] = mime_type.split('/') + except: + self.content_type = 'text' + self.sub_type = 'plain' + super(FileUpload, self).save(*args, **kwargs) + +
\ No newline at end of file |