-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
623 lines (501 loc) · 19.7 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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
"""Sheets app util functions"""
import datetime
import email.utils
from collections import namedtuple
from urllib.parse import urljoin, quote_plus
from enum import Enum
import pytz
from django.conf import settings
from django.urls import reverse
from sheets.constants import (
GOOGLE_AUTH_URI,
GOOGLE_TOKEN_URI,
GOOGLE_AUTH_PROVIDER_X509_CERT_URL,
ASSIGNMENT_SHEET_PREFIX,
GOOGLE_SHEET_FIRST_ROW,
GOOGLE_SERVICE_ACCOUNT_EMAIL_DOMAIN,
SHEETS_VALUE_REQUEST_PAGE_SIZE,
SHEET_TYPE_COUPON_REQUEST,
WORKSHEET_TYPE_REFUND,
SHEET_TYPE_COUPON_ASSIGN,
WORKSHEET_TYPE_DEFERRAL,
SHEET_TYPE_ENROLL_CHANGE,
)
def generate_google_client_config():
"""Helper method to generate Google client config based on app settings"""
return {
"web": {
"client_id": settings.DRIVE_CLIENT_ID,
"client_secret": settings.DRIVE_CLIENT_SECRET,
"project_id": settings.DRIVE_API_PROJECT_ID,
"redirect_uris": [
urljoin(settings.SITE_BASE_URL, reverse("complete-google-auth"))
],
"auth_uri": GOOGLE_AUTH_URI,
"token_uri": GOOGLE_TOKEN_URI,
"auth_provider_x509_cert_url": GOOGLE_AUTH_PROVIDER_X509_CERT_URL,
}
}
def get_column_letter(column_index):
"""
Returns the spreadsheet column letter that corresponds to a given index (e.g.: 0 -> 'A', 3 -> 'D')
Args:
column_index (int):
Returns:
str: The column index expressed as a letter
"""
if column_index > 25:
raise ValueError("Cannot generate a column letter past 'Z'")
uppercase_a_ord = ord("A")
return chr(column_index + uppercase_a_ord)
class SheetMetadata:
"""Metadata for a type of Google Sheet that this app interacts with"""
sheet_type = None
sheet_name = None
worksheet_type = None
worksheet_name = None
first_data_row = None
non_input_column_indices = set()
num_columns = 0
@property
def form_input_column_indices(self):
"""
Returns indices of columns that contain data entered by a user in a form
Returns:
set: Indices of columns that contain data entered by a user in a form
"""
return set(range(self.num_columns)).difference(self.non_input_column_indices)
def handler_url_stub(self, file_id=None):
"""
Returns the URL that Google should send requests to when a change is made to a watched
spreadsheet.
Args:
file_id (str): (Optional) The id of the spreadsheet as it appears in the spreadsheet's URL.
If the spreadsheet being watched is a singleton, this isn't necessary.
Returns:
str: The URL that Google will send file watch requests to
"""
params = dict(sheet=quote_plus(self.sheet_type))
if file_id:
params["fileId"] = file_id
param_str = "&".join([f"{k}={v}" for k, v in params.items()])
return "{}?{}".format(reverse("handle-watched-sheet-update"), param_str)
def get_form_input_columns(self, row_data):
"""
Returns a list of column values for columns that contain data entered by a user in a form
(i.e.: no auto-generated values, or values entered by this app)
Args:
row_data (iterable): Cell values from a row in a sheet
Returns:
list: Values for columns that contain data entered by a user in a form
"""
return [
col for i, col in enumerate(row_data) if i in self.form_input_column_indices
]
class SingletonSheetMetadata(SheetMetadata):
"""
Metadata for a type of Google Sheet that this app interacts with, and of which only one should exist
"""
sheet_file_id = None
class CouponRequestSheetMetadata(
SingletonSheetMetadata
): # pylint: disable=too-many-instance-attributes
"""Metadata for the coupon request spreadsheet"""
PURCHASE_ORDER_COL_INDEX = 0
COUPON_NAME_COL_INDEX = 1
PROCESSED_COL = settings.SHEETS_REQ_PROCESSED_COL
ERROR_COL = settings.SHEETS_REQ_ERROR_COL
SKIP_ROW_COL = ERROR_COL + 1
def __init__(self): # pylint: disable=too-many-instance-attributes
self.sheet_type = SHEET_TYPE_COUPON_REQUEST
self.sheet_name = "Coupon Request sheet"
self.first_data_row = GOOGLE_SHEET_FIRST_ROW + 1
self.non_input_column_indices = settings.SHEETS_REQ_CALCULATED_COLUMNS
self.num_columns = self.ERROR_COL + 1
self.sheet_file_id = settings.COUPON_REQUEST_SHEET_ID
self.PROCESSED_COL_LETTER = get_column_letter(self.PROCESSED_COL)
self.ERROR_COL_LETTER = get_column_letter(self.ERROR_COL)
class RefundRequestSheetMetadata(
SingletonSheetMetadata
): # pylint: disable=too-many-instance-attributes
"""Metadata for the refund request spreadsheet"""
FORM_RESPONSE_ID_COL = 0
PROCESSOR_COL = 11
COMPLETED_DATE_COL = 12
ERROR_COL = 13
SKIP_ROW_COL = 14
def __init__(self):
self.sheet_type = SHEET_TYPE_ENROLL_CHANGE
self.sheet_name = "Enrollment Change Request sheet"
self.worksheet_type = WORKSHEET_TYPE_REFUND
self.worksheet_name = "Refunds"
self.first_data_row = settings.SHEETS_REFUND_FIRST_ROW
self.num_columns = self.SKIP_ROW_COL + 1
self.non_input_column_indices = set(
# Response ID column
[self.FORM_RESPONSE_ID_COL]
+
# Every column from the finance columns to the end of the row
list(range(8, self.num_columns))
)
self.sheet_file_id = settings.ENROLLMENT_CHANGE_SHEET_ID
self.PROCESSOR_COL_LETTER = get_column_letter(self.PROCESSOR_COL)
self.ERROR_COL_LETTER = get_column_letter(self.ERROR_COL)
class DeferralRequestSheetMetadata(
SingletonSheetMetadata
): # pylint: disable=too-many-instance-attributes
"""Metadata for the deferral request spreadsheet"""
FORM_RESPONSE_ID_COL = 0
PROCESSOR_COL = 7
COMPLETED_DATE_COL = 8
ERROR_COL = 9
SKIP_ROW_COL = 10
def __init__(self):
self.sheet_type = SHEET_TYPE_ENROLL_CHANGE
self.sheet_name = "Enrollment Change Request sheet"
self.worksheet_type = WORKSHEET_TYPE_DEFERRAL
self.worksheet_name = "Deferrals"
self.first_data_row = settings.SHEETS_DEFERRAL_FIRST_ROW
self.num_columns = self.SKIP_ROW_COL + 1
self.non_input_column_indices = set(
# Response ID column
[self.FORM_RESPONSE_ID_COL]
+
# Every column from the finance columns to the end of the row
list(range(self.PROCESSOR_COL, self.num_columns))
)
self.sheet_file_id = settings.ENROLLMENT_CHANGE_SHEET_ID
self.PROCESSOR_COL_LETTER = get_column_letter(self.PROCESSOR_COL)
self.ERROR_COL_LETTER = get_column_letter(self.ERROR_COL)
class CouponAssignSheetMetadata(
SheetMetadata
): # pylint: disable=too-many-instance-attributes
"""Metadata for a coupon assignment spreadsheet"""
def __init__(self):
self.sheet_type = SHEET_TYPE_COUPON_ASSIGN
self.sheet_name = "Coupon Assignment sheet"
self.first_data_row = GOOGLE_SHEET_FIRST_ROW + 1
self.column_headers = [
"Coupon Code",
"Email (Assignee)",
"Status",
"Status Date",
]
self.non_input_column_indices = {0, 2, 3}
self.num_columns = len(self.column_headers)
self.LAST_COL_LETTER = get_column_letter(self.num_columns - 1)
self.STATUS_COL = next(
i for i, header in enumerate(self.column_headers) if header == "Status"
)
request_sheet_metadata = CouponRequestSheetMetadata()
refund_sheet_metadata = RefundRequestSheetMetadata()
deferral_sheet_metadata = DeferralRequestSheetMetadata()
assign_sheet_metadata = CouponAssignSheetMetadata()
class ResultType(Enum):
"""Enum of possible row results"""
IGNORED = "ignored"
FAILED = "failed"
OUT_OF_SYNC = "out_of_sync"
PROCESSED = "processed"
def __lt__(self, other):
return self.value < other.value # pylint: disable=comparison-with-callable
RowResult = namedtuple(
"RowResult", ["row_index", "row_db_record", "row_object", "message", "result_type"]
)
ProcessedRequest = namedtuple(
"ProcessedRequest", ["row_index", "coupon_req_row", "request_id", "date_processed"]
)
FailedRequest = namedtuple(
"FailedRequest", ["row_index", "exception", "sheet_error_text"]
)
IgnoredRequest = namedtuple("IgnoredRequest", ["row_index", "coupon_req_row", "reason"])
def assignment_sheet_file_name(coupon_req_row):
"""
Generates the filename for a coupon assignment Sheet
Args:
coupon_req_row (sheets.coupon_request_api.CouponRequestRow):
Returns:
str: File name for a coupon assignment Sheet
"""
return " - ".join(
[
ASSIGNMENT_SHEET_PREFIX,
coupon_req_row.company_name,
coupon_req_row.purchase_order_id,
coupon_req_row.product_text_id,
]
)
def get_data_rows(worksheet, include_trailing_empty=False):
"""
Yields the data rows of a spreadsheet that has a header row
Args:
worksheet (pygsheets.worksheet.Worksheet): Worksheet object
include_trailing_empty (bool): Whether to include empty trailing cells/values after last non-zero value
Yields:
list of str: List of cell values in a given row
"""
row_iter = iter(
worksheet.get_all_values(
# These param names are a typo in the pygsheets library
include_tailing_empty=include_trailing_empty,
include_tailing_empty_rows=False,
)
)
try:
# Skip header row
next(row_iter)
except StopIteration:
return
yield from row_iter
def get_data_rows_after_start(
worksheet,
start_row,
start_col,
end_col,
page_size=SHEETS_VALUE_REQUEST_PAGE_SIZE,
**kwargs,
):
"""
Yields the data rows of a spreadsheet starting with a given row and spanning a given column range
until empty rows are encountered.
Args:
worksheet (pygsheets.worksheet.Worksheet): Worksheet object
start_row (int): Zero-based index of the first row for which you want data returned
start_col (int): Zero-based index of the start of the column range
end_col (int): Zero-based index of the end of the column range
page_size (int): The number of rows to fetch per individual API request
kwargs (dict): Option params to pass along to pygsheets.worksheet.Worksheet.get_values
Yields:
list of str: List of cell values in a given row
"""
request_count = 0
values = []
while request_count == 0 or (values and len(values) == page_size):
end_row = start_row + page_size - 1
values = worksheet.get_values(
start=(start_row, start_col),
end=(end_row, end_col),
include_tailing_empty=True,
include_tailing_empty_rows=False,
returnas="matrix",
**kwargs,
)
request_count += 1
yield from values
start_row = end_row + 1
def spreadsheet_repr(spreadsheet=None, spreadsheet_metadata=None):
"""
Returns a simple string representation of a Spreadsheet object
Args:
spreadsheet (pygsheets.spreadsheet.Spreadsheet or None):
spreadsheet_metadata (dict or None): A dict of spreadsheet metadata
Returns:
str: String representation of the spreadsheet
"""
if spreadsheet:
sheet_id, title = spreadsheet.id, spreadsheet.title
elif spreadsheet_metadata:
sheet_id, title = spreadsheet_metadata["id"], spreadsheet_metadata["name"]
else:
sheet_id, title = None, None
if not sheet_id or not title:
raise ValueError("Invalid spreadsheet/metadata provided")
return "'{}', id: {}".format(title, sheet_id)
def clean_sheet_value(value):
"""
Takes a spreadsheet cell value and returns a cleaned version
Args:
value (str): A raw spreadsheet cell value
Returns:
str or None: A string with whitespace stripped, or None if the resulting value was an empty string
"""
stripped = value.strip()
return None if stripped == "" else stripped
def format_datetime_for_google_api(dt):
"""
String-ifies a datetime value in the format expected by Google APIs
Args:
dt (datetime.datetime):
Returns:
str: The datetime formatted for use in a Google API request
"""
return dt.isoformat()
def format_datetime_for_google_timestamp(dt):
"""
Formats a datetime for use in a Google API request that expects a timestamp
(e.g.: file watch expiration – https://developers.google.com/drive/api/v3/reference/files/watch#request-body)
Args:
dt (datetime.datetime):
Returns:
int: The datetime formatted as a timestamp for use in a Google API request
"""
# Google expects the timestamp to be in milliseconds, not seconds, hence the '* 1000'
return int(dt.timestamp() * 1000)
def format_datetime_for_mailgun(dt):
"""
String-ifies a datetime value in the format expected by the Mailgun API
Args:
dt (datetime.datetime):
Returns:
str: The datetime formatted for use in a Mailgun API request
"""
return email.utils.format_datetime(dt)
def format_datetime_for_sheet_formula(dt):
"""
String-ifies a datetime value in a format that will result in a valid date entry in a Google Sheets cell
Args:
dt (datetime.datetime):
Returns:
str: The datetime formatted for a Google Sheets cell value
"""
return f"=DATE({dt.year},{dt.month},{dt.day}) + TIME({dt.hour},{dt.minute},{dt.second})"
def _parse_sheet_date_str(date_str, date_format):
"""
Parses a string that represents a date/datetime and returns the UTC datetime (or None)
Args:
date_str (str): The date/datetime string
date_format (str): The strptime-compatible format string that the string is expected to match
Returns:
datetime.datetime or None: The parsed datetime (in UTC) or None
"""
if not date_str:
return None
dt = datetime.datetime.strptime(date_str, date_format).astimezone(
settings.SHEETS_DATE_TIMEZONE
)
return dt if settings.SHEETS_DATE_TIMEZONE == pytz.UTC else dt.astimezone(pytz.UTC)
def parse_sheet_datetime_str(datetime_str):
"""
Parses a string that represents a datetime and returns the UTC datetime (or None)
Args:
datetime_str (str): The datetime string
Returns:
datetime.datetime or None: The parsed datetime (in UTC) or None
"""
return _parse_sheet_date_str(datetime_str, settings.SHEETS_DATE_FORMAT)
def parse_sheet_date_only_str(date_str):
"""
Parses a string that represents a date and returns the UTC datetime (or None)
Args:
date_str (str): The datetime string
Returns:
datetime.datetime or None: The parsed datetime (in UTC) or None
"""
return _parse_sheet_date_str(date_str, settings.SHEETS_DATE_ONLY_FORMAT)
def google_timestamp_to_datetime(google_timestamp):
"""
Parses a timestamp value from a Google API response as a normal datetime (UTC)
Args:
google_timestamp (str or int): A timestamp value from a Google API response
Returns:
datetime.datetime: The parsed timestamp with UTC timezone
"""
# Google timestamps are expressed in milliseconds, hence the '/ 1000'
timestamp_in_seconds = int(google_timestamp) / 1000
return datetime.datetime.fromtimestamp(timestamp_in_seconds, pytz.UTC)
def google_date_string_to_datetime(google_date_str):
"""
Parses a datetime string value from a Google API response as a normal datetime (UTC)
Args:
google_date_str (str): A datetime string value from a Google API response
Returns:
datetime.datetime: The parsed timestamp with UTC timezone
"""
return datetime.datetime.strptime(
google_date_str, "%Y-%m-%dT%H:%M:%S.%fZ"
).astimezone(pytz.UTC)
def mailgun_timestamp_to_datetime(timestamp):
"""
Parses a timestamp value from a Mailgun API response as a datetime
Args:
timestamp (float): A timestamp value from a Mailgun API response
Returns:
datetime.datetime: The parsed timestamp
"""
return datetime.datetime.fromtimestamp(timestamp, pytz.UTC)
def build_multi_cell_update_request_body(
row_index, column_index, values, worksheet_id=0
):
"""
Builds a dict for use in the body of a Google Sheets API batch update request
Args:
row_index (int): The index of the cell row that should be updated (starting with 0)
column_index (int): The index of the first cell column that should be updated (starting with 0)
values (list of dict): The updates to be performed
worksheet_id (int):
Returns:
dict: A single update request object for use in a Google Sheets API batch update request
"""
return {
"updateCells": {
"range": {
"sheetId": worksheet_id,
"startRowIndex": row_index,
"endRowIndex": row_index + 1,
"startColumnIndex": column_index,
"endColumnIndex": column_index + len(values),
},
"rows": [{"values": values}],
"fields": "*",
}
}
def build_protected_range_request_body(
start_row_index,
num_rows,
start_col_index,
num_cols,
worksheet_id=0,
warning_only=False,
description=None,
): # pylint: disable=too-many-arguments
"""
Builds a request body that will be sent to the Google Sheets API to create a protected range on a spreadsheet.
Args:
start_row_index (int): The zero-based index of the row of the range that will be protected
num_rows (int): The number of rows that this range will span
start_col_index (int): The zero-based index of the column of the range that will be protected
num_cols (int): The number of columns that this range will span
worksheet_id (int): The worksheet id in the given spreadsheet (the first worksheet id is always 0)
warning_only (bool): If True, the range will be editable, but will display a warning/confirmation dialog
before edits are accepted
description (str or None): An optional description for the protected range
Returns:
dict: A request body that will be sent to the Google Sheets API to create a protected range
"""
extra_params = {} if description is None else {"description": description}
return {
"addProtectedRange": {
"protectedRange": {
"range": {
"sheetId": worksheet_id,
"startRowIndex": start_row_index,
"endRowIndex": start_row_index + num_rows,
"startColumnIndex": start_col_index,
"endColumnIndex": start_col_index + num_cols,
},
"warningOnly": warning_only,
**extra_params,
}
}
}
def build_drive_file_email_share_request(file_id, email_to_share):
"""
Builds the body of a Drive file share request
Args:
file_id (str): The file id of the Drive file being shared
email_to_share (str): The email of the user to whom the file will be shared
Returns:
dict: A dictionary of parameters for the body of a share request
"""
added_kwargs = (
{"sendNotificationEmail": False}
if email_to_share.endswith(GOOGLE_SERVICE_ACCOUNT_EMAIL_DOMAIN)
else {}
)
return dict(
fileId=file_id,
body={"type": "user", "role": "writer", "emailAddress": email_to_share},
fields="id",
supportsTeamDrives=True,
**added_kwargs,
)