📄 order.py
字号:
# -*- coding: UTF-8 -*-# $Id: order.py 4014 2007-06-13 13:33:00Z jodal $## Copyright 2003, 2004 Norwegian University of Science and Technology# Copyright 2007 UNINETT AS## This file is part of Network Administration Visualized (NAV)## NAV is free software; you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation; either version 2 of the License, or# (at your option) any later version.## NAV is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License# along with NAV; if not, write to the Free Software# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA### Authors: Hans J酶rgen Hoel <hansjorg@orakel.ntnu.no># Stein Magnus Jodal <stein.magnus.jodal@uninett.no#"""Order page with helper classes of Device Management"""### Importsimport mx.DateTimeimport psycopgtry: from mod_python import utilexcept: pass # To allow use of pycheckerimport nav.db.managefrom nav.web.templates.deviceManagementTemplate import deviceManagementTemplatefrom nav.web.devicemanagement.constants import *from nav.web.devicemanagement.common import *from nav.web.devicemanagement import db as dtTablesfrom nav.web.devicemanagement.formattedlist import FormattedListfrom nav.web.devicemanagement.history import *from nav.web.devicemanagement.page import Pagefrom nav.web.devicemanagement.widget import Widget### Functionsdef order(req,path): subpath = path[1] page = Page() form = req.form alternativeOutput = None submenu = [('Active orders','Show and edit active orders', BASEPATH+'order/'), ('Add order','Add new order',BASEPATH+'order/add'), ('Order history','Show closed orders', BASEPATH+'order/history'), ('Add product','Add new product to the database', '/editdb/product/edit/')] page.submenu = submenu if not subpath or form.has_key(CN_CANCEL): subpath = 'main' addOrderKeys = [CN_YEAR,CN_MONTH,CN_DAY,CN_RETAILER,CN_COMMENT, CN_ORDERNUMBER,CN_PRODUCT,CN_AMOUNT,CN_ORG] formData = {} for key in addOrderKeys: if form.has_key(key): formData[key] = form[key] else: formData[key] = None page.name = 'order' page.widgets = {} page.title = 'Order devices' page.action = '' showConfirmButton = False orderAction = 'add' if form.has_key(CN_UPDATE_SUBMIT) or form.has_key(CN_UPDATE_CONFIRM): orderAction = 'edit' if form.has_key(CN_ADD_SUBMIT) or form.has_key(CN_ADD_CONFIRM) or \ form.has_key(CN_UPDATE_SUBMIT) or form.has_key(CN_UPDATE_CONFIRM): # Validate order form if form[CN_PRODUCT]: if form[CN_ORG]: if len(form[CN_AMOUNT]): validAmount = True try: amount = int(form[CN_AMOUNT]) except ValueError: validAmount = False amount = 0 page.errors.append('Invalid amount') subpath = orderAction if (amount < 1) and orderAction == 'add': validAmount = False page.errors.append('Invalid amount') subpath = orderAction elif (amount < 0): # If updating, 0 is a valid amount validAmount = False page.errors.append('Invalid amount') subpath = orderAction if (amount > MAX_NUMBER_OF_DEVICES_ORDERED) and \ not (form.has_key(CN_ADD_CONFIRM) or \ form.has_key(CN_UPDATE_CONFIRM)): validAmount = False showConfirmButton = True page.messages.append('Are you sure you want to ' +\ 'order this many devices?') subpath = orderAction validDate = True try: testDate = mx.DateTime.Date(int(formData[CN_YEAR]), int(formData[CN_MONTH]), int(formData[CN_DAY])) except mx.DateTime.RangeError: validDate = False page.errors.append('Invalid date') subpath = orderAction if validDate and validAmount: # Form validated, add new order if orderAction == 'edit': updateOrder(req,formData) page.messages.append('Updated order') else: registerOrder(req,formData) page.messages.append('Added order') else: page.errors.append('You must enter an amount') subpath = orderAction else: page.errors.append('You must select an organisation') subpath = orderAction else: page.errors.append('You must select a product') subpath = orderAction elif form.has_key(CN_DELETE_CONFIRM): # Confirm delete pressed deviceorderid = form['deviceorderid'] page = deleteOrder(deviceorderid,page) if subpath == 'add' or subpath == 'edit': page.action = BASEPATH + 'order/' if subpath == 'add': page.description = 'Add new order. Product, amount and ' +\ 'organisation is required to add an order.' page.subname = 'add' if showConfirmButton: page.widgets['submit'] = Widget(CN_ADD_CONFIRM, 'submit','Cofirm') else: page.widgets['submit'] = Widget(CN_ADD_SUBMIT, 'submit','Add order') elif subpath == 'edit': if len(path) > 2: deviceorderid = path[2] page.hiddenInputs.append(('deviceorderid',deviceorderid)) getDbOrder = True else: # No orderid from path, get from form deviceorderid = form['deviceorderid'] page.hiddenInputs.append(('deviceorderid',deviceorderid)) getDbOrder = False page.description = 'Edit an existing order. The amount is ' +\ 'number of devices not yet ' +\ 'registered as arrived. Decreasing the ' +\ 'amount will not remove any devices that are ' +\ 'already registered. Setting amount to zero ' +\ 'will close the order.' page.subname = 'edit' if showConfirmButton: page.widgets['submit'] = Widget(CN_UPDATE_CONFIRM, 'submit','Cofirm') else: page.widgets['submit'] = Widget(CN_UPDATE_SUBMIT, 'submit','Update order') if getDbOrder: # Lookup order amountsql = "SELECT count(*) FROM device WHERE " +\ "device.deviceorderid=deviceorder.deviceorderid" arrivedsql = "SELECT count(*) FROM device WHERE " +\ "device.deviceorderid=deviceorder.deviceorderid"+\ " AND device.active=true" sql = "SELECT ordered,ordernumber,comment,retailer," +\ "productid,orgid,(%s),(%s) " % (amountsql,arrivedsql)+\ "FROM deviceorder " +\ "WHERE deviceorderid='%s'" % (deviceorderid,) result = executeSQL(sql,fetch=True) if result: result = result[0] formData[CN_YEAR] = str(result[0].year) formData[CN_MONTH] = str(result[0].month) formData[CN_DAY] = str(result[0].day) formData[CN_AMOUNT] = str(result[6] - result[7]) formData[CN_ORDERNUMBER] = result[1] formData[CN_COMMENT] = result[2] formData[CN_RETAILER] = result[3] formData[CN_PRODUCT] = str(result[4]) formData[CN_ORG] = result[5] else: page.errors.append('Order does not exist') # Create widgets page.widgets['orderdate'] = Widget([CN_DAY,CN_MONTH,CN_YEAR],'date', 'Order date', [formData[CN_YEAR], formData[CN_MONTH], formData[CN_DAY]]) page.widgets['retailer'] = Widget(CN_RETAILER,'text','Retailer', formData[CN_RETAILER]) page.widgets['amount'] = Widget(CN_AMOUNT,'text','Amount', formData[CN_AMOUNT], options={'size': '5'},required=True) page.widgets['comment'] = Widget(CN_COMMENT,'text','Comment', formData[CN_COMMENT], options={'style': 'width: 100%;'}) page.widgets['ordernumber'] = Widget(CN_ORDERNUMBER,'text', 'Ordernumber', formData[CN_ORDERNUMBER]) # Make orglist memberOrgs = req.session['user'].getOrgIds() where = '' first = True for org in memberOrgs: if not first: where += "OR " where += "orgid='" + org + "' " first = False orgOptions = [(None,'Select an organisation',True)] if not len(memberOrgs): page.errors.append('You must be member of an organisation ' +\ 'to add an order') else: orgs = nav.db.manage.Org.getAllIterator(where=where) for org in orgs: orgOptions.append((org.orgid,org.descr + ' (' + org.orgid + ')', False)) page.widgets['org'] = Widget(CN_ORG,'select','Organisation', formData[CN_ORG], options={'options': orgOptions, 'style': 'width: 100%;'}, required=True) # Make productlist products = nav.db.manage.Product.getAllIterator() options = [(None,'Select a product',True)] for product in products: options.append((str(product.productid), product.productno + ' (' + product.descr + ')', False))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -