Basic support for Electronic docs (file uploads).
authorgfawcett <gfawcett@6d9bc8c9-1ec2-4278-b937-99fde70a366f>
Thu, 15 Jan 2009 03:21:55 +0000 (03:21 +0000)
committergfawcett <gfawcett@6d9bc8c9-1ec2-4278-b937-99fde70a366f>
Thu, 15 Jan 2009 03:21:55 +0000 (03:21 +0000)
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
conifer/syrup/urls.py
conifer/syrup/views.py
conifer/templates/item_add_elec.xhtml [new file with mode: 0644]
conifer/templates/item_add_url.xhtml
conifer/templates/item_metadata.xhtml

index e80f280..a6ffc62 100644 (file)
@@ -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()
index cd95c76..0c652ef 100644 (file)
@@ -17,6 +17,7 @@ urlpatterns = patterns('conifer.syrup.views',
     (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
index 5c4d253..162f4a1 100644 (file)
@@ -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 (file)
index 0000000..595ca52
--- /dev/null
@@ -0,0 +1,29 @@
+<?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>
index dd794e7..c33704b 100644 (file)
@@ -18,10 +18,10 @@ course_title = '%s: %s (%s)' % (course.code, course.title, course.term)
       <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>
index 5a6efec..ece8add 100644 (file)
@@ -24,5 +24,12 @@ title = item.title
       <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>