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()
(r'^instructors/$', 'instructors'),
(r'^course/(?P<course_id>\d+)/$', 'course_detail'),
(ITEM_PREFIX + r'$', 'item_detail'),
+ (ITEM_PREFIX + r'dl/(?P<filename>.*)$', 'item_download'),
(ITEM_PREFIX + r'meta/$', 'item_metadata'),
(ITEM_PREFIX + r'edit/$', 'item_edit'),
(ITEM_PREFIX + r'add/$', 'item_add'), # for adding sub-things
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(),
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,
--- /dev/null
+<?python
+title = 'Add a new Electronic Document'
+course_title = '%s: %s (%s)' % (course.code, course.title, course.term)
+?>
+<html xmlns="http://www.w3.org/1999/xhtml"
+ xmlns:xi="http://www.w3.org/2001/XInclude"
+ xmlns:py="http://genshi.edgewall.org/">
+ <xi:include href="master.xhtml"/>
+ <xi:include href="components/item.xhtml"/>
+ <head>
+ <title>${title}</title>
+ </head>
+ <body>
+ <div class="courseident">
+ <div>${course.department}</div>
+ <h1><a href="${course_url(course)}">${course_title}</a></h1>
+ <div py:if="parent_item">${nested_title(parent_item)}</div>
+ <h2>${title}</h2>
+ <form action=".?item_type=${item_type}" method="POST"
+ enctype="multipart/form-data">
+ <table>
+ <tr><th>Title of document</th><td><input type="text" name="title"/></td></tr>
+ <tr><th>File</th><td><input type="file" name="file"/></td></tr>
+ </table>
+ <p><input type="submit" value="Upload file and Create item"/></p>
+ </form>
+ </div>
+</body>
+</html>
<h2>${title}</h2>
<form action=".?item_type=${item_type}" method="POST">
<table>
- <tr><th>Heading</th><td><input type="text" name="title"/></td></tr>
+ <tr><th>Title</th><td><input type="text" name="title"/></td></tr>
<tr><th>URL</th><td><input type="text" name="url"/></td></tr>
</table>
- <p><input type="submit" value="Create heading"/></p>
+ <p><input type="submit" value="Create item"/></p>
</form>
</div>
</body>
<tr><th>Author</th><td>${item.author}</td></tr>
<tr py:if="item.url"><th>URL</th><td><a href="${item.url}">${item.url}</a></td></tr>
</table>
+ <div py:if="item.item_type=='ELEC'">
+ <p><a href="${item_url(item)}dl/${item.fileobj.name.split('/')[-1]}">Download</a></p>
+ <table>
+ <tr><th>Content type</th><td>${item.fileobj_mimetype}</td></tr>
+ <tr><th>Content length</th><td>${item.fileobj.size}</td></tr>
+ </table>
+ </div>
</body>
</html>