Created
March 31, 2012 20:58
-
-
Save alexanderjulo/2268448 to your computer and use it in GitHub Desktop.
foodloader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from datetime import datetime | |
from werkzeug.contrib.atom import AtomFeed | |
from flask import Flask, request, render_template, flash, url_for | |
www = Flask(__name__) | |
www.config['SECRET_KEY'] = 'kjsas fo98qw 83' | |
www.debug = True | |
from flaskext.uploads import UploadSet, IMAGES, configure_uploads | |
photos = UploadSet('photos', IMAGES) | |
www.config['UPLOADED_PHOTOS_DEST'] = 'static/uploads' | |
configure_uploads(www, (photos)) | |
from flaskext.sqlalchemy import SQLAlchemy | |
from sqlalchemy import func, desc | |
www.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://alexex:recfathifPistewlip@localhost/alexex_food' | |
db = SQLAlchemy(www) | |
class Photo(db.Model): | |
id = db.Column(db.Integer, primary_key=True) | |
title = db.Column(db.String(60)) | |
uploader = db.Column(db.String(60)) | |
date = db.Column(db.DateTime) | |
uploaded = db.Column(db.DateTime) | |
path = db.Column(db.String(60)) | |
def __init__(self, title, uploader, path, date): | |
self.title = title | |
self.uploader = uploader | |
self.path = path | |
self.date = date | |
self.uploaded = datetime.utcnow() | |
@www.route('/', methods=['GET', 'POST']) | |
def upload(): | |
if request.method == 'POST' and 'photo' in request.files: | |
if not request.form['uploader'] or not request.form['title']: | |
flash('Specify title and name please.') | |
else: | |
filename = photos.save(request.files['photo']) | |
photo = Photo(request.form['title'], request.form['uploader'], filename, request.form['date']) | |
db.session.add(photo) | |
db.session.commit() | |
flash('Photo uploaded successful.') | |
else: | |
flash('You have to specify a photo.') | |
return render_template('home.html', active='upload') | |
@www.route('/id/<int:id>/') | |
def single(id): | |
photo = Photo.query.get_or_404(id) | |
return render_template('id.html', photo=photo) | |
@www.route('/random/') | |
def random(): | |
photo = Photo.query.order_by(func.rand()).first() | |
return render_template('id.html', photo=photo, active='random') | |
@www.route('/all/') | |
def list(): | |
photos = Photo.query.order_by(desc('uploaded')).limit(21).all() | |
return render_template('list.html', photos=photos, active='all') | |
@www.route('/atom/') | |
def atom(): | |
feed = AtomFeed('Foodloader 2012', feed_url=request.url, url=request.host_url) | |
for photo in Photo.query.order_by(desc('uploaded')).limit(10).all(): | |
feed.add(photo.title, content_type='html', author=photo.uploader, url=url_for('single', id=photo.id), id=photo.id, published=photo.uploaded, updated=photo.uploaded) | |
return feed.get_response() | |
@www.context_processor | |
def inject_vars(): | |
photo_count = Photo.query.count() | |
# TODO: add user count | |
user_count = 'random' | |
def get_size(start_path = '.'): | |
total_size = 0 | |
for dirpath, dirnames, filenames in os.walk(start_path): | |
for f in filenames: | |
fp = os.path.join(dirpath, f) | |
total_size += os.path.getsize(fp) | |
return total_size | |
def readable_size(num): | |
for x in ['B','KB','MB','GB']: | |
if num < 1024.0: | |
return "%3.1f%s" % (num, x) | |
num /= 1024.0 | |
return "%3.1f%s" % (num, 'TB') | |
photo_size = readable_size(get_size('static/uploads')) | |
photo_recent = Photo.query.order_by(desc('uploaded')).limit(4).all() | |
return dict(photo_count=photo_count, photo_size=photo_size, photo_recent=photo_recent) | |
from flaskext.script import Manager | |
manager = Manager(www) | |
@manager.command | |
def initdb(): | |
db.create_all() | |
print "Tables created." | |
if __name__ == '__main__': | |
manager.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment