[open-ils-commits] r126 - in servres/trunk/conifer: . syrup templates templates/components (gfawcett)

svn at svn.open-ils.org svn at svn.open-ils.org
Wed Feb 18 21:29:36 EST 2009


Author: gfawcett
Date: 2009-02-18 21:29:35 -0500 (Wed, 18 Feb 2009)
New Revision: 126

Modified:
   servres/trunk/conifer/genshi_namespace.py
   servres/trunk/conifer/syrup/models.py
   servres/trunk/conifer/syrup/views.py
   servres/trunk/conifer/templates/components/course.xhtml
   servres/trunk/conifer/templates/item_add_elec.xhtml
   servres/trunk/conifer/templates/item_add_heading.xhtml
   servres/trunk/conifer/templates/item_add_url.xhtml
   servres/trunk/conifer/templates/item_metadata.xhtml
   servres/trunk/conifer/templates/search_results.xhtml
Log:
rough in-app editing of course items; no longer based on Django admin ui.

I also did some housekeeping in genshi_namespace, which was getting
cluttered: moved some URL-of-item functions into models, as methods of
the classes in question.


Modified: servres/trunk/conifer/genshi_namespace.py
===================================================================
--- servres/trunk/conifer/genshi_namespace.py	2009-02-18 04:15:31 UTC (rev 125)
+++ servres/trunk/conifer/genshi_namespace.py	2009-02-19 02:29:35 UTC (rev 126)
@@ -6,28 +6,6 @@
 from itertools import cycle
 from conifer.syrup import models
 
-# Root-relative URLs Django has its own way of doing this, by doing
-# reverse lookups in urlpatterns. Is there a benefit to their
-# approach?
-
-def item_url(item, suffix=''):
-    if item.item_type == 'ELEC' and suffix == '':
-        return item_download_url(item)
-    if item.item_type == 'URL' and suffix == '':
-        return item.url
-    else:
-        return '/syrup/course/%d/item/%d/%s' % (item.course_id, item.id, suffix)
-
-def item_download_url(item):
-    assert item.item_type == 'ELEC'
-    return '/syrup/course/%d/item/%d/dl/%s' % (
-        item.course_id, item.id, item.fileobj.name.split('/')[-1])
-
-def course_url(course, suffix=''):
-    return '/syrup/course/%d/%s' % (course.id, suffix)
-
+# this probably ought to be a method on User, or another model class.
 def instructor_url(instructor, suffix=''):
     return '/syrup/instructor/%d/%s' % (instructor.id, suffix)
-
-def department_url(department, suffix=''):
-    return '/syrup/department/%d/%s' % (department.id, suffix)

Modified: servres/trunk/conifer/syrup/models.py
===================================================================
--- servres/trunk/conifer/syrup/models.py	2009-02-18 04:15:31 UTC (rev 125)
+++ servres/trunk/conifer/syrup/models.py	2009-02-19 02:29:35 UTC (rev 126)
@@ -174,6 +174,10 @@
             return False
         return mbr.role in (u'INSTR', u'PROXY')
 
+    def course_url(self, suffix=''):
+        return '/syrup/course/%d/%s' % (self.id, suffix)
+
+
 class Member(m.Model):
     course = m.ForeignKey(Course)
     user = m.ForeignKey(User)
@@ -331,6 +335,23 @@
 
         return self.item_type in ('ELEC', 'URL')
 
+    def item_url(self, suffix=''):
+        if self.item_type == 'ELEC' and suffix == '':
+            return '/syrup/course/%d/item/%d/dl/%s' % (
+                self.course_id, self.id, 
+                self.fileobj.name.split('/')[-1])
+        if self.item_type == 'URL' and suffix == '':
+            return self.url
+        else:
+            return '/syrup/course/%d/item/%d/%s' % (
+                self.course_id, self.id, suffix)
+    
+    def parent_url(self, suffix=''):
+        if self.parent_heading:
+            return self.parent_heading.item_url()
+        else:
+            return self.course.course_url()
+
 #------------------------------------------------------------
 
 class NewsItem(m.Model):

Modified: servres/trunk/conifer/syrup/views.py
===================================================================
--- servres/trunk/conifer/syrup/views.py	2009-02-18 04:15:31 UTC (rev 125)
+++ servres/trunk/conifer/syrup/views.py	2009-02-19 02:29:35 UTC (rev 126)
@@ -10,7 +10,6 @@
 from django.contrib.auth.models import User
 from django.db.models import Q
 from datetime import datetime
-from genshi_namespace import item_url, course_url
 
 #------------------------------------------------------------
 # Authentication
@@ -168,13 +167,6 @@
         return g.render('item_metadata.xhtml', course=item.course,
                         item=item)
 
- at login_required
-def item_edit(request, course_id, item_id):
-    """Edit an item."""
-    # For now, just pop to the Admin interface.
-    admin_url = '/admin/syrup/item/%s/' % item_id
-    return HttpResponseRedirect(admin_url)
-    
 def _heading_url(request, item):
     return HttpResponseRedirect(item.url)
 
@@ -183,6 +175,7 @@
     return g.render('item_heading_detail.xhtml', item=item)
 
 
