1. Dan Jacob
  2. newsmeme

Commits

danjac  committed 1113733

added upload form

  • Participants
  • Parent commits e01104d
  • Branches photos

Comments (0)

Files changed (10)

File newsmeme/__init__.py

View file
 from newsmeme import helpers
 from newsmeme.models import User, Tag
 from newsmeme.helpers import render_template
-from newsmeme.extensions import db, mail, oid, cache, uploads
+from newsmeme.extensions import db, mail, oid, cache, photos, thumbs
 
 DEBUG = True
 
 
     setup_themes(app)
 
-    configure_uploads(app, (uploads,))
+    configure_uploads(app, (photos, thumbs))
     patch_request_class(app)
 
     principal = Principal(app)

File newsmeme/extensions.py

View file
 from flaskext.cache import Cache
 from flaskext.uploads import UploadSet, IMAGES
 
-__all__ = ['oid', 'mail', 'db', 'cache', 'uploads']
+__all__ = ['oid', 'mail', 'db', 'cache', 'photos', 'thumbs']
 
 oid = OpenID()
 mail = Mail()
 db = SQLAlchemy()
 cache = Cache()
 
-uploads = UploadSet('photos', extensions=IMAGES)
+# uploads
 
+photos = UploadSet('photos', extensions=IMAGES)
+thumbs = UploadSet('thumbs', extensions=IMAGES)
+

File newsmeme/forms.py

View file
     forms.py
     ~~~~~~~~
 
-    Description of the module goes here...
+    form validation
 
     :copyright: (c) 2010 by Dan Jacob.
     :license: BSD, see LICENSE for more details.
 
 from flaskext.wtf import Form, TextField, PasswordField, HiddenField, \
     BooleanField, RadioField, RecaptchaField, TextAreaField, SubmitField, \
-    ValidationError, required, email, equal_to, url, optional, regexp
+    FileField, ValidationError, required, email, equal_to, url, optional, \
+    regexp
 
 from flaskext.babel import gettext, lazy_gettext as _
 
         if posts.count():
             raise ValidationError, gettext("This link has already been posted")
 
