Bläddra i källkod

add title to xslt and add filtering

Pi 1 år sedan
förälder
incheckning
bbabee7be2
1 ändrade filer med 43 tillägg och 13 borttagningar
  1. 43 13
      app/rest/pyapi.py

+ 43 - 13
app/rest/pyapi.py

@@ -4,7 +4,8 @@
 #
 # THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY
 from bottle import route, request, run, response, abort
-from psycopg import sql, Cursor, connect
+from psycopg import Cursor, connect
+from psycopg.sql import SQL, Literal
 from datetime import date, datetime
 import os
 import matplotlib.pyplot as plt
@@ -77,10 +78,19 @@ style = """
                   select="$schema/xsd:element[@name=name(current())]/@type"/>
     <xsl:variable name="rowtypename"
                   select="$schema/xsd:complexType[@name=$tabletypename]/xsd:sequence/xsd:element[@name='row']/@type"/>
+    <xsl:variable name="tablename" select="$schema/xsd:complexType[@name=$rowtypename]/xsd:sequence/xsd:element[1]/@name"/>
 
     <html>
       <head>
-        <title><xsl:value-of select="name(current())"/></title>
+        <title>
+        <xsl:choose>
+          <xsl:when test="$tablename = 'Group'">Groups</xsl:when>
+          <xsl:when test="$tablename = 'Category'">Categories</xsl:when>
+          <xsl:when test="$tablename = 'Product'">Products</xsl:when>
+          <xsl:otherwise></xsl:otherwise>
+        </xsl:choose>
+        </title>
+        <meta name="viewport" content="width=device-width, initial-scale=1"/>
         <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/purecss@2.1.0/build/pure-min.css" integrity="sha384-yHIFVG6ClnONEA5yB5DJXfW2/KC173DIQrYoZMEtBvGzmf0PKiGyNEqe9N6BNDBH" crossorigin="anonymous"/>
         <link rel="stylesheet" href="https://shandan.one/css/grids-responsive-min.css"/>
         <link rel="stylesheet" href="https://shandan.one/css/responsive-visibility-collapse.css"/>
@@ -108,7 +118,7 @@ style = """
 </xsl:stylesheet>
 """
 @route('/grocery/style/table')
-def groups():
+def table():
     response.content_type = 'application/xhtml+xml; charset=utf-8'
     return style
 
@@ -132,36 +142,56 @@ ORDER BY 1', false, false, ''::text)
     return f"{heading}" + '\n'.join(xml)
 
 @route('/grocery/categories')
-def groups():
+def categories():
+    fields = { 'group': None }
     try:
         with conn.cursor() as cur:
-            xml = cur.execute("""
-SELECT query_to_xml_and_xmlschema('SELECT
+            fields.update({
+                k: request.query[k] for k in request.query.keys() if k == 'group'
+            })
+            xml = cur.execute(SQL("""
+SELECT query_to_xml_and_xmlschema($$SELECT
   c.name AS "Category",
   g.name AS "Group"
 FROM categories c
 JOIN groups g ON c.group_id = g.id
-ORDER BY 1, 2', false, false, ''::text)
-""").fetchone()[0].splitlines()
+WHERE g.name = COALESCE({group}, g.name)
+ORDER BY 1, 2$$, false, false, ''::text)
+""").format(
+  **{ k: Literal(v) for k,v in fields.items() }
+)).fetchone()[0].splitlines()
     finally:
         conn.commit()
     response.content_type = 'application/xhtml+xml; charset=utf-8'
     return f"{heading}" + '\n'.join(xml)
 
 @route('/grocery/products')
-def groups():
+def products():
+    fields = {
+        'group': None,
+        'category': None,
+    }
     try:
         with conn.cursor() as cur:
-            xml = cur.execute("""
-SELECT query_to_xml_and_xmlschema('SELECT
+            fields.update({
+                k: request.query[k] for k in request.query.keys() if k in ('group', 'category')
+            })
+            xml = cur.execute(SQL("""
+SELECT query_to_xml_and_xmlschema($$SELECT
   p.name AS "Product",
   c.name AS "Category",
   g.name AS "Group"
 FROM products p
 JOIN categories c ON p.category_id = c.id
 JOIN groups g ON c.group_id = g.id
-ORDER BY 1, 2, 3', false, false, ''::text)
-""").fetchone()[0].splitlines()
+WHERE
+  g.name = COALESCE({group}, g.name)
+AND
+  c.name = COALESCE({category}, c.name)
+ORDER BY 1, 2, 3$$, false, false, ''::text)
+""").format(
+  **{ k: Literal(v) for k,v in fields.items() }
+)).fetchone()[0].splitlines()
     finally:
         conn.commit()
     response.content_type = 'application/xhtml+xml; charset=utf-8'