-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.py
74 lines (54 loc) · 2.22 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
'''
This file contains definitions for the models. These are by peewee to create the
necessaary database tables into which our products will be stored. To create the
database and associated table please run this file directly with python
'''
import datetime
from peewee import *
# Name of Database file. Please change it to suitable representation
DATABASE = 'db.sqlite3'
# create a peewee database instance -- our models will use this database to
# persist information
database = SqliteDatabase(DATABASE)
# model definitions -- the standard "pattern" is to define a base model class
# that specifies which database to use. then, any subclasses will automatically
# use the correct storage. for more information, see:
# http://charlesleifer.com/docs/peewee/peewee/models.html
class BaseModel(Model):
class Meta:
database = database
# This is where we store user credentials
class User(BaseModel):
username = CharField(unique=True)
password = CharField()
created = DateTimeField(default=datetime.datetime.now)
is_admin = BooleanField(default=False)
class Meta:
order_by = ('username',)
# This model holds the definitions of the product in the store
class Product(BaseModel):
code = CharField(unique=True)
name = CharField()
price = DecimalField()
created = DateTimeField(default=datetime.datetime.now)
class Meta:
order_by = ('name',)
def details(self):
# return a description of product
return self.code + ' : ' + self.name + ' - $' + str(self.price)
# This model is used to hold all product counts. One product may have many count
# occurances. They are added up to get a total for the report.
class ProductCount(BaseModel):
count = IntegerField()
product = ForeignKeyField(Product, related_name='product')
counted_by = ForeignKeyField(User, related_name='user')
created = DateTimeField(default=datetime.datetime.now)
class Meta:
order_by = ('created',)
# simple utility function to create database and fill it with blanktables
def create_tables():
database.connect()
database.create_tables([User, Product, ProductCount])
if __name__ == '__main__':
create_tables()
print("Database has been created, ready for filling up")