Exemple #1
0
def hello():
    current_user = auth1.current_user
    postsPerPage = 4
    baseRef = db.collection(u'questions')
    total_question = 0
    for question_in in baseRef.get():
        total_question = total_question + 1
    total_pages = total_question / postsPerPage
    total_pages = math.ceil(total_pages)
    currentPageRef = baseRef.limit(postsPerPage)
    currentPageQuerySnapshot = currentPageRef.get()
    # users_ref = db.collection(u'questions')
    # docs = users_ref.get()
    # # print(docs)
    question_print = db.collection(u'questions').document(
        'FrcE4xi97gyoI6U9BBR3').get()
    for tag in question_print.get('tags'):
        print(tag)
    docs1 = {}
    docs1 = currentPageQuerySnapshot
    return render_template('home.html',
                           current_user=current_user,
                           questions=docs1,
                           current_page=1,
                           total_pages=total_pages)
Exemple #2
0
def add_answer(post_id):
    current_user = auth1.current_user
    if (auth1.current_user):
        form = AddAnswer()
        if request.method == 'POST':
            content = form.content.data
            author = (auth1.get_account_info(
                current_user['idToken']).get('users')[0]).get('displayName')
            author_id = (auth1.get_account_info(
                current_user['idToken']).get('users')[0]).get('localId')
            data = {
                u'author': author,
                u'author_id': author_id,
                u'content': content,
                u'date_posted': datetime.datetime.now()
            }

            # db.collection(u'questions').document(post_id).update({u'answers': ([data])})
            quest_doc = db.collection(u'questions').document(post_id)
            quest = quest_doc.get()
            answers = []
            answers = quest.get('answers')
            answers.append(data)
            db.collection(u'questions').document(post_id).update(
                {u'answers': answers})
            flash('Your Answer Has Been Added!', 'success')
            return redirect(url_for('hello'))
    else:
        flash('Please Login To access This Page', 'danger')
        return redirect(url_for('login'))

    return render_template('add_answer.html',
                           form=form,
                           current_user=current_user)
Exemple #3
0
def delete_answer(post_id, index):
    current_user = auth1.current_user
    if (auth1.current_user):
        quest_doc = db.collection(u'questions').document(post_id)
        quest = quest_doc.get()
        answers = []
        answers = quest.get('answers')
        newanswers = []
        # print((answers))
        author_id = (auth1.get_account_info(
            current_user['idToken']).get('users')[0]).get('localId')

        if answers[index].get('author_id') != author_id:
            flash('You cannot delete that Answer!', 'danger')
            return redirect(url_for('hello'))
        else:
            indexs = 0
            for answer in answers:
                if indexs != index:
                    newanswers.append(answer)
                indexs = indexs + 1

            db.collection(u'questions').document(post_id).update(
                {u'answers': newanswers})
            flash('Your Answer has been deleted!', 'success')
            return redirect(url_for('hello'))
    else:
        flash('Please Login To access This Page', 'danger')
        return redirect(url_for('login'))
Exemple #4
0
def delete_post(post_id):
    current_user = auth1.current_user
    if (auth1.current_user):
        question = db.collection(u'questions').document(post_id).get()
        author_id = (auth1.get_account_info(
            current_user['idToken']).get('users')[0]).get('localId')
        if question.get('author_id') != author_id:
            flash('You cannot delete that Question!', 'danger')
            return redirect(url_for('hello'))
        else:
            question = db.collection(u'questions').document(post_id).delete()
            flash('Your Question has been deleted!', 'success')
            return redirect(url_for('hello'))
    else:
        flash('Please Login To access This Page', 'danger')
        return redirect(url_for('login'))
Exemple #5
0
def pred_tags():
    current_user = auth1.current_user
    if (auth1.current_user):
        form = OnlyPostForm()
        if request.method == 'POST':
            author = (auth1.get_account_info(
                current_user['idToken']).get('users')[0]).get('displayName')
            author_id = (auth1.get_account_info(
                current_user['idToken']).get('users')[0]).get('localId')
            answers = []
            tag1 = form.tag1.data
            tag2 = form.tag2.data
            tag3 = form.tag3.data
            tags = []
            tags.append(tag1)
            tags.append(tag2)
            tags.append(tag3)
            data = {
                u'author': author,
                u'author_id': author_id,
                u'content': content,
                u'date_posted': datetime.datetime.now(),
                u'title': title,
                u'answers': answers,
                u'tags': tags
            }
            answers_fire = db.collection(u'questions').document().set(data)
            flash('Question Added', 'success')
        elif request.method == 'GET':
            global title_question
            global content_question
            global tags_predicted
            form.title.data = title_question
            form.content.data = content_question
            length_tag = len(tags_predicted)
            if length_tag == 0:
                flash('No Tags Predicted', 'danger')
            elif length_tag == 1:
                form.tag1.data = tags_predicted[0]
                flash('1 Tag Predicted', 'success')
            elif length_tag == 2:
                form.tag1.data = tags_predicted[0]
                form.tag2.data = tags_predicted[1]
                flash('2 Tags Predicted', 'success')
            elif length_tag >= 3:
                form.tag1.data = tags_predicted[0]
                form.tag2.data = tags_predicted[1]
                form.tag3.data = tags_predicted[2]
                flash('3 Tags Predicted', 'success')
            tags_predicted = []
            title_question = " "
            content_question = " "
    else:
        flash('Please Login To access This Page', 'danger')
        return redirect(url_for('login'))
    return render_template('create_pred.html',
                           title='NEW QUESTION',
                           form=form,
                           legend='New Question',
                           current_user=current_user)
