From: Jan Szejko Date: Wed, 29 Jun 2016 16:39:26 +0000 (+0200) Subject: attachments X-Git-Url: https://git.mdrn.pl/edumed.git/commitdiff_plain/1cb8ffe0e48893c57524d964292a9d3a86f8f8b4?ds=sidebyside;hp=eab7f0e20df316e13a7029508a1bc649dec748be attachments --- diff --git a/catalogue/forms.py b/catalogue/forms.py index 84f2e43..7230b23 100644 --- a/catalogue/forms.py +++ b/catalogue/forms.py @@ -1,4 +1,10 @@ # -*- coding: utf-8 -*- +import json +import os.path +import shutil +import urllib +from tempfile import mkdtemp + from django.forms import Form, CharField from librarian import IOFile @@ -7,6 +13,23 @@ from catalogue.models import Lesson class LessonImportForm(Form): lesson_xml = CharField() + gallery_url = CharField(required=False) + attachments = CharField(required=False) + + def save(self): + temp_dir = mkdtemp() + attachment_names = json.loads(self.cleaned_data['attachments']) + attachments = {} + remote_gallery_url = self.cleaned_data['gallery_url'] + if remote_gallery_url and attachment_names: + for attachment_name in attachment_names: + attachment_url = ('%s%s' % (remote_gallery_url, attachment_name)).encode('utf-8') + temp_filename = os.path.join(temp_dir, attachment_name) + urllib.urlretrieve(attachment_url, temp_filename) + attachments[attachment_name] = IOFile.from_filename(temp_filename) - def save(self, commit=True, **kwargs): - return Lesson.publish(IOFile.from_string(self.cleaned_data['lesson_xml'])) + lesson = Lesson.publish( + IOFile.from_string(self.cleaned_data['lesson_xml'], attachments=attachments)) + if os.path.isdir(temp_dir): + shutil.rmtree(temp_dir) + return lesson diff --git a/catalogue/models.py b/catalogue/models.py index ee07651..bae04ed 100644 --- a/catalogue/models.py +++ b/catalogue/models.py @@ -123,8 +123,7 @@ class Lesson(models.Model): def publish(cls, infile, ignore_incomplete=False): from librarian.parser import WLDocument from django.core.files.base import ContentFile - xml = infile.get_string() - wldoc = WLDocument.from_string(xml) + wldoc = WLDocument(infile) # Check if not section metadata block. if wldoc.book_info.parts: @@ -138,7 +137,7 @@ class Lesson(models.Model): lesson = cls(slug=slug, order=0) # Save XML file - lesson.xml_file.save('%s.xml' % slug, ContentFile(xml), save=False) + lesson.xml_file.save('%s.xml' % slug, ContentFile(infile.get_string()), save=False) lesson.title = wldoc.book_info.title lesson.level = Level.objects.get(meta_name=wldoc.book_info.audience)