Anonymous avatar Anonymous committed d0eacce

Yeah! First working filtering prototype (Meta fields from EXIF and thumbnails variants).
Lots of fixes/improvements.

Comments (0)

Files changed (6)

 
 class StreamAdmin(admin.ModelAdmin):
     prepopulated_fields = {"slug": ("name",)}
+    list_display = ('__unicode__', 'author')
 
 class MetaAdmin(admin.ModelAdmin):
     list_display = ('__unicode__', 'item')
 
+class VariantAdmin(admin.ModelAdmin):
+    list_display = ('__unicode__', 'mime', 'default')
+    list_filter = ('default', 'mime')
+
 class ItemAdmin(admin.ModelAdmin):
     list_display = ('__unicode__', 'mime', 'upload_date')
     prepopulated_fields = {"slug": ("name",)}
 
 admin.site.register(Item, ItemAdmin)
 admin.site.register(Meta, MetaAdmin)
+admin.site.register(Variant, VariantAdmin)
 admin.site.register(Stream, StreamAdmin)
 
     """
     return True
 
-def filter_obj(object, params=None):
+def filter_obj(objekt, params=None):
     """
-    Main filter body. Object is instance of mediatr.models.Item, params is a filter specific tunings.
+    Main filter body. objekt is instance of mediatr.models.Item, params is a filter specific tunings.
     """
-    if object.mime.name != 'image/jpeg':
+    if objekt.mime.name != 'image/jpeg':
         return
-    tags = EXIF.process_file(object.file)
+    tags = EXIF.process_file(objekt.file)
     if not tags:
         return
     fnum = str(tags.get('EXIF FNumber')).split('/')
         focal = "%smm" % (str(focal))
     # trying to update exiting aperture value
     try:
-        ma = object.meta_set.get(field='Aperture')
+        ma = objekt.meta_set.get(field='Aperture')
         if not aperture:
             ma.delete()
         else:
             ma.value=aperture
             ma.save()
     # add aperture value if empty
-    except object.meta_set.model.DoesNotExist:
+    except objekt.meta_set.model.DoesNotExist:
         if aperture:
-            object.meta_set.create(field='Aperture', value=aperture)
+            objekt.meta_set.create(field='Aperture', value=aperture)
     # trying to update exposure value
     try:
-        me = object.meta_set.get(field='Exposure')
+        me = objekt.meta_set.get(field='Exposure')
         if not exposure:
             me.delete()
         else:
             me.value=exposure
             me.save()
     # add exposure value if empty
-    except object.meta_set.model.DoesNotExist:
+    except objekt.meta_set.model.DoesNotExist:
         if exposure:
-            object.meta_set.create(field='Exposure', value=exposure)
+            objekt.meta_set.create(field='Exposure', value=exposure)
     # trying to update focal length value
     try:
-        mf = object.meta_set.get(field='Focal Length')
+        mf = objekt.meta_set.get(field='Focal Length')
         if not focal:
             mf.delete()
         else:
             mf.value=focal
             mf.save()
     # add focal length value if empty
-    except object.meta_set.model.DoesNotExist:
+    except objekt.meta_set.model.DoesNotExist:
         if focal:
-            object.meta_set.create(field='Focal Length', value=focal)
+            objekt.meta_set.create(field='Focal Length', value=focal)

filters/sample.py

     """
     return False
 
-def filter_obj(object, params=None):
+def filter_obj(objekt, params=None):
     """
-    Main filter body. Object is instance of mediatr.models.Item, params is a filter specific tunings.
+    Main filter body. objekt is instance of mediatr.models.Item, params is a filter specific tunings.
     """

filters/thumbnail.py

