Commits

Anonymous committed aec5faa

edit photos

Comments (0)

Files changed (6)

newsmeme/forms.py

         if not photos.file_allowed(field.file, field.file.filename):
             raise ValidationError, _("Not a proper photo")
 
+
+class EditPhotoForm(PhotoForm):
+
+    def validate_photo(self, field):
+
+        if field.file and not \
+                photos.file_allowed(field.file, field.file.filename):
+            raise ValidationError, _("Not a proper photo")
+
+
 class CommentForm(Form):
 
     comment = TextAreaField(validators=[

newsmeme/models/photos.py

 
 class Photo(BasePost):
 
+    PER_PAGE = 32
+
     _photo = db.Column("photo", db.String(200), unique=True)
     _thumb = db.Column("thumb", db.String(200), unique=True)
 

newsmeme/templates/photos/photo.html

 | <a href="{{ photo.permalink }}">permalink</a> 
     {% if g.user %}
     {% if photo.permissions.edit %}
-    | <a href="{{ url_for('post.edit', post_id=photo.id) }}">edit this post</a> 
+    | <a href="{{ url_for('photos.edit', photo_id=photo.id) }}">edit this post</a> 
     {% if photo.permissions.delete %}
-    | <a href="#" onclick="$('#delete-post').toggle(); return false;">delete this post</a> 
+    | <a href="#" onclick="$('#delete-post').toggle(); return false;">delete this photo</a> 
     <div id="delete-post" style="display:none;">
         <strong>{{ _('Are you sure you want to delete this photo ?') }}</strong>
-        <a href="#" onclick="newsmeme.ajax_post('{{ url_for('post.delete', post_id=photo.id) }}'); return false;">{{ _('yes') }}</a> / 
+        <a href="#" onclick="newsmeme.ajax_post('{{ url_for('photos.delete', photo_id=photo.id) }}'); return false;">{{ _('yes') }}</a> / 
         <a href="#" onclick="$('#delete-post').toggle(); return false;">{{ _('no') }}</a>
     </div>
     {% endif %}

newsmeme/views/photos.py

-from flask import Module, g, url_for, flash, redirect
+from flask import Module, g, url_for, flash, redirect, jsonify
+
 from flaskext.babel import gettext as _
+from flaskext.mail import Message
 
 from newsmeme.helpers import cached, render_template
 from newsmeme.decorators import keep_login_url
-from newsmeme.extensions import db
+from newsmeme.extensions import db, mail
 from newsmeme.permissions import auth
-from newsmeme.models import Photo
-from newsmeme.forms import PhotoForm, CommentForm
+from newsmeme.models import Photo, Comment
+from newsmeme.forms import PhotoForm, EditPhotoForm, CommentForm
 
 photos = Module(__name__)
 
     return render_template("photos/submit.html", form=form)
 
 
+@photos.route("/<int:photo_id>/edit/", methods=("GET", "POST"))
+@auth.require(401)
+def edit(photo_id):
+
+    photo = Photo.query.get_or_404(photo_id)
+    photo.permissions.edit.test(403)
+    
+    form = EditPhotoForm(obj=photo)
+    if form.validate_on_submit():
+
+        form.populate_obj(photo)
+        if form.photo.file:
+            photo.save_photo(form.photo.file)
+
+        db.session.commit()
+
+        if g.user.id != photo.author_id:
+            body = render_template("emails/photo_edited.html",
+                                   photo=photo)
+
+            message = Message(subject="Your photo has been edited",
+                              body=body,
+                              recipients=[photo.author.email])
+
+            mail.send(message)
+
+            flash(_("The photo has been updated"), "success")
+        
+        else:
+            flash(_("Your photo has been updated"), "success")
+        return redirect(photo.url)
+
+    return render_template("photos/edit.html", 
+                           photo=photo, 
+                           form=form)
+
+
+@photos.route("/<int:photo_id>/delete/", methods=("POST",))
+@auth.require(401)
+def delete(post_id):
+
+    photo = Photo.query.get_or_404(post_id)
+    photo.permissions.delete.test(403)
+    
+    Comment.query.filter_by(post=photo).delete()
+
+    photo.delete_files()
+
+    db.session.delete(photo)
+    db.session.commit()
+
+    if g.user.id != photo.author_id:
+        body = render_template("emails/photo_deleted.html",
+                               photo=photo)
+
+        message = Message(subject="Your photo has been deleted",
+                          body=body,
+                          recipients=[photo.author.email])
+
+        mail.send(message)
+
+        flash(_("The post has been deleted"), "success")
+    
+    else:
+        flash(_("Your post has been deleted"), "success")
+
+    return jsonify(success=True,
+                   redirect_url=url_for('photos.index'))
+
+
+
 @photos.route("/<int:photo_id>/")
 @photos.route("/<int:photo_id>/<slug>/")
 @keep_login_url

newsmeme/views/post.py

         
         else:
             flash(_("Your post has been updated"), "success")
-        return redirect(url_for("post.view", post_id=post_id))
+        return redirect(post.url)
 
     return render_template("post/edit_post.html", 
                            post=post, 

tests/test_views.py

                 }
 
         response = self.client.post("/post/%d/edit/" % post.id, data=data)
-        self.assert_redirects(response, "/post/%d/" % post.id)
+        print response.location
+        print post.url
+        self.assert_redirects(response, "/post/%d/s/testing-123/" % post.id)
         
         post = Post.query.first()
 
         with mail.record_messages() as outbox:
             response = self.client.post("/post/%d/edit/" % post.id, data=data)
         
-            self.assert_redirects(response, "/post/%d/" % post.id)
+            self.assert_redirects(response, "/post/%d/s/testing-123/" % post.id)
             assert len(outbox) == 1