-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
227 lines (221 loc) · 11.2 KB
/
utils.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
import json
import csv
import xlrd
import xlwt
from django.http import HttpResponse
from django.utils.encoding import smart_unicode
def write_xls(spots):
response = HttpResponse(mimetype='application/vnd.ms-excel')
response['Content-Disposition'] = "attachment; filename=spot_data.xls"
workbook = xlwt.Workbook(encoding='utf-8')
worksheet = workbook.add_sheet('spot_data')
extended = []
for spot in spots:
for info in spot['extended_info']:
if not info in extended:
extended.append(info.encode('utf-8'))
header = ['id', 'name', 'room_number', 'floor', 'building_name', 'latitude', 'longitude', 'organization', 'manager'] + extended + ["height_from_sea_level", "capacity", "display_access_restrictions", "type", 'available_hours']
collumn = 0
for info in header:
worksheet.write(0, collumn, info)
collumn += 1
row = 1
for spot in spots:
days = ["monday", "tuesday", "wednesday", "thursday", "saturday", "sunday"]
available_hours = ''
types = ''
count = 0
extended_info = []
for info in extended:
info = info.decode('utf-8')
try:
extended_info.append(spot['extended_info'][info].encode('utf-8'))
except:
extended_info.append('')
for Type in spot['type']:
if count == 0:
types += Type.encode('utf-8')
else:
types += ', ' + Type.encode('utf-8')
count = 1
count1 = 0
for day in days:
try:
if count1 == 0:
if day == "thursday" or day == "saturday" or day == "sunday":
available_hours += day[0] + day[1] + ': ' + spot['available_hours'][day][0][0] + '-' + spot['available_hours'][day][0][1]
else:
available_hours += day[0] + ': ' + spot['available_hours'][day][0][0] + '-' + spot['available_hours'][day][0][1]
else:
if day == "thursday" or day == "saturday" or day == "sunday":
available_hours += ', ' + day[0] + day[1] + ': ' + spot['available_hours'][day][0][0] + '-' + spot['available_hours'][day][0][1]
else:
available_hours += ', ' + day[0] + ': ' + spot['available_hours'][day][0][0] + '-' + spot['available_hours'][day][0][1]
except:
pass
count1 = 1
available_hours = smart_unicode(available_hours)
types = smart_unicode(types)
data_row = [spot['id'], spot['name'].encode('utf-8'), spot['location']['room_number'], spot['location']['floor'].encode('utf-8'), spot['location']['building_name'].encode('utf-8'), spot['location']['latitude'], spot['location']['longitude'], spot['organization'].encode('utf-8'), spot['manager'].encode('utf-8')] + extended_info + [spot['location']["height_from_sea_level"], spot['capacity'], spot['display_access_restrictions'].encode('utf-8'), types, available_hours]
collumn = 0
for info in data_row:
worksheet.write(row, collumn, info)
collumn += 1
row += 1
workbook.save(response)
return response
def write_csv(spots):
response = HttpResponse(mimetype='text/csv')
response['Content-Disposition'] = "attachment; filename=spot_data.csv"
f = csv.writer(response)
#extended info isn't the same for all. Need to build up a dict of all extended info keys
extended = []
for spot in spots:
for info in spot['extended_info']:
if not info in extended:
extended.append(info.encode('utf-8'))
f.writerow(['id', 'name', 'room_number', 'floor', 'building_name', 'latitude', 'longitude', 'organization', 'manager'] + extended + ["height_from_sea_level", "capacity", "display_access_restrictions", "type", 'available_hours'])
for spot in spots:
days = ["monday", "tuesday", "wednesday", "thursday", "saturday", "sunday"]
available_hours = ''
types = ''
count = 0
extended_info = []
for info in extended:
info = info.decode('utf-8')
try:
extended_info.append(spot['extended_info'][info].encode('utf-8'))
except:
extended_info.append('')
for Type in spot['type']:
if count == 0:
types += Type.encode('utf-8')
else:
types += ', ' + Type.encode('utf-8')
count = 1
count1 = 0
for day in days:
try:
if count1 == 0:
if day == "thursday" or day == "saturday" or day == "sunday":
available_hours += day[0] + day[1] + ': ' + spot['available_hours'][day][0][0] + '-' + spot['available_hours'][day][0][1]
else:
available_hours += day[0] + ': ' + spot['available_hours'][day][0][0] + '-' + spot['available_hours'][day][0][1]
else:
if day == "thursday" or day == "saturday" or day == "sunday":
available_hours += ', ' + day[0] + day[1] + ': ' + spot['available_hours'][day][0][0] + '-' + spot['available_hours'][day][0][1]
else:
available_hours += ', ' + day[0] + ': ' + spot['available_hours'][day][0][0] + '-' + spot['available_hours'][day][0][1]
except:
pass
count1 = 1
available_hours = smart_unicode(available_hours)
types = smart_unicode(types)
f.writerow([spot['id'], spot['name'].encode('utf-8'), spot['location']['room_number'], spot['location']['floor'].encode('utf-8'), spot['location']['building_name'].encode('utf-8'), spot['location']['latitude'], spot['location']['longitude'], spot['organization'].encode('utf-8'), spot['manager'].encode('utf-8')] + extended_info + [spot['location']["height_from_sea_level"], spot['capacity'], spot['display_access_restrictions'].encode('utf-8'), types, available_hours])
return response
def file_to_json(docfile):
if docfile.content_type == 'text/csv':
data = csv.DictReader(docfile.file)
elif docfile.content_type == 'application/vnd.ms-excel':
#convert to dict
workbook = xlrd.open_workbook(file_contents=docfile.read())
sheet = workbook.sheet_by_index(0)
keys = sheet.row_values(0)
data = []
for row in range(1, sheet.nrows):
items = sheet.row_values(row)
data_row = {}
for item in range(len(items)):
try:
data_row[keys[item]] = items[item].encode('utf-8')
except:
if items[item] == int(items[item]):
data_row[keys[item]] = str(int(items[item]))
else:
data_row[keys[item]] = str(items[item])
data.append(data_row)
else:
raise TypeError("Invalid file type %s" % (docfile.content_type()))
requests = []
errors = []
for current in data:
#create a dictionary of the data that later gets json'ed
#the non_extended array is a list of the items the server specifically asks for.
non_extended = ["id", "name", "type", "capacity", "display_access_restrictions", "available_hours", "manager", "organization"]
location = ["longitude", "latitude", "height_from_sea_level", "building_name", "floor", "room_number", "description"]
spot_data = {}
extended = {}
hours = {}
location_data = {}
spot_id = ""
try:
for entry in current:
#Remove extraneous quotes
current[entry] = current[entry].replace("\"", "")
#Don't send empty values
if current[entry]:
current[entry] = current[entry].decode('utf-8')
if entry == 'id':
spot_id = current[entry]
#Handle location dict
elif entry in location:
location_data[entry] = current[entry]
#Handle main dict
elif entry in non_extended:
if entry == 'available_hours':
#assumes "M: 07:30-17:030, T: 07:30-17:30, etc" format
days = current[entry]
days = days.split(",")
weekdays = {"m": "monday", "t": "tuesday", "w": "wednesday", "th": "thursday", "f": "friday", "s": "saturday", "su": "sunday"}
for day in days:
times = day.split(": ")
day = times[0]
day = day.strip()
day = day.lower()
times = times[len(times) - 1].split("-")
if len(times) == 2:
try:
hours[weekdays[day]]
prev = True
except:
prev = False
try:
if not prev:
hours[weekdays[day]] = [times]
else:
hours[weekdays[day]] += [times]
except:
errors.append({"name": current["name"], "location": entry, "error": "unable to parse hours"})
raise Warning
else:
errors.append({"name": current["name"], "location": entry, "error": "unable to parse hours"})
raise Warning
elif entry == "type":
spot_types = current[entry].split(", ")
types = []
for spot_type in spot_types:
types.append(spot_type)
spot_data[entry] = types
else:
spot_data[entry] = current[entry]
#Handle images
elif entry == "images":
spot_images = current[entry].split(",")
images = []
for image in spot_images:
if image != "":
image.strip()
images.append(image)
spot_data[entry] = images
#Handle extended info
else:
extended[entry] = current[entry]
#Combine all the dictionaries and sub-dictionaries into one
spot_data['extended_info'] = extended
spot_data['available_hours'] = hours
spot_data['location'] = location_data
spot_request = {"id": spot_id, "data": json.dumps(spot_data)}
requests.append(spot_request)
except:
pass
return {"data": requests, "errors": errors}