Esempio n. 1
0
def answer(slug):
    
    reply_form = ReplyForm()
    question = Question.query.filter_by(slug=slug).first_or_404()
    if request.method == 'POST':
        
        # if request.remote_addr == SITE_MIRROR.split(":")[1][2:]:
        #     g.user = User.query.get(request.form["user"])
        if g.user is None or not g.user.is_authenticated():
            flash(gettext('You need to be signed in to reply to questions.'))
            return redirect(url_for('about.answer', slug=question.slug))
            
        # if "user" not in request.form:
        #     form_json = {"user": g.user.id, "reply": reply_form.reply.data, "parent": reply_form.parent.data}
        #     try:
        #         opener = urllib2.urlopen("{0}{1}".format(SITE_MIRROR,request.path[1:]),urllib.urlencode(form_json),5)
        #     except:
        #         flash(gettext("The server is not responding. Please try again later."))
        #         return redirect(url_for('.answer', slug=question.slug))
            
        timestamp = datetime.utcnow()
        if not reply_form.parent.data:
            parent_id = 0 
            
        reply = Reply(body=reply_form.reply.data, timestamp=timestamp, 
                        user=g.user, question=question, parent_id=parent_id)
        db.session.add(reply)
        db.session.commit()
        if not reply_form.parent.data:
            reply.parent_id = reply.id
            db.session.add(reply)
            db.session.commit()
        flash(gettext('Reply submitted.'))
        
        #envia email para o admin
        send_mail('Aviso de nova publicacao no DataViva', [ADMINISTRATOR_EMAIL], render_template('about/ask/ask_feedback.html', question=question))
            
        return redirect(url_for('about.answer', slug=question.slug))
    else:
        
        question.vote = False
        if g.user.is_authenticated():
            vote = Vote.query.filter_by(type = 0, type_id = question.id, user_id = g.user.id).first()
            if vote:
                question.vote = True
                    
        return render_template("about/ask/answer.html",
            reply_form = reply_form,
            question = question, page = "ask")
Esempio n. 2
0
def answer(slug):

    reply_form = ReplyForm()
    question = Question.query.filter_by(slug=slug).first_or_404()
    if request.method == 'POST':

        # if request.remote_addr == SITE_MIRROR.split(":")[1][2:]:
        #     g.user = User.query.get(request.form["user"])
        if g.user is None or not g.user.is_authenticated():
            flash(gettext('You need to be signed in to reply to questions.'))
            return redirect(url_for('about.answer', slug=question.slug))

        # if "user" not in request.form:
        #     form_json = {"user": g.user.id, "reply": reply_form.reply.data, "parent": reply_form.parent.data}
        #     try:
        #         opener = urllib2.urlopen("{0}{1}".format(SITE_MIRROR,request.path[1:]),urllib.urlencode(form_json),5)
        #     except:
        #         flash(gettext("The server is not responding. Please try again later."))
        #         return redirect(url_for('.answer', slug=question.slug))

        timestamp = datetime.utcnow()
        if not reply_form.parent.data:
            parent_id = 0
        else:
            parent_id = reply_form.parent.data

        hiddenFld = 2;

        from ..utils.profanities_filter import ProfanitiesFilter

        file_banned_words = open(os.path.join(basedir, "dataviva/static/txt/blacklist.txt"))
        banned_words = [line.strip() for line in file_banned_words]

        filter = ProfanitiesFilter(banned_words, replacements = '*')
        _body =  filter.clean(str(reply_form.reply.data))


        reply = Reply(body=_body, timestamp=timestamp,
                        user=g.user, question=question, parent_id=parent_id, hidden=hiddenFld)

        db.session.add(reply)
        db.session.commit()
        if not reply_form.parent.data:
            reply.parent_id = reply.id
            db.session.add(reply)
            db.session.commit()

        try:
            flash(gettext('Reply submitted. Your reply will show up after we review it.'))
             #envia email para o admin
            send_mail('Aviso de nova publicacao no DataViva', [ADMINISTRATOR_EMAIL], render_template('about/ask/ask_feedback.html', question=question))
        except:
                flash(gettext('Your Reply was not sent. Try later, please.'))

        return redirect(url_for('about.answer', slug=question.slug))
    else:

        question.vote = False
        if g.user.is_authenticated():
            vote = Vote.query.filter_by(type = 0, type_id = question.id, user_id = g.user.id).first()
            if vote:
                question.vote = True

        return render_template("about/ask/answer.html",
            reply_form = reply_form,
            question = question, page = "ask")
Esempio n. 3
0
def answer(slug):

    reply_form = ReplyForm()
    question = Question.query.filter_by(slug=slug).first_or_404()
    if request.method == 'POST':

        # if request.remote_addr == SITE_MIRROR.split(":")[1][2:]:
        #     g.user = User.query.get(request.form["user"])
        if g.user is None or not g.user.is_authenticated():
            flash(gettext('You need to be signed in to reply to questions.'))
            return redirect(url_for('about.answer', slug=question.slug))

        # if "user" not in request.form:
        #     form_json = {"user": g.user.id, "reply": reply_form.reply.data, "parent": reply_form.parent.data}
        #     try:
        #         opener = urllib2.urlopen("{0}{1}".format(SITE_MIRROR,request.path[1:]),urllib.urlencode(form_json),5)
        #     except:
        #         flash(gettext("The server is not responding. Please try again later."))
        #         return redirect(url_for('.answer', slug=question.slug))

        timestamp = datetime.utcnow()
        if not reply_form.parent.data:
            parent_id = 0
        else:
            parent_id = reply_form.parent.data
        
        hiddenFld = 2;
        
        from ..utils.profanities_filter import ProfanitiesFilter
        
        file_banned_words = open(os.path.join(basedir, "dataviva/static/txt/blacklist.txt"))
        banned_words = [line.strip() for line in file_banned_words]
        
        filter = ProfanitiesFilter(banned_words, replacements = '*')          
        _body =  filter.clean(str(reply_form.reply.data))
        

        reply = Reply(body=_body, timestamp=timestamp, 
                        user=g.user, question=question, parent_id=parent_id, hidden=hiddenFld)

        db.session.add(reply)
        db.session.commit()
        if not reply_form.parent.data:
            reply.parent_id = reply.id
            db.session.add(reply)
            db.session.commit()
        
        try:
            flash(gettext('Reply submitted. Your reply will show up after we review it.'))
             #envia email para o admin
            send_mail('Aviso de nova publicacao no DataViva', [ADMINISTRATOR_EMAIL], render_template('about/ask/ask_feedback.html', question=question))
        except:
                flash(gettext('Your Reply was not sent. Try later, please.'))
        
        return redirect(url_for('about.answer', slug=question.slug))
    else:

        question.vote = False
        if g.user.is_authenticated():
            vote = Vote.query.filter_by(type = 0, type_id = question.id, user_id = g.user.id).first()
            if vote:
                question.vote = True

        return render_template("about/ask/answer.html",
            reply_form = reply_form,
            question = question, page = "ask")