def serialize(self, fields = []): from sys2do.util.common import _info result = {} m = self.__mapper__.columns for cname in fields: if cname not in m.keys(): continue colClz = m[cname] if isinstance(colClz, Column): v = getattr(self, cname) if v is None: v = u'' #the value is None elif m[cname].foreign_keys : #it's a foreign key tmp_name = cname.replace("_id", '') try: v = unicode(getattr(self, tmp_name)) except: _info(traceback.print_exc()) v = u'' elif isinstance(v, dt) : v = dt.strftime(SYSTEM_DATETIME_FORMAT) elif isinstance(v, date) : v = dt.strftime(SYSTEM_DATE_FORMAT) elif isinstance(v, (int, float)) : v = u'%s' % v else: v = unicode(v) result[cname] = (v, colClz.doc or cname) # _info(result) return result
def export(self): ids = _gl('order_ids') _info(ids) def _f(obj): if obj.destination_city_id: return "".join(map(lambda v : unicode(v), [obj.destination_province, obj.destination_city])) return unicode(obj.destination_province) data = [] for r in DBSession.query(OrderHeader).filter(OrderHeader.id.in_(ids)).order_by(OrderHeader.create_time): row = [r.order_time, r.ref_no, _f(r), unicode(r.destination_company), r.destination_contact, r.qty, r.weight, r.destination_tel, '', ] # A - H deliver_header = r.get_deliver_header() if deliver_header : row.extend(['', deliver_header.no, deliver_header.sendout_time, '', '', deliver_header.expect_time, deliver_header.actual_time, '', ]) # I - P else: row.extend(['', '', '', '', '', '', '', '', ]) # I - P pickup_info = ['', '', '', '', '', '0.5', '', '', ] tmp_count = 0 for index, d in enumerate(r.pickup_details): if index > 2: break if d.qty : pickup_info[index + 1] = d.qty tmp_count += d.qty pickup_info[4] = r.qty - tmp_count row.extend(pickup_info) # Q - X row.extend(['', '', '', 'Y' if r.actual_time > r.expect_time else 'N', 'Y' if r.signed_time else 'N', r.signed_contact or '', r.signed_time, '', '', ]) # Y - AG data.append(row) if not data : data = [['', ], ] if not os.path.exists(TMP_FOLDER): os.makedirs(TMP_FOLDER) current = dt.now() templatePath = os.path.join(TEMPLATE_FOLDER, "template.xls") tempFileName = os.path.join(TMP_FOLDER, "report_tmp_%s_%d.xls" % (current.strftime("%Y%m%d%H%M%S"), random.randint(0, 1000))) realFileName = os.path.join(TMP_FOLDER, "report_%s.xls" % (dt.now().strftime("%Y%m%d%H%M%S"))) shutil.copy(templatePath, tempFileName) report_xls = SummaryReport(templatePath = tempFileName, destinationPath = realFileName) report_xls.inputData(data = data) report_xls.outputData() try: os.remove(tempFileName) except: pass return send_file(realFileName, as_attachment = True)
def sms(self): _info(request.values) return 'OK'