Wrap input CSV in an TextIOWrapper

Closes #24
This commit is contained in:
James Polley 2017-10-15 14:59:57 +10:00
parent 8ded9ff4fc
commit b154d90eed
2 changed files with 7 additions and 2 deletions

View file

@ -1,5 +1,6 @@
import csv
import time
from io import TextIOWrapper
from datetime import datetime
@ -66,6 +67,9 @@ class ScheduleSectionForm(forms.Form):
def __init__(self, *args, **kwargs):
self.schedule = kwargs.pop("schedule")
if 'encoding' in kwargs:
self.encoding = kwargs['encoding']
kwargs.pop('encoding')
super(ScheduleSectionForm, self).__init__(*args, **kwargs)
def clean_filename(self):
@ -118,7 +122,8 @@ class ScheduleSectionForm(forms.Form):
def build_schedule(self):
created_items = []
reader = csv.DictReader(self.cleaned_data.get('filename'))
f = TextIOWrapper(self.cleaned_data.get('filename'), encoding=self.encoding)
reader = csv.DictReader(f)
data = [dict((k.strip(), v.strip()) for k, v in x.items()) for x in reader]
# build rooms
created_items.extend(self._build_rooms(data))

View file

@ -129,7 +129,7 @@ def schedule_edit(request, slug=None):
if request.method == "POST":
form = ScheduleSectionForm(
request.POST, request.FILES, schedule=schedule
request.POST, request.FILES, schedule=schedule, encoding=request.encoding
)
if form.is_valid():
if 'submit' in form.data: