Skip to content

Instantly share code, notes, and snippets.

@tgalv
Created June 20, 2016 17:11
Show Gist options
  • Save tgalv/4bff333031fce46c80b16221bd71422a to your computer and use it in GitHub Desktop.
Save tgalv/4bff333031fce46c80b16221bd71422a to your computer and use it in GitHub Desktop.
"""
Spike for template rendering.
$ export PYTHONPATH=/<path>/<to>/deed-api
"""
from contextlib import contextmanager
import datetime
import json
import os
from flask import Flask, current_app
from flask import render_template, jsonify
@contextmanager
def working_directory(path):
cwd = os.getcwd()
os.chdir(path)
yield
os.chdir(cwd)
with working_directory(os.getenv("PYTHONPATH")):
from application.deed.address_utils import format_address_string
def render_pdf(deed_dict):
if 'effective_date' in deed_dict:
temp = datetime.datetime.strptime(deed_dict['effective_date'], "%Y-%m-%d %H:%M:%S")
deed_dict["effective_date"] = temp.strftime("%d/%m/%Y")
deed_dict["property_address"] = format_address_string(','.join(deed_dict["property_address"]))
deed_data = {'deed': deed_dict}
template = os.path.join(os.getenv("PYTHONPATH"), 'application/deed/templates/viewdeed.html')
return render_template(template, deed_data=deed_data, signed=True)
DEED_DICT = {
"additional_provisions": [{
"additional_provision_code": "addp001",
"description": "Description"
}, {
"additional_provision_code": "addp002",
"description": "Description"
}],
"borrowers": [{
"address": "test address with postcode, PL14 3JR",
"forename": "lisa",
"middle_name": "ann",
"surname": "bloggette",
"token": "AAAAAA"
}, {
"address": "Test Address With Postcode, PL14 3JR",
"forename": "frank",
"middle_name": "ann",
"surname": "bloggette",
"token": "BBBBBB"
}],
"charge_clause": {
"cre_code": "CRE001",
"description": "Description"
},
"effective_clause": "Effective clause goes here",
"identity_checked": "Y",
"lender": {
"address": "Test Address, London NW10 7TQ",
"name": "Bank of England Plc",
"registration": "Company registration number: 123456"
},
"md_ref": "e-MD12344",
"property_address": [
"5 The Drive",
"This Town",
"This County",
"PL4 4TH"
],
"title_number": "DN100"
}
if __name__ == "__main__":
app = Flask(__name__)
with app.app_context():
open('deed.html','w').write(render_pdf(DEED_DICT))
import sys; sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment