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

Further work on private messages

sh4nks 10 лет назад
Родитель
Сommit
93631c814c

+ 7 - 7
flaskbb/app.py

@@ -132,14 +132,14 @@ def configure_extensions(app):
     def load_user(user_id):
         """Loads the user. Required by the `login` extension."""
 
-        #unread_count = db.session.query(db.func.count(PrivateMessage.id)).\
-        #    filter(PrivateMessage.unread,
-        #           PrivateMessage.user_id == user_id).subquery()
-        #u = db.session.query(User, unread_count).filter(User.id == user_id).\
-        #    first()
-        u = User.query.get(user_id)
+        unread_count = db.session.query(db.func.count(Conversation.id)).\
+            filter(Conversation.unread,
+                   Conversation.user_id == user_id).subquery()
+        u = db.session.query(User, unread_count).filter(User.id == user_id).\
+            first()
+
         if u:
-            user_instance, user_instance.pm_unread = u, 0
+            user_instance, user_instance.pm_unread = u
             return user_instance
         else:
             return None

+ 4 - 2
flaskbb/message/models.py

@@ -35,11 +35,13 @@ class Conversation(db.Model):
 
     @property
     def first_message(self):
-        return self.messages[0].message
+        """Returns the first message object."""
+        return self.messages[0]
 
     @property
     def last_message(self):
