]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/commitdiff
Added support for table wrreportcache.
authorphilipp <philipp@7aebc617-e5e2-0310-91dc-80fb5f6d2477>
Mon, 20 Jun 2011 19:29:37 +0000 (19:29 +0000)
committerphilipp <philipp@7aebc617-e5e2-0310-91dc-80fb5f6d2477>
Mon, 20 Jun 2011 19:29:37 +0000 (19:29 +0000)
git-svn-id: http://www.winterrodeln.org/svn/servermediawiki/trunk/wrpylib@828 7aebc617-e5e2-0310-91dc-80fb5f6d2477

setup.py
wrpylib/mwdb.py
wrpylib/wrmwcache.py
wrpylib/wrmwdb.py

index 0d3b52d71800a84e406763a229c48f05cc7bb30f..f3cde2171a6cf1185a6072f206652bd18ee45ee6 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -3,7 +3,7 @@
 from setuptools import setup
 
 setup(name='wrpylib',
 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',
     description='Winterrodeln Python Library',
     author='Philipp Spitzer',
     author_email='philipp.spitzer@winterrodeln.org',
index 6858c279adb91c7e80c56b7543aded081aa6ffaa..7e7da44cfc65b18666601ff4ab9e2135397c4a5f 100644 (file)
@@ -2,7 +2,7 @@
 # -*- coding: iso-8859-15 -*-
 # $Id$
 # $HeadURL$
 # -*- 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
 easy. The module uses sqlalchemy to access the database.
 """
 from sqlalchemy import Table, Column, types
index b46fe5f442df6ad187b666c468c0487413dc6a51..3e41c43fe863723197ddd8c3c271f90cf90be22e 100644 (file)
@@ -3,6 +3,7 @@
 # $Id$
 # $HeadURL$
 """Contains functions that maintain/update the cache tables."""
 # $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
 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()
 
             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()
+
index c977f14e71bce066b545ad6ae265afdc669b0c4b..761a8db3c500ea76f08c2ccd42278a672818ccd8 100644 (file)
@@ -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),
+    )
+
+