+class PhotoForm(PostForm):
+
+    photo = FileField(_("Photo"), validators=[
+        required(message=_("Photo required"))])
+
+    def validate_photo(self, field):
+        # TBD: check if photo ext matches IMAGES
+        pass
+
 class CommentForm(Form):
 
     comment = TextAreaField(validators=[

File newsmeme/models/comments.py

View file
 
     __tablename__ = "comments"
 
+    PER_PAGE = 20
+
     query_class = CommentQuery
 
     id = db.Column(db.Integer, primary_key=True)

File newsmeme/models/photos.py

View file
-from newsmeme.extensions import db, uploads
+from newsmeme.extensions import db, photos, thumbs
 from newsmeme.models.posts import Post
 
 class Photo(Post):

File newsmeme/models/posts.py

View file
     FRIENDS = 200
     PRIVATE = 300
 
+    PER_PAGE = 20
+
     query_class = PostQuery
 
     id = db.Column(db.Integer, primary_key=True)

File newsmeme/views/frontend.py

View file
 from newsmeme.decorators import keep_login_url
 from newsmeme.permissions import auth
 
-PER_PAGE = 20
-
 frontend = Module(__name__)
 
 @frontend.route("/")
     
     page_obj = Post.query.popular().hottest().\
         restricted(g.user).as_list().\
-        paginate(page, per_page=PER_PAGE)
+        paginate(page, per_page=Post.PER_PAGE)
         
     page_url = lambda page: url_for("frontend.index", page=page)
 
 def latest(page=1):
     
     page_obj = Post.query.popular().restricted(g.user).as_list().\
-        paginate(page, per_page=PER_PAGE)
+        paginate(page, per_page=Post.PER_PAGE)
 
     page_url = lambda page: url_for("frontend.latest", page=page)
 
 @keep_login_url
 def deadpool(page=1):
     page_obj = Post.query.deadpooled().restricted(g.user).as_list().\
-        paginate(page, per_page=PER_PAGE)
+        paginate(page, per_page=Post.PER_PAGE)
 
     page_url = lambda page: url_for("frontend.deadpool", page=page)
 
         return redirect(url_for("frontend.index"))
 
     page_obj = Post.query.search(keywords).restricted(g.user).\
-            as_list().paginate(page, per_page=PER_PAGE)
+            as_list().paginate(page, per_page=Post.PER_PAGE)
 
     if page_obj.total == 1:
 
     tag = Tag.query.filter_by(slug=slug).first_or_404()
 
     page_obj = tag.posts.restricted(g.user).as_list().\
-        paginate(page, per_page=PER_PAGE)
+        paginate(page, per_page=Post.PER_PAGE)
 
     page_url = lambda page: url_for('frontend.tag',
                                     slug=slug,

File newsmeme/views/photos.py

View file
-from flask import Module
+from flask import Module, g, url_for, flash, redirect
+from flaskext.babel import gettext as _
+
+from newsmeme.helpers import cached, render_template
+from newsmeme.decorators import keep_login_url
+from newsmeme.extensions import db
+from newsmeme.permissions import auth
+from newsmeme.models import Photo
+from newsmeme.forms import PhotoForm
 
 photos = Module(__name__)
 
 @photos.route("/")
-def index():
-    pass
+@photos.route("/<int:page>/")
+@cached()
+@keep_login_url
+def index(page=1):
+    
+    page_obj = Photo.query.popular().hottest().\
+        restricted(g.user).as_list().\
+        paginate(page, per_page=Photo.PER_PAGE)
+        
+    page_url = lambda page: url_for("photos.index", page=page)
+
+    return render_template("photos/index.html", 
+                           page_obj=page_obj, 
+                           page_url=page_url)
+
 
 @photos.route("/submit/")
+@auth.require(401)
+@keep_login_url
 def submit():
-    pass
+    
+    form = PhotoForm()
 
+    if form.validate_on_submit():
 
+        photo = Photo(author=g.user)
+        form.populate_obj(photo)
+        photo.save_photo(form.photo.file)
+
+        db.session.add(photo)
+        db.session.commit()
+
+        flash(_("Your photo has been uploaded"), "success")
+
+        return redirect(url_for("photos.index"))
+    
+    return render_template("photos/submit.html", form=form)
+

File newsmeme/views/user.py

View file
 from newsmeme.extensions import mail
 from newsmeme.permissions import auth
 
-PER_PAGE = 30
-
 user = Module(__name__)
 
 @user.route("/message/<int:user_id>/", methods=("GET", "POST"))
     user = User.query.filter_by(username=username).first_or_404()
 
     page_obj = Post.query.filter_by(author=user).restricted(g.user).\
-        as_list().paginate(page, PER_PAGE)
+        as_list().paginate(page, Post.PER_PAGE)
     
     page_url = lambda page: url_for('user.posts',
                                     username=username,
 
     page_obj = Comment.query.filter_by(author=user).\
         order_by(Comment.id.desc()).restricted(g.user).\
-        paginate(page, PER_PAGE)
+        paginate(page, Comment.PER_PAGE)
     
     page_url = lambda page: url_for('user.comments',
                                     username=username,

File tests/test_views.py

View file
 """
 
 from newsmeme.signals import comment_added
-from newsmeme.models import User, Post, Comment
+from newsmeme.models import User, Post, Comment, Photo
 from newsmeme.extensions import db, mail
 
 from tests import TestCase
 
+class TestPhotos(TestCase):
+
+    def setUp(self):
+
+        super(TestPhotos, self).setUp()
+
+        self.user = User(username="tester",
+                         email="tester@example.com",
+                         password="test")
+
+        self.photo = Photo(author=self.user,
+                           title="test")
+
+        db.session.add_all([self.user, self.photo])
+        db.session.commit()
+
+    def test_index(self):
+
+        response = self.client.get("/photos/")
+        self.assert_200(response)
+
+    
 class TestApi(TestCase):
 
     def setUp(self):