-        return self.messages[-1].message
+        """Returns the last message object."""
+        return self.messages[-1]
 
     def save(self, message=None, user_id=None, from_user=None, to_user=None):
         """Saves a conversation.

+ 13 - 4
flaskbb/message/views.py

@@ -48,6 +48,10 @@ def view_conversation(conversation_id):
         # just abort with 404
         abort(404)
 
+    if conversation.unread:
+        conversation.unread = False
+        conversation.save()
+
     form = MessageForm()
     if form.validate_on_submit():
 
@@ -108,11 +112,14 @@ def new_conversation():
         if "save_message" in request.form and form.validate():
             to_user = User.query.filter_by(username=form.to_user.data).first()
 
+            shared_id = uuid.uuid4()
+
             form.save(from_user=current_user.id,
                       to_user=to_user.id,
                       user_id=current_user.id,
                       unread=False,
-                      as_draft=True)
+                      as_draft=True,
+                      shared_id=shared_id)
 
             flash(_("Message saved."), "success")
             return redirect(url_for("message.drafts"))
@@ -182,7 +189,8 @@ def edit_conversation(conversation_id):
             to_user = User.query.filter_by(username=form.to_user.data).first()
 
             conversation.draft = True
-            conversation.to_user = to_user.id
+            conversation.to_user_id = to_user.id
+            conversation.first_message.message = form.message.data
             conversation.save()
 
             flash(_("Message saved."), "success")
@@ -194,7 +202,8 @@ def edit_conversation(conversation_id):
             form.save(from_user=current_user.id,
                       to_user=to_user.id,
                       user_id=to_user.id,
-                      unread=True)
+                      unread=True,
+                      shared_id=conversation.shared_id)
 
             # Move the message from ``Drafts`` to ``Sent``.
             conversation.draft = False
@@ -207,7 +216,7 @@ def edit_conversation(conversation_id):
     else:
         form.to_user.data = conversation.to_user.username
         form.subject.data = conversation.subject
-        form.message.data = conversation.first_message
+        form.message.data = conversation.first_message.message
 
     return render_template("message/message_form.html", form=form,
                            title=_("Edit Message"))

+ 22 - 5
flaskbb/templates/message/conversation.html

@@ -10,13 +10,22 @@
     <div class="panel-heading">Subject: <strong>{{ conversation.subject }}</strong></div>
     <div class="panel-body">
         <section class="conversation-list">
-            {% for message in conversation.messages %}
+            {% if conversation.draft %}
+                {% set messages = [conversation.first_message] %}
+            {% else %}
+                {% set messages = conversation.messages %}
+            {% endif %}
+            {% for message in messages %}
             <!-- First Comment -->
             <article class="row" id="mid{{message.id}}">
                 {% if current_user.id == message.user_id %}
                 <div class="col-md-2 col-sm-2 hidden-xs">
                         <figure class="thumbnail">
-                        <img class="img-responsive" src="http://www.keita-gaming.com/assets/profile/default-avatar-c5d8ec086224cb6fc4e395f4ba3018c2.jpg" />
+                        {% if message.user.avatar %}
+                            <img class="img-responsive" src="{{ message.user.avatar }}" />
+                        {% else %}
+                            <img class="img-responsive" src="http://www.keita-gaming.com/assets/profile/default-avatar-c5d8ec086224cb6fc4e395f4ba3018c2.jpg" />
+                        {% endif %}
                         <figcaption class="text-center"><strong><a href="{{ message.user.url }}" class="conversation-username">{{ message.user.username }}</a></strong></figcaption>
                     </figure>
                 </div>
@@ -30,14 +39,22 @@
                             <div class="conversation-message">
                                 {{ message.message|markup|safe }}
                             </div>
-                            <p class="text-right"><a href="#" class="btn btn-default btn-sm reply-btn" data-message-id="{{ message.id }}"><i class="fa fa-reply"></i> reply</a></p>
+                            {% if conversation.draft %}
+                                <p class="text-right"><a href="{{ url_for('message.edit_conversation', conversation_id=conversation.id) }}" class="btn btn-default btn-sm"><i class="fa fa-pencil"></i> edit</a></p>
+                            {% else %}
+                                <p class="text-right"><a href="#" class="btn btn-default btn-sm reply-btn" data-message-id="{{ message.id }}"><i class="fa fa-reply"></i> reply</a></p>
+                            {% endif %}
                         </div>
                     </div>
                 </div>
                 {% if current_user.id != message.user_id %}
                 <div class="col-md-2 col-sm-2 hidden-xs">
                         <figure class="thumbnail">
-                        <img class="img-responsive" src="http://www.keita-gaming.com/assets/profile/default-avatar-c5d8ec086224cb6fc4e395f4ba3018c2.jpg" />
+                            {% if message.user.avatar %}
+                                <img class="img-responsive" src="{{ message.user.avatar }}" />
+                            {% else %}
+                                <img class="img-responsive" src="http://www.keita-gaming.com/assets/profile/default-avatar-c5d8ec086224cb6fc4e395f4ba3018c2.jpg" />
+                            {% endif %}
                         <figcaption class="text-center"><strong><a href="{{ message.user.url }}" class="conversation-username">{{ message.user.username }}</a></strong></figcaption>
                     </figure>
                 </div>
@@ -47,7 +64,7 @@
         </section>
     </div>
 </div>
-{% if form %}
+{% if not conversation.draft %}
     {% from "macros.html" import render_field, render_submit_field %}
     <form class="form" action="#" method="post">
         {{ form.hidden_tag() }}

+ 5 - 2
flaskbb/templates/message/drafts.html

@@ -17,7 +17,7 @@
                 <div class="row">
                     <div class="col-xs-2 col-md-1">
                         {% if conversation.to_user.avatar %}
-                        <img src="{{ url_for('conversation.to_user.avatar') }}" class="img-circle img-responsive" alt="" />
+                        <img src="{{ conversation.to_user.avatar }}" class="img-circle" alt="avatar" width="65px" height="65px" />
                         {% else %}
                         <span class="fa fa-user" style="font-size: 80px; color: #E5E5E5"></span>
                         {% endif %}
@@ -33,12 +33,15 @@
                         </div>
                         <div class="comment-text">
                             {# the first message of conversation is always the starting message #}
-                            {{ conversation.messages[0].message }}
+                            {{ 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>
+                            <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>
+                            </a>
                         </div>
                     </div>
                 </div>

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

@@ -17,7 +17,7 @@
                 <div class="row">
                     <div class="col-xs-2 col-md-1">
                         {% if conversation.from_user.avatar %}
-                        <img src="{{ url_for('conversation.from_user.avatar') }}" class="img-circle img-responsive" alt="" />
+                        <img src="{{ conversation.from_user.avatar }}" class="img-circle" alt="avatar" width="65px" height="65px" />
                         {% else %}
                         <span class="fa fa-user" style="font-size: 80px; color: #E5E5E5"></span>
                         {% endif %}
@@ -25,7 +25,11 @@
                     <div class="col-xs-10 col-md-11">
                         <div>
                             <a href="{{ url_for('message.view_conversation', conversation_id=conversation.id) }}">
-                                <strong>{{ conversation.subject }}</strong>
+                                {% if conversation.unread %}
+                                    <strong>{{ conversation.subject }}</strong>
+                                {% else %}
+                                    {{ conversation.subject }}
+                                {% endif %}
                             </a>
                             <div class="mic-info">
                                 By: <a href="#">{{ conversation.from_user.username }}</a> on {{ conversation.date_created|format_date("%d %B %Y - %H:%M") }}
@@ -33,7 +37,7 @@
                         </div>
                         <div class="comment-text">
                             {# the first message of conversation is always the starting message #}
-                            {{ conversation.messages[0].message }}
+                            {{ conversation.first_message.message|truncate(200)|markup|safe }}
                         </div>
                         <div class="action">
                             <button type="button" class="btn btn-danger btn-xs" title="Delete">

+ 1 - 1
flaskbb/templates/message/message_layout.html

@@ -12,7 +12,7 @@
     <div class="col-sm-2">
         <div class="sidebar">
             <ul class="nav sidenav">
-                <a href="#" class="btn btn-success btn-compose">Compose</a>
+                <a href="{{ url_for('message.new_conversation') }}" class="btn btn-success btn-compose">Compose</a>
                 {{ navlink('message.inbox', _('Inbox')) }}
                 {{ navlink('message.sent', _('Sent')) }}
                 {{ navlink('message.drafts', _('Drafts')) }}

+ 2 - 2
flaskbb/templates/message/sent.html

@@ -18,7 +18,7 @@
                 <div class="row">
                     <div class="col-xs-2 col-md-1">
                         {% if conversation.to_user.avatar %}
-                        <img src="{{ url_for('conversation.to_user.avatar') }}" class="img-circle img-responsive" alt="" />
+                        <img src="{{ conversation.to_user.avatar }}" class="img-circle" alt="avatar" width="65px" height="65px" />
                         {% else %}
                         <span class="fa fa-user" style="font-size: 80px; color: #E5E5E5"></span>
                         {% endif %}
@@ -34,7 +34,7 @@
                         </div>
                         <div class="comment-text">
                             {# the first message of conversation is always the starting message #}
-                            {{ conversation.messages[0].message }}
+                            {{ conversation.first_message.message }}
                         </div>
                         <div class="action">
                             <button type="button" class="btn btn-danger btn-xs" title="Delete">

+ 2 - 2
flaskbb/templates/message/trash.html

@@ -18,7 +18,7 @@
                 <div class="row">
                     <div class="col-xs-2 col-md-1">
                         {% if conversation.from_user.avatar %}
-                        <img src="{{ url_for('conversation.from_user.avatar') }}" class="img-circle img-responsive" alt="" />
+                        <img src="{{ conversation.from_user.avatar }}" class="img-circle" alt="avatar" width="65px" height="65px" />
                         {% else %}
                         <span class="fa fa-user" style="font-size: 80px; color: #E5E5E5"></span>
                         {% endif %}
@@ -34,7 +34,7 @@
                         </div>
                         <div class="comment-text">
                             {# the first message of conversation is always the starting message #}
-                            {{ conversation.messages[0].message }}
+                            {{ conversation.first_message.message }}
                         </div>
                         <div class="action">
                             <button type="button" class="btn btn-danger btn-xs" title="Delete">