-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwine_bottle.py
320 lines (284 loc) · 12.8 KB
/
wine_bottle.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import os
import shutil
import datetime
import tempfile
import brother_ql
import brother_ql.backends
import brother_ql.backends.helpers
import brother_ql.backends.pyusb
from db_man import *
from data_tools import search_db, fetch_db, lookup_db, enter_db, drop_row, update_winedata_row, update_userinv_row, get_rowid
from barcode import *
from barcode.writer import ImageWriter
class Wine:
'''
A generic object to handle different wines and
the associated methods that may be useful to them
'''
def __init__(self, wine_info):
self.wine_info = wine_info
self.wine_search_flag = False
# Create a temporary directory to hold generated barcodes. This will be removed
self.temp_dir = tempfile.mkdtemp()
# self.wine_id = self.get_wine_id()
# wine_info = {"wine_id":None,
# "upc":'791863140506',
# "winery":'Burnt Bridge Cellars',
# "region":'Walla Walla',
# "name":None,
# "varietal":'Merlot',
# "wtype":'Table',
# "vintage":2013,
# "msrp":'$30',
# "value":'$30'}
def search_wine(self):
# This function is meant to take a partial wine dataset
# and turn it into a full dataset (or as full as it's
# going to get). It simply updates the bottle's attributes.
# Ideally, this will be done with a wine_id lookup:
if 'wine_id' in self.wine_info and self.wine_info['wine_id'] != None:
result = lookup_db(self.wine_info['wine_id'], 'winedata')
# If not, we'll try to find it with the search function. This may
# return multiple entries, so we have to return them all (long-ass list)
else:
result = search_db(self.wine_info, 'winedata')
# Now we need to format it. Due to the way the output is formatted, we need
# to ensure that it becomes a list of tuples, regardless of how many entries
# there actually are. We also need to handle the case that there may not be
# any matches at all. We also need to dynamically assign names because we can't
# guarantee the structure of the db
if result is not None:
self.wine_search_flag = True
db_names = DatabaseManager()
keys = db_names.db_getcolnames(table='winedata')
if isinstance(result, tuple):
result = [result]
res_list = []
for res in result:
res_list.append(res)
if len(res_list) is 1:
self.wine_info = {}
for i, key in enumerate(keys):
self.wine_info[key] = res_list[0][i]
return res_list
else:
return None
def get_wine_id(self):
# Returns just the wine_id, or a list of wine_ids if there
# are multiple. Useful for stitching together the inv and data
# tables. If the bottle doesn't have a wine_id attached, it'll
# try to find it
if 'wine_id' in self.wine_info and self.wine_info['wine_id'] is not None:
return self.wine_info['wine_id']
else:
result = self.search_wine()
if isinstance(result, list) and result != []:
self.wine_info['wine_id'] = result[0][int(0)]
return self.wine_info['wine_id']
elif result == []:
return None
elif result is not None:
id_list = []
for i in range(len(result)):
id_list.append(result[i][int('wine_id')])
return id_list
else:
return None
def add_wine_to_db(self):
# Adds a wine to the database after checking if
# it's already there. If the search flag is true,
# the database has already been searched and there's
# no need to do it again
result = None
if self.wine_search_flag is False:
result = self.search_wine()
if result is None:
new_id = enter_db(self.wine_info, 'winedata', ret_id=True)
return new_id
def update_wine(self):
update_winedata_row(self.wine_info)
def generate_label(self):
# Function to generate a barcode for the wine based off the unique wine
# id. First, it grabs the wine_id however it needs to. Then, it generates
# a unique ITF barcode and stores it as an svg in the temp file and returns
# the file path
self.wine_id = self.get_wine_id()
if self.wine_id != None:
tag_num = (12 - len(str(self.wine_id))) * '0' + str(self.wine_id)
options = {'dpi': 162,
'module_width': 0.25,
'module_height': 12,
'quiet_zone': 0,
'font_size': 14,
'text_distance': 0.25}
output = self.temp_dir + '/' + str(self.wine_id)
writer = ImageWriter()
writer.dpi = 600
generate('ITF', tag_num, writer=writer , output=output, writer_options = options)
return output
else:
raise Exception('Cannot generate barcode because wine has no id')
def print_label(self):
# Prints the generated label for the wine as long as it has been generated
image = self.temp_dir + '/' + str(self.wine_id) + '.png'
# If the image file doesn't exist, generate it first
if not os.path.isfile(image):
self.generate_label()
ql_printer = brother_ql.BrotherQLRaster(model='QL-500')
ql_printer.cut_at_end = False
brother_ql.create_label(ql_printer, image, label_size='12', cut=False, dither=True, compress=False, red=False, rotate=90)
printer = brother_ql.backends.pyusb.list_available_devices()[0]['identifier']
brother_ql.backends.helpers.send(ql_printer.data, printer_identifier=printer, backend_identifier='pyusb')
def __del__(self):
# Clean up after ourselves by deleting the temporary directory for barcodes
shutil.rmtree(self.temp_dir)
class Bottle(Wine):
def __init__(self, wine_info, bottle_info=None):
super().__init__(wine_info)
self.bottle_info = bottle_info
if self.bottle_info == None:
self.bottle_info = {}
self.bottle_search_flag = False
def search_bottle(self, in_cellar=True, sort_by=None):
# Starts by searching for a matching wine. This will add the wine_id to the
# bottle info as well.
wine_res_list = None
if self.wine_search_flag is False and self.wine_info != None:
wine_res_list = self.search_wine()
# assigns wine_id only if the exact wine is positively identified
if wine_res_list != None and len(wine_res_list) == 1:
self.bottle_info['wine_id'] = self.wine_info['wine_id']
# takes the path of least resistance to find the bottle or bottles we're looking for
if 'wine_id' in self.bottle_info and self.bottle_info['wine_id'] is not None:
result = fetch_db({'wine_id':self.bottle_info['wine_id']}, in_cellar=in_cellar, sort_by=sort_by)
else:
result = search_db(self.bottle_info, in_cellar=in_cellar, sort_by=sort_by)
if result is not None:
self.bottle_search_flag = True
if self.wine_search_flag == False:
if self.wine_info == None:
self.wine_info = {}
self.wine_info['wine_id'] = result[0][0]
self.search_wine()
db_names = DatabaseManager()
keys = db_names.db_getcolnames(table='userinventory')
if isinstance(result, tuple):
result = [result]
res_list = []
for res in result:
res_list.append(res)
if len(res_list) is 1:
self.bottle_info = {}
for i, key in enumerate(keys):
self.bottle_info[key] = res_list[0][i]
return res_list
else:
return None
def add_new(self):
# This method adds a wine to the inventory by first checking if the wine exists,
# adding it if it does not, acquiring the wine_id, then checking in a bottle of
# the new wine. Meant to be called from the advanced search screen.
bottle_id = self.get_wine_id()
if 'wine_id' not in self.bottle_info or self.bottle_info['wine_id'] == None:
if isinstance(bottle_id, list):
raise Exception('Multiple wine entries found. Please be more specific.')
elif bottle_id == None:
bottle_id = self.add_wine_to_db()
self.bottle_info['wine_id'] = bottle_id
self.check_in()
return bottle_id
def check_in(self, new_location=None, new_bottle_size=None):
# This method adds a wine to the inventory based on a selected wine_id. Since it will
# only be called if there's a wine_id, it can be assumed that it exists. Meant to be
# called from the main inventory screen.
if new_location != None:
self.bottle_info['location'] = new_location
if new_bottle_size != None:
self.bottle_info['bottle_size'] = new_bottle_size
self.bottle_info['date_in'] = '{date:%Y-%m-%d %H:%M:%S}'.format(date=datetime.datetime.now())
self.bottle_info['date_out'] = None
enter_db(self.bottle_info)
def check_out(self):
# Checks out a bottle from the inventory (really just adds a date out
# entry) based on the row id of the selected wine_id and location
row_id = get_rowid(self.bottle_info)
# row_id = get_rowid({'wine_id':self.bottle_info['wine_id'], 'location':self.bottle_info['location']})
date_out = '{date:%Y-%m-%d %H:%M:%S}'.format(date=datetime.datetime.now())
update_dict = {'date_out':date_out}
update_userinv_row(update_dict, row_id)
def update_bottle(self, new_info):
# Updates the selected bottle with new information, such
# as location and bottle size
row_id = get_rowid(self.bottle_info)
if 'wine_id' in new_info:
del new_info['wine_id']
if 'date_in' in new_info:
del new_info['date_in']
self.bottle_info = new_info
update_userinv_row(self.bottle_info, row_id)
def delete_wine(self):
# Deletes a wine from the database. Dangerous command. Use with care.
# Requires an explicit wine_id input to function.
drop_row(self.wine_info['wine_id'], table='winedata')
drop_row(self.wine_info['wine_id'])
def delete_bottle(self):
# Deletes a bottle from the db. Less dangerous than deleting wines,
# but still meant to be handled with care. Uses wine_id and other data
# to get row id, then uses that to delete the entry.
row_id = get_rowid(self.bottle_info)
drop_row(self.wine_info['wine_id'], rowid=row_id)
def clear_bottle(self):
self.bottle_info = {}
self.wine_info = {}
self.wine_search_flag = False
####################################################################
############################ Test Code #############################
####################################################################
# wine_id = '000000000006'
# wine_id = None
# wine_dict = {"wine_id":wine_id,
# "upc":None,
# "winery":'Turly',
# "region":'Walla Walla',
# "name":None,
# "varietal":'Zinfandel',
# "wtype":'Table',
# "vintage":2011,
# "msrp":'$40',
# "value":'$25',
# "comments":'Young vines'}
# wine_dict = {"wine_id":wine_id}
# bottle_dict = {}
# wine_dict = {"wine_id":'000000000003',
# "upc":None,
# "winery":None,
# "region":None,
# "name":None,
# "varietal":None,
# "wtype":None,
# "vintage":None,
# "msrp":None,
# "value":None}
# bottle_dict = {"wine_id":wine_id,
# "bottle_size":'Standard (750 mL)',
# "location":'C9',
# "date_in":None,
# "date_out":None}
# new_bottle_dict = {"wine_id":None,
# "bottle_size":'Standard (750 mL)',
# "location":'B12',
# "date_in":None,
# "date_out":None}
# new_bottle = Bottle(wine_dict, bottle_dict)
# new_bottle.generate_label()
# new_bottle.check_in()
# new_bottle.add_wine_to_db()
# new_bottle.add_new()
# new_bottle.check_out()
# new_bottle.update_bottle(new_bottle_dict)
# new_bottle.delete_wine()
# new_bottle.delete_bottle()
# new_bottle = Bottle(wine_dict, None)
# print(new_bottle.search_bottle())
# print(new_bottle.wine_info)
# print(new_bottle.bottle_info)