<commit_before>from sqlalchemy import func
from flask import Blueprint, render_template, g, redirect, url_for, \
    flash, request, jsonify
from flask.ext.login import login_required
from flask.ext.babel import gettext
from dataviva import db
from datetime import datetime
# models
from dataviva.account.models import User
from dataviva.ask.models import Question, Status, Reply, Flag
# forms
from dataviva.admin.forms import AdminQuestionUpdateForm

import urllib2, urllib
from config import SITE_MIRROR

mod = Blueprint('admin', __name__, url_prefix='/admin')


@mod.before_request
def before_request():
    g.page_type = "admin"

###############################
# Views for ALL logged in users
# ---------------------------
@mod.route('/')
@login_required
def admin():
    return redirect(url_for('.admin_users'))

@mod.route('/users/')
@login_required
def admin_users():
    
    offset = request.args.get('offset', 0)
    limit = 50
    
    if request.is_xhr:
        
        # get all users EXCEPT the logged in user
        query = User.query.filter(User.id != g.user.id)
        
        items = query.limit(limit).offset(offset).all()
        items = [i.serialize() for i in items]
        
        return jsonify({"activities":items})
    
    return render_template("admin/admin_users.html")

@mod.route('/user/<int:user_id>/', methods = ['PUT','POST'])
def update_user(user_id):
    # test with:
    # curl -i -H "Content-Type: application/json" -X PUT 
    #   -d '{"role":2}' http://localhost:5000/admin/user/1
    
    if (g.user.is_authenticated() and g.user.role == 1) or (request.remote_addr == SITE_MIRROR.split(":")[1][2:]):
        
        user = User.query.get(user_id)

        if request.remote_addr != SITE_MIRROR.split(":")[1][2:]:
            
            user.role = request.json.get('role', user.role)
        
            form_json = {"role": user.role}
            print "\n\n\n"
            print form_json
            print "\n\n\n"
            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 jsonify({"error": gettext("The server is not responding. Please try again later.")})
        else:
            user.role = request.role
            
        db.session.add(user)
        db.session.commit()
    
        return jsonify( {'user': user.serialize()} )
    else:
        abort(404)

@mod.route('/questions/')
@mod.route('/questions/<status>/')
@login_required
def admin_questions(status=None):
    
    if not status:
        return redirect(url_for(".admin_questions", status="pending"))
    
    if request.is_xhr:
    
        offset = request.args.get('offset', 0)
        limit = 50
    
        # get all users EXCEPT the logged in user
        curr_status = Status.query.filter_by(name=status).first_or_404()
        query = Question.query.filter_by(status = curr_status)
    
        items = query.limit(limit).offset(offset).all()
        items = [i.serialize() for i in items]
    
        return jsonify({"activities":items})
    
    return render_template("admin/admin_questions.html")

@mod.route('/questions/<status>/<int:question_id>/', methods=['GET', 'POST'])
@login_required
def admin_questions_edit(status, question_id):
    
    q = Question.query.get_or_404(question_id)
    s = Status.query.filter_by(name=status).first_or_404()
    form = AdminQuestionUpdateForm()
    
    if form.validate_on_submit() or request.remote_addr == SITE_MIRROR.split(":")[1][2:]:

        if request.remote_addr != SITE_MIRROR.split(":")[1][2:]:
            form_json = {"status": form.status, "answer": form.answer, "previous_status": form.previous_status}
            try:
                opener = urllib2.urlopen("{0}{2}".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('.admin_questions_edit', status=status,question_id=question_id,form=form))
                
        previous_status = form.previous_status.data
        q.status = form.status.data
        q.status_notes = form.answer.data
        db.session.add(q)
        db.session.commit()
        flash(gettext('This question has now been updated.'))
        return redirect(url_for('.admin_questions', status=previous_status))
    
    # set defaults
    form.status.data = s
    form.previous_status.data = s.name
    form.answer.data = q.status_notes
    
    return render_template("admin/admin_questions_edit.html", 
                            question=q, status=status, form=form)<commit_msg>more SITE_MIRROR bug fixes<commit_after>from sqlalchemy import func
from flask import Blueprint, render_template, g, redirect, url_for, \
    flash, request, jsonify