+"""
+Make thumbnail for jpeg images.
+"""
+from PIL import Image
+from StringIO import StringIO
+from django.core.files.base import ContentFile
+
+
+def enabled():
+    """
+    Various cheks goes here (settings, imports, etc)
+    """
+    return True
+
+def filter_obj(objekt, params={'max_size': 500, 'default': True}):
+    """
+    Main filter body. objekt is instance of mediatr.models.Item, params is a filter specific tunings.
+    """
+    if objekt.mime.name != 'image/jpeg':
+        return
+    size = (params.get('max_size'), params.get('max_size'))
+    tmpfile = StringIO()
+    im = Image.open(objekt.file.path)
+    im.thumbnail(size, Image.ANTIALIAS)
+    im.save(tmpfile, im.format)
+    variant = objekt.variant_set.model()
+    variant.parent = objekt
+    variant.name="thumb_%spx" % (params.get('max_size'))
+    variant.default = params.get('default')
+    variant.file.save("variants/%s_%s.jpg" % (objekt.slug, variant.name), ContentFile(tmpfile.getvalue()))
+    variant.save()
 from django.template.defaultfilters import slugify
 from django.db.models import permalink
 
-from signals import add_mime_type, make_slug
+from signals import add_mime_type, make_slug, default_variant_fix
 import filters
 
 
     def is_image(self):
         if self.mime.name in ['image/png', 'image/jpeg', 'image/gif']:
             return True
+    def get_default_variant(self):
+        return self.variant_set.get(default=True)
 
     @permalink
     def get_absolute_url(self):
     template = models.TextField(default="<a href={{ object.get_absolute_url }}>{{ object.name }}</a>")
     default = models.BooleanField(default=False)
 
-    class Admin:
-        list_display = ('name',)
-        search_fields = ('name',)
-
     def __unicode__(self):
         return self.name
 
 class Variant(models.Model):
-    """File item variant"""
+    """
+    File item variant
+    """
     name = models.CharField(max_length=200)
     parent = models.ForeignKey(Item)
     file = models.FileField(upload_to='variants')
-    mime = models.ForeignKey(MimeType)
-    filter = models.ForeignKey(Filter)
+    mime = models.ForeignKey(MimeType, blank=True, null=True)
+    filtered = models.ForeignKey(Filter, blank=True, null=True)
     default = models.BooleanField(default=False)
-    
-    class Meta:
-        unique_together = (("filter", "file"),)
-
-    class Admin:
-        list_display = ('name',)
-        search_fields = ('name',)
 
     def __unicode__(self):
-        return self.name
-
-# class MetaField(models.Model):
-#     """
-#     MetaField aka meta type
-#     """
-#     name = models.CharField(max_length=150)
-# 
-#     def __unicode__(self):
-#         return self.name
+        return "%s for %s" % (self.name, self.parent)
+    
+    def set_mime(self, mime):
+        obj, created = MimeType.objects.get_or_create(name=mime, slug=slugify(mime))
+        self.mime = obj
 
 class Meta(models.Model):
     """
     value = models.CharField(max_length=250)
 
     class Meta:
-        unique_together = (("item", "field", "value"),)
+        unique_together = (("item", "field"),)
 
     def __unicode__(self):
         return u"%s: %s" % (self.field, self.value)
     log = models.TextField(blank=True)
     variant = models.ForeignKey(Variant, blank=True, null=True)
 
-    class Admin:
-        list_display = ('file', 'filter', 'date_added', 'done')
-
     def __unicode__(self):
         return self.file
 
 models.signals.pre_save.connect(add_mime_type, sender=Item)
+models.signals.pre_save.connect(add_mime_type, sender=Variant)
 models.signals.pre_save.connect(make_slug, sender=Item)
-models.signals.post_save.connect(filters.run, sender=Item)
+models.signals.post_save.connect(filters.run, sender=Item)
+models.signals.post_save.connect(default_variant_fix, sender=Variant)
             instance.slug = slugify(instance.name)
         else:
             instance.slug = make_hash()
+
+def default_variant_fix(instance, **kwargs):
+    """
+    If variant has default flag we need to check if parent item has other variants
+    with default flag and fix them.
+    """
+    if not instance.default:
+        return
+    defaults = instance.parent.variant_set.filter(default=True).exclude(pk=instance.id)
+    if len(defaults) > 0:
+        for v in defaults:
+            v.default = False
+            v.save()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.