Rev 94 | Rev 122 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
'''Created on 23-Mar-2010@author: ashish'''from elixir import *from shop2020.model.v1.catalog.impl import DataServicefrom shop2020.model.v1.catalog.impl.DataService import Category, Item, Vendor,\Warehouse, ItemInventoryHistory, CurrentInventorySnapshot, ItemInfo,\ItemChangeLogfrom shop2020.thriftpy.model.v1.catalog.ttypes import Category as TCategory,\CatalogServiceExceptionfrom shop2020.thriftpy.model.v1.catalog.ttypes import Item as TItemfrom shop2020.thriftpy.model.v1.catalog.ttypes import Vendor as TVendorfrom shop2020.thriftpy.model.v1.catalog.ttypes import Warehouse as TWarehouseimport datetimefrom shop2020.utils.Utils import status_active, status_added, status_retireddef initialize():DataService.initialize()def is_category_present(id_key):category = Category.get_by(id=id_key)if category:return Truereturn Falsedef get_category(id_key):category = Category.get_by(id=id_key)return categorydef get_all_categories():return Category.query.all()def get_all_items_for_category(category_id):category = Category.get_by(id=category_id)if not category:raise CatalogServiceException(108, "Some unforeseen error while obtaining")return category.itemsdef get_all_items(is_active):if is_active:return Item.get_by(status=status_active)else:return Item.query.all()def get_all_items_by_status(status):if not status:#return all itemsreturn get_all_items(False)else:return Item.get_by(status=status).all()def get_Item(item_id):return Item.get_by(id=item_id)def get_all_vendors():return Vendor.query.all()def get_all_active_vendors():return Vendor.get_by(status=status_active).all()def get_Vendor(vendor_id):return Vendor.get_by(id=vendor_id)def get_Warehouse(warehouse_id):return Warehouse.get_by(id=warehouse_id)def get_all_warehouses_for_item(item_id):item = get_Item(item_id)if not item:raise CatalogServiceException(108, "Some unforeseen error while obtaining item")return item.get_all_warehouses()def add_warehouse(warehouse):if not warehouse:raise CatalogServiceException(108, "Bad warehouse")if get_Warehouse(warehouse.id):#warehouse is already present.raise CatalogServiceException(101, "Warehouse already present")ds_warehouse = Warehouse()ds_warehouse.id = warehouse.idds_warehouse.location = warehouse.locationds_warehouse.status = status_activeds_warehouse.addedOn = datetime.datetime.now()session.commit()return ds_warehouse.iddef add_vendor(vendor):if get_Vendor(vendor.id):raise CatalogServiceException(101, "Vendor already present")ds_vendor = Vendor()if not vendor:raise CatalogServiceException(108, "Bad vendor in request")ds_vendor.id = vendor.idds_vendor.name = vendor.namesession.commit()return ds_vendor.iddef add_item(item):if not item:raise CatalogServiceException(108, "Bad item in request")if get_Item(item.id):raise CatalogServiceException(101, "Item already exists")ds_item = Item()ds_item.id = item.idds_item.status = status_addedds_item.addedOn = datetime.datetime.now()ds_item.price = item.priceif item.otherInfo:for k,v in item.otherInfo.iteritems():info = ItemInfo()info.key = kinfo.value = vds_item.iteminfo.append(info)#check if categories present. If yes, add them to systemif item.categories:for category_id in item.categories:category = get_category(category_id)if category:ds_item.category.append(category)else:raise CatalogServiceException(108, "Category not found ".category_id)session.commit();return ds_item.iddef add_category(category):try:ds_category = Category()ds_category.id = category.idds_category.name = category.nameds_category.addedOn = datetime.datetime.now()ds_category.isRoot = category.isTopLevelif category.parent_id:ds_category.parent= Category.get_by(id=category.parent_id)if category.childCategories:for child in category.childCategories:ds_category.children.append(get_category(child))ds_category.status = status_active#save the category nowsession.commit()return ds_category.idexcept:return 0def add_item_to_category(id, item):category = get_category(id)if not category:raise CatalogServiceException(101, "no such category exists")if not item:raise CatalogServiceException(104, "bad item")try:item_id = add_item(item)added_item = get_Item(item_id)added_item.categories.append(category)session.commit()return item_idexcept:raise CatalogServiceException(101, "intenral error")def add_item_to_category_by_id(category_id, item_id):category = get_category(category_id)item = get_Item(item_id)if not category:raise CatalogServiceException(101, "no such category exists")if not item:raise CatalogServiceException(104, "bad item")item.categories.append(category)session.commit()def update_inventory(item_id, warehouse_id, quantity, timestamp):item = get_Item(item_id)warehouse = get_Warehouse(warehouse_id)time = datetime.datetime.fromtimestamp(timestamp)if not item:raise CatalogServiceException(106, "Item not found, how did you get this?")if not warehouse:raise CatalogServiceException(107, "Warehouse? Where?")try:item_inventory_history = ItemInventoryHistory()item_inventory_history.warehouse = warehouseitem_inventory_history.item = itemitem_inventory_history.timestamp = timeitem_inventory_history.availability = quantitycurrent_inventory_snapshot = CurrentInventorySnapshot.get_by(item=item, warehouse=warehouse)current_inventory_snapshot.availibility = quantitycurrent_inventory_snapshot.checkedOn = timesession.commit()except:raise CatalogServiceException(108, "Some unforeseen error while updating inventory")def get_item_inventoy(item_id):inventory = Item.get_by(id=item_id).currentInventoryif not inventory:raise CatalogServiceException(108, "Some unforeseen error while updating inventory")return inventorydef retire_category(category_id):if not category_id:raise CatalogServiceException(101, "Bad category id")category = get_category(category_id)if not category:raise CatalogServiceException(108, "Category not present")category.status = status_retiredsession.commit()def retire_warehouse(warehouse_id):if not warehouse_id:raise CatalogServiceException(101, "Bad warehouse id")warehouse = get_Warehouse(warehouse_id)if not warehouse:raise CatalogServiceException(108, "warehouse id not present")warehouse.status = status_retired;session.commit()def retire_vendor(vendor_id):if not vendor_id:raise CatalogServiceException(101, "bad vendor id")vendor = get_Vendor(vendor_id)if not vendor:raise CatalogServiceException(108, "vendor id not present")vendor.status = status_retiredsession.commit()def retire_item(item_id):if not item_id:raise CatalogServiceException(101, "bad item id")item = get_Item(item_id)if not item:raise CatalogServiceException(108, "item id not present")item.status = status_retiredsession.commit()def start_item_on(item_id, timestamp):if not item_id:raise CatalogServiceException(101, "bad item id")item = get_Item(item_id)if not item:raise CatalogServiceException(108, "item id not present")item.status = status_activeitem.startDate = datetime.datetime.fromtimestamp(timestamp)add_status_change_log(item, status_active)session.commit()def retire_item_on(item_id, timestamp):if not item_id:raise CatalogServiceException(101, "bad item id")item = get_Item(item_id)if not item:raise CatalogServiceException(108, "item id not present")item.status = status_retireditem.retireDate = datetime.datetime.fromtimestamp(timestamp)add_status_change_log(item, status_retired)session.commit()def add_status_change_log(item, new_status):item_change_log = ItemChangeLog()item_change_log.new_status = new_statusitem_change_log.old_status = item.statusitem_change_log.timestamp = datetime.datetime.now()item_change_log.item = itemsession.commit()def change_item_status(item_id, new_status):if not item_id:raise CatalogServiceException(101, "bad item id")item = get_Item(item_id)if not item:raise CatalogServiceException(108, "item id not present")item.status = status_retiredadd_status_change_log(item, status_retired)session.commit()def get_item_availability_for_warehouse(warehouse_id, item_id):if not warehouse_id:raise CatalogServiceException(101, "bad warehouse_id")if not item_id:raise CatalogServiceException(101, "bad item_id")warehouse = get_Warehouse(warehouse_id)if not warehouse:raise CatalogServiceException(108, "warehouse does not exist")item = get_Item(item_id)if not item:raise CatalogServiceException(108, "item does not exist")current_inventory_snapshot = CurrentInventorySnapshot.query.filter(CurrentInventorySnapshot.warehouse.id == warehouse_id, CurrentInventorySnapshot.item.id == item_id).one()if not current_inventory_snapshot:return 0else:return current_inventory_snapshot.availibility