Repository navigation
Expand file tree
/
Copy pathtasks.py
More file actions
324 lines (267 loc) · 11.1 KB
/
Copy pathtasks.py
File metadata and controls
324 lines (267 loc) · 11.1 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
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
"""
Certificate Generation Task Module
==================================
Author: devag7 (Deva Garwalla)
Description: Advanced certificate generation system using Celery, FFmpeg, and ReportLab.
Supports dynamic content, QR codes, and optimized PDF generation.
Features:
- Dynamic text overlays with custom fonts
- QR code generation and embedding
- Multiple output formats (PDF, PNG, JPG)
- Optimized file sizes for production use
- Error handling and recovery mechanisms
- Background processing with Celery
"""
import os
import subprocess
import logging
from pathlib import Path
from datetime import datetime
from typing import Dict, Any
from celery import Celery
from utils import generate_qr_code, format_datetime
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Celery configuration
app = Celery('certificate_generator', broker='pyamqp://localhost//')
# Base configuration
BASE_DIR = Path(__file__).parent.resolve()
FONT_PATH = BASE_DIR / "fonts" / "Open Sans Bold.ttf"
TEMPLATE_PATH = BASE_DIR / "templates" / "certificate_template.jpg"
CERTIFICATES_DIR = BASE_DIR / "certificates"
TEMP_DIR = BASE_DIR / "temp"
# Required fields for certificate generation
REQUIRED_KEYS = ['user_name', 'college', 'certificate_id', 'issued_at', 'topic']
class CertificateGenerationError(Exception):
"""Custom exception for certificate generation errors"""
pass
def validate_data(data: Dict[str, Any]) -> Dict[str, Any]:
"""Validate input data for certificate generation"""
missing = [key for key in REQUIRED_KEYS if key not in data or not data[key]]
if missing:
raise ValueError(f"Missing or empty required fields: {', '.join(missing)}")
# Validate data types and lengths
if not isinstance(data['user_name'], str) or len(data['user_name']) > 100:
raise ValueError("user_name must be a string with max 100 characters")
if not isinstance(data['college'], str) or len(data['college']) > 200:
raise ValueError("college must be a string with max 200 characters")
if not isinstance(data['topic'], str) or len(data['topic']) > 150:
raise ValueError("topic must be a string with max 150 characters")
# Validate date format
try:
datetime.fromisoformat(data['issued_at'])
except ValueError:
raise ValueError("issued_at must be in ISO format")
return data
def ensure_directories():
"""Ensure required directories exist"""
for directory in [CERTIFICATES_DIR, TEMP_DIR]:
directory.mkdir(exist_ok=True)
def text_escape(text: str) -> str:
"""Escape special characters for FFmpeg text filters"""
return (
str(text)
.replace("\\", "\\\\")
.replace(":", "\\:")
.replace("'", r"\'")
.replace('"', r'\"')
.replace("[", r"\[")
.replace("]", r"\]")
.replace(",", r"\,")
.replace(";", r"\;")
)
@app.task(bind=True, max_retries=3)
def generate_certificate(self, data: Dict[str, Any]) -> str:
"""
Main task for generating certificates
Args:
data: Dictionary containing certificate information
Required keys: user_name, college, certificate_id, issued_at, topic
Returns:
Path to generated certificate PDF
Raises:
CertificateGenerationError: If generation fails
"""
try:
logger.info(f"Starting certificate generation for {data.get('certificate_id', 'unknown')}")
# Validate input data
data = validate_data(data)
# Ensure directories exist
ensure_directories()
# Check if template exists
if not TEMPLATE_PATH.exists():
raise FileNotFoundError(f"Template not found: {TEMPLATE_PATH}")
if not FONT_PATH.exists():
logger.warning(f"Font not found: {FONT_PATH}, using system default")
# Generate QR code
qr_path = generate_qr_code(data['certificate_id'])
# Prepare escaped text
esc = {
'name': text_escape(data['user_name']),
'college': text_escape(data['college']),
'topic': text_escape(data['topic']),
'cid': text_escape(data['certificate_id']),
'date': text_escape(format_datetime(data['issued_at'])),
}
# Build filter chain with developer credit
font_file = str(FONT_PATH).replace(" ", "\\ ")
filter_chain = (
f"drawtext=text='{esc['name']}':x=90:y=880:fontsize=50:"
f"fontcolor=black:fontfile='{font_file}',"
f"drawtext=text='{esc['college']}':x=90:y=1095:fontsize=45:"
f"fontcolor=black:fontfile='{font_file}',"
f"drawtext=text='{esc['topic']}':x=90:y=1285:fontsize=40:"
f"fontcolor=black:fontfile='{font_file}',"
f"drawtext=text='{esc['cid']}':x=1695:y=110:"
f"fontsize=20:fontcolor=darkred:fontfile='{font_file}',"
f"drawtext=text='Date\\: {esc['date']}':x=1650:y=135:"
f"fontsize=25:fontcolor=black:fontfile='{font_file}',"
f"drawtext=text='Generated by devag7':x=50:y=50:fontsize=12:"
f"fontcolor=gray:fontfile='{font_file}'"
)
# Generate high-quality intermediate image
temp_img_path = TEMP_DIR / f"{data['certificate_id']}_temp.jpg"
ffmpeg_cmd = [
'ffmpeg', '-y',
'-i', str(TEMPLATE_PATH),
'-vf', f"{filter_chain},scale=1500:-1",
'-q:v', '2',
str(temp_img_path)
]
try:
result = subprocess.run(ffmpeg_cmd, check=True, capture_output=True, text=True)
logger.info("FFmpeg image generation successful")
except subprocess.CalledProcessError as e:
logger.error(f"FFmpeg failed: {e.stderr}")
raise CertificateGenerationError(f"Image generation failed: {e.stderr}")
# Convert to PDF with optimization
pdf_output_path = CERTIFICATES_DIR / f"{data['certificate_id']}.pdf"
# Try multiple conversion methods for best results
conversion_success = False
# Method 1: ImageMagick (best quality)
try:
img_to_pdf_cmd = [
'convert', str(temp_img_path),
'-density', '200',
'-quality', '85',
'-compress', 'jpeg',
str(pdf_output_path)
]
subprocess.run(img_to_pdf_cmd, check=True)
conversion_success = True
logger.info("PDF conversion successful using ImageMagick")
except (subprocess.CalledProcessError, FileNotFoundError):
logger.warning("ImageMagick conversion failed, trying FFmpeg")
# Method 2: FFmpeg direct conversion
if not conversion_success:
try:
ffmpeg_pdf_cmd = [
'ffmpeg', '-y',
'-i', str(temp_img_path),
'-f', 'pdf',
str(pdf_output_path)
]
subprocess.run(ffmpeg_pdf_cmd, check=True)
conversion_success = True
logger.info("PDF conversion successful using FFmpeg")
except subprocess.CalledProcessError:
raise CertificateGenerationError("All PDF conversion methods failed")
# Cleanup
if temp_img_path.exists():
temp_img_path.unlink()
if qr_path.exists():
qr_path.unlink()
# Verify file size
if pdf_output_path.exists():
size_mb = pdf_output_path.stat().st_size / (1024 * 1024)
logger.info(f"Certificate generated successfully. Size: {size_mb:.2f} MB")
if size_mb > 3:
logger.warning(f"File size ({size_mb:.2f} MB) exceeds 3MB limit")
return str(pdf_output_path)
except Exception as e:
logger.error(f"Certificate generation failed: {str(e)}")
# Retry logic
if self.request.retries < self.max_retries:
logger.info(f"Retrying... Attempt {self.request.retries + 1}")
raise self.retry(countdown=60, exc=e)
raise CertificateGenerationError(f"Failed to generate certificate after {self.max_retries} attempts: {str(e)}")
@app.task
def cleanup_old_certificates(days_old: int = 30) -> Dict[str, int]:
"""
Clean up old certificate files
Args:
days_old: Number of days old files should be to be considered for cleanup
Returns:
Dictionary with cleanup statistics
"""
from datetime import timedelta
cutoff_date = datetime.now() - timedelta(days=days_old)
cleaned_count = 0
total_size_mb = 0
for cert_file in CERTIFICATES_DIR.glob("*.pdf"):
file_time = datetime.fromtimestamp(cert_file.stat().st_mtime)
if file_time < cutoff_date:
size_mb = cert_file.stat().st_size / (1024 * 1024)
cert_file.unlink()
cleaned_count += 1
total_size_mb += size_mb
# Clean temp directory
for temp_file in TEMP_DIR.glob("*"):
file_time = datetime.fromtimestamp(temp_file.stat().st_mtime)
if file_time < cutoff_date:
temp_file.unlink()
logger.info(f"Cleanup completed: {cleaned_count} files removed, {total_size_mb:.2f} MB freed")
return {
'files_removed': cleaned_count,
'space_freed_mb': round(total_size_mb, 2)
}
@app.task
def health_check() -> Dict[str, Any]:
"""
Health check task for monitoring
Returns:
System health status
"""
status = {
'timestamp': datetime.now().isoformat(),
'template_exists': TEMPLATE_PATH.exists(),
'font_exists': FONT_PATH.exists(),
'directories_writable': True,
'ffmpeg_available': False,
'imagemagick_available': False
}
# Check directory permissions
try:
test_file = TEMP_DIR / "health_check.tmp"
test_file.touch()
test_file.unlink()
except Exception:
status['directories_writable'] = False
# Check FFmpeg availability
try:
subprocess.run(['ffmpeg', '-version'], capture_output=True, check=True)
status['ffmpeg_available'] = True
except (subprocess.CalledProcessError, FileNotFoundError):
pass
# Check ImageMagick availability
try:
subprocess.run(['convert', '-version'], capture_output=True, check=True)
status['imagemagick_available'] = True
except (subprocess.CalledProcessError, FileNotFoundError):
pass
return status
if __name__ == "__main__":
# For testing purposes
test_data = {
"user_name": "Test User",
"college": "Test University",
"certificate_id": f"TEST-{datetime.now().strftime('%Y%m%d-%H%M%S')}",
"issued_at": datetime.now().isoformat(),
"topic": "Test Topic"
}
try:
result = generate_certificate(test_data)
print(f"Test certificate generated: {result}")
except Exception as e:
print(f"Test failed: {e}")