+# fixme, not just login required! Must be in right course.
 @login_required
 def item_add(request, course_id, item_id):
     # The parent_item_id is the id for the parent-heading item. Zero
@@ -210,6 +203,7 @@
         'Sorry, only HEADINGs, URLs and ELECs can be added right now.'
 
     if request.method == 'GET':
+        item = models.Item()    # dummy object
         return g.render('item_add_%s.xhtml' % item_type.lower(),
                         **locals())
     else:
@@ -269,10 +263,31 @@
             raise NotImplementedError
 
         if parent_item:
-            return HttpResponseRedirect(item_url(parent_item, 'meta'))
+            return HttpResponseRedirect(parent_item.item_url('meta'))
         else:
-            return HttpResponseRedirect(course_url(course))
+            return HttpResponseRedirect(course.course_url())
 
+# fixme, not just login required! Must be in right course.
+ at login_required
+def item_edit(request, course_id, item_id):
+    course = get_object_or_404(models.Course, pk=course_id)
+    item = get_object_or_404(models.Item, pk=item_id, course__id=course_id)
+    template = 'item_add_%s.xhtml' % item.item_type.lower()
+    if request.method == 'GET':
+        return g.render(template, **locals())
+    else:
+        if 'file' in request.FILES:
+            # this is a 'replace-current-file' action.
+            upload = request.FILES.get('file')
+            item.fileobj.save(upload.name, upload)
+            item.fileobj_mimetype = upload.content_type
+        else:
+            # generally update the item.
+            [setattr(item, k, v) for (k,v) in request.POST.items()]
+        item.save()
+        return HttpResponseRedirect(item.parent_url())
+        
+    
 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)

Modified: servres/trunk/conifer/templates/components/course.xhtml
===================================================================
--- servres/trunk/conifer/templates/components/course.xhtml	2009-02-18 04:15:31 UTC (rev 125)
+++ servres/trunk/conifer/templates/components/course.xhtml	2009-02-19 02:29:35 UTC (rev 126)
@@ -5,7 +5,7 @@
   
   <div py:def="course_search(course)"
        id="coursesearch" style="float: right;">
-    <form method="get" action="${course_url(course, 'search')}" 
+    <form method="get" action="${course.course_url('search')}" 
 	  onsubmit="if(q.value.replace(/^\s*/, '').replace(/\s*$/, '') =='') return false;">
       <input id="q" name="q" maxlength="100" size="25" type="text" 
 	     value="search this course..." onblur="if(this.value=='') this.value='search this course...';" 
@@ -17,7 +17,7 @@
        id="coursebanner">
     <div class="deptident">${course.department}</div>
     ${course_search(course)}
-    <h1><a href="${course_url(course)}">${course.code}: ${course.title}</a></h1>
+    <h1><a href="${course.course_url()}">${course.code}: ${course.title}</a></h1>
   </div>
   
   <!-- !show_tree: display a tree of items in a hierarchical style. -->
@@ -25,10 +25,13 @@
       py:if="tree"
       class="itemtree">
     <li py:for="item, subs in tree" class="item_${item.item_type}">
-      <a href="${item_url(item)}">${item}</a> 
-      <span py:if="item.needs_meta_link()" class="menublock">
-	<a href="${item_url(item, 'meta')}">about</a>
+      <a href="${item.item_url()}">${item}</a> 
+      <span class="menublock">
+	<span py:if="item.needs_meta_link()">
+	<a href="${item.item_url('meta')}">about</a> &bull;</span>
+	<a href="${item.item_url('edit/')}">edit</a>
       </span>
+      <!-- !to show a full tree, uncomment the following: -->
       <!-- ${show_tree(subs, edit)} -->
     </li>
   </ul>
@@ -38,8 +41,8 @@
        py:with="hier=item.hierarchy()[:-1]; lastnum=len(hier)"
        class="nestedtitle">
     <div class="breadcrumbs">
-      <span><a href="${course_url(item.course)}">Top</a></span> &raquo;
-      <span py:for="n, x in enumerate(hier)"><a href="${item_url(x)}">${x.title}</a> &raquo; </span>
+      <span><a href="${item.course.course_url()}">Top</a></span> &raquo;
+      <span py:for="n, x in enumerate(hier)"><a href="${x.item_url()}">${x.title}</a> &raquo; </span>
       <b>${item.title}</b>
     </div>
   </div>

Modified: servres/trunk/conifer/templates/item_add_elec.xhtml
===================================================================
--- servres/trunk/conifer/templates/item_add_elec.xhtml	2009-02-18 04:15:31 UTC (rev 125)
+++ servres/trunk/conifer/templates/item_add_elec.xhtml	2009-02-19 02:29:35 UTC (rev 126)
@@ -1,5 +1,6 @@
 <?python
-title = 'Add a new Electronic Document'
+is_edit = bool(item.id)
+title = is_edit and 'Edit an electronic document' or 'Add a new electronic document'
 course_title = '%s: %s (%s)' % (course.code, course.title, course.term)
 ?>
 <html xmlns="http://www.w3.org/1999/xhtml"
