Commits

ronald martinez committed d5a9179

conversations v1

Comments (0)

Files changed (4)

 
 BROKER_URL = 'sqs://%s:%s@' % (settings.AWS_KEY, settings.AWS_SECRET) 
 
-print BROKER_URL
-
 #BROKER_USER = settings.AWS_KEY
 #BROKER_PASSWORD = settings.AWS_SECRET
 

controllers/product.py

                 if not self.have_comments:
 
                     for c in product.conversations:
+
+                        _comments = [dict(user=x.user_data.name,
+                            text=x.description,
+                            created_at=x.created_at_f) \
+                                for x in c.comments]
+
                         tot_unread_comments += int(c.unread_comments)
                         conversations.append(dict(id=c.id,
                             user=c.user_data.name,
-                            unread_comments=c.unread_comments))
+                            unread_comments=c.unread_comments,
+                            comments=_comments))
                 else:
 
                     try:
             self.send_error(404)
             return
 
-        status_code = 0
-
         user = self.get_current_user()
-        message = self.get_argument('message', '')
-        product_id = self.get_argument('product_id', None)
+        is_owner = True if user.email == product.user else False
 
-        logging.info('from_user: %s' % user.email)
-        logging.info('to_user: %s' % product.user)
-        logging.info('product: %s' % product.slug)
+        if not is_owner:
 
-        if product.user == user.email:
-            logging.info('the same user!')
-            status_code = 3
-        else:
-            if len(message) < 3:
-                status_code = 1
+            status_code = 0
+
+            message = self.get_argument('message', '')
+            #product_id = self.get_argument('product_id', None)
+            product_id = product.id
+            conversation_id = self.get_argument('conversation_id', None)
+
+            logging.info('from_user: %s' % user.email)
+            logging.info('to_user: %s' % product.user)
+            logging.info('product: %s' % product.slug)
+
+            if product.user == user.email:
+                logging.info('the same user!')
+                status_code = 3
             else:
-                try:
-                    conversation = Conversation.objects.filter(
-                        product=product_id,
-                        with_user=user.email
-                    )
-                except Exception as exc:
-                    logging.error(exc)
+                if len(message) < 3:
+                    status_code = 1
                 else:
                     try:
-                        conversation = conversation[0]
-                    except:
-                        pass
+                        conversation = Conversation.objects.filter(
+                            product=product_id,
+                            with_user=user.email
+                        )
+                    except Exception as exc:
+                        logging.error(exc)
+                    else:
+                        try:
+                            conversation = conversation[0]
+                        except:
+                            pass
 
-                if not conversation:
-                    conversation = Conversation()
-                    conversation.product = product_id
-                    conversation.with_user = user.email
+                    if not conversation:
+                        conversation = Conversation()
+                        conversation.product = product_id
+                        conversation.with_user = user.email
+
+                        try:
+                            conversation.save()
+                        except Exception as exc:
+                            logging.error(exc)
+                            status_code = 2
+
+                    comment = Comment()
+                    comment.description = message
+                    comment.conversation = conversation.id
+                    comment.user = user.email
 
                     try:
-                        conversation.save()
+                        comment.save()
                     except Exception as exc:
                         logging.error(exc)
                         status_code = 2
+                    else:
+                        conversation.unread_comments = int(
+                            conversation.unread_comments) + 1
+
+                        try:
+                            conversation.update()
+                        except Exception as exc:
+                            logging.error(exc)
+                            status_code = 3
+        else:
+
+            status_code = 0
+
+            user = self.get_current_user()
+            message = self.get_argument('message', '')
+            to_user = self.get_argument('to_user', None)
+
+            try:
+                to_user = User.objects.get(to_user)
+            except Exception as exc:
+                logging.error(exc)
+                self.send_error(404)
+
+            try:
+                product = Product.objects.filter(slug=slug)[0]
+            except:
+                self.send_error(404)
+                return
+
+            if len(message) < 3:
+                status_code = 1
+            else:
+                conversation = Conversation.objects.filter(
+                    product=product.id,
+                    with_user=to_user.email
+                )[0]
+
+                if not conversation:
+                    self.send_error(404)
 
                 comment = Comment()
                 comment.description = message
                 except Exception as exc:
                     logging.error(exc)
                     status_code = 2
-                else:
-                    conversation.unread_comments = int(
-                        conversation.unread_comments) + 1
 
-                    try:
-                        conversation.update()
-                    except Exception as exc:
-                        logging.error(exc)
-                        status_code = 3
 
         logging.info('status_code: %s' % status_code)
         self.get(slug)
             conversation = Conversation.objects.filter(
                 product=self.id, with_user=with_user)[0]
         except Exception as exc:
-            logging.error(exc)
+            logging.warn(exc)
         return conversation
 
     @property
     product = models.Field(required=True)
     status = models.Field(default='disabled')
     created_at = models.DateTimeField(default=datetime.now())
-    last_modified_at = models.DateTimeField(default='')
+    last_modified_at = models.DateTimeField()
     unread_comments = models.NumberField(default=0)
     with_user = models.Field(required=True)
 
     conversation = models.Field(required=True)
     status = models.Field(default='disabled')
     created_at = models.DateTimeField(default=datetime.now())
-    published_at = models.DateTimeField(default='')
+    published_at = models.DateTimeField()
     user = models.Field(required=True)
 
     @property

templates/site/product.html

                 </div>{% endfor %}
                 <form action="" method="post" class="box-comment">{{ xsrf }}
                   <input type="hidden" name="product_id" value="{{ product.id }}"/>
-                  <textarea placeholder="Send a message to this user." name="comment" class="textarea"></textarea>
+                  <textarea placeholder="Send a message to this user." name="message" class="textarea"></textarea>
                   <input type="submit" value="SEND" class="submit"/>
                 </form>
               </div>
             {% endfor %}
 
 
-
               <!-- CON USUARIO 2-->
               <div class="comment-tit accordion-trigger"><img src="{{ handler.static_url('images/site/product-user3.png') }}" class="com-user"/><span class="com-name">Username X</span><span class="com-num">2            </span></div>
               <div class="comment-box accordion-content">
                     <div class="clear"></div>
                   </div>
                 </div>
-              </div>
+            </div>
+
             </div>{% endif %}
             {% endif %}
             <div class="clear"></div>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.