From 928fbad0ea4b79af10e541be9d5b5514fade353e Mon Sep 17 00:00:00 2001 From: gfawcett Date: Thu, 15 Jan 2009 03:21:55 +0000 Subject: [PATCH] Basic support for Electronic docs (file uploads). It's ugly, but the mechanism works. This commit changes the Item model; if you don't want to rebuild your tables you can do this: sqlite3 syrup.sqlite "alter table syrup_item add column fileobj_mimetype varchar(128) NULL;" git-svn-id: svn://svn.open-ils.org/ILS-Contrib/servres/trunk@113 6d9bc8c9-1ec2-4278-b937-99fde70a366f --- conifer/syrup/models.py | 2 ++ conifer/syrup/urls.py | 1 + conifer/syrup/views.py | 31 +++++++++++++++++++++++++++++-- conifer/templates/item_add_elec.xhtml | 29 +++++++++++++++++++++++++++++ conifer/templates/item_add_url.xhtml | 4 ++-- conifer/templates/item_metadata.xhtml | 7 +++++++ 6 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 conifer/templates/item_add_elec.xhtml diff --git a/conifer/syrup/models.py b/conifer/syrup/models.py index e80f280..a6ffc62 100644 --- a/conifer/syrup/models.py +++ b/conifer/syrup/models.py @@ -290,6 +290,8 @@ class Item(m.Model): fileobj = m.FileField(upload_to='uploads/%Y/%m/%d', max_length=255, blank=True, null=True, default=None) + fileobj_mimetype = m.CharField(max_length=128, blank=True, null=True, default=None) + date_created = m.DateTimeField(auto_now_add=True) last_modified = m.DateTimeField() diff --git a/conifer/syrup/urls.py b/conifer/syrup/urls.py index cd95c76..0c652ef 100644 --- a/conifer/syrup/urls.py +++ b/conifer/syrup/urls.py @@ -17,6 +17,7 @@ urlpatterns = patterns('conifer.syrup.views', (r'^instructors/$', 'instructors'), (r'^course/(?P\d+)/$', 'course_detail'), (ITEM_PREFIX + r'$', 'item_detail'), + (ITEM_PREFIX + r'dl/(?P.*)$', 'item_download'), (ITEM_PREFIX + r'meta/$', 'item_metadata'), (ITEM_PREFIX + r'edit/$', 'item_edit'), (ITEM_PREFIX + r'add/$', 'item_add'), # for adding sub-things diff --git a/conifer/syrup/views.py b/conifer/syrup/views.py index 5c4d253..162f4a1 100644 --- a/conifer/syrup/views.py +++ b/conifer/syrup/views.py @@ -173,8 +173,8 @@ def item_add(request, course_id, item_id): item_type = request.GET.get('item_type') assert item_type, 'No item_type parameter was provided.' - # for the moment, only HEADINGs and URLs can be added. - assert item_type in ('HEADING', 'URL'), 'Sorry, only HEADINGs and URLs can be added right now.' + # for the moment, only HEADINGs, URLs and ELECs can be added. + assert item_type in ('HEADING', 'URL', 'ELEC'), 'Sorry, only HEADINGs, URLs and ELECs can be added right now.' if request.method == 'GET': return g.render('item_add_%s.xhtml' % item_type.lower(), @@ -216,8 +216,35 @@ def item_add(request, course_id, item_id): url = url) item.save() return HttpResponseRedirect(item_url(item) + 'meta/') + elif item_type == 'ELEC': + title = request.POST.get('title', '').strip() + upload = request.FILES.get('file') + item = models.Item( + course=course, + item_type='ELEC', + parent_heading=parent_item, + title=title, + author=author, + activation_date=datetime.now(), + last_modified=datetime.now(), + fileobj_mimetype = upload.content_type, + ) + item.fileobj.save(upload.name, upload) + item.save() + + return HttpResponseRedirect(item_url(item) + 'meta/') else: raise NotImplementedError + +def item_download(request, course_id, item_id, filename): + course = get_object_or_404(models.Course, pk=course_id) + item = get_object_or_404(models.Item, pk=item_id, course__id=course_id) + assert item.item_type == 'ELEC', 'Can only download ELEC documents!' + fileiter = item.fileobj.chunks() + resp = HttpResponse(fileiter) + resp['Content-Type'] = item.fileobj_mimetype or 'application/octet-stream' + #resp['Content-Disposition'] = 'attachment; filename=%s' % name + return resp def normalize_query(query_string, findterms=re.compile(r'"([^"]+)"|(\S+)').findall, diff --git a/conifer/templates/item_add_elec.xhtml b/conifer/templates/item_add_elec.xhtml new file mode 100644 index 0000000..595ca52 --- /dev/null +++ b/conifer/templates/item_add_elec.xhtml @@ -0,0 +1,29 @@ + + + + + + ${title} + + +
+
${course.department}
+

${course_title}

+
${nested_title(parent_item)}
+

${title}

+
+ + + +
Title of document
File
+

+
+
+ + diff --git a/conifer/templates/item_add_url.xhtml b/conifer/templates/item_add_url.xhtml index dd794e7..c33704b 100644 --- a/conifer/templates/item_add_url.xhtml +++ b/conifer/templates/item_add_url.xhtml @@ -18,10 +18,10 @@ course_title = '%s: %s (%s)' % (course.code, course.title, course.term)

${title}

- +
Heading
Title
URL
-

+

diff --git a/conifer/templates/item_metadata.xhtml b/conifer/templates/item_metadata.xhtml index 5a6efec..ece8add 100644 --- a/conifer/templates/item_metadata.xhtml +++ b/conifer/templates/item_metadata.xhtml @@ -24,5 +24,12 @@ title = item.title Author${item.author} URL${item.url} +
+

Download

+ + + +
Content type${item.fileobj_mimetype}
Content length${item.fileobj.size}
+
-- 2.11.0