From a5095aaddecc95263976ee9fb02d0d3f8dfedc87 Mon Sep 17 00:00:00 2001 From: philipp Date: Mon, 20 Jun 2011 19:29:37 +0000 Subject: [PATCH] Added support for table wrreportcache. git-svn-id: http://www.winterrodeln.org/svn/servermediawiki/trunk/wrpylib@828 7aebc617-e5e2-0310-91dc-80fb5f6d2477 --- setup.py | 2 +- wrpylib/mwdb.py | 2 +- wrpylib/wrmwcache.py | 56 ++++++++++++++++++++++++++++++++++++++++++++ wrpylib/wrmwdb.py | 19 +++++++++++++++ 4 files changed, 77 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index 0d3b52d..f3cde21 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ from setuptools import setup setup(name='wrpylib', - version='0.0.11', + version='0.0.12', description='Winterrodeln Python Library', author='Philipp Spitzer', author_email='philipp.spitzer@winterrodeln.org', diff --git a/wrpylib/mwdb.py b/wrpylib/mwdb.py index 6858c27..7e7da44 100644 --- a/wrpylib/mwdb.py +++ b/wrpylib/mwdb.py @@ -2,7 +2,7 @@ # -*- coding: iso-8859-15 -*- # $Id$ # $HeadURL$ -"""This module contains code to make tha access of MediaWiki tables +"""This module contains code to make the access of MediaWiki tables easy. The module uses sqlalchemy to access the database. """ from sqlalchemy import Table, Column, types diff --git a/wrpylib/wrmwcache.py b/wrpylib/wrmwcache.py index b46fe5f..3e41c43 100644 --- a/wrpylib/wrmwcache.py +++ b/wrpylib/wrmwcache.py @@ -3,6 +3,7 @@ # $Id$ # $HeadURL$ """Contains functions that maintain/update the cache tables.""" +from xml.etree import ElementTree from sqlalchemy import schema from sqlalchemy.sql import select import formencode @@ -94,3 +95,58 @@ def update_wrinncache(connection): raise UpdateCacheError(error_msg, inn_page.page_title, e) transaction.commit() + +def update_wrreportcache(connection, page_id=None): + """Updates the wrreportcache table. + :param connection: sqlalchemy connection + :param page_id: Updates only the reportcache table for the sledrun described on the Winterrodeln wiki page + with the specified page_id. Use None for this parameter to update the whole table. + + >>> from sqlalchemy.engine import create_engine + >>> engine = create_engine('mysql://philipp@localhost:3306/winterrodeln_wiki?charset=utf8&use_unicode=1') + >>> update_wrreportcache(engine.connect()) + """ + metadata = schema.MetaData() + wrreport = wrmwdb.wrreport_table(metadata) + wrreportcache = wrmwdb.wrreportcache_table(metadata) + transaction = connection.begin() + + # Delte the datasets we are going to update + sql_del = wrreportcache.delete() + if not page_id is None: sql_del = sql_del.where(wrreportcache.c.page_id == page_id) + connection.execute(sql_del) + + def insert_row(connection, rowlist): + if len(rowlist) == 0: return + # Build XML + reports_xml = ElementTree.Element('reports') + for row in rowlist: + report_xml = ElementTree.SubElement(reports_xml, 'report') + report_xml.set('report_id', unicode(row.report_id)) + report_xml.set('date_report', unicode(row.report_date_report)) + report_xml.set('condition', unicode(row.report_condition)) + report_xml.set('author_name', unicode(row.report_author_name)) + report_xml.set('author_username', unicode(row.report_author_username)) + report_xml.text = unicode(row.report_description) + reports_xml.set('page_id', unicode(row.page_id)) + reports_xml.set('page_title', row.page_title) + reports_xml = unicode(ElementTree.tostring(reports_xml, 'utf8'), 'utf8') # there is not ElementTree.tounicode()) + # Insert the report(s) + row = dict(rowlist[0]) + row['reports_xml'] = reports_xml + connection.execute(wrreportcache.insert(values=row)) + + # Select the rows to update + sql = 'select page_id, page_title, wrreport.id as report_id, date_report as report_date_report, `condition` as report_condition, description as report_description, author_name as report_author_name, if(author_userid is null, null, author_username) as report_author_username from wrreport where {0}`condition` is not null and date_invalid > now() and delete_date is null order by page_id, date_report desc, date_entry desc'.format('' if page_id is None else 'page_id={0} and '.format(page_id)) + cursor = connection.execute(sql) + page_id = None + rowlist = [] + for row in cursor: + if row.page_id != page_id: + insert_row(connection, rowlist) + page_id = row.page_id + rowlist = [] + rowlist.append(row) + insert_row(connection, rowlist) + transaction.commit() + diff --git a/wrpylib/wrmwdb.py b/wrpylib/wrmwdb.py index c977f14..761a8db 100644 --- a/wrpylib/wrmwdb.py +++ b/wrpylib/wrmwdb.py @@ -112,3 +112,22 @@ def wrinncache_table(metadata): ) +def wrreportcache_table(metadata): + """Returns the sqlalchemy Table representing the "wrreportcache" Winterrodeln table in MediaWiki. + Current table definition. + * version 1.5 (introduction) + :param metadata: metadata = sqlalchemy.MetaData() + """ + return Table("wrreportcache", metadata, + Column("page_id", types.Integer, primary_key=True), + Column("page_title", types.Unicode(255), nullable=False), + Column("report_id", types.Integer), + Column("report_date_report", types.Date), + Column("report_condition", types.Integer), + Column("report_description", types.Unicode), + Column("report_author_name", types.Unicode(30)), + Column("report_author_username", types.Unicode(30)), + Column("reports_xml", types.Unicode), + ) + + -- 2.39.5