Skip to content

Commit 30f8090

Browse files
committed
Use dataclass for config objects
1 parent bd3c7f5 commit 30f8090

File tree

14 files changed

+252
-355
lines changed

14 files changed

+252
-355
lines changed

web/creator/fotocalendar/bo/config.py

Lines changed: 163 additions & 266 deletions
Large diffs are not rendered by default.

web/creator/fotocalendar/creator.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
from creator.fotocalendar.templates.vintage import VintageFotoCalendar
66
from creator.fotocalendar.templates.design2026 import Design2026FotoCalendar
77
from creator.fotocalendar.templates.landscape_modern import LandscapeModernFotoCalendar
8-
from creator.fotocalendar.bo.config import CalendarConfig, DefaultConfig
8+
from creator.fotocalendar.bo.config import CalendarConfig
99
from creator.fotocalendar.icsparser import get_events_from_ics
1010
from PIL import Image
1111
from dateutil.relativedelta import relativedelta
1212
from datetime import datetime
1313
from copy import deepcopy
1414

1515

16-
def create_for_format(format) -> FotoCalendar:
16+
def create_for_format(format: str) -> FotoCalendar:
1717
if format == 'L':
1818
print("Creating LandscapeFotoCalendar for format", format)
1919
return LandscapeFotoCalendar(False)
@@ -43,7 +43,7 @@ def create_for_format(format) -> FotoCalendar:
4343
return PortraitFotoCalendar()
4444

4545

46-
def get_default_config_for_format(format) -> CalendarConfig:
46+
def get_default_config_for_format(format: str) -> CalendarConfig:
4747
first_month = datetime.now()
4848
if first_month.month > 10:
4949
first_month = first_month + relativedelta(years=1, month=1, day=1)
@@ -56,7 +56,7 @@ def get_default_config_for_format(format) -> CalendarConfig:
5656
return config
5757

5858

59-
def get_default_config_for_request(request, number_of_months=12) -> CalendarConfig:
59+
def get_default_config_for_request(request, number_of_months: int = 12) -> CalendarConfig:
6060
config = get_default_config_for_format(request.POST.get('format', 'P'))
6161
defaut_config = config.months[0]
6262
defaut_config.update_from_request(request)
@@ -82,7 +82,7 @@ def get_default_config_for_request(request, number_of_months=12) -> CalendarConf
8282
return config
8383

8484

85-
def get_config_for_request(request) -> DefaultConfig:
85+
def get_config_for_request(request) -> CalendarConfig:
8686
config = get_default_config_for_format(request.POST.get('format', 'P'))
8787
defaut_config = config.months[0]
8888
config.months = []
@@ -103,8 +103,7 @@ def create_calendar_for_request(request) -> FotoCalendar:
103103
return create_from_config(config)
104104

105105

106-
def create_from_config(config):
107-
print("Create from Config", config)
106+
def create_from_config(config: CalendarConfig) -> FotoCalendar:
108107
calendar = create_for_format(config.format)
109108
# if 'title' in config:
110109
# calendar.addTitle(config['title'])
@@ -115,7 +114,7 @@ def create_from_config(config):
115114
return calendar
116115

117116

118-
def create_preview_from_request(request):
117+
def create_preview_from_request(request) -> FotoCalendar:
119118

120119
if request.method == 'POST':
121120
config = get_default_config_for_request(request, 1)
@@ -159,6 +158,6 @@ def create_preview_from_request(request):
159158
w = 1000
160159

161160
h = w / float(config.months[0].image_aspect_ratio)
162-
config.months[0].image = image.crop((x, y, x + w, y + h))
161+
config.months[0].set_image(image.crop((x, y, x + w, y + h)))
163162

164163
return create_from_config(config)

web/creator/fotocalendar/fotocalendar.py

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -221,49 +221,21 @@ def _dayNameAbbrev(self, dayOfWeek):
221221
return self._dayNamesAbbrev[dayOfWeek - 1]
222222

223223
def _set_font(self, config: FontConfig):
224-
style = self._to_font_style(config)
225-
self.fpdf.set_font(style=style, size=config.size, family=config.family)
224+
self.fpdf.set_font(style=config.font_style, size=config.size, family=config.family)
226225
self.fpdf.set_text_color(config.color)
227226

228227
def _set_font_for_day(self, day: DayConfig, config: MonthConfig):
229-
if day.is_saturday:
230-
self._set_font(config.fonts.saturday)
228+
if day.is_holiday:
229+
self._set_font(config.fonts.events)
231230
elif day.is_sunday:
232231
self._set_font(config.fonts.sunday)
233-
elif day.is_holiday:
234-
self._set_font(config.fonts.events)
232+
elif day.is_saturday:
233+
self._set_font(config.fonts.saturday)
235234
else:
236235
self._set_font(config.fonts.weekday)
237236

238-
def _to_font_style(self, config: FontConfig):
239-
style = ''
240-
if config.bold:
241-
style += 'B'
242-
if config.italic:
243-
style += 'I'
244-
if config.underline:
245-
style += 'U'
246-
return style
247-
248-
def _hex_color_to_tuple(self, color):
249-
if color.startswith('#'):
250-
color = color.lstrip('#')
251-
return tuple(int(color[i:i + 2], 16) for i in (0, 2, 4))
252-
253-
def _set_fill_color(self, color):
254-
pdf = self.fpdf
255-
pdf.set_fill_color(color[0], color[1], color[2])
256-
257-
def _set_text_color(self, color):
258-
pdf = self.fpdf
259-
pdf.set_text_color(color[0], color[1], color[2])
260-
261-
def _set_draw_color(self, color):
262-
pdf = self.fpdf
263-
pdf.set_draw_color(color[0], color[1], color[2])
264-
265-
def get_image_aspect_ratio(self):
237+
def get_image_aspect_ratio(self) -> str:
266238
return "{:.2f}".format(self.image_with / self.image_height)
267239

268240
def output(self):
269-
return bytes(self.fpdf.output())
241+
return bytes(self.fpdf.output())

web/creator/fotocalendar/icsparser.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from creator.fotocalendar.bo.config import Event
33

44

5-
65
def get_events_from_ics(ics_url, start, end):
76
print("Setting ics_url: ", ics_url)
87
eventlist = {}
@@ -12,6 +11,6 @@ def get_events_from_ics(ics_url, start, end):
1211
datekey = evt.start.strftime("%Y%m")
1312
if datekey not in eventlist:
1413
eventlist[datekey] = []
15-
eventlist[datekey].append(Event(evt.start.strftime("%Y-%m-%d"), evt.summary, False))
14+
eventlist[datekey].append(Event(evt.start.strftime, evt.summary, False))
1615

1716
return eventlist

web/creator/fotocalendar/templates/design1.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,24 @@ def _add_text(self, config, matrix):
2727
grid_left = pdf.epw - col_width
2828

2929
pdf.set_font(style='B', size=15)
30-
self._set_text_color(self.__dark)
31-
self._set_draw_color(self.__dark)
32-
self._set_fill_color(self.__bg_bright)
30+
pdf.set_text_color(self.__dark)
31+
pdf.set_draw_color(self.__dark)
32+
pdf.set_fill_color(self.__bg_bright)
3333

3434
pdf.set_xy(grid_left, self.tmargin)
3535
pdf.cell(col_width - 20, 10, txt=self.get_month_name(date), align=config.month_align, new_x="RIGHT", fill=True)
3636
pdf.cell(20, 10, txt=self._year(date), align="R", new_x="LEFT", new_y="NEXT", fill=True)
3737

3838
for day in matrix:
3939
if matrix[day].is_sunday or matrix[day].is_holiday:
40-
self._set_text_color(self.__fg_bright)
41-
self._set_fill_color(self.__medium1)
40+
pdf.set_text_color(self.__fg_bright)
41+
pdf.set_fill_color(self.__medium1)
4242
elif matrix[day].is_saturday:
43-
self._set_text_color(self.__dark)
44-
self._set_fill_color(self.__medium2)
43+
pdf.set_text_color(self.__dark)
44+
pdf.set_fill_color(self.__medium2)
4545
else:
46-
self._set_text_color(self.__dark)
47-
self._set_fill_color(self.__bg_bright)
46+
pdf.set_text_color(self.__dark)
47+
pdf.set_fill_color(self.__bg_bright)
4848
pdf.set_x(grid_left)
4949
pdf.set_font(size=10)
5050
pdf.cell(7, line_height, txt=matrix[day].day, border='T' if config.table_border else 0, align="R", new_y="TOP", fill=True)
@@ -55,5 +55,5 @@ def _add_text(self, config, matrix):
5555
pdf.cell(col_width - 7, line_height, txt=text, border='T' if config.table_border else 0, align="L", new_y="NEXT", fill=True)
5656

