-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathticketrenderer.py
More file actions
102 lines (84 loc) · 3.66 KB
/
Copy pathticketrenderer.py
File metadata and controls
102 lines (84 loc) · 3.66 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
# coding=utf-8
from os import path
import random
from jinja2 import Environment
import datetime
def datetimeformat(value, format='%d/%m/%Y %H:%M'):
"""
Jinja filter used to format date
:param value:
:param format:
:return:
"""
return value.strftime(format)
JINJA_ENV = Environment()
JINJA_ENV.filters['datetimeformat'] = datetimeformat
class TicketRenderer(object):
def __init__(self, ticket_template, media_url, css_url):
"""
:param ticket_template: a ticket template used to configure a photobooth
:param media_url: base url to fetch images
:param css_url: url to find css files
:return:
"""
self.template = ticket_template
self.media_url = media_url
self.css_url = css_url
def render(self, picture, code, date, counter, place, event):
context = {
'title': self.template['title'],
'description': self.template['description'],
'picture': picture,
'datetime': date,
'code': code,
'css_url': self.css_url,
'place_name': place['name'] if place else None,
'place_code': place['code'] if place else None,
'event_name': event['name'] if event else None,
'event_code': event['code'] if event else None,
}
if event and event['portraits_expiration']:
context['days_left'] = event['portraits_expiration']
elif place and place['portraits_expiration']:
context['days_left'] = place['portraits_expiration']
else:
#Default value
context['days_left'] = 30
#Get real expiration date
ed = (date + datetime.timedelta(context['days_left'])).timetuple()
#Server delete portraits at 4:00
hours = [4, 0]
#Return an expiration_date to the next `hours`
if ed[3] <= hours[0] or ed[4] <= hours[1]:
context['expiry_date'] = datetime.datetime(ed[0], ed[1], ed[2], hours[0], hours[1])
else:
context['expiry_date'] = datetime.datetime(ed[0], ed[1], ed[2], hours[0], hours[1]) + datetime.timedelta(1)
for image in self.template['images']:
image_url = self.get_image_url(image['name'])
context['image_%s' % image['id']] = image_url
for image_variable in self.template['image_variables']:
if image_variable['items']:
if image_variable['mode'] == 'random':
# random mode
choice = random.choice(image_variable['items'])
else:
# sequential mode
number_of_items = len(image_variable['items'])
choice = image_variable['items'][counter % number_of_items]
uid = 'imagevariable_%s' % image_variable['id']
context[uid] = self.get_image_url(choice['name'])
for text_variable in self.template['text_variables']:
if text_variable['items']:
if text_variable['mode'] == 'random':
# random mode
choice = random.choice(text_variable['items'])
else:
# sequential mode
number_of_items = len(text_variable['items'])
choice = text_variable['items'][counter % number_of_items]
uid = 'textvariable_%s' % text_variable['id']
context[uid] = choice['text']
template = JINJA_ENV.from_string(self.template['html'])
return template.render(context)
def get_image_url(self, image_name):
return path.join(self.media_url, 'images', image_name)