Commits

Wang Dingwei  committed d56d756

Added csv export function

  • Participants
  • Parent commits 2714de7

Comments (0)

Files changed (3)

File rqs/application.py

         (views.checkin, "/checkin"),
         (views.checkout, "/checkout"),
         (views.change_rack, "/change_rack"),
+        (views.export, "/export"),
         )
 
 def create_app(config=None, app_name=None, modules=None):

File rqs/views/export.py

+from flask import Module, g, send_file
+
+from flask import current_app as app
+
+from rqs.model import db, Unit
+from rqs.util import outsiders
+
+import csv
+from cStringIO import StringIO
+
+line = Module(__name__)
+
+@line.route('/')
+def export_csv():
+    result = Unit.query.all()
+    if app.config['USE_WSDL']:
+        pushed_out = outsiders(result, app.config['SOAP_CLIENT'])
+        for u in pushed_out:
+            result.remove(u)
+            db.session.delete(u)
+        db.session.commit()
+
+    csvlog = StringIO()
+    logwriter = csv.writer(csvlog)
+    logwriter.writerow([
+        'ServiceTag',
+        'Model',
+        'PDLINE',
+        'Rack',
+        'Input_Time',
+        'Age',
+        ])
+    for u in result:
+        u.getage(timeout=g.timeout)
+        logwriter.writerow([
+            u.ServiceTag,
+            u.Model,
+            u.RuninLine,
+            u.rack,
+            u.InputTime,
+            u.age,
+            ])
+    
+    csvlog.seek(0)
+    return send_file(csvlog, attachment_filename="export.csv",
+                     as_attachment=True)
+

File rqs/views/line.py

     "Query by line"
     session['default_line'] = line
 
-    result = Unit.query.filter_by(PDLINE=line)
+    result = Unit.query.filter_by(RuninLine=line)
 
     # Category data by rack
     by_rack = defaultdict(list)
 @cache.cached()
 def rack_info(line, rack):
     "Displays rack information of given pd line."
-    raw_data = Unit.query.filter(Unit.PDLINE==line).filter(Unit.Rack==rack)
+    raw_data = Unit.query.filter(Unit.RuninLine==line).filter(Unit.Rack==rack)
 
     result = []