Skip to content

Instantly share code, notes, and snippets.

@tommiewatts
Last active September 24, 2021 21:22
Show Gist options
  • Save tommiewatts/8ae5b8059c8cb6486dbe013b011dd6c9 to your computer and use it in GitHub Desktop.
Save tommiewatts/8ae5b8059c8cb6486dbe013b011dd6c9 to your computer and use it in GitHub Desktop.
Generate auto policy documents - Surepreme
import os
from collections import defaultdict
from decimal import Decimal
from platform_core.apps.core_protections.verticals.base.billing.billing_amount import BillingAmount
os.environ['DJANGO_SETTINGS_MODULE'] = 'surepreme.settings.testing'
import django
django.setup()
from surepreme.apps.protections.verticals.new_ventures.auto_base.constants import NewVenturesAutoBaseVariableCodes
from surepreme.apps.protections.verticals.new_ventures.auto_base.document_builders.constants import COVERAGE_MAP
from surepreme.apps.protections.verticals.new_ventures.auto_base.document_builders.utils import split_into_two
import pendulum
from pathlib import Path
from django.template import loader
from platform_core.utils.pdf import template_to_pdf
from surepreme.apps.protections.verticals.toggle.auto.state_constants import SouthCarolina, Illinois, Ohio, Indiana, Arizona
# Update this to test state specific policy documents
state = SouthCarolina
vehicle_coverages = {
NewVenturesAutoBaseVariableCodes.BODILY_INJURY_LIMIT: {
'amount': 123,
},
NewVenturesAutoBaseVariableCodes.PROPERTY_DAMAGE_LIMIT: {
'amount': 707,
},
NewVenturesAutoBaseVariableCodes.COLLISION_DEDUCTIBLE: {
'limit': 101,
'amount': 300,
},
NewVenturesAutoBaseVariableCodes.COMPREHENSIVE_DEDUCTIBLE: {
'limit': 1000,
'amount': 10000,
},
NewVenturesAutoBaseVariableCodes.GLASS: {
'included': True,
},
NewVenturesAutoBaseVariableCodes.TOWING_ROAD_SERVICES: {
'amount': 500,
},
NewVenturesAutoBaseVariableCodes.TEMPORARY_RIDE: {
'limit': 34,
'amount': 234,
},
NewVenturesAutoBaseVariableCodes.SIDE_HUSTLE: {
'included': True,
},
NewVenturesAutoBaseVariableCodes.GAP: {
'amount': 50,
},
NewVenturesAutoBaseVariableCodes.OEM_PARTS: {
'amount': 1337,
},
NewVenturesAutoBaseVariableCodes.NEW_CAR_REPLACEMENT: {
'amount': 40000,
},
NewVenturesAutoBaseVariableCodes.MEDICAL_LIMIT: {
'amount': 20000,
},
NewVenturesAutoBaseVariableCodes.UNINSURED_MOTORIST_BODILY_INJURY_LIMIT: {
'amount': 10000,
},
NewVenturesAutoBaseVariableCodes.UNDERINSURED_MOTORIST_BODILY_INJURY_LIMIT: {
'amount': 10000,
},
NewVenturesAutoBaseVariableCodes.PET_PASSENGER: {
'amount': 341231,
},
NewVenturesAutoBaseVariableCodes.UNINSURED_MOTORIST_PROPERTY_DAMAGE_LIMIT: {
'amount': 250,
'limit': 30000.23,
},
}
context = {
"show_page_numbers": True,
'spouse_name': 'spouseFirst spouseLast',
'policy_number': '123232123',
'policy_starts_at': pendulum.now(),
'policy_ends_at': pendulum.now(),
'effective_since': pendulum.now(),
'currency': '$',
'policy_premium': 420.012312,
'regulatory_charges': 420.99999,
'fees': 13.37,
'policy_premium_and_fees': 1337,
'monthly_charge': BillingAmount(
billing_cadence='monthly',
total=Decimal("700.3234"),
subtotal=Decimal("700"),
taxes=Decimal("0.3234"),
),
'vehicle_count': 2,
'policyholder_phone_number': '(123) 456 7890',
'policyholder_email': 'alejandro@sureapp.com',
'policyholder_name': 'Bruce Wayne',
'policyholder_address_line1': '10779 Woodbine Street',
'policyholder_address_line2': 'Apt 2000',
'policyholder_address_city': 'Los Angeles',
'policyholder_address_region_code': 'CA',
'policyholder_address_postal': '90034',
'underwriter_name': state.UNDERWRITER.name,
'underwriter_address_line1': state.UNDERWRITER.address_line1,
'underwriter_address_line3': state.UNDERWRITER.address_line3,
'underwriter_phone_number': state.UNDERWRITER.phone_number,
'underwriter_phone': state.UNDERWRITER.phone_number,
'underwriter_identifier': state.UNDERWRITER.identifier,
'vehicles': [
{
'index': 1,
'year': 2011,
'make': 'Toyota',
'model': 'Corolla',
'vin': 'IMAVIN',
'style': 'stylish',
'garaging_location': {
'postal': '90034',
},
'coverages': defaultdict(lambda:
defaultdict(lambda: Decimal("0"))
),
'premium': 420.69,
'details': 'LE',
'additional_interest_name': 'Joe Mohma',
'additional_interest_address': '69 Fake Street',
},
{
'index': 2,
'year': 2012,
'make': 'Toyota',
'model': 'Corolla',
'vin': 'IMAVIN',
'style': 'stylish',
'garaging_location': {
'postal': '90034',
},
'coverages': vehicle_coverages,
'premium': 420.69,
'details': 'LE',
},
{
'index': 3,
'year': 2013,
'make': 'Toyota',
'model': 'Corolla',
'vin': 'IMAVIN',
'style': 'stylish',
'garaging_location': {
'postal': '90034',
},
'coverages': vehicle_coverages,
'premium': 420.69,
'details': 'LE',
},
{
'index': 4,
'year': 2014,
'make': 'Toyota',
'model': 'Corolla',
'vin': 'IMAVIN',
'style': 'stylish',
'garaging_location': {
'postal': '90034',
},
'coverages': vehicle_coverages,
'premium': 420.69,
'details': 'LE',
},
{
'index': 5,
'year': 2015,
'make': 'Toyota',
'model': 'Corolla',
'vin': 'IMAVIN',
'style': 'stylish',
'garaging_location': {
'postal': '90034',
},
'coverages': vehicle_coverages,
'premium': 420.69,
'details': 'LE',
},
{
'index': 6,
'year': 2016,
'make': 'Toyota',
'model': 'Corolla',
'vin': 'IMAVIN',
'style': 'stylish',
'garaging_location': {
'postal': '90034',
},
'lienholder_name': 'Mike Wazowski',
'lienholder_address': {
'line1': '123 Fake St',
'city': 'Los Angeles',
'postal': '90034',
'region_code': 'CA',
},
'coverages': vehicle_coverages,
'premium': 420.69,
},
],
'empty_vehicle_slots': ['1', '2'],
'discounts_groups': [
[{
'type': 'discount_type',
'applies_to_vehicles': ['1', '2'],
}],
[{
'type': 'ANOTHA ONE',
'applies_to_vehicles': ['3'],
}]
],
'report_run_time': pendulum.now(),
'form_number': 'formnumberhere',
'form_edition': 'formeditionhere',
'initials': 'AA',
'signature': 'mysig',
'reason': 'You\'ve been a bad noodle!',
'cancellation_date': pendulum.now().date(),
'cancellation_time': pendulum.now().time(),
'issuance_date': pendulum.now(),
'history_groups': [
{
'name': 'Alejandro',
'dob': pendulum.now(),
'driving_license_number': '1234567890',
'driving_license_state': 'California',
'history_items': [
{
'description': 'speeding',
'date': pendulum.now(),
},
{
'description': 'tailgating',
'date': pendulum.now(),
},
],
},
{
'name': 'Michael Scott',
'dob': pendulum.datetime(1965, 3, 15).date(),
'driving_license_number': '4209696969',
'driving_license_state': 'Pennsylvania',
'is_claims': True,
'clue_ref': '42069',
'history_items': [{
'description': 'Hit and Run',
'date': pendulum.now(),
}],
},
],
'drivers': [
{
'name': 'Alejandro',
'dob': pendulum.now(),
'driving_license_number': '1234567890',
'is_excluded': True,
'is_non_driver': True,
'relationship': 'other',
},
{
'name': 'Alejandro2',
'dob': pendulum.now(),
'driving_license_number': '1234567890',
'is_excluded': False,
'is_non_driver': False,
'relationship': 'self',
'history': [
{
'is_claim': True,
'description': 'is claim is true',
'violation_code': None,
'date': pendulum.now(),
},
{
'is_claim': False,
'description': 'is claim is false',
'violation_code': 'XXX',
'date': pendulum.now(),
},
],
},
{
'name': 'Alejandrooooo',
'dob': pendulum.now(),
'driving_license_number': '1234567890',
'is_excluded': False,
'is_non_driver': True,
'relationship': 'other',
},
],
'endorsements': [
'endorsement1',
'endorsement2',
],
'coverage_map': COVERAGE_MAP,
'state_coverages': state.UNDISCLOSED_DRIVER_STATE_COVERAGES,
'coverages': {
'some_var': {
'limit': 9001,
'limit_per_occurrence': 420,
},
NewVenturesAutoBaseVariableCodes.BODILY_INJURY_LIMIT: {
'limit': 9001,
'limit_per_occurrence': 420,
},
NewVenturesAutoBaseVariableCodes.PROPERTY_DAMAGE_LIMIT: {
'limit': 80085,
'limit_per_occurrence': 80085,
},
NewVenturesAutoBaseVariableCodes.MEDICAL_LIMIT: {
'limit': 666,
},
NewVenturesAutoBaseVariableCodes.UNINSURED_MOTORIST_BODILY_INJURY_LIMIT: {
'limit': 69,
'limit_per_occurrence': 420,
},
NewVenturesAutoBaseVariableCodes.UNDERINSURED_MOTORIST_BODILY_INJURY_LIMIT: {
'limit': 1012321,
'limit_per_occurrence': 23432432,
},
NewVenturesAutoBaseVariableCodes.UNINSURED_MOTORIST_PROPERTY_DAMAGE_LIMIT: {
'limit': 10000.2343,
}
},
'uw_name': state.UNDERWRITER.name,
'amount_due': 13,
'due_date': pendulum.now().date(),
'policy_end_date': pendulum.now().date(),
'reasons': [
'You\'ve been a bad noodle',
'Because I said so',
"WHAT IS LOVE, BABY DON'T HURT ME"
],
'notification_date': pendulum.now(),
'additional_insured_name': 'Alfred Pennyworth',
'region_code': state.REGION_CODE,
# Update this to test partner specific policy documents or comment out to test Toggle
'partner_code': 'toggle_toyota',
'umpd_household_drivers': Decimal('420.69'),
'id_cards_per_copy': 2
}
context['vehicles_groups'] = split_into_two(context['vehicles'])
context['drivers_groups'] = split_into_two(context['drivers'])
context['grouped_vehicles'] = [context['vehicles'][:4]]
if len(context['grouped_vehicles']) > 4:
context['grouped_vehicles'].append(context['vehicles'][4:8])
def policy_var(var_type, field, is_state_default=False):
key = 'state_coverages' if is_state_default else 'coverages'
return context[key].get(var_type, {}).get(field)
context['policy_var'] = policy_var
def vehicle_var(vehicle_idx, var_type, field):
return context['vehicles'][vehicle_idx - 1]['coverages'].get(var_type, {}).get(field)
context['vehicle_var'] = vehicle_var
# Update this to generate specific documents
templates_to_generate = [
#'accident_violations',
#'adverse_action_loss_history',
#'adverse_action_prior_carrier',
#'application_page',
#'confirmation_of_cancellation',
#'confirmation_of_cancellation_non_payment',
#'credit_report_notice',
#'dec_page',
#'driver_exclusion',
'id_card_side_a',
'id_card_side_b',
#'new_car_endorsement',
#'noc_by_company',
#'noc_non_payment',
#'oem_parts_endorsement'
#'notice_of_non_renewal',
]
# First element in tuple is template directory, 2nd element is directory to save output in
verticals = [
('toggle', 'toyota'),
# ('farmers_direct', 'farmers')
]
for vertical in verticals:
template_path = f'{vertical[0]}/auto/policies/'
if state != SouthCarolina:
template_path += state.REGION_CODE.lower()
storage_path = os.path.join(Path.home(), vertical[1], 'after')
for template in templates_to_generate:
html = loader.render_to_string(f'{template_path}/{template}.html', context)
filepath = os.path.join(storage_path, f'{template}.html')
with open(filepath, 'w') as output_file:
output_file.write(html)
pdf = template_to_pdf(f'{template_path}/{template}.html', context)
filepath = os.path.join(storage_path, f'{template}.pdf')
with open (filepath, 'wb') as output_file:
output_file.write(pdf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment