27 |
27 |
from django import forms
|
28 |
28 |
from django.conf import settings
|
29 |
29 |
from django.contrib.auth.models import Group
|
|
30 |
from django.contrib.humanize.templatetags.humanize import ordinal
|
30 |
31 |
from django.core.exceptions import FieldDoesNotExist
|
31 |
32 |
from django.core.validators import URLValidator
|
32 |
33 |
from django.db import transaction
|
... | ... | |
35 |
36 |
from django.template import Context, Template, TemplateSyntaxError, VariableDoesNotExist
|
36 |
37 |
from django.utils.encoding import force_str
|
37 |
38 |
from django.utils.formats import date_format
|
|
39 |
from django.utils.html import format_html, mark_safe
|
38 |
40 |
from django.utils.timezone import localtime, make_aware, now
|
39 |
41 |
from django.utils.translation import gettext_lazy as _
|
40 |
42 |
|
... | ... | |
1060 |
1062 |
super().__init__(**kwargs)
|
1061 |
1063 |
|
1062 |
1064 |
def clean_events_csv_file(self):
|
|
1065 |
class ValidationErrorWithOrdinal(ValidationError):
|
|
1066 |
def __init__(self, message, event_no):
|
|
1067 |
super().__init__(message)
|
|
1068 |
self.message = format_html(message, event_no=mark_safe(ordinal(event_no + 1)))
|
|
1069 |
|
1063 |
1070 |
content = self.cleaned_data['events_csv_file'].read()
|
1064 |
1071 |
if b'\0' in content:
|
1065 |
1072 |
raise ValidationError(_('Invalid file format.'))
|
... | ... | |
1092 |
1099 |
if not csvline:
|
1093 |
1100 |
continue
|
1094 |
1101 |
if len(csvline) < 3:
|
1095 |
|
raise ValidationError(_('Invalid file format. (line %d)') % (i + 1))
|
|
1102 |
raise ValidationErrorWithOrdinal(_('Invalid file format. ({event_no} event)'), i)
|
1096 |
1103 |
if i == 0 and csvline[0].strip('#') in ('date', 'Date', _('date'), _('Date')):
|
1097 |
1104 |
continue
|
1098 |
1105 |
|
... | ... | |
1146 |
1153 |
event.start_datetime = event_datetime
|
1147 |
1154 |
break
|
1148 |
1155 |
else:
|
1149 |
|
raise ValidationError(_('Invalid file format. (date/time format, line %d)') % (i + 1))
|
|
1156 |
raise ValidationErrorWithOrdinal(
|
|
1157 |
_('Invalid file format. (date/time format, {event_no} event)'), i
|
|
1158 |
)
|
1150 |
1159 |
try:
|
1151 |
1160 |
event.places = int(csvline[2])
|
1152 |
1161 |
except ValueError:
|
1153 |
|
raise ValidationError(_('Invalid file format. (number of places, line %d)') % (i + 1))
|
|
1162 |
raise ValidationError(_('Invalid file format. (number of places, {event_no} event)'), i)
|
1154 |
1163 |
if len(csvline) >= 4:
|
1155 |
1164 |
try:
|
1156 |
1165 |
event.waiting_list_places = int(csvline[3])
|
1157 |
1166 |
except ValueError:
|
1158 |
1167 |
raise ValidationError(
|
1159 |
|
_('Invalid file format. (number of places in waiting list, line %d)') % (i + 1)
|
|
1168 |
_('Invalid file format. (number of places in waiting list, {event_no} event)'), i
|
1160 |
1169 |
)
|
1161 |
1170 |
|
1162 |
1171 |
column_index = 7
|
... | ... | |
1183 |
1192 |
except ValueError:
|
1184 |
1193 |
continue
|
1185 |
1194 |
else:
|
1186 |
|
raise ValidationError(_('Invalid file format. (date/time format, line %d)') % (i + 1))
|
|
1195 |
raise ValidationError(_('Invalid file format. (date/time format, {event_no} event)'), i)
|
1187 |
1196 |
|
1188 |
1197 |
if len(csvline) >= 11 and csvline[10]: # duration is optional
|
1189 |
1198 |
try:
|
1190 |
1199 |
event.duration = int(csvline[10])
|
1191 |
1200 |
except ValueError:
|
1192 |
|
raise ValidationError(_('Invalid file format. (duration, line %d)') % (i + 1))
|
|
1201 |
raise ValidationError(_('Invalid file format. (duration, {event_no} event)'), i)
|
1193 |
1202 |
|
1194 |
1203 |
try:
|
1195 |
1204 |
event.full_clean(exclude=['desk', 'meeting_type', 'primary_event'])
|