Exemple #6
0
def hello1(pageNo):
    current_user = auth1.current_user
    if (auth1.current_user):
        email = (auth1.get_account_info(
            current_user['idToken']).get('users')[0]).get('email')
        print(auth1.get_account_info(current_user['idToken']))
    postsPerPage = 4
    baseRef = db.collection(u'questions')
    total_question = 0
    for question_in in baseRef.get():
        total_question = total_question + 1
    total_pages = total_question / postsPerPage
    total_pages = (math.ceil(total_pages))
    print(total_pages)
    if (pageNo == 1):
        currentPageRef = baseRef.limit(postsPerPage)
    else:
        lastVisibleRef = baseRef.limit((pageNo - 1) * postsPerPage)
        lastVisibleQuerySnapshot = lastVisibleRef.get()
        for question_in in lastVisibleQuerySnapshot:
            last_question = question_in
        currentPageRef = baseRef.start_after(last_question).limit(postsPerPage)
    currentPageQuerySnapshot = currentPageRef.get()
    # users_ref = db.collection(u'questions')
    # docs = users_ref.get()
    # print(docs)
    docs1 = {}
    docs1 = currentPageQuerySnapshot
    return render_template('home.html',
                           current_user=current_user,
                           questions=docs1,
                           current_page=pageNo,
                           total_pages=total_pages)
Exemple #7
0
def post(post_id):
    current_user = auth1.current_user
    if (auth1.current_user):
        uid = (auth1.get_account_info(
            current_user['idToken']).get('users')[0]).get('localId')
    else:
        uid = None
    quest_doc = db.collection(u'questions').document(post_id)

    quest = quest_doc.get()
    answers = []
    answers = quest.get('answers')
    #  print((quest.get('answers')[0].get('content')))
    # for i in quest.get('answers'):
    #   print(i.get('content'))
    return render_template('question.html',
                           title=quest.get('title'),
                           question=quest,
                           current_user=current_user,
                           author_id=uid,
                           answers=answers,
                           index=-1)
Exemple #8
0
def quest():
    current_user = auth1.current_user
    if (auth1.current_user):
        form = PostForm()
        if request.method == 'POST':
            if form.submit2.data:
                title = form.title.data
                content = form.content.data
                global title_question
                title_question = title
                global content_question
                content_question = content
                title = re.sub(r'[^A-Za-z]+', ' ', title)
                content = re.sub(r'[^A-Za-z]+', ' ', content)
                print(title)
                main_url = 'http://127.0.0.1:5000/'
                url = main_url + 'query=' + title + " " + content + '/query_content=' + content
                print(url)
                try:
                    json = requests.get(url).json()
                except:
                    flash('An Error has occured', 'danger')
                    return redirect(url_for('quest'))
                tags = json.get('Tags')
                global tags_predicted
                tags_predicted = tags
                print(tags)
                for tag in tags:
                    print(tag)
                print(json)
                return redirect(url_for('pred_tags'))
            elif form.submit.data:
                author = (auth1.get_account_info(
                    current_user['idToken']).get('users')[0]
                          ).get('displayName')
                author_id = (auth1.get_account_info(
                    current_user['idToken']).get('users')[0]).get('localId')
                answers = []
                tag1 = form.tag1.data
                tag2 = form.tag2.data
                tag3 = form.tag3.data
                tags = []
                tags.append(tag1)
                tags.append(tag2)
                tags.append(tag3)
                data = {
                    u'author': author,
                    u'author_id': author_id,
                    u'content': content,
                    u'date_posted': datetime.datetime.now(),
                    u'title': title,
                    u'answers': answers,
                    u'tags': tags
                }
                answers_fire = db.collection(u'questions').document().set(data)
                flash('Question Added', 'success')
                return redirect(url_for('hello'))
    else:
        flash('Please Login To access This Page', 'danger')
        return redirect(url_for('login'))

    return render_template('create.html',
                           title='NEW QUESTION',
                           form=form,
                           legend='New Question',
                           current_user=current_user)