-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_contract_renderer.py
More file actions
101 lines (79 loc) · 3.01 KB
/
test_contract_renderer.py
File metadata and controls
101 lines (79 loc) · 3.01 KB
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
#!/usr/bin/env python3
"""
Test script for the contract renderer functionality.
This script demonstrates how to use the contract renderer to generate HTML and PDF documents.
"""
import argparse
import asyncio
import sys
from pathlib import Path
from prozorro.render.render.contract import render_contract_html, render_contract_pdf
from prozorro.render.utils.api import get_template_name
from prozorro.render.utils.contract import (
generate_contract_template_context,
generate_contract_template_filename,
)
# Define directories
FILE_DIR = Path(__file__).parent
MEDIA_DIR = FILE_DIR / "media"
# Create data directory if it doesn't exist
MEDIA_DIR.mkdir(exist_ok=True)
# Add the src directory to the Python path
sys.path.insert(0, str(Path(__file__).parent / "src"))
async def async_main():
"""Test the contract renderer with sample data."""
# Parse command line arguments
parser = argparse.ArgumentParser(description="Test contract renderer")
parser.add_argument("--url", help="URL to fetch contract data from")
parser.add_argument("--template", help="Template name to use for rendering")
args = parser.parse_args()
# Generate context and fetch data if URL provided
print("=" * 50)
print("Preparing data...")
print("=" * 50)
try:
context = await generate_contract_template_context(args.url)
if "contract_url" in context:
print(f"Successfully fetched contract data from: {context['contract_url']}")
if "tender_url" in context:
print(f"Successfully fetched tender data from: {context['tender_url']}")
except ValueError as e:
print(f"Error fetching data: {e}")
return 1
# Get template name
try:
template_name = get_template_name(template=args.template, contract_data=context["contract"])
print(f"Using template: {template_name}")
except ValueError as e:
print(f"Error: {e}")
return 1
print("=" * 50)
print("Testing Contract Renderer...")
print("=" * 50)
filename = generate_contract_template_filename(context["contract"].get("contractID"), template_name)
try:
# Test HTML rendering
print("1. Generating HTML...")
html_content = render_contract_html(context, template_name)
# Save HTML to file
html_path = MEDIA_DIR / f"{filename}.html"
with open(html_path, "w", encoding="utf-8") as f:
f.write(html_content)
print(f" ✓ HTML saved to {html_path}")
# Test PDF rendering
print("2. Generating PDF...")
pdf_content = render_contract_pdf(context, template_name)
# Save PDF to file
pdf_path = MEDIA_DIR / f"{filename}.pdf"
with open(pdf_path, "wb") as f:
f.write(pdf_content)
print(f" ✓ PDF saved to {pdf_path}")
except Exception as e:
print(f"Error during testing: {e}")
return 1
return 0
def main():
"""Entry point for the script."""
return asyncio.run(async_main())
if __name__ == "__main__":
exit(main())