forked from OpenLightingProject/rdm-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
product_loader.py
208 lines (175 loc) · 6.17 KB
/
product_loader.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
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# product_loader.py
# Copyright (C) 2012 Simon Newton
# Loads product data
import common
import logging
from model import *
class ProductLoader(object):
"""Load Product definition into the datastore."""
def __init__(self, data, product_type):
self._product_data = data
self._product_type = product_type
# manufacturer_id to Manufacturer object
self._manufacturers = {}
# string to ProductTag objects
self._tags = {}
def _LookupManufacturer(self, manufacturer_id):
"""Lookup a Manufacturer entity by id and cache the result.
Returns:
The entity object, or None if not found.
"""
if manufacturer_id not in self._manufacturers:
self._manufacturers[manufacturer_id] = common.GetManufacturer(
manufacturer_id)
return self._manufacturers[manufacturer_id]
def _LookupOrAddTag(self, tag_label):
"""Lookup a ProductTag or add it if it doesn't exist.
Returns:
The entity object, or None if not found.
"""
if tag_label not in self._tags:
query = ProductTag.all()
query.filter('label = ', tag_label)
query.filter('product_type = ', self._product_type.class_name())
tags = query.fetch(1)
if tags:
self._tags[tag_label] = tags[0]
else:
tag_entity = ProductTag(label=tag_label,
product_type=self._product_type.class_name())
tag_entity.put()
self._tags[tag_label] = tag_entity
logging.info('added %s -> %s' % (tag_label, tag_entity))
return self._tags[tag_label]
def _LookupProduct(self, manufacturer_key, product_name):
"""Given a manufacturer key and product name, lookup the Product.
"""
products = self._product_type.all()
products.filter('name = ', product_name)
products.filter('manufacturer = ', manufacturer_key)
if products.count() > 1:
logging.error('More than one product exists for %s and 0x%hx' %
(product_name, manufacturer_key.esta_id))
product_data = products.fetch(1)
if not product_data:
return None
return product_data[0]
def _UpdateProduct(self, product, product_info):
"""Update this product entity if there is new data.
Returns:
True if this entity was updated, false otherwise.
"""
modified = False
# update link url
link_url = product_info.get('link')
if link_url is not None and link_url != product.link:
product.link = link_url
modified = True
# update image url
image_url = product_info.get('image_url')
if image_url is not None and image_url != product.image_url:
product.image_url = image_url
product.image_data = None
modified = True
if modified:
product.put()
return modified
def _AddProduct(self, manufacturer, product_info):
"""Add a product to the data store.
Args:
manufacturer:
product_info: The dict with the product information
Returns:
The new Product entity.
"""
product = self._product_type(
manufacturer = manufacturer,
name = product_info['name'])
# add link and image_url if they exist
link_url = product_info.get('link')
if link_url:
product.link = link_url
image_url = product_info.get('image_url')
if image_url:
product.image_url = image_url
product.put()
return product
def _UpdateTags(self, product, new_tags):
"""Update the tags for a product
Args:
product: The Product entity to update
new_tags: a list of new tags
Returns:
True if the product was modified, false otherwise.
"""
new_tags = set(new_tags)
modified = False
for relationship in product.tag_set:
label = relationship.tag.label
if label in new_tags:
new_tags.remove(label)
else:
logging.info('Deleting %s from %s' % (label, product.name))
relationship.delete()
modified = True
for tag_label in new_tags:
tag_entity = self._LookupOrAddTag(tag_label)
relationship = ProductTagRelationship(
tag = tag_entity,
product = product)
relationship.put()
modified = True
return modified
def Update(self):
"""Update the datastore with the new data.
Returns:
A tuple in the form (added, updated), with the number of entities added
or updated.
"""
added = []
updated = []
logging.info(self._product_data)
for manufacturer_id, products in self._product_data.iteritems():
manufacturer = self._LookupManufacturer(manufacturer_id)
if not manufacturer:
logging.error('No manufacturer found for %hx' % manufacturer_id)
continue
for product_info in products:
was_added = False
was_modified = False
name = product_info['name']
product = self._LookupProduct(manufacturer.key(), name)
if product:
# update
if self._UpdateProduct(product, product_info):
logging.info(' product changed')
was_modified = True
else:
# add a new one
product = self._AddProduct(manufacturer, product_info)
was_added = True
# add / update any tags
# we act conservatively here and don't delete unless we get an empty
# list
if 'tags' in product_info:
if self._UpdateTags(product, product_info['tags']):
was_modified = True
if was_added:
added.append(name)
elif was_modified:
updated.append(name)
return added, updated