Gráficas de compras por mes y año
authordanigm <dani@danigm.net>
Thu, 12 Nov 2009 22:17:00 +0000 (23:17 +0100)
committerdanigm <dani@danigm.net>
Thu, 12 Nov 2009 22:17:00 +0000 (23:17 +0100)
README [new file with mode: 0644]
compra.py
db.py
static/css/index.css
static/css/purchase.css [new file with mode: 0644]
templates/purchase.html
view/barchar.py [new file with mode: 0644]
view/items.py

diff --git a/README b/README
new file mode 100644 (file)
index 0000000..ebf597a
--- /dev/null
+++ b/README
@@ -0,0 +1,5 @@
+paquetes de los que depende:
+
+    - web.py
+    - pychart
+    - sqlalchemy
index 2402496..1522abc 100755 (executable)
--- a/compra.py
+++ b/compra.py
@@ -13,6 +13,7 @@ urls = (
         '/delete/(.*)', 'view.items.Delete',
         '/delete', 'view.items.Delete',
         '/update', 'view.items.Update', 
+        '/purchase/(.*)', 'view.items.Purchase',
         '/purchase', 'view.items.Purchase',
         '/prefs', 'view.login.Prefs',
         '/(.*)', 'view.items.List')
diff --git a/db.py b/db.py
index 3f4e5de..cf8f16f 100755 (executable)
--- a/db.py
+++ b/db.py
@@ -22,6 +22,9 @@ class Purchase(Base):
         self.cost = cost
         self.items = "|".join(items)
 
+    def getitems(self):
+        return ", ".join(self.items.split("|"))
+
 
 class Item(Base):
     __tablename__ = 'item'
index 608e862..8ac9fc0 100644 (file)
@@ -1,6 +1,6 @@
 body{ background-color: #eee; }
-#header h1 { border-bottom: 2px dotted #55e; color: #55e; }
+h1 { border-bottom: 2px dotted #55e; color: #55e; }
+h3 { border-bottom: 1px dotted #55e; color: #55e; }
 #content {margin: 0 auto 0 auto; width:95%; }
 .clear { clear: both; }
 #add input { margin-top: 10px; font-size: x-large; }
-
diff --git a/static/css/purchase.css b/static/css/purchase.css
new file mode 100644 (file)
index 0000000..f190953
--- /dev/null
@@ -0,0 +1,2 @@
+#chart, #links { text-align: center; }
+#chart img { border: 1px solid gray; padding: 5px; background-color: #aaf }
index 6872582..80586bd 100644 (file)
@@ -1,14 +1,31 @@
-$def with (purchase)
+$def with (year, chart, purchases, years)
 
-$var title: Compras
-$var cssfiles: /static/css/list.css
+$var title: Compras del $year
+$var cssfiles: /static/css/list.css /static/css/purchase.css
 
 <a href="/">index</a> | <a href="/prefs">prefs</a> | <a href="/logout">salir</a>
 
-<h2>Compras</h2>
+<h2>Compras del $year</h2>
+
+<div id="chart">
+    <img src="/$chart" alt="$chart" title="Grafica de gastos"/>
+</div>
+
+<div id="links">
+    $for i in years:
+        <a href="/purchase/$i.atime.year">$i.atime.year</a>
+        $if not loop.last:
+            |
+</div>
 
 <div id="add">
-    $for p in purchase:
-        $p.atime ## $p.cost ## $p.items
+    $for p in purchases:
+        <h3>$p[0] ($p[1])</h3>
+        <ul>
+            $for i in p[2]:
+            <li>
+                $i.cost - $i.getitems()
+            </li>
+        </ul>
 </div>
 
diff --git a/view/barchar.py b/view/barchar.py
new file mode 100644 (file)
index 0000000..7fb7bcc
--- /dev/null
@@ -0,0 +1,23 @@
+import sys
+
+from pychart import * 
+
+def create_chart(file, data, size=(700, 300)):
+    can = canvas.init(file)
+
+    theme.use_color = True
+    theme.default_font_size = 11
+
+    ar = area.T(size=size,
+                x_coord=category_coord.T(data, 0),
+                y_axis = axis.Y(label="gastado", tic_interval=20),
+                x_axis = axis.X(label=None),
+                legend=None)
+
+    ar.add_plot(bar_plot.T(cluster=(0,1), data=data, width=50, hcol=1,
+                           data_label_format="/o/7{}%d",
+                           fill_style=fill_style.red))
+
+    ar.draw(can)
+    can.close()
+
index 06c04f0..1c10728 100644 (file)
@@ -4,6 +4,10 @@ import login
 
 import datetime
 
+import barchar
+import random
+import os
+
 class List:
     @login.authenticated
     def GET(self, *args):
@@ -81,10 +85,46 @@ class Update:
 
 class Purchase:
     @login.authenticated
-    def GET(self):
+    def GET(self, y=None):
         orm = web.ctx.orm
-        purchases = orm.query(db.Purchase).all()
+        if not y:
+            y = datetime.datetime.now().year
+        else:
+            y = int(y)
+
+        months = ('', 'enero', 'febrero', 'marzo', 'abril', 'mayo',
+        'junio', 'julio', 'agosto', 'septiembre', 'octubre',
+        'noviembre', 'diciembre')
+
+        data = []
+        purchases = []
+        for i in range(1,13):
+            m = months[i]
+            allm = orm.query(db.func.sum(db.Purchase.cost)).\
+                filter(db.and_(db.extract('year', db.Purchase.atime) == y,
+                               db.extract('month', db.Purchase.atime) == i))
+
+
+            all2 = orm.query(db.Purchase).\
+                filter(db.and_(db.extract('year', db.Purchase.atime) == y,
+                               db.extract('month', db.Purchase.atime) == i)).all()
+
+            x = allm.one()[0]
+            if not x: x = 0
+            else: x = float(x)
+            data.append((m, x))
+
+            if x:
+                purchases.append((m, x, all2))
+
+        years = orm.query(db.Purchase).group_by(db.extract('year', db.Purchase.atime)).all()
+            
+        directory = 'static/charts/'
+        #for i in os.listdir(directory):
+        #    os.remove(directory+i)
+        chart = directory + "chart%d.png" % random.randint(1,100)
+        barchar.create_chart(chart, data)
         
-        return web.ctx.render.purchase(purchases)
+        return web.ctx.render.purchase(y, chart, purchases, years)