Rev 4503 | Rev 4552 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
'''Created on 29-Jul-2011@author: Chandranshu'''from elixir import metadata, setup_all, sessionfrom shop2020.purchase.main.model.LineItem import LineItemfrom shop2020.purchase.main.model.Purchase import Purchasefrom shop2020.purchase.main.model.PurchaseOrder import PurchaseOrderfrom shop2020.purchase.main.model.Supplier import Supplierfrom shop2020.thriftpy.purchase.ttypes import PurchaseServiceExceptionfrom sqlalchemy import create_engineimport datetimeimport logginglogging.basicConfig(level=logging.DEBUG)class PurchaseServiceHandler:'''classdocs'''def __init__(self, dbname='warehouse', db_hostname='localhost', echoOn=True):'''Constructor'''engine = create_engine('mysql://root:shop2020@' + db_hostname + '/' + dbname, pool_recycle=7200)metadata.bind = enginemetadata.bind.echo = echoOnsetup_all(True)def createPurchaseOrder(self, tPurchaseOrder):"""Creates a purchase order based on the data in the given purchase order object.This method populates a number of missing fieldsParameters:- purchaseOrder"""try:purchaseOrder = PurchaseOrder(tPurchaseOrder)session.commit()purchaseOrder.set_po_number()session.commit()return purchaseOrder.idfinally:self.close_session()def getAllPurchaseOrders(self, status):"""Returns a list of all the purchase orders in the given stateParameters:- status"""try:pos = PurchaseOrder.query.filter_by(status=status).all()return [po.to_thrift_object() for po in pos ]finally:self.close_session()def getPurchaseOrder(self, id):"""Returns the purchase order with the given id. Throws an exception if there is no such purchase order.Parameters:- id"""try:purchaseOrder = PurchaseOrder.get_by(id=id)if not purchaseOrder:raise PurchaseServiceException(101, "No purchase order can be found with id:" + str(id))return purchaseOrder.to_thrift_object()finally:self.close_session()def getSupplier(self, id):"""Returns the supplier with the given order id. Throws an exception if there is no such supplier.Parameters:- id"""try:return Supplier.get_by(id=id).to_thrift_object()finally:self.close_session()def startPurchase(self, purchaseOrderId, invoiceNumber, freightCharges):"""Creates a purchase for the given purchase order.Throws an exception if no more purchases are allowed against the given purchase order.Parameters:- purchaseOrderId- invoiceNumber- freightCharges"""try:purchase_order = PurchaseOrder.get_by(id = purchaseOrderId)purchase = Purchase(purchase_order, invoiceNumber, freightCharges)session.commit()return purchase.idfinally:self.close_session()def closePurchase(self, purchaseId):"""Marks a purchase as complete and updates the receivedOn time.Throws an exception if no such purchase exists.Parameters:- purchaseId"""try:purchase = Purchase.get_by(id=purchaseId)purchase.receivedOn = datetime.datetime.now()session.commit()finally:self.close_session()def getAllPurchases(self, purchaseOrderId, open):"""Returns all open or closed purchases for the given purchase order. Throws an exception if no such purchase order existsParameters:- purchaseOrderId- open"""try:if open:purchases = Purchase.query.filter_by(purchaseOrder_id = purchaseOrderId, receivedOn = None).all()else:purchases = Purchase.query.filter_by(purchaseOrder_id = purchaseOrderId).filter(receivedOn != None).all()return [purchase.to_thrift_object() for purchase in purchases]finally:self.close_session()def getPrice(self, purchaseId, itemId):"""Returns the price at which we bought an item from a supplierParameters:- purchaseId- itemId"""purchaseOrderId = Purchase.query.filter_by(id = purchaseId).one().purchaseOrder_idreturn LineItem.query.filter_by(purchaseOrder_id = purchaseOrderId).filter_by(itemId = itemId).one().unitPricedef close_session(self):if session.is_active:print "session is active. closing it."session.close()def isAlive(self, ):"""For checking weather service is active alive or not. It also checks connectivity with database"""try:session.query(Supplier.id).limit(1).all()return Trueexcept:return Falsefinally:self.close_session()