@@ -17,13 +18,34 @@
     ${course_banner(course)}
     ${nested_title(parent_item)}
     <h3>${title}</h3>
+    <div py:if="not is_edit">
       <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>Title of document</th><td><input type="text" 
+	  name="title"
+	  value="${item.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>
+
+    <div py:if="is_edit">
+      <form action="." method="POST">
+	<table>
+	  <tr><th>Title of document</th><td><input type="text" name="title"
+	  value="${item.title}"/></td></tr>
+	</table>
+	<p><input type="submit" value="Update item"/></p>
+      </form>
+      <h3>Replace the current file with a new file</h3>
+      <form action="." method="POST" enctype="multipart/form-data">
+	<table>
+	  <tr><th>File</th><td><input type="file" name="file"/></td></tr>
+	</table>
+	<p><input type="submit" value="Upload new file"/></p>
+      </form>
+    </div>
 </body>
 </html>

Modified: servres/trunk/conifer/templates/item_add_heading.xhtml
===================================================================
--- servres/trunk/conifer/templates/item_add_heading.xhtml	2009-02-18 04:15:31 UTC (rev 125)
+++ servres/trunk/conifer/templates/item_add_heading.xhtml	2009-02-19 02:29:35 UTC (rev 126)
@@ -1,5 +1,6 @@
 <?python
-title = 'Add a new subheading'
+is_edit = bool(item.id)
+title = is_edit and 'Edit a subheading' or 'Add a new subheading'
 course_title = '%s: %s (%s)' % (course.code, course.title, course.term)
 ?>
 <html xmlns="http://www.w3.org/1999/xhtml"
@@ -19,9 +20,16 @@
     <h3>${title}</h3>
     <form action=".?item_type=${item_type}" method="POST">
       <table>
-	<tr><th>Heading</th><td><input type="text" name="title"/></td></tr>
+	<tr><th>Heading</th><td>
+	<input type="text" name="title"
+	       value="${item.title}"/>
+	</td></tr>
       </table>
-      <p><input type="submit" value="Create heading"/></p>
+      <p>
+	<input py:if="not is_edit" type="submit" value="Add heading"/>
+	<input py:if="is_edit" type="submit" value="Update heading"/>
+      </p>
+
     </form>
 </body>
 </html>

Modified: servres/trunk/conifer/templates/item_add_url.xhtml
===================================================================
--- servres/trunk/conifer/templates/item_add_url.xhtml	2009-02-18 04:15:31 UTC (rev 125)
+++ servres/trunk/conifer/templates/item_add_url.xhtml	2009-02-19 02:29:35 UTC (rev 126)
@@ -1,5 +1,6 @@
 <?python
-title = 'Add a new URL'
+is_edit = bool(item.id)
+title = is_edit and 'Edit a URL' or 'Add a new URL'
 course_title = '%s: %s (%s)' % (course.code, course.title, course.term)
 ?>
 <html xmlns="http://www.w3.org/1999/xhtml"
@@ -19,10 +20,13 @@
     <h3>${title}</h3>
     <form action=".?item_type=${item_type}" method="POST">
       <table>
-	<tr><th>Title</th><td><input type="text" name="title"/></td></tr>
-	<tr><th>URL</th><td><input type="text" name="url"/></td></tr>
+	<tr><th>Title</th><td><input type="text" name="title" value="${item.title}"/></td></tr>
+	<tr><th>URL</th><td><input type="text" name="url" value="${item.url}"/></td></tr>
       </table>
-      <p><input type="submit" value="Create item"/></p>
+      <p>
+	<input py:if="not is_edit" type="submit" value="Add URL"/>
+	<input py:if="is_edit" type="submit" value="Update URL"/>
+      </p>
     </form>
 </body>
 </html>

Modified: servres/trunk/conifer/templates/item_metadata.xhtml
===================================================================
--- servres/trunk/conifer/templates/item_metadata.xhtml	2009-02-18 04:15:31 UTC (rev 125)
+++ servres/trunk/conifer/templates/item_metadata.xhtml	2009-02-19 02:29:35 UTC (rev 126)
@@ -22,7 +22,7 @@
       <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_download_url(item)}">Download</a></p>
+    <p><a href="${item.item_url()}">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>

Modified: servres/trunk/conifer/templates/search_results.xhtml
===================================================================
--- servres/trunk/conifer/templates/search_results.xhtml	2009-02-18 04:15:31 UTC (rev 125)
+++ servres/trunk/conifer/templates/search_results.xhtml	2009-02-19 02:29:35 UTC (rev 126)
@@ -72,8 +72,8 @@
   
     <span py:def="pagerow(item)">
         <td>${Markup(item.author_hl(norm_query))}</td>
-        <td><a href="${item_url(item)}">${Markup(item.title_hl(norm_query))}</a></td>
-	<td><a href="../course/${item.course.id}/">${item.course.title}</a></td>
+        <td><a href="${item.item_url('meta')}">${Markup(item.title_hl(norm_query))}</a></td>
+	<td><a href="${item.course.course_url()}">${item.course.title}</a></td>
     </span>
     ${pagetable(paginator, count, pagerow, pageheader)}
 



More information about the open-ils-commits mailing list