Jump to content

User:ChristieBot/app.py

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mike Christie (talk | contribs) at 11:15, 15 April 2024 (Update). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
from flask import Flask, request
import pywikibot
import re
import datetime as dt
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

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("/", methods=['GET'])
def main():
    main_page = "<h1>Good article statistics</h1>"
    main_page += GANstats.get_html_top()
    main_page += GANstats.get_style()
    main_page += '<form method="GET" action="editor_query">'
    main_page += 'Editor name: <input type="text" name="editor_name" /><br />'
    main_page += '<br />'
    main_page += '<input type="submit" value="Submit" />'
    main_page += '</form>'
    main_page += GANstats.get_html_bottom()
    return(main_page)

@app.route("/editor_query", methods=['GET'])
def 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 = ""
    editor_name = request.args.get('editor_name')
    editor_name = 'Tim O\'Doherty'
    editor = Editor(gan_conn, editor_name, config)
    app.logger.warning('In editor query for ' + editor_name)

    wbgan = {}
    database = "s54328__goodarticles_p" 
    wbg_conn = pymysql.connections.Connection(user=config['client']['user'], password=config['client']['password'], database="s54328__goodarticles_p", host='tools.db.svc.eqiad.wmflabs')
    with wbg_conn.cursor() as cursor:
        sql = "select lower(nominator) as nominator, count(*) as GA_count from nominators group by nominator order by count(*) desc"
        try:
            cursor.execute(sql)
        except pymysql.Error as e:
            #GAN.log(gan_conn,"editor_query:wbgan",editor_name,str(e))
            pass
        result = cursor.fetchall()
        for row in result: 
            wbgan[row[0]] = row[1]

    name_changes = {}
    cursor = gan_conn.cursor(pymysql.cursors.DictCursor)
    sql = "select n.old_name, n.new_name from " + GA_config.strings['name changes table name'] + " n "
    try:
        cursor.execute(sql)
        for row in cursor.fetchall():
            name_changes[row['old_name']] = row['new_name']
    except pymysql.Error as e:
        print(sql + " : " + str(e))

    current_GAs = 0
    if editor_name.lower() in wbgan.keys():
        current_GAs = int(wbgan[editor_name.lower()])

    if editor_name in name_changes.keys():
        if name_changes[editor_name].lower() in wbgan.keys():
            current_GAs += int(wbgan[name_changes[editor_name].lower()])

    if editor_name in name_changes.values():
        old_names = [x for x in name_changes.keys() if name_changes[x] == editor_name]
        for old_name in old_names:
            if old_name.lower() in wbgan.keys():
                current_GAs += int(wbgan[old_name.lower()])

    #name_changes = Name_changes.get_name_changes(gan_conn)
    #wbgan = WBGAN.get_wbgan(config) # The WBGAN database that holds the number of promoted GAs by each nominator.  Maintained by another tool.
    #current_GAs = WBGAN.get_GA_count(wbgan, editor.editor, name_changes)
    editor.current_GAs = current_GAs

    page_text = ""
    page_text += GANstats.get_html_top()
    page_text += GANstats.get_style()
    page_text += "<h1>Good article statistics for user: " + editor.editor + "</h1>\n"

    page_text += '<br /><a href = "https://ganfilter.toolforge.org">Return to search page</a><br /><br />'

    page_text+= editor.display_summary()

    constraints = {'nominator': editor.editor, 'type': 'GAN'}
    page_text += GAN_records.display_table(gan_conn, "Nomination list", constraints, "Good article nominations")

    constraints = {'reviewer': editor.editor, 'type': 'GAN'}
    page_text += GAN_records.display_table(gan_conn, "Review list", constraints, "Good article reviews")

    page_text += GANstats.get_html_bottom()
    return(page_text)