User:ChristieBot/app.py

from flask import Flask, request, render_template, url_for, flash, redirect
import pywikibot
import re
import datetime
import pymysql
import configparser
import os
import urllib.parse
import sys

# Local modules
sys.path.append('./www/python/src') 
from GA import Subtopic, GAN
import GA_config
from GA_web import GAN_record, GAN_records, GANstats, Editor
from FA_web import FAC_editor

HOME=os.environ.get('HOME') #get environment variable $HOME
replica_path=HOME + '/replica.my.cnf'
if os.path.exists(replica_path):          #check that the file is found
    config = configparser.ConfigParser()
    config.read(replica_path)
else:
    print('replica.my.cnf file not found')
    #GAN.log(gan_conn,"GANbot","Looking for replica.my.cnf","File not found")

# Set up the connection to the GAN database
database = "s55175__ganfilter"
gan_conn = pymysql.connections.Connection(user=config['client']['user'], password=config['client']['password'], database="s55175__ganfilter", host='tools.db.svc.eqiad.wmflabs')

app = Flask(__name__)

@app.route("/editor_query", methods=['GET'])
def editor_query():
    return render_template('index.html')

@app.route("/", methods=['GET'])
def main():
    return render_template('index.html')

@app.route('/f_editor_query/')
def f_editor_query():

    editor_name = request.args.get('editor_name')
    if editor_name is None:
        editor_name = ""

    if len(editor_name) != 0:
        app.logger.warning('In FA editor query for ' + editor_name)
        editor = FAC_editor(editor_name, config)
        editor_dict = {'name': editor.editor}
        editor_dict['quoted_name'] = urllib.parse.quote_plus(editor.editor)
        editor_dict['nominations'] = editor.nominations
        editor_dict['reviews'] = editor.reviews
        editor_dict['review_counts'] = editor.review_counts
        editor_dict['reviews_per_nomination'] = "{:.1f}".format(editor.reviews_per_nomination)
        editor_dict['nom_details'] = editor.nom_details
        editor_dict['nom_details_totals'] = editor.nom_details_totals
        editor_dict['review_details'] = editor.review_details
        editor_dict['review_details_totals'] = editor.review_details_totals
        editor_dict['review_types'] = editor.review_types
        editor_dict['review_types_totals'] = editor.review_types_totals
        editor_dict['nominations_list'] = editor.nominations_list
        editor_dict['reviews_list'] = editor.reviews_list
        return render_template('f_editor_query.html', editor_dict = editor_dict)
    else:
        return render_template('f_editor_query.html')

@app.route('/g_editor_query/')
def g_editor_query():

    database = "s55175__ganfilter"
    gan_conn = pymysql.connections.Connection(user=config['client']['user'], password=config['client']['password'], database="s55175__ganfilter", host='tools.db.svc.eqiad.wmflabs')

    editor_name = request.args.get('editor_name')
    if editor_name is None:
        editor_name = ""
    if len(editor_name) != 0:
        app.logger.warning('In GA editor query for ' + editor_name)
        wbgan = GANstats.get_wbgan(config)
        name_changes = GANstats.get_name_changes(config)
        editor = Editor(gan_conn, editor_name, config, wbgan, name_changes)
        constraints = {'nominator': editor.editor, 'type': 'GAN'}
        gans = GAN_records.get_table_data(gan_conn, "Nomination list", constraints)
        constraints = {'reviewer': editor.editor, 'type': 'GAN'}
        reviews = GAN_records.get_table_data(gan_conn, "Review list", constraints)
        editor_dict = {'name': editor.editor, 'quoted_name': urllib.parse.quote_plus(editor.editor), 'nominations': editor.nominations, 'GAs': editor.GAs, 'current_GAs': editor.current_GAs, 'reviews': editor.reviews, 'ratio': editor.ratio}
        return render_template('g_editor_query.html', editor_dict = editor_dict, gan_list = gans, review_list = reviews)
    else:
        return render_template('g_editor_query.html')