Subversion Repositories SmartDukaan

Rev

Rev 103 | Rev 122 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
94 ashish 1
'''
2
Created on 22-Mar-2010
3
 
4
@author: ashish
5
'''
6
from elixir import *
7
from elixir.entity import Entity
8
from elixir.fields import Field
9
from sqlalchemy.types import Integer
10
from elixir.relationships import OneToMany, ManyToOne
11
import datetime
12
 
13
 
103 ashish 14
 
94 ashish 15
class Category(Entity):
16
    '''
17
    This class represents the catalog. 
18
    '''
115 ashish 19
    #id = Field(Integer, primary_key=True)
94 ashish 20
    name = Field(String(30))
21
    isRoot = Field(Integer)
22
    addedOn = Field(DateTime)
103 ashish 23
    status = Field(Integer)
24
    items = ManyToMany('Item')
94 ashish 25
    children = OneToMany('Category', inverse='parent')
26
    parent = ManyToOne('Category', inverse='children')
27
    def __repr__(self):
28
        return "<id>%id </id>"%(self.id)
29
 
30
 
31
class Vendor(Entity):
115 ashish 32
    #id = Field(Integer, primary_key=True)
94 ashish 33
    name = Field(String(30))
103 ashish 34
    status = Field(Integer)
35
    addedOn = Field(datetime)
94 ashish 36
    warehouses = ManyToMany('Warehouse')
37
    def __repr__(self):
38
        return "<Vendor>%s</Vendor>" %(self.name)
39
 
40
 
41
class Warehouse(Entity):
115 ashish 42
    #id = Field(Integer, primary_key=True)
94 ashish 43
    location = Field(String(20))
103 ashish 44
    status = Field(Integer)
45
    addedOn = Field(datetime)
94 ashish 46
    vendors = ManyToMany('Vendor')
47
    inventory = OneToMany('CurrentInventorySnapshot')
48
    inventoryHistory = OneToMany('ItemInventoryHistory')
49
 
50
    def __repr__(self):
51
        return "<warehouse>%s</warehouse>" %(self.location)
52
 
53
 
54
class Item(Entity):
115 ashish 55
    #id = Field(Integer, primary_key=True)
94 ashish 56
    name = Field(String(20))
103 ashish 57
    price = Field(Float)
58
    addedOn = Field(datetime)
59
    status = Field(Integer)
60
    startDate = Field(datetime)
61
    retireDate = Field(datetime)
94 ashish 62
    currentInventory = OneToMany('CurrentInventorySnapshot')
63
    inventoryHistory = OneToMany('ItemInventoryHistory')
103 ashish 64
    category = ManyToMany("Category")
65
    iteminfo = OneToMany("ItemInfo")
66
    statusChangeLog = OneToMany("ItemChangeLog")
67
 
94 ashish 68
    def __repr__(self):
69
        return "<Item>%s</item>" % (self.name)
70
 
115 ashish 71
    #@property
94 ashish 72
    def get_total_inventory(self):
73
        if self.currentInventory:
74
            i = 0
75
            for entry in self.currentInventory:
76
                i += entry.availability
77
            return i
78
 
79
        else:
80
            return 0;
103 ashish 81
 
115 ashish 82
    #@property
94 ashish 83
    def get_all_warehouses(self):
84
        warehouses = []
85
        for warehouse in self.currentInventory.warehouse:
86
            warehouses.append(warehouse)
87
        return warehouses
88
 
115 ashish 89
 
103 ashish 90
class ItemInfo(Entity):
115 ashish 91
    #id = Field(Integer, primary_key=True)
103 ashish 92
    key = Field(String(30))
93
    value = Field(String(100))
94
    item = ManyToOne("Item")
95
 
96
    def __repr__(self):
97
        return "<ItemInfo><key>%s</key><value>%s</value></ItemInfo>" % (self.key, self.value)
115 ashish 98
 
94 ashish 99
 
100
class CurrentInventorySnapshot(Entity):
115 ashish 101
    #id = Field(Integer, primary_key=True)
94 ashish 102
    checkedOn = Field(DateTime, default=datetime.datetime)
103
    availibility = Field(Integer)
104
    item = ManyToOne("Item")
105
    warehouse = ManyToOne("Warehouse")
106
 
107
    def __repr__(self):
108
        return "<current inventory><availability> %s </availability><time>%s</time><item>%s</item><warehouse>%s</warehouse></current inventory>" %(self.availibility, self.checkedOn, self.item, self.warehouse)
109
 
110
#===============================================================================
111
# This is just a placeholder, will be using naveen's code for this.
112
#===============================================================================
113
 
114
class ItemInventoryHistory(Entity):
115 ashish 115
    #id = Field(Integer, primary_key=True)    
94 ashish 116
    timestamp = Field(DateTime, default=datetime.datetime.now())
117
    availibility = Field(Integer)
118
    item = ManyToOne("Item")
119
    warehouse = ManyToOne("Warehouse")
120
 
121
    def __repr__(self):
122
        pass
103 ashish 123
 
124
class ItemChangeLog(Entity):
115 ashish 125
    #id = Field(Integer, primary_key=True)
103 ashish 126
    old_status = Field(Integer)
127
    new_status = Field(Integer)
128
    timestamp = Field(datetime)
129
    item = ManyToOne("Item")
94 ashish 130
 
103 ashish 131
    def __repr__(self):
132
        return "<Log><id>%d</id><item>%d</item><old_status>%d</old_status><new_status>%d</new_status></Log>" %(self.id,self.item.id, self.old_status, self.new_status)    
94 ashish 133
 
103 ashish 134
 
94 ashish 135
def initialize():
115 ashish 136
    metadata.bind = "sqlite:///catalog1.sqlite"
94 ashish 137
    metadata.bind.echo = True
138
    setup_all(True)
115 ashish 139
 
140
if __name__=="__main__":
141
    initialize()