Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

section: Upload picture to section and display it when filtering.

  • Loading branch information...
commit f687d90d2caebad728f3e619903924c7f51cd2dd 1 parent 047a0b7
Gary England smoothgrips authored
2  raggregate/models/section.py
View
@@ -24,8 +24,10 @@ class Section(Base):
enabled = Column(Boolean, default=True)
added_on = Column(DateTime(timezone=True), default=sqlalchemy.sql.func.now())
modified_on = Column(DateTime(timezone=True), default=sqlalchemy.sql.func.now())
+ picture = Column(GUID, ForeignKey('section_pictures.id'))
stories = relationship("Submission", backref="sections")
+ picture_ref = relationship("SectionPicture")
def __init__(self, name = None, description = None, parent = None, added_by = None, modified_on = None, subscribe_by_default = False, enabled = True):
self.name = name
33 raggregate/models/sectionpicture.py
View
@@ -0,0 +1,33 @@
+from raggregate.models import *
+
+import sqlalchemy
+from sqlalchemy import Column
+from sqlalchemy import Integer
+from sqlalchemy import Unicode
+from sqlalchemy import UnicodeText
+from sqlalchemy import DateTime
+from sqlalchemy import text
+from sqlalchemy import Boolean
+from sqlalchemy import ForeignKey
+
+from raggregate.guid_recipe import GUID
+
+class SectionPicture(Base):
+ __tablename__ = 'section_pictures'
+
+ id = Column(GUID, primary_key=True)
+ orig_filename = Column(UnicodeText)
+ filename = Column(UnicodeText)
+ file_hash = Column(UnicodeText)
+ # 0 for section's computer
+ # 1 for twitter
+ # 2 for facebook
+ source = Column(Integer, default=0)
+ # @TODO: we should implement storage_schemes, etc., here.
+
+ def __init__(self, orig_filename, filename, file_hash, source):
+ self.orig_filename = orig_filename
+ self.filename = filename
+ self.file_hash = file_hash
+ self.source = source
+
47 raggregate/queries/section.py
View
@@ -21,3 +21,50 @@ def get_section_by_id(id):
def get_section_by_name(name):
return dbsession.query(Section).filter(Section.name == name).one()
+def add_section_picture(orig_filename, new_prefix, up_dir, image_file):
+ import time
+ import os
+ import tempfile
+
+ new_filename = "{0}-{1}.jpg".format(new_prefix, time.time())
+
+ full_path = os.path.join(up_dir, new_filename)
+
+ import hashlib
+ skip_seek = False
+
+ try:
+ image_file.seek(0)
+ except AttributeError:
+ # we want a file, so if this isn't a file, make one.
+ tmp_f = tempfile.TemporaryFile()
+ # urllib2.urlopen object passed, read is implemented
+ # or maybe not, and then just assume the string is the binary data
+ # and ready to be written directly
+ if hasattr(image_file, 'read'):
+ # im_b for "image binary"
+ im_b = image_file.read()
+ else:
+ im_b = image_file
+ tmp_f.write(im_b)
+ image_file = tmp_f
+
+ image_file.seek(0)
+ sha = hashlib.sha1()
+ sha.update(image_file.read())
+ sha = sha.hexdigest()
+
+ if not skip_seek:
+ image_file.seek(0)
+ f = image_file
+ from PIL import Image
+ im = Image.open(f)
+ im.thumbnail((50, 50), Image.ANTIALIAS)
+
+ im.save(full_path, 'JPEG')
+
+ from raggregate.models.sectionpicture import SectionPicture
+ sp = SectionPicture(orig_filename, new_filename, sha, 0)
+ dbsession.add(sp)
+ dbsession.flush()
+ return sp.id
17 raggregate/rg_migrations/versions/010_Add_picture_column_to_sections.py
View
@@ -0,0 +1,17 @@
+from sqlalchemy import *
+from migrate import *
+from raggregate.guid_recipe import GUID
+
+
+def upgrade(migrate_engine):
+ meta = MetaData(bind=migrate_engine)
+ sections = Table('sections', meta, autoload=True)
+ section_pictures = Table('section_pictures', meta, autoload=True)
+ picture_c = Column('picture', GUID, ForeignKey('section_pictures.id'))
+ picture_c.create(sections)
+
+
+def downgrade(migrate_engine):
+ meta = MetaData(bind=migrate_engine)
+ sections = Table('sections', meta, autoload=True)
+ sections.c.picture.drop()
10 raggregate/templates/base/base.mak
View
@@ -205,7 +205,15 @@
% endif
<body>
<div id="logo_bar">
- <a href="${request.route_url('list')}"><img src="${static_base}images/logo.png" style="border: 0" /></a>
+ % if filtered_section and filtered_section != 'all':
+ % if filtered_section.picture_ref:
+ <a href="${request.route_url('list')}"><img src="/section_imgs/${filtered_section.picture_ref.filename}" style="border: 0" /></a>
+ % else:
+ <a href="${request.route_url('list')}"><img src="${static_base}images/logo.png" style="border: 0" /></a>
+ % endif
+ % else:
+ <a href="${request.route_url('list')}"><img src="${static_base}images/logo.png" style="border: 0" /></a>
+ % endif
</div>
% if success == False:
17 raggregate/templates/base/section.mak
View
@@ -12,3 +12,20 @@
<li><a href="${request.route_url('list', _query=[('section', s.name)])}">${s.name}</a></li>
% endfor
</ul>
+
+ Add picture to existing section: <br />
+ <form action="${request.route_url('section')}" method="POST" enctype="multipart/form-data">
+ Section:
+ <select name="section_id">
+ % if sections and len(sections) > 0:
+ % for section in sections:
+ <option value="${section.id}">${section.name}</option>
+ % endfor
+ % else:
+ <option value="">No Sections</option>
+ % endif
+ </select>
+ <br />
+ Picture: <input type="file" name="picture" /><br />
+ <input type="submit" name="add_pic_button" value="Submit" />
+ </form>
13 raggregate/views/section.py
View
@@ -25,5 +25,18 @@ def story(request):
dbsession.add(new_sect)
s['message'] = "Section {0} successfully added.".format(p['name'])
+ if 'add_pic_button' in p:
+ section = None
+ if 'section_id' in p and p['section_id'] != '':
+ section = section_queries.get_section_by_id(p['section_id'])
+ if section:
+ if r.POST['picture'] != '':
+ orig_filename = r.POST['picture'].filename
+ sp_dir = r.registry.settings['section.picture_upload_directory']
+
+ section.picture = section_queries.add_section_picture(orig_filename,
+ str(section.id)[:7], sp_dir, r.POST['picture'].file)
+ dbsession.add(section)
+
sections = section_queries.get_sections()
return {'sections': sections}
2  setup.py
View
@@ -22,7 +22,7 @@
'httplib2',
'lxml',
'twython',
- 'PIL',
+ 'pillow',
'pytz',
'pyramid-tm',
'Markdown',
Please sign in to comment.
Something went wrong with that request. Please try again.