2016-02-13 15:31:28 +01:00
|
|
|
# chrono - agendas system
|
|
|
|
# Copyright (C) 2016 Entr'ouvert
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify it
|
|
|
|
# under the terms of the GNU Affero General Public License as published
|
|
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-09-16 15:04:02 +02:00
|
|
|
import csv
|
|
|
|
import datetime
|
|
|
|
|
2016-02-13 15:31:28 +01:00
|
|
|
from django import forms
|
2016-09-16 15:04:02 +02:00
|
|
|
from django.forms import ValidationError
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2016-02-13 15:31:28 +01:00
|
|
|
|
2016-09-11 11:31:29 +02:00
|
|
|
from chrono.agendas.models import Event, MeetingType, TimePeriod
|
2016-02-13 15:31:28 +01:00
|
|
|
|
2016-07-22 14:57:11 +02:00
|
|
|
from . import widgets
|
|
|
|
|
2016-02-13 15:31:28 +01:00
|
|
|
|
|
|
|
DATETIME_OPTIONS = {
|
2016-07-22 14:27:08 +02:00
|
|
|
'weekStart': 1,
|
|
|
|
'autoclose': True,
|
2016-02-13 15:31:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class DateTimeWidget(widgets.DateTimeWidget):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(DateTimeWidget, self).__init__(*args, options=DATETIME_OPTIONS, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
class EventForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Event
|
|
|
|
widgets = {
|
|
|
|
'agenda': forms.HiddenInput(),
|
|
|
|
'start_datetime': DateTimeWidget(),
|
|
|
|
}
|
2016-09-11 11:31:29 +02:00
|
|
|
exclude = ['full', 'meeting_type']
|
|
|
|
|
|
|
|
|
2016-10-28 19:06:51 +02:00
|
|
|
class NewMeetingTypeForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = MeetingType
|
|
|
|
widgets = {
|
|
|
|
'agenda': forms.HiddenInput(),
|
|
|
|
}
|
|
|
|
exclude = ['slug']
|
|
|
|
|
|
|
|
|
2016-09-11 11:31:29 +02:00
|
|
|
class MeetingTypeForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = MeetingType
|
|
|
|
widgets = {
|
|
|
|
'agenda': forms.HiddenInput(),
|
|
|
|
}
|
|
|
|
exclude = []
|
|
|
|
|
|
|
|
|
|
|
|
class TimePeriodForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = TimePeriod
|
|
|
|
widgets = {
|
|
|
|
'agenda': forms.HiddenInput(),
|
|
|
|
'start_time': widgets.TimeWidget(),
|
|
|
|
'end_time': widgets.TimeWidget(),
|
|
|
|
}
|
|
|
|
exclude = []
|
2016-09-16 15:04:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ImportEventsForm(forms.Form):
|
2017-05-05 15:56:33 +02:00
|
|
|
events_csv_file = forms.FileField(
|
|
|
|
label=_('Events File'),
|
|
|
|
required=True,
|
2016-09-16 15:04:02 +02:00
|
|
|
help_text=_('CSV file with date, time, number of places, '
|
|
|
|
'number of places in waiting list, and label '
|
|
|
|
'as columns.'))
|
2016-09-16 15:22:27 +02:00
|
|
|
events = None
|
2016-09-16 15:04:02 +02:00
|
|
|
|
|
|
|
def clean_events_csv_file(self):
|
|
|
|
content = self.cleaned_data['events_csv_file'].read()
|
|
|
|
if '\0' in content:
|
|
|
|
raise ValidationError(_('Invalid file format.'))
|
|
|
|
|
|
|
|
try:
|
|
|
|
dialect = csv.Sniffer().sniff(content)
|
2016-09-16 15:22:27 +02:00
|
|
|
except csv.Error:
|
2016-09-16 15:04:02 +02:00
|
|
|
dialect = None
|
|
|
|
|
|
|
|
events = []
|
2016-09-16 15:22:27 +02:00
|
|
|
for i, csvline in enumerate(csv.reader(content.splitlines(), dialect=dialect)):
|
2016-09-16 15:04:02 +02:00
|
|
|
if not csvline:
|
|
|
|
continue
|
2016-09-16 15:27:03 +02:00
|
|
|
if len(csvline) < 3:
|
2016-09-16 15:04:02 +02:00
|
|
|
raise ValidationError(_('Invalid file format. (line %d)') % (i+1))
|
2016-09-16 15:28:09 +02:00
|
|
|
if i == 0 and csvline[0].strip('#') in ('date', 'Date', _('date'), _('Date')):
|
2016-09-16 15:04:02 +02:00
|
|
|
continue
|
|
|
|
event = Event()
|
|
|
|
for datetime_fmt in ('%Y-%m-%d %H:%M', '%d/%m/%Y %H:%M',
|
|
|
|
'%d/%m/%Y %Hh%M', '%Y-%m-%d %H:%M:%S', '%d/%m/%Y %H:%M:%S'):
|
|
|
|
try:
|
|
|
|
event_datetime = datetime.datetime.strptime(
|
|
|
|
'%s %s' % tuple(csvline[:2]), datetime_fmt)
|
|
|
|
except ValueError:
|
|
|
|
continue
|
|
|
|
event.start_datetime = event_datetime
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise ValidationError(_('Invalid file format. (date/time format, line %d)') % (i+1))
|
|
|
|
try:
|
|
|
|
event.places = int(csvline[2])
|
|
|
|
except ValueError:
|
|
|
|
raise ValidationError(_('Invalid file format. (number of places, line %d)') % (i+1))
|
|
|
|
if len(csvline) >= 4:
|
|
|
|
try:
|
|
|
|
event.waiting_list_places = int(csvline[3])
|
|
|
|
except ValueError:
|
|
|
|
raise ValidationError(_('Invalid file format. (number of places in waiting list, line %d)') % (i+1))
|
|
|
|
if len(csvline) >= 5:
|
|
|
|
event.label = ' '.join(csvline[4:])
|
|
|
|
events.append(event)
|
|
|
|
self.events = events
|