-
Notifications
You must be signed in to change notification settings - Fork 3
/
models.py
305 lines (270 loc) · 10.9 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
"""
Product Model that uses Redis
You must initlaize this class before use by calling inititlize().
This class looks for an environment variable called VCAP_SERVICES
to get it's database credentials from. If it cannot find one, it
tries to connect to Redis on the localhost. If that fails it looks
for a server name 'redis' to connect to.
"""
import os
import json
import logging
import pickle
from redis import Redis
from cerberus import Validator
from redis.exceptions import ConnectionError
######################################################################
# Custom Exceptions
######################################################################
class DataValidationError(Exception):
""" Used for an data validation errors when deserializing """
pass
class DatabaseConnectionError(Exception):
pass
class BadRequestError(Exception):
pass
class NotFoundError(Exception):
pass
######################################################################
# Product Model for database
# This class must be initialized with use_db(redis) before using
# where redis is a value connection to a Redis database
######################################################################
class Product(object):
"""
Class that represents a Product
This version uses an in-memory collection of products for testing
"""
#lock = threading.Lock()
#data = []
#index = 0
logger = logging.getLogger(__name__)
redis = None
schema = {
'id': {'type': 'integer'},
'name': {'type': 'string', 'required': True},
'category': {'type': 'string', 'required': True},
'price': {'type': 'integer', 'required': True},
'description': {'type': 'string', 'required': True},
'color': {'type': 'string', 'required': True},
'count': {'type': 'integer', 'required': True}
}
__validator = Validator(schema)
def __init__(self, id=0, name='', category='',
price='', description='', color='', count=''):
""" Initialize a Product """
self.id = int(id)
self.name = name
self.category = category
self.price = price
self.color = color
self.description = description
self.count = count
def save(self):
"""
Saves a Product to the data store
"""
if self.id == 0:
self.id = Product.__next_index()
# Product.data.append(self)
Product.redis.set(self.id, pickle.dumps(self.serialize()))
# print Product.data[-1].id
# else:
# for i in range(len(Product.data)):
# if Product.data[i].id == self.id:
# Product.data[i] = self
# break
def delete(self):
""" Removes a Product from the data store """
# Product.data.remove(self)
Product.redis.delete(self.id)
def serialize(self):
""" Serializes a Product into a dictionary """
return {"id": self.id, "name": self.name, "category": self.category,
"price": self.price, "description": self.description,
"color": self.color, "count": self.count}
def deserialize(self, data):
"""
Deserializes a Product from a dictionary
Args:
data (dict): A dictionary containing the Product data
"""
# if not isinstance(data, dict):
# raise DataValidationError('Invalid product: body of request contained bad or no data')
# if data.has_key('id'):
# self.id = data['id']
try:
self.name = data['name']
self.category = data['category']
self.price = data['price']
self.description = data['description']
self.color = data['color']
self.count = data['count']
except KeyError as err:
raise DataValidationError(
'Invalid product: missing ' + err.args[0])
except TypeError as error:
raise DataValidationError(
'Invalid product: body of request contained bad or no data')
return self
######################################################################
# S T A T I C D A T A B S E M E T H O D S
######################################################################
@staticmethod
def __next_index():
""" Generates the next index in a continual sequence """
# with Product.lock:
# Product.index += 1
# return Product.index
return Product.redis.incr('index')
@staticmethod
def all():
""" Returns all of the Products in the database """
# return [p for p in Product.data]
results = []
for key in Product.redis.keys():
if key != 'index': # filer out our id index
data = pickle.loads(Product.redis.get(key))
product = Product(data['id']).deserialize(data)
results.append(product)
return results
@staticmethod
def available():
""" Returns all of the Products in the database
with count greater than 0"""
results = []
for key in Product.redis.keys():
if key != 'index': # filer out our id index
data = pickle.loads(Product.redis.get(key))
product = Product(data['id']).deserialize(data)
if product.count > 0:
results.append(product)
return results
@staticmethod
def remove_all():
""" Removes all of the Products from the database """
#del Product.data[:]
#Product.index = 0
# return Product.data
Product.redis.flushall()
######################################################################
# F I N D E R M E T H O D S
######################################################################
@staticmethod
def find(product_id):
""" Finds a Product by it's ID """
# if not Product.data:
# return None
#product = [p for p in Product.data if p.id == product_id]
# if product:
# return product[0]
# return None
if Product.redis.exists(product_id):
data = pickle.loads(Product.redis.get(product_id))
product = Product(data['id']).deserialize(data)
return product
return None
@staticmethod
def __find_by(attribute, value):
""" Generic Query that finds a key with a specific value """
Product.logger.info('Processing %s query for %s', attribute, value)
# if isinstance(value, str):
search_criteria = value.lower() # make case insensitive
# else:
#print ("INFB")
#print (value)
#search_criteria = value
results = []
for key in Product.redis.keys():
if key != 'index': # filer out our id index
# print("Key:" + key)
data = pickle.loads(Product.redis.get(key))
# print(data[attribute])
# perform case insensitive search on strings
# if isinstance(data[attribute], str):
test_value = data[attribute].lower()
# else:
#test_value = data[attribute]
# print(search_criteria, test_value)
if test_value == search_criteria:
results.append(Product(data['id']).deserialize(data))
return results
@staticmethod
def find_by_category(category):
""" Returns all of the Products in a category
Args:
category (string): the category of the Products you want to match
"""
#print ("IN FBC")
return Product.__find_by('category', category)
# return [p for p in Product.data if p.category == category]
@staticmethod
def find_by_name(name):
""" Returns all Products with the given name
Args:
name (string): the name of the Products you want to match
"""
# return [p for p in Product.data if p.name == name]
return Product.__find_by('name', name)
######################################################################
# R E D I S D A T A B A S E C O N N E C T I O N M E T H O D S
######################################################################
@staticmethod
def connect_to_redis(hostname, port, password):
""" Connects to Redis and tests the connection """
Product.logger.info("Testing Connection to: %s:%s", hostname, port)
Product.redis = Redis(host=hostname, port=port, password=password)
try:
Product.redis.ping()
Product.logger.info("Connection established")
except ConnectionError:
Product.logger.info("Connection Error from: %s:%s", hostname, port)
Product.redis = None
return Product.redis
@staticmethod
def init_db(redis=None):
"""
Initialized Redis database connection
This method will work in the following conditions:
1) In Bluemix with Redis bound through VCAP_SERVICES
2) With Redis running on the local server as with Travis CI
3) With Redis --link in a Docker container called 'redis'
4) Passing in your own Redis connection object
Exception:
----------
redis.ConnectionError - if ping() test fails
"""
if redis:
Product.logger.info("Using client connection...")
Product.redis = redis
try:
Product.redis.ping()
Product.logger.info("Connection established")
except ConnectionError:
Product.logger.error("Client Connection Error!")
Product.redis = None
raise ConnectionError('Could not connect to the Redis Service')
return
# Get the credentials from the Bluemix environment
if 'VCAP_SERVICES' in os.environ:
Product.logger.info("Using VCAP_SERVICES...")
vcap_services = os.environ['VCAP_SERVICES']
services = json.loads(vcap_services)
creds = services['rediscloud'][0]['credentials']
Product.logger.info("Conecting to Redis on host %s port %s",
creds['hostname'], creds['port'])
Product.connect_to_redis(creds['hostname'], creds[
'port'], creds['password'])
else:
Product.logger.info(
"VCAP_SERVICES not found, checking localhost for Redis")
Product.connect_to_redis('127.0.0.1', 6379, None)
if not Product.redis:
Product.logger.info(
"No Redis on localhost, looking for redis host")
Product.connect_to_redis('redis', 6379, None)
if not Product.redis:
# if you end up here, redis instance is down.
Product.logger.fatal(
'*** FATAL ERROR: Could not connect to the Redis Service')
raise ConnectionError('Could not connect to the Redis Service')