from flask.ext.login import login_required
from flask.ext.babel import gettext
from dataviva import db
from datetime import datetime
# models
from dataviva.account.models import User
from dataviva.ask.models import Question, Status, Reply, Flag
# forms
from dataviva.admin.forms import AdminQuestionUpdateForm

import urllib2, urllib
from config import SITE_MIRROR

mod = Blueprint('admin', __name__, url_prefix='/admin')


@mod.before_request
def before_request():
    g.page_type = "admin"

###############################
# Views for ALL logged in users
# ---------------------------
@mod.route('/')
@login_required
def admin():
    return redirect(url_for('.admin_users'))

@mod.route('/users/')
@login_required
def admin_users():
    
    offset = request.args.get('offset', 0)
    limit = 50
    
    if request.is_xhr:
        
        # get all users EXCEPT the logged in user
        query = User.query.filter(User.id != g.user.id)
        
        items = query.limit(limit).offset(offset).all()
        items = [i.serialize() for i in items]
        
        return jsonify({"activities":items})
    
    return render_template("admin/admin_users.html")

@mod.route('/user/<int:user_id>/', methods = ['PUT','POST'])
def update_user(user_id):
    # test with:
    # curl -i -H "Content-Type: application/json" -X PUT 
    #   -d '{"role":2}' http://localhost:5000/admin/user/1
    
    if (g.user.is_authenticated() and g.user.role == 1) or (request.remote_addr == SITE_MIRROR.split(":")[1][2:]):
        
        user = User.query.get(user_id)

        if request.remote_addr != SITE_MIRROR.split(":")[1][2:]:
            
            user.role = request.json.get('role', user.role)
            form_json = {"role": user.role}
            
            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 jsonify({"error": gettext("The server is not responding. Please try again later.")})
        else:
            user.role = request.form.get("role")
            
        db.session.add(user)
        db.session.commit()
    
        return jsonify( {'user': user.serialize()} )
    else:
        abort(404)

@mod.route('/questions/')
@mod.route('/questions/<status>/')
@login_required
def admin_questions(status=None):
    
    if not status:
        return redirect(url_for(".admin_questions", status="pending"))
    
    if request.is_xhr:
    
        offset = request.args.get('offset', 0)
        limit = 50
    
        # get all users EXCEPT the logged in user
        curr_status = Status.query.filter_by(name=status).first_or_404()
        query = Question.query.filter_by(status = curr_status)
    
        items = query.limit(limit).offset(offset).all()
        items = [i.serialize() for i in items]
    
        return jsonify({"activities":items})
    
    return render_template("admin/admin_questions.html")

@mod.route('/questions/<status>/<int:question_id>/', methods=['GET', 'POST'])
@login_required
def admin_questions_edit(status, question_id):
    
    q = Question.query.get_or_404(question_id)
    s = Status.query.filter_by(name=status).first_or_404()
    form = AdminQuestionUpdateForm()
    
    if form.validate_on_submit() or request.remote_addr == SITE_MIRROR.split(":")[1][2:]:

        if request.remote_addr != SITE_MIRROR.split(":")[1][2:]:
            
            previous_status = form.previous_status.data
            q.status = form.status.data
            q.status_notes = form.answer.data
        
            form_json = {"status": q.status, "answer": q.status_notes, "previous_status": previous_status}
            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('.admin_questions_edit', status=status,question_id=question_id,form=form))
        else:
            previous_status = form.previous_status.data
            q.status = form.status.data
            q.status_notes = form.answer.data
            
        db.session.add(q)
        db.session.commit()
        flash(gettext('This question has now been updated.'))
        return redirect(url_for('.admin_questions', status=previous_status))
    
    # set defaults
    form.status.data = s
    form.previous_status.data = s.name
    form.answer.data = q.status_notes
    
    return render_template("admin/admin_questions_edit.html", 
                            question=q, status=status, form=form)
{'fraction_non_alphanumeric': 0.14043783560512185, 'fraction_numerical': 0.009087154068566708, 'mean_word_length': 2.6409774436090228, 'pattern_counts': {'":': 24, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5180946', 'n_tokens_mistral': 3061, 'n_tokens_neox': 2968, 'n_words': 611}