def permission(self): method = _g('m', 'LIST') if method not in ['LIST', 'NEW', 'UPDATE', 'DELETE', 'SAVE_NEW', 'SAVE_UPDATE']: flash(MSG_NO_SUCH_ACTION, MESSAGE_ERROR); return redirect(url_for('.view', action = 'index')) if method == 'LIST': page = _g('page') or 1 objs = DBSession.query(Permission).filter(Permission.active == 0).order_by(Permission.name).all() def url_for_page(**params): return url_for('bpAdmin.view', action = 'permission', m = 'LIST', page = params['page']) records = paginate.Page(objs, page, show_if_single_page = True, items_per_page = PAGINATE_PER_PAGE, url = url_for_page) return render_template('admin/permission_index.html', records = records) elif method == 'NEW': groups = Group.all() return render_template('admin/permission_new.html', groups = groups) elif method == 'UPDATE': id = _g('id', None) if not id : flash(MSG_NO_ID_SUPPLIED, MESSAGE_ERROR) return redirect(url_for('.view', action = 'permission')) obj = Permission.get(id) if not obj : flash(MSG_RECORD_NOT_EXIST, MESSAGE_ERROR) return redirect(url_for('.view', action = 'permission')) gids = map(lambda v:v.id, obj.groups) all_groups = Group.all() return render_template('admin/permission_update.html', v = obj.populate(), gids = gids, all_groups = all_groups) elif method == 'DELETE': id = _g('id', None) if not id : flash(MSG_NO_ID_SUPPLIED, MESSAGE_ERROR) return redirect(url_for('.view', action = 'permission')) obj = Permission.get(id) if not obj : flash(MSG_RECORD_NOT_EXIST, MESSAGE_ERROR) return redirect(url_for('.view', action = 'permission')) obj.active = 1 obj.groups = [] DBSession.commit() flash(MSG_DELETE_SUCC, MESSAGE_INFO) return redirect(url_for('.view', action = 'permission')) elif method == 'SAVE_NEW': obj = Permission.saveAsNew(request.values) obj.groups = DBSession.query(Group).filter(Group.id.in_(_gl("gids"))).all() DBSession.commit() flash(MSG_SAVE_SUCC, MESSAGE_INFO) return redirect(url_for('.view', action = 'permission')) elif method == 'SAVE_UPDATE': id = _g('id', None) if not id : flash(MSG_NO_ID_SUPPLIED, MESSAGE_ERROR) return redirect(url_for('.view', action = 'permission')) obj = Permission.get(id) if not obj : flash(MSG_RECORD_NOT_EXIST, MESSAGE_ERROR) return redirect(url_for('.view', action = 'permission')) obj.saveAsUpdate(request.values) obj.groups = DBSession.query(Group).filter(Group.id.in_(_gl('gids'))).all() obj.commit() flash(MSG_UPDATE_SUCC, MESSAGE_INFO) return redirect(url_for('.view', action = 'permission'))
def barcode(self): method = _g('m', 'LIST') if method not in ['LIST', 'NEW', 'PRINT', 'SAVE_NEW']: flash(MSG_NO_SUCH_ACTION, MESSAGE_ERROR); return redirect(url_for('.view', action = 'index')) DBObj = Barcode index_page = 'admin/barcode_index.html' new_page = 'admin/barcode_new.html' print_page = 'admin/barcode_print.html' action = 'barcode' if method == 'LIST': if _g('SEARCH_SUBMIT'): # come from search values = {'page' : 1} for f in ['value', 'ref_no', 'status', 'create_time_from', 'create_time_to'] : values[f] = _g(f) values['field'] = _g('field', None) or 'create_time' values['direction'] = _g('direction', None) or 'desc' else: #come from paginate or return values = session.get('master_barcode_values', {}) if _g('page') : values['page'] = int(_g('page')) elif 'page' not in values : values['page'] = 1 session['master_barcode_values'] = values conditions = [DBObj.active == 0] if values.get('value', None): conditions.append(DBObj.value.op('like')('%%%s%%' % values['value'])) if values.get('ref_no', None): conditions.append(DBObj.ref_no.op('like')('%%%s%%' % values['ref_no'])) if values.get('status', None): conditions.append(DBObj.status == values['status']) if values.get('create_time_from', None): conditions.append(DBObj.create_time > values['create_time_from']) if values.get('create_time_to', None): conditions.append(DBObj.create_time < '%s 23:59' % values['create_time_to']) field = values.get('field', 'create_time') if values.get('direction', 'desc') == 'desc': result = DBSession.query(DBObj).filter(and_(*conditions)).order_by(desc(getattr(DBObj, field))) else: result = DBSession.query(DBObj).filter(and_(*conditions)).order_by(getattr(DBObj, field)) def url_for_page(**params): return url_for('bpAdmin.view', action = action, m = 'LIST', page = params['page']) records = paginate.Page(result, values['page'], show_if_single_page = True, items_per_page = 100, url = url_for_page) return render_template(index_page, records = records, action = action, values = values) elif method == 'NEW': return render_template(new_page, action = action) elif method == 'PRINT': ids = _gl('ids') records = DBSession.query(DBObj).filter(DBObj.id.in_(ids)).order_by(desc(DBObj.create_time)) return render_template(print_page, records = records) elif method == 'SAVE_NEW': qty = _g('qty') records = [DBObj.getOrCreate(None, None, status = 1) for i in range(int(qty))] DBSession.commit() if _g('type') == 'CREATE': flash(MSG_SAVE_SUCC, MESSAGE_INFO) return redirect(url_for('.view', action = action)) else: return render_template(print_page, records = records)
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 add_deliver(self): ids = _gl('order_ids') if not ids: ids = [_g('order_ids'), ] order_headers = DBSession.query(OrderHeader).filter(OrderHeader.id.in_(ids)) if order_headers.count() == 1: h = order_headers[0] destination_province_id = h.destination_province_id destination_city_id = h.destination_city_id destination_address = h.destination_address destination_contact = h.destination_contact destination_tel = h.destination_tel destination_mobile = h.destination_mobile payment_id = h.payment_id pickup_type_id = h.pickup_type_id else: destination_province_id = destination_city_id = destination_address = destination_contact = destination_tel = destination_mobile = None payment_id = pickup_type_id = None total_qty = total_vol = total_weight = 0 for h in order_headers: if h.status >= SORTING[0]: flash(MSG_ORDER_NOT_FIT_FOR_DELIVER, MESSAGE_ERROR) if request.referrer: return redirect(request.referrer) else: return redirect(self.default()) else: total_qty += h.qty or 0 total_vol += h.vol or 0 total_weight += h.weight or 0 suppliers = getMasterAll(Supplier) return {'result' : order_headers, 'suppliers' : suppliers, 'destination_province_id' : destination_province_id, 'destination_city_id' : destination_city_id, 'destination_address' : destination_address, 'destination_contact' : destination_contact, 'destination_tel' : destination_tel, 'destination_mobile' : destination_mobile, 'payment_id' : payment_id, 'pickup_type_id' : pickup_type_id, 'total_qty' : total_qty, 'total_vol' : total_vol, 'total_weight' : total_weight, }
def save_update(self): id = _g("id") if not id: flash(MSG_NO_ID_SUPPLIED, MESSAGE_ERROR) return redirect(self.default()) header = DBSession.query(OrderHeader).get(id) fields = [ 'customer_id', 'order_time', 'source_province_id', 'source_city_id', 'destination_province_id', 'destination_city_id', 'ref_no', 'source_company_id', 'source_address', 'source_contact', 'source_tel', 'source_mobile', 'payment_id', 'qty', 'weight', 'vol', 'shipment_type_id', 'destination_company_id', 'destination_address', 'destination_contact', 'destination_tel', 'destination_mobile', 'estimate_time', 'expect_time', 'actual_time', 'qty_ratio', 'weight_ratio', 'vol_ratio', 'amount', 'cost', 'remark', 'pickup_type_id', 'pack_type_id', 'insurance_charge', 'sendout_charge', 'receive_charge', 'package_charge', 'other_charge', 'load_charge', 'unload_charge', 'proxy_charge', 'note_id', 'note_no', 'source_sms', 'destination_sms', ] _remark = [] old_info = header.serialize(fields) # to used for the history log checkbox_fields = ['source_sms', 'destination_sms', ] try: for f in fields: setattr(header, f, _g(f)) for f in checkbox_fields: vs = _gl(f) if vs : setattr(header, f, vs[0]) else : setattr(header, f, None) no = _g('no') if no != header.no: try: old_barcode = DBSession.query(Barcode).filter(Barcode.value == header.no).one() old_barcode.status = 1 # mark the old barcode to be reserved except: pass try: new_barcode = DBSession.query(Barcode).filter(Barcode.value == no).one() new_barcode.status = 0 # mark the new barcode to be used except NoResultFound : DBSession.add(Barcode(value = no)) except: pass header.no = no header.barcode = generate_barcode_file(header.no) if header.status == -2 : header.status = 0 item_ids = [c.id for c in header.item_details] item_json = _g('item_json', '') for c in json.loads(item_json): if not c.get('id', None) : continue if isinstance(c['id'], basestring) and c['id'].startswith("old_"): # existing item cid = c['id'].split("_")[1] t = DBSession.query(ItemDetail).get(cid) t.item_id = c.get('item_id', None) t.qty = c.get('qty', None) t.weight = c.get('weight', None) t.vol = c.get('vol', None) t.remark = c.get('remark', None) item_ids.remove(t.id) else: DBSession.add(ItemDetail( header = header, item_id = c.get('item_id', None), qty = c.get('qty', None), weight = c.get('weight', None), vol = c.get('vol', None), remark = c.get('remark', None), )) DBSession.query(ItemDetail).filter(ItemDetail.id.in_(item_ids)).update({'active' : 1}, False) # handle the file upload old_attachment_ids = map(lambda (k, v) : v, _gp("old_attachment_")) old_attachment_ids.extend(multiupload()) header.attachment = old_attachment_ids DBSession.commit() # handle the system log new_info = header.serialize(fields) change_result = header.compare(old_info, new_info) header.insert_system_logs(change_result) flash(MSG_UPDATE_SUCC, MESSAGE_INFO) except: _error(traceback.print_exc()) DBSession.rollback() flash(MSG_SERVER_ERROR, MESSAGE_ERROR) return redirect(url_for('.view', action = 'review', id = header.id))
def out_note_new(self): ids = _gl('note_ids') records = DBSession.query(InventoryLocationItem).filter(and_(InventoryLocationItem.active == 0, InventoryLocationItem.id.in_(ids))).order_by(InventoryLocationItem.item_id) return { "records" : records, }