5757
pdf.set_x(grid_left)
58-
self._set_fill_color(self.__bg_bright)
58+
pdf.set_fill_color(self.__bg_bright)
5959
pdf.cell(col_width, 2, txt="", border=config.table_border, fill=True)

web/creator/fotocalendar/templates/landscape_modern.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
class LandscapeModernFotoCalendar(LandscapeFotoCalendar):
66
def __init__(self):
7-
# super().__init__(False)
8-
# self.image_height = 149
97
super().__init__(True)
108
self.image_height = 167
119
self._add_font("MonsieurLaDoulaise")
@@ -23,7 +21,7 @@ def _add_table_background(self, config, x, y, w, h):
2321
pass
2422

2523
def _add_month_name(self, config):
26-
self.fpdf.set_y(177)
24+
self.fpdf.set_y(173)
2725
super()._add_month_name(config)
2826

2927
def _add_days(self, matrix, x, y, width, config):

web/creator/fotocalendar/templates/portrait.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def _add_text(self, config, matrix):
7373

7474
if len(events) > 0:
7575
pdf.ln()
76-
self._set_font(config.font.event)
76+
self._set_font(config.fonts.event)
7777
pdf.cell(txt=config.event_separator.join(events), w=pdf.epw, align=config.eventlist_align, new_x="LEFT", new_y="NEXT")
7878

7979
def __toWeekMatrix(self, month_matrix):

web/creator/templates/creator/months.html

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
<div class="tab-content" id="monthTabsContent">
3232
{% for month in months %}
3333
<div class="tab-pane fade{% if forloop.first %} show active{% endif %}" id="month_pane_{{ month.id }}" role="tabpanel" aria-labelledby="month_tab_{{ month.id }}" tabindex="0">
34-
<input type="hidden" name="date_{{ month.id }}" value="{{ month.date }}" />
34+
<input type="hidden" name="id_{{ month.id }}" value="{{ month.id }}" />
35+
<input type="hidden" name="name_{{ month.id }}" value="{{ month.name }}" />
36+
<input type="hidden" name="date_{{ month.id }}" value="{{ month.date | date:'Y-m-d' }}" />
3537

3638
<div class="container-fluid pt-3">
3739
<div class="row mb-3">
@@ -79,7 +81,7 @@
7981
-->
8082

8183
<div class="btn-group">
82-
<button type="button" id="button-save-{{ month.id }}" class="btn btn-primary" title="Projekt Speichern" data-bs-toggle="modal" data-bs-target="#upload-images-modal" onclick="uploadImages(true)" disabled>
84+
<button type="button" id="button-save-{{ month.id }}" class="btn btn-primary" title="Projekt Speichern" data-bs-toggle="modal" data-bs-target="#upload-images-modal" onclick="uploadImages(true)">
8385
<span class="fa fa-floppy-disk"></span> Speichern
8486
</button>
8587
</div>
@@ -112,7 +114,7 @@ <h5 class="mb-3">Termine</h5>
112114

113115
<div class="row pt-3">
114116
<div class="col docs-buttons">
115-
<button type="button" class="btn btn-primary" onclick="addEvent({{ month.id }}, '{{ month.date }}', '')" >
117+
<button type="button" class="btn btn-primary" onclick="addEvent({{ month.id }}, '{{ month.date | date:'Y-m-d' }}', '')" >
116118
<span class="fa fa-calendar-plus"></span> Termin hinzufügen
117119
</button>
118120
</div>
@@ -250,14 +252,13 @@ <h1 class="modal-title fs-5" id="upload-images-modal-label">Lade Bilder zum Serv
250252

