def validate_address(address_line, column): if Columns.make_key(column) in Columns.from_keys(optional_address_columns).keys(): return address_line if Columns.make_key(column) not in Columns.from_keys(first_column_headings['letter']).keys(): raise TypeError if not address_line or not strip_whitespace(address_line): raise InvalidAddressError('Missing') return address_line
def values(self, value): if not value: self._values = {} else: placeholders = Columns.from_keys(self.placeholders) self._values = Columns(value).as_dict_with_keys( self.placeholders | set(key for key in value.keys() if Columns.make_key(key) not in placeholders.keys()))
def get_spreadsheet_column_headings_from_template(template): column_headings = [] for column_heading in (first_column_headings[template.template_type] + list(template.placeholders)): if column_heading not in Columns.from_keys(column_headings): column_headings.append(column_heading) return column_headings
def values_with_default_optional_address_lines(self): keys = Columns.from_keys( set(self.values.keys()) | { 'address line 3', 'address line 4', 'address line 5', 'address line 6', }).keys() return {key: Columns(self.values).get(key) or '' for key in keys}
def get_spreadsheet_column_headings_from_template(template): column_headings = [] if template.template_type == 'letter': # We want to avoid showing `address line 7` for now recipient_columns = letter_address_columns else: recipient_columns = first_column_headings[template.template_type] for column_heading in (recipient_columns + list(template.placeholders)): if column_heading not in Columns.from_keys(column_headings): column_headings.append(column_heading) return column_headings
def has_recipient_columns(self): if self.template_type == 'letter': sets_to_check = [ Columns.from_keys( address_lines_1_to_6_and_postcode_keys).keys(), Columns.from_keys(address_lines_1_to_7_keys).keys(), ] else: sets_to_check = [ self.recipient_column_headers_as_column_keys, ] for set_to_check in sets_to_check: if len( # Work out which columns are shared between the possible # letter address columns and the columns in the user’s # spreadsheet (`&` means set intersection) set_to_check & self.column_headers_as_column_keys ) >= self.count_of_required_recipient_columns: return True return False
def get_example_csv_rows(template, use_example_as_example=True, submitted_fields=False): return { 'email': ['*****@*****.**'] if use_example_as_example else [current_user.email_address], 'sms': ['6502532222'] if use_example_as_example else [current_user.mobile_number], 'letter': [(submitted_fields or {}).get( key, get_example_letter_address(key) if use_example_as_example else key) for key in first_column_headings['letter']] }[template.template_type] + get_example_csv_fields( (placeholder for placeholder in template.placeholders if placeholder not in Columns.from_keys(first_column_headings[template.template_type])), use_example_as_example, submitted_fields)
def is_optional_address_column(self, key): return (self.template_type == 'letter' and Columns.make_key(key) in Columns.from_keys(optional_address_columns).keys())
def column_headers_as_column_keys(self): return Columns.from_keys(self.column_headers).keys()
def __init__(self, old_template, new_template): self.old_placeholders = Columns.from_keys(old_template.placeholders) self.new_placeholders = Columns.from_keys(new_template.placeholders)
address_lines_1_to_7_keys, ) uk_prefix = '44' first_column_headings = { 'email': ['email address'], 'sms': ['phone number'], 'letter': [ line.replace('_', ' ') for line in address_lines_1_to_6_and_postcode_keys + [address_line_7_key] ], } address_columns = Columns.from_keys(first_column_headings['letter']) class RecipientCSV(): max_rows = 50000 def __init__( self, file_data, template, max_errors_shown=20, max_initial_rows_shown=10, whitelist=None, remaining_messages=sys.maxsize, allow_international_sms=False,