User:ChristieBot/app.py: Difference between revisions
Appearance
Content deleted Content added
Update |
Update |
||
Line 1: | Line 1: | ||
<pre> |
<pre> |
||
from flask import Flask, request |
from flask import Flask, request, render_template, url_for, flash, redirect |
||
import pywikibot |
import pywikibot |
||
import re |
import re |
||
import datetime |
import datetime |
||
import pymysql |
import pymysql |
||
import configparser |
import configparser |
||
Line 15: | Line 15: | ||
import GA_config |
import GA_config |
||
from GA_web import GAN_record, GAN_records, GANstats, Editor |
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 |
HOME=os.environ.get('HOME') #get environment variable $HOME |
||
Line 30: | Line 31: | ||
app = Flask(__name__) |
app = Flask(__name__) |
||
@app.route("/editor_query", methods=['GET']) |
|||
def editor_query(): |
|||
return render_template('index.html') |
|||
@app.route("/", methods=['GET']) |
@app.route("/", methods=['GET']) |
||
def main(): |
def main(): |
||
return render_template('index.html') |
|||
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( |
@app.route('/f_editor_query/') |
||
def |
def f_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 = request.args.get('editor_name') |
||
if editor_name is None: |
|||
editor_name = 'Tim O\'Doherty' |
|||
editor_name = "" |
|||
app.logger.warning('In editor query for ' + editor_name) |
|||
if len(editor_name) != 0: |
|||
app.logger.warning('In FA editor query for ' + editor_name) |
|||
database = "s54328__goodarticles_p" |
|||
editor = FAC_editor(editor_name, config) |
|||
wbg_conn = pymysql.connections.Connection(user=config['client']['user'], password=config['client']['password'], database="s54328__goodarticles_p", host='tools.db.svc.eqiad.wmflabs') |
|||
editor_dict = {'name': editor.editor} |
|||
with wbg_conn.cursor() as cursor: |
|||
editor_dict['quoted_name'] = urllib.parse.quote_plus(editor.editor) |
|||
sql = "select lower(nominator) as nominator, count(*) as GA_count from nominators group by nominator order by count(*) desc" |
|||
editor_dict['nominations'] = editor.nominations |
|||
try: |
|||
editor_dict['reviews'] = editor.reviews |
|||
editor_dict['review_counts'] = editor.review_counts |
|||
except pymysql.Error as e: |
|||
editor_dict['reviews_per_nomination'] = "{:.1f}".format(editor.reviews_per_nomination) |
|||
#GAN.log(gan_conn,"editor_query:wbgan",editor_name,str(e)) |
|||
editor_dict['nom_details'] = editor.nom_details |
|||
pass |
|||
editor_dict['nom_details_totals'] = editor.nom_details_totals |
|||
result = cursor.fetchall() |
|||
editor_dict['review_details'] = editor.review_details |
|||
for row in result: |
|||
editor_dict['review_details_totals'] = editor.review_details_totals |
|||
wbgan[row[0]] = row[1] |
|||
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/') |
|||
name_changes = {} |
|||
def g_editor_query(): |
|||
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)) |
|||
database = "s55175__ganfilter" |
|||
current_GAs = 0 |
|||
gan_conn = pymysql.connections.Connection(user=config['client']['user'], password=config['client']['password'], database="s55175__ganfilter", host='tools.db.svc.eqiad.wmflabs') |
|||
if editor_name.lower() in wbgan.keys(): |
|||
current_GAs = int(wbgan[editor_name.lower()]) |
|||
editor_name = request.args.get('editor_name') |
|||
if editor_name is None: |
|||
editor_name = "" |
|||
current_GAs += int(wbgan[name_changes[editor_name].lower()]) |
|||
if len(editor_name) != 0: |
|||
app.logger.warning('In GA editor query for ' + editor_name) |
|||
if editor_name in name_changes.values(): |
|||
wbgan = GANstats.get_wbgan(config) |
|||
old_names = [x for x in name_changes.keys() if name_changes[x] == editor_name] |
|||
name_changes = GANstats.get_name_changes(config) |
|||
for old_name in old_names: |
|||
editor = Editor(gan_conn, editor_name, config, wbgan, name_changes) |
|||
if old_name.lower() in wbgan.keys(): |
|||
constraints = {'nominator': editor.editor, 'type': 'GAN'} |
|||
current_GAs += int(wbgan[old_name.lower()]) |
|||
gans = GAN_records.get_table_data(gan_conn, "Nomination list", constraints) |
|||
constraints = {'reviewer': editor.editor, 'type': 'GAN'} |
|||
#name_changes = Name_changes.get_name_changes(gan_conn) |
|||
reviews = GAN_records.get_table_data(gan_conn, "Review list", constraints) |
|||
#wbgan = WBGAN.get_wbgan(config) # The WBGAN database that holds the number of promoted GAs by each nominator. Maintained by another tool. |
|||
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} |
|||
#current_GAs = WBGAN.get_GA_count(wbgan, editor.editor, name_changes) |
|||
return render_template('g_editor_query.html', editor_dict = editor_dict, gan_list = gans, review_list = reviews) |
|||
editor.current_GAs = current_GAs |
|||
else: |
|||
return render_template('g_editor_query.html') |
|||
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) |
|||
</pre> |
</pre> |
Latest revision as of 01:03, 22 May 2024
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')