834785f2c186fc995920643eb82640609cd60534
[edumed.git] / catalogue / forms.py
1 # -*- coding: utf-8 -*-
2 from django.forms import Form, FileField, CharField, ValidationError
3
4 from catalogue.models import Lesson
5
6
7 class LessonImportForm(Form):
8     lesson_xml_file = FileField(required=False)
9     lesson_xml = CharField(required=False)
10
11     def clean(self):
12         from django.core.files.base import ContentFile
13
14         if not self.cleaned_data['lesson_xml_file']:
15             if self.cleaned_data['lesson_xml']:
16                 self.cleaned_data['lesson_xml_file'] = \
17                     ContentFile(self.cleaned_data['lesson_xml'].encode('utf-8'))
18             else:
19                 raise ValidationError(u"Proszę dostarczyć XML.")
20         return super(LessonImportForm, self).clean()
21
22     def save(self, commit=True, **kwargs):
23         return Lesson.publish(self.cleaned_data['book_xml_file'])