Commits

Viliam Segeda committed 56fcf31

new image file name convention

  • Participants
  • Parent commits 546b4b6

Comments (0)

Files changed (3)

File reports/models.py

+import os
 from collections import Counter
 from datetime import timedelta
 from django.core.exceptions import ValidationError, NON_FIELD_ERRORS
 from countries.utils import current_country_id
 from conf import DISTANCE_UNIT_CHOICES, TEAM_CHOICES, NOTEAM
 from utils import make_title, make_stamp
-from reports.utils import make_location_uuid
+from reports.utils import make_location_uuid, get_folder, image_subpath
 from tasks import generate_report_thumbnails, generate_missing_thumbnail, delete_report_thumbnails
 
 
         ordering = ('team','position',)
 
     def __unicode__(self):
-        return u"({0})-{1}-{2}".format(self.report_id, self.team, self.position)
-
+        return u"({0})-{1}".format(self.report_id, self.position)
+    
+    #TODO:  move image file naming and moving here
+#     def clean(self):
+#         pass
+    
     def save(self, *args, **kwargs):
         if self.position == 0:
             self.position = ReportImage.objects.next_position(self.report, self.team)

File reports/serializers.py

 from filebrowser_safe.functions import get_directory
 from filebrowser_safe.base import FileObject
 from models import Report, ReportImage
-from utils import makeup_filename
+from utils import makeup_filename, image_subpath
 from reports.conf import NOTEAM
 
 
     class Meta:
         model = ReportImage
         fields = ('report', 'image', 'team', 'description', 'description_local', 'show_in_report', 'tags')
-
+        
     def save_image(self, instance, imagedata):
         _, ext = os.path.splitext(imagedata.name)
-        imagedata.name = makeup_filename(instance.report, instance.team, instance.position, ext)
-
-        # HANDLE UPLOAD
+        subpath = image_subpath(instance.report, instance.position, ext)
         folder = 'reports/{0}'.format(instance.report.country.pk)
-        path = os.path.join(get_directory(), folder)
-        file_name = os.path.join(path, imagedata.name)
-        exists = default_storage.exists(file_name)
-        uploadedfile = default_storage.save(file_name, imagedata)
+        path = os.path.join(get_directory(), folder, subpath)
+        exists = default_storage.exists(path)
+        uploadedfile = default_storage.save(path, imagedata)
         # if file already exists, default_storage will save new file under a new name,
         # therefore we rename it to the original one
         if exists:
-            default_storage.move(smart_unicode(uploadedfile), smart_unicode(file_name), allow_overwrite=True)
-        instance.image = FileObject(file_name)
+            default_storage.move(smart_unicode(uploadedfile), smart_unicode(path), allow_overwrite=True)
+        instance.image = FileObject(path)
 
     def restore_object(self, attrs, instance=None):
         # pull out the image file

File reports/utils.py

     folder =  '/'.join([prefix, country_id])
     return folder
 
+def image_subpath(report, position, ext):
+    """ format YYYY/MMDD/{report_id}-{position}.{ext}"""
+    md = dateformat.format(report.date, 'md')
+    return u"{year}/{md}/{report_id}-{position}{ext}".format(
+                year = str(report.date.year),
+                md=md, 
+                report_id=str(report.pk), 
+                position=str(position), 
+                ext=ext.lower())
 
 def makeup_filename(report, team, position, ext):
     """ format : YYYY-MM-DD-{team}-{position}.jpg """