Permalink
Browse files

added support for bulk import of paper reports

  • Loading branch information...
1 parent fb9b478 commit 518e31203ba6dc9885758eaa612812ba2aea253f @rgaudin rgaudin committed Mar 7, 2012
Showing with 61 additions and 0 deletions.
  1. +61 −0 tools/import_paper_reports.py
@@ -0,0 +1,61 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# vim: ai ts=4 sts=4 et sw=4 nu
+
+import os
+import shutil
+import pprint
+
+from pnlp_core.excel import MalariaExcelForm
+from bolibana.tools.utils import get_autobot
+from bolibana.reporting.excel import IncorrectReportData
+
+def submit_excel_file(filepath, author, success='success', error='error'):
+
+ # store basename
+ filename = os.path.basename(filepath)
+
+ print('Submitting %s' % filename)
+
+ # create Excel form from path
+ form = MalariaExcelForm(filepath)
+
+ # test all logic checks on form
+ if form.is_valid(author=author, bulk_import=True):
+ try:
+ print("form is valid")
+ # create the report.
+ form.create_report(author=author)
+ print("report created")
+ # move file to success folder
+ shutil.move(filepath, os.path.join(success, filename))
+ return True
+ except IncorrectReportData:
+ print("can't create report")
+ pass
+ else:
+ print("form is not valid")
+ # form is not valid or report failed to create. move to failed folder
+ shutil.move(filepath, os.path.join(error, filename))
+ # get all errors string and write them to an error file
+ errors = form.errors.all(True)
+ pprint.pprint(errors)
+ err_f = open(os.path.join(error, filename + '.error.txt'), 'w')
+ err_f.write(pprint.pformat(errors))
+ err_f.close()
+ return False
+
+def import_all(src_folder):
+ author = get_autobot()
+ success_dir = 'success'
+ error_dir = 'error'
+ if not os.path.exists(success_dir):
+ os.makedirs(success_dir)
+ if not os.path.exists(error_dir):
+ os.makedirs(error_dir)
+ for fname in os.listdir(src_folder):
+ if not fname.endswith('.xls'):
+ print('skipping %s' % fname)
+ continue
+ submit_excel_file(os.path.join(src_folder, fname),
+ author, success_dir, error_dir)

0 comments on commit 518e312

Please sign in to comment.