Просмотр исходного кода

Private Messages - Everything should work now. Still needs some refactoring

sh4nks 10 лет назад
Родитель
Сommit
349252072d

+ 4 - 0
flaskbb/message/models.py

@@ -71,6 +71,10 @@ class Conversation(db.Model):
             message.save(self)
             return self
 
+        db.session.add(self)
+        db.session.commit()
+        return self
+
     def delete(self):
         """Deletes a private message"""
 

+ 60 - 0
flaskbb/message/views.py

@@ -83,6 +83,18 @@ def view_conversation(conversation_id):
                 Conversation.shared_id == conversation.shared_id
             ).first()
 
+        # user deleted the conversation, start a new conversation with just
+        # the recieving message
+        if conversation is None:
+            conversation = Conversation(
+                subject=old_conv.subject,
+                from_user_id=current_user.id,
+                to_user=to_user_id,
+                user_id=to_user_id,
+                shared_id=old_conv.shared_id
+            )
+            conversation.save()
+
         form.save(conversation=conversation, user_id=current_user.id,
                   unread=True)
 
@@ -222,6 +234,54 @@ def edit_conversation(conversation_id):
                            title=_("Edit Message"))
 
 
+@message.route("/<int:conversation_id>/move", methods=["POST"])
+@login_required
+def move_conversation(conversation_id):
+    conversation = Conversation.query.filter_by(
+        id=conversation_id).first_or_404()
+
+    if conversation.user_id != current_user.id:
+        # if a user tries to view a conversation which does not belong to him
+        # just abort with 404
+        abort(404)
+
+    conversation.trash = True
+    conversation.save()
+
+    return redirect(url_for("message.inbox"))
+
+
+@message.route("/<int:conversation_id>/restore", methods=["POST"])
+@login_required
+def restore_conversation(conversation_id):
+    conversation = Conversation.query.filter_by(
+        id=conversation_id).first_or_404()
+
+    if conversation.user_id != current_user.id:
+        # if a user tries to view a conversation which does not belong to him
+        # just abort with 404
+        abort(404)
+
+    conversation.trash = False
+    conversation.save()
+    return redirect(url_for("message.inbox"))
+
+
+@message.route("/<int:conversation_id>/delete", methods=["POST"])
+@login_required
+def delete_conversation(conversation_id):
+    conversation = Conversation.query.filter_by(
+        id=conversation_id).first_or_404()
+
+    if conversation.user_id != current_user.id:
+        # if a user tries to view a conversation which does not belong to him
+        # just abort with 404
+        abort(404)
+
+    conversation.delete()
+    return redirect(url_for("message.inbox"))
+
+
 @message.route("/sent")
 @login_required
 def sent():

+ 13 - 4
flaskbb/templates/message/drafts.html

@@ -36,11 +36,20 @@
                             {{ conversation.first_message.message }}
                         </div>
                         <div class="action">
-                            <button type="button" class="btn btn-danger btn-xs" title="Delete">
-                                <span class="glyphicon glyphicon-trash"></span>
-                            </button>
+                            <form class="inline-form" method="POST" action="{{ url_for('message.delete_conversation', conversation_id=conversation.id) }}">
+                                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
+                                <button type="submit" class="btn btn-danger btn-xs" title="Delete">
+                                    <span class="fa fa-trash"></span>
+                                </button>
+                            </form>
+                            <form class="inline-form" method="POST" action="{{ url_for('message.move_conversation', conversation_id=conversation.id) }}">
+                                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
+                                <button type="submit" class="btn btn-info btn-xs" title="Move to Trash">
+                                    <span class="fa fa-archive"></span>
+                                </button>
+                            </form>
                             <a class="btn btn-success btn-xs" alt="Edit" href="{{ url_for('message.edit_conversation', conversation_id = conversation.id) }}">
-                                <span class="glyphicon glyphicon-pencil"></span>
+                                <span class="fa fa-pencil"></span>
                             </a>
                         </div>
                     </div>

+ 6 - 3
flaskbb/templates/message/inbox.html

@@ -40,9 +40,12 @@
                             {{ conversation.first_message.message|truncate(200)|markup|safe }}
                         </div>
                         <div class="action">
-                            <button type="button" class="btn btn-danger btn-xs" title="Delete">
-                                <span class="glyphicon glyphicon-trash"></span>
-                            </button>
+                            <form class="inline-form" method="POST" action="{{ url_for('message.move_conversation', conversation_id=conversation.id) }}">
+                                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
+                                <button type="submit" class="btn btn-info btn-xs" title="Move to Trash">
+                                    <span class="fa fa-archive"></span>
+                                </button>
+                            </form>
                         </div>
                     </div>
                 </div>

+ 12 - 3
flaskbb/templates/message/sent.html

@@ -37,9 +37,18 @@
                             {{ conversation.first_message.message }}
                         </div>
                         <div class="action">
-                            <button type="button" class="btn btn-danger btn-xs" title="Delete">
-                                <span class="glyphicon glyphicon-trash"></span>
-                            </button>
+                            <form class="inline-form" method="POST" action="{{ url_for('message.delete_conversation', conversation_id=conversation.id) }}">
+                                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
+                                <button type="submit" class="btn btn-danger btn-xs" title="Delete">
+                                    <span class="fa fa-trash"></span>
+                                </button>
+                            </form>
+                            <form class="inline-form" method="POST" action="{{ url_for('message.restore_conversation', conversation_id=conversation.id) }}">
+                                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
+                                <button type="submit" class="btn btn-success btn-xs" title="Restore">
+                                    <span class="fa fa-undo"></span>
+                                </button>
+                            </form>
                         </div>
                     </div>
                 </div>

+ 12 - 3
flaskbb/templates/message/trash.html

@@ -37,9 +37,18 @@
                             {{ conversation.first_message.message }}
                         </div>
                         <div class="action">
-                            <button type="button" class="btn btn-danger btn-xs" title="Delete">
-                                <span class="glyphicon glyphicon-trash"></span>
-                            </button>
+                            <form class="inline-form" method="POST" action="{{ url_for('message.delete_conversation', conversation_id=conversation.id) }}">
+                                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
+                                <button type="submit" class="btn btn-danger btn-xs" title="Delete">
+                                    <span class="fa fa-trash"></span>
+                                </button>
+                            </form>
+                            <form class="inline-form" method="POST" action="{{ url_for('message.restore_conversation', conversation_id=conversation.id) }}">
+                                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
+                                <button type="submit" class="btn btn-success btn-xs" title="Restore">
+                                    <span class="fa fa-undo"></span>
+                                </button>
+                            </form>
                         </div>
                     </div>
                 </div>