Commits

Anonymous committed 7e040c1

wrap photo attrs in object

  • Participants
  • Parent commits 71893d7
  • Branches photos

Comments (0)

Files changed (3)

newsmeme/models/photos.py

 from newsmeme.extensions import db, photos, thumbs
 from newsmeme.models.posts import Post
 
+class _PhotoObj(object):
+
+    def __init__(self, filename, upload_set):
+        self.upload_set = upload_set
+        self.filename = filename
+        
+    @cached_property
+    def image(self):
+        if os.path.isfile(self.path):
+            return Image.open(self.path)
+
+    @cached_property
+    def path(self):
+        return self.upload_set.path(self.filename)
+
+    @cached_property
+    def url(self):
+        return self.upload_set.url(self.filename)
+
+    @cached_property
+    def size(self):
+        if self.image:
+            return self.image.size
+        return (0,0)
+
+    @cached_property
+    def height(self):
+        return self.size[1]
+
+    @cached_property
+    def width(self):
+        return self.size[0]
+
+    def delete(self):
+        if self.filename and os.path.isfile(self.path):
+            os.remove(self.path)
+
+
 class Photo(Post):
 
-    photo = db.Column(db.String(200))
-    thumb = db.Column(db.String(200))
+    _photo = db.Column("photo", db.String(200), unique=True)
+    _thumb = db.Column("thumb", db.String(200), unique=True)
 
     __mapper_args__ = {'polymorphic_identity' : 'photo'}
     
+    def _get_photo(self):
+        return _PhotoObj(self._photo, photos)
+
+    def _set_photo(self, filename):
+        self._photo = filename
+
+    photo = db.synonym("_photo", descriptor=property(_get_photo, _set_photo))
+
+    def _get_thumb(self):
+        return _PhotoObj(self._thumb, thumbs)
+
+    def _set_thumb(self, filename):
+        self._thumb = filename
+
+    thumb = db.synonym("_thumb", descriptor=property(_get_thumb, _set_thumb))
+
     def save_photo(self, storage):
 
         # ensure old photos deleted
         # code adapted from http://djangosnippets.org/snippets/224/
         self.thumb = thumbs.save(storage, folder, "tn-" + name)
         
-        image = Image.open(self.thumb_path)
+        image = Image.open(self.thumb.path)
 
         s_width, s_height = image.size
         s_ratio = float(s_width) / float(s_height)
                             y_offset + int(crop_height)))
 
         image = image.resize((d_width, d_height), Image.ANTIALIAS)
-        image.save(self.thumb_path)
+        image.save(self.thumb.path)
 
     def delete_files(self):
-        if self.photo and os.path.exists(self.photo_path):
-            os.remove(self.photo_path)
-
-        if self.thumb and os.path.exists(self.thumb_path):
-            os.remove(self.thumb_path)
-
-    @cached_property
-    def photo_path(self):
-        return photos.path(self.photo)
-
-    @cached_property
-    def photo_url(self):
-        return photos.url(self.photo)
-
-    @cached_property
-    def thumb_path(self):
-        return thumbs.path(self.thumb)
-
-    @cached_property
-    def thumb_url(self):
-        return thumbs.url(self.thumb)
+        self.photo.delete()
+        self.thumb.delete()
 
     def _url(self, _external=False):
         return url_for('photos.view', 

newsmeme/templates/photos/index.html

 
 <ul class="photos">
     {% for photo in page_obj.items %}
-    <li><a title="{{ photo.title }}" href="{{ photo.url }}"><img src="{{ photo.thumb_url }}"></a></li>
+    <li><a title="{{ photo.title }}" href="{{ photo.url }}"><img src="{{ photo.thumb.url }}" height="{{ photo.thumb.height }}" width="{{ photo.thumb.width }}"></a></li>
     {% endfor %}
 </ul>
 

newsmeme/templates/photos/photo.html

 $(document).ready(function()
 {
 
+var maxHeight = window.innerHeight - 100;
+var maxWidth = window.innerWidth - 100;
+
+var height = maxHeight < {{ photo.photo.height }} ? maxHeight : {{ photo.photo.height }};
+var width = maxWidth < {{ photo.photo.width }} ? maxWidth : {{ photo.photo.width }};
+
+
 $('#photo').dialog({ autoOpen : false, 
-                     height: window.innerHeight,
-                     width: window.innerWidth,
+                     height: height,
+                     width: width,
                      modal : true,
                      show : 'fade',
                      hide : 'fade',
 
 {% block content %}
 <div id="photo" style="display:none;">
-    <img src="{{ photo.photo_url }}">
+    <img src="{{ photo.photo.url }}" height="{{ photo.photo.height }}" width="{{ photo.photo.width }}">
 </div>
 
 <h2>
 
 </h2>
 
-<a href="#" id="thumbnail"><img src="{{ photo.thumb_url }}"></a>
+<a href="#" id="thumbnail"><img src="{{ photo.thumb.url }}" height="{{ photo.thumb.height }}" width="{{ photo.thumb.width }}"></a>
 
 {% if photo.description %}
 {{ photo.markdown }}