Beispiel #1
0
 def perform_destroy(self, instance):
     question = Question()
     qs = question.collection.find(
         {"comments": {
             "$all": [str(instance.get("_id"))]
         }})
     if qs.count() == 1:
         question_obj = qs.next()
         comments = question_obj.get("comments")
         # delete comment id from question comment list
         comments.remove(str(instance.get("_id")))
         data = {"comments": comments}
         question.update({"_id": question_obj.get("_id")}, data)
         self.comment.remove({"_id": instance.get("_id")})
Beispiel #2
0
    def create(self, validated_data):
        comment = Comment()
        request = self.context.get("request")
        token = request.headers.get("Authorization").split()[1]
        payload = jwt_decode_handler(token)
        user_id = payload.get("user_id")
        comment.set_user_id(user_id)
        comment.set_message(validated_data.get("message"))
        data = {
            "user_id": ObjectId(user_id),
            "message": comment.get_message(),
            "date_created": comment.get_date_created(),
        }
        c_id = comment.save(data)
        comment.set_id(c_id)
        # Add comment's id to question comment list
        if self.context.get("question_id"):
            question = Question()
            qs = question.collection.find(
                {"_id": ObjectId(self.context.get("question_id"))})
            if qs.count() == 1:
                question_obj = qs.next()
                comments_list = question_obj.get("comments")
                comments_list.append(str(comment.get_id()))

                data = {"comments": comments_list}
                question.update({"_id": question_obj.get("_id")}, data)
        # Add comment's id to answer comment list
        elif self.context.get("answer_id"):
            answer = Answer()
            qs = answer.collection.find(
                {"_id": ObjectId(self.context.get("answer_id"))})
            if qs.count() == 1:
                answer_obj = qs.next()
                comments_list = answer_obj.get("comments")
                comments_list.append(str(comment.get_id()))

                data = {"comments": comments_list}
                answer.update({"_id": answer_obj.get("_id")}, data)
        return comment
Beispiel #3
0
    def on_message_callback(self, channel, method, properties, body):
        msg = body.decode("utf-8")
        data = json.loads(msg)
        print(" [x] Received %r" % data)

        if data.get("action") == "Add question views":
            self.create_question_views(data)
            self.add_question_views(ObjectId(data.get("question_id")))
            print("add question views done.")

        elif data.get("action") == "Add question votes":
            vote_id = self.create_vote(data)
            question = Question()
            question_votes = data.get("question_votes")
            question_votes.append(str(vote_id))
            context = {"votes": question_votes}
            question.update({"_id": ObjectId(data.get("data_id"))}, context)
            print("add question votes done.")

        elif data.get("action") == "undo question votes":
            vote_id = ObjectId(data.get("vote_id"))
            question = Question()
            question_votes = data.get("question_votes")
            question_votes.remove(str(vote_id))
            context = {"votes": question_votes}
            question.update({"_id": ObjectId(data.get("question_id"))},
                            context)
            vote = Vote()
            vote.remove({"_id": vote_id})
            print("undo question votes done.")

        elif data.get("action") == "increase question votes":
            vote = Vote()
            context = {"vote_type": 1}
            vote.update({"_id": ObjectId(data.get("vote_id"))}, context)
            print("increase question votes done.")

        elif data.get("action") == "decrease question votes":
            vote = Vote()
            context = {"vote_type": -1}
            vote.update({"_id": ObjectId(data.get("vote_id"))}, context)
            print("decrease question votes done.")

        elif data.get("action") == "Add answer votes":
            vote_id = self.create_vote(data)
            answer = Answer()
            answer_votes = data.get("answer_votes")
            answer_votes.append(str(vote_id))
            context = {"votes": answer_votes}
            answer.update({"_id": ObjectId(data.get("data_id"))}, context)
            print("add answer votes done.")

        elif data.get("action") == "undo answer votes":
            vote_id = ObjectId(data.get("vote_id"))
            answer = Answer()
            answer_votes = data.get("answer_votes")
            answer_votes.remove(str(vote_id))
            context = {"votes": answer_votes}
            answer.update({"_id": ObjectId(data.get("answer_id"))}, context)
            vote = Vote()
            vote.remove({"_id": vote_id})
            print("undo answer votes done.")

        elif data.get("action") == "increase answer votes":
            vote = Vote()
            context = {"vote_type": 1}
            vote.update({"_id": ObjectId(data.get("vote_id"))}, context)
            print("increase answer votes done.")

        elif data.get("action") == "decrease answer votes":
            vote = Vote()
            context = {"vote_type": -1}
            vote.update({"_id": ObjectId(data.get("vote_id"))}, context)
            print("decrease answer votes done.")