Ejemplo n.º 1
0
def postQuestion():
    form = QuestionForm()
    if request.method == 'POST':
        try:
            username = current_user.get_id()
            question = QUESTIONS_COLLECTION.find_one(
                {'short_description': form.short_description.data})
            if question:
                flash("This question has already been asked", category='error')
                return render_template('post-a-question.html',
                                       title='HoverSpace | Post a Question',
                                       form=form)
            tags = request.form.getlist('tag')
            ques_obj = Question(username, form.short_description.data,
                                form.long_description.data, tags)
            quesID = ques_obj.postQuestion()
            tag_obj = TagMethods(quesID, tags)
            tag_obj.addQuestion()
            srch.add_string(quesID, form.short_description.data)
            flash("Your question has been successfully posted.",
                  category='success')
            return redirect(url_for('viewQuestion', quesID=quesID))
        except KeyError:
            return redirect(url_for('login'))
    return render_template('post-a-question.html',
                           title='HoverSpace | Post a Question',
                           form=form,
                           choices=tag_choices)
Ejemplo n.º 2
0
 def updateVotes(self, vote):
     QUESTIONS_COLLECTION.find_one_and_update(
         {'_id': ObjectId(self.quesID)}, {'$inc': {
             'votes': vote
         }})
     votes = (QUESTIONS_COLLECTION.find_one({'_id': ObjectId(self.quesID)
                                             }))['votes']
     return votes
Ejemplo n.º 3
0
 def get_answers(self, quesID):
     answers = []
     try:
         ans_obj = (QUESTIONS_COLLECTION.find_one({'_id': ObjectId(quesID)}))['ansID']
         for answer in ans_obj:
             answers.append(ANSWERS_COLLECTION.find_one({'_id': ObjectId(answer)}))
     except TypeError:
         answers = []
     return answers
Ejemplo n.º 4
0
 def setAcceptedAns(self, ansID, username):
     usr = (QUESTIONS_COLLECTION.find_one({'_id': ObjectId(self.quesID)
                                           }))['postedBy']
     if username == usr:
         QUESTIONS_COLLECTION.find_one_and_update(
             {'_id': ObjectId(self.quesID)},
             {'$set': {
                 'accepted_ans': ansID
             }})
Ejemplo n.º 5
0
def post_question():
    form = QuestionForm()
    if request.method == 'POST':
        try:
            username = current_user.get_id()
            question = QUESTIONS_COLLECTION.find_one( {'short_description': form.short_description.data} )
            if question:
                flash("This queston has already been asked", category='error')
                return render_template('post-a-question.html', title='HoverSpace | Post a Question', form=form)
            ques_obj = Question(username, form.short_description.data, form.long_description.data)
            quesID = ques_obj.post_question()
            flash("Your question has been successfully posted.", category='success')
            #print(quesID)
            return redirect(url_for('view_question', quesID=quesID))
        except KeyError:
            return redirect(url_for('login'))
    return render_template('post-a-question.html', title='HoverSpace | Post a Question', form=form)
Ejemplo n.º 6
0
 def addFlaggedBy(self, userID, postedBy):
     ques_obj = QUESTIONS_COLLECTION.find_one(
         {'_id': ObjectId(self.quesID)})
     flags = ques_obj['flaggedBy']
     votes = ques_obj['votes']
     if userID in flags:
         return "alreadyFlagged"
     QUESTIONS_COLLECTION.find_one_and_update(
         {'_id': ObjectId(self.quesID)},
         {'$addToSet': {
             'flaggedBy': userID
         }})
     if (len(flags) >= 10):
         flag = "True"
         self.setFlag(postedBy, votes, flag)
         usr = User(postedBy)
         usr.update_karma(-votes)
         return "quesRemoved"
     return "flagged"
Ejemplo n.º 7
0
 def getAcceptedAns(self):
     return (QUESTIONS_COLLECTION.find_one({'_id': ObjectId(self.quesID)
                                            }))['accepted_ans']
Ejemplo n.º 8
0
 def getQuestion(self):
     ques_dict = QUESTIONS_COLLECTION.find_one(
         {'_id': ObjectId(self.quesID)})
     return ques_dict
Ejemplo n.º 9
0
 def getFlag(self):
     return (QUESTIONS_COLLECTION.find_one({'_id':
                                            ObjectId(self.quesID)}))['flag']