251253
xhr.onload = function() {
252254
if (xhr.status == 200) {
253-
closeModal("upload-images-modal", function() {
254-
if (saveProject) {
255-
showBlob(xhr.response, "kalender.calendaronline")
256-
}
257-
else {
258-
showBlob(xhr.response, "kalender.pdf")
259-
}
260-
})
255+
if (saveProject) {
256+
showBlob(xhr.response, "kalender.calendaronline")
257+
}
258+
else {
259+
showBlob(xhr.response, "kalender.pdf")
260+
}
261+
closeModal("upload-images-modal")
261262
} else {
262263
closeModal("upload-images-modal")
263264
console.error("Hochladen der Bilder ist mit Status " + xhr.status + " fehlgeschlagen:", xhr.response);
@@ -311,6 +312,7 @@ <h1 class="modal-title fs-5" id="upload-images-modal-label">Lade Bilder zum Serv
311312

312313
function closeModal(modalId, onClosed) {
313314
var modalEl = document.getElementById(modalId);
315+
const myModalAlternative = new bootstrap.Modal('#myModal', options)
314316
console.log("Hiding modal with Id " + modalId + " and element ", modalEl)
315317

316318
if (modalEl != null) {
@@ -333,28 +335,30 @@ <h1 class="modal-title fs-5" id="upload-images-modal-label">Lade Bilder zum Serv
333335
}
334336

335337
function updatePreview(input, id, aspectRatio) {
338+
let file = input.files[0];
339+
let key = "{{ format }}-" + file.name + "-" + file.size
340+
let reader = new FileReader();
341+
reader.readAsDataURL(file);
342+
reader.onload = function () {
343+
setCropperImage(id, aspectRatio, key, reader.result)
344+
}
345+
}
346+
347+
function setCropperImage(id, aspectRatio, key, imageData) {
336348
let img = document.getElementById("image-preview-" + id);
337349

338350
if (cropper[id]) {
339351
cropper[id].destroy();
340352
}
341353

342-
let file = input.files[0];
343-
key = "{{ format }}-" + file.name + "-" + file.size
344-
345354
cropper[id] = enableCropper(img, id, key, aspectRatio);
346-
347-
let reader = new FileReader();
348-
reader.readAsDataURL(file);
349-
reader.onload = function () {
350-
cropper[id].imageReplaced = true;
351-
cropper[id].replace(reader.result);
352-
}
355+
cropper[id].imageReplaced = true;
356+
cropper[id].replace(imageData);
353357
}
354358

355359
function enableCropper(img, id, storedCropperKey, aspectRatio) {
356360
var storedCropperData = false
357-
if (localStorage.getItem(storedCropperKey)) {
361+
if (storedCropperKey && localStorage.getItem(storedCropperKey)) {
358362
storedCropperData = JSON.parse(localStorage.getItem(storedCropperKey))
359363
}
360364

@@ -383,6 +387,7 @@ <h1 class="modal-title fs-5" id="upload-images-modal-label">Lade Bilder zum Serv
383387
{% for event in month.events %}
384388
addEvent({{month.id}}, '{{ event.date }}', '{{ event.text }}', true);
385389
{% endfor %}
390+
{% if month.image %}setCropperImage('{{ month.id }}', '{{month.image_aspect_ratio}}', false, 'data:image/jpeg;base64,{{ month.image }}'){% endif %}
386391
{% endfor %}
387392
});
388393
{% endif %}

web/creator/templates/creator/options.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ <h5 class="mb-3">Optionen für den Kalender</h5>
2121
<div class="row mb-3">
2222
<label for="startDate" class="col-sm-2 col-form-label">Erster Monat</label>
2323
<div class="col-sm-10">
24-
<input id="startDate" class="form-control" type="date" name="start" value="{{ first_month }}"/>
24+
<input id="startDate" class="form-control" type="date" name="start" value="{{ first_month | date:'Y-m-d' }}"/>
2525
<span id="startDateSelected"></span>
2626
</div>
2727
</div>

web/creator/templates/creator/options_general.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<label class="col-sm-2 col-form-check-label" for="center-month{{ fieldpostfix }}">Montatsname zentriert</label>
3333
<div class="col-sm-10">
3434
<div class="form-check form-switch">
35-
<input class="form-check-input" type="checkbox" role="switch" id="center-month{{ fieldpostfix }}" name="center_month{{ fieldpostfix }}" value="1"{% if month.center_month %} checked{% endif %}>
35+
<input class="form-check-input" type="checkbox" role="switch" id="center-month{{ fieldpostfix }}" name="center_month{{ fieldpostfix }}" value="1"{% if month.month_align == 'C' %} checked{% endif %}>
3636
</div>
3737
</div>
3838
</div>

0 commit comments

Comments
 (0)