From 7a5832c68e34d4a9e60c4d44b30fd90b47843afb Mon Sep 17 00:00:00 2001 From: gfawcett Date: Thu, 19 Feb 2009 02:29:35 +0000 Subject: [PATCH] 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. git-svn-id: svn://svn.open-ils.org/ILS-Contrib/servres/trunk@126 6d9bc8c9-1ec2-4278-b937-99fde70a366f --- conifer/genshi_namespace.py | 24 +-------------------- conifer/syrup/models.py | 21 +++++++++++++++++++ conifer/syrup/views.py | 35 ++++++++++++++++++++++--------- conifer/templates/components/course.xhtml | 17 ++++++++------- conifer/templates/item_add_elec.xhtml | 26 +++++++++++++++++++++-- conifer/templates/item_add_heading.xhtml | 14 ++++++++++--- conifer/templates/item_add_url.xhtml | 12 +++++++---- conifer/templates/item_metadata.xhtml | 2 +- conifer/templates/search_results.xhtml | 4 ++-- 9 files changed, 103 insertions(+), 52 deletions(-) diff --git a/conifer/genshi_namespace.py b/conifer/genshi_namespace.py index 75bbaf7..c4acd81 100644 --- a/conifer/genshi_namespace.py +++ b/conifer/genshi_namespace.py @@ -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) diff --git a/conifer/syrup/models.py b/conifer/syrup/models.py index 26e9047..51f5e0e 100644 --- a/conifer/syrup/models.py +++ b/conifer/syrup/models.py @@ -174,6 +174,10 @@ class Course(m.Model): 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 @@ class Item(m.Model): 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): diff --git a/conifer/syrup/views.py b/conifer/syrup/views.py index a8ecac3..ab5b6fa 100644 --- a/conifer/syrup/views.py +++ b/conifer/syrup/views.py @@ -10,7 +10,6 @@ from conifer.syrup import models 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 @@ def item_metadata(request, course_id, item_id): return g.render('item_metadata.xhtml', course=item.course, item=item) -@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 @@ def _heading_detail(request, item): 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 @@ def item_add(request, course_id, item_id): '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 @@ def item_add(request, course_id, item_id): 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. +@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) diff --git a/conifer/templates/components/course.xhtml b/conifer/templates/components/course.xhtml index 5ea0266..e7b28b8 100644 --- a/conifer/templates/components/course.xhtml +++ b/conifer/templates/components/course.xhtml @@ -5,7 +5,7 @@
-
${course.department}
${course_search(course)} -

${course.code}: ${course.title}

+

${course.code}: ${course.title}

@@ -25,10 +25,13 @@ py:if="tree" class="itemtree">
  • - ${item} - - about + ${item} + + + about + edit +
  • @@ -38,8 +41,8 @@ py:with="hier=item.hierarchy()[:-1]; lastnum=len(hier)" class="nestedtitle"> diff --git a/conifer/templates/item_add_elec.xhtml b/conifer/templates/item_add_elec.xhtml index 7386da0..6768088 100644 --- a/conifer/templates/item_add_elec.xhtml +++ b/conifer/templates/item_add_elec.xhtml @@ -1,5 +1,6 @@ ${title} +
    - +
    Title of document
    Title of document
    File

    +
    + +
    +
    + + +
    Title of document
    +

    +
    +

    Replace the current file with a new file

    +
    + + +
    File
    +

    +
    +
    diff --git a/conifer/templates/item_add_heading.xhtml b/conifer/templates/item_add_heading.xhtml index bfbe03b..4779892 100644 --- a/conifer/templates/item_add_heading.xhtml +++ b/conifer/templates/item_add_heading.xhtml @@ -1,5 +1,6 @@ ${title}
    - +
    Heading
    Heading + +
    -

    +

    + + +

    +
    diff --git a/conifer/templates/item_add_url.xhtml b/conifer/templates/item_add_url.xhtml index 48b45f4..5e60881 100644 --- a/conifer/templates/item_add_url.xhtml +++ b/conifer/templates/item_add_url.xhtml @@ -1,5 +1,6 @@ ${title}
    - - + +
    Title
    URL
    Title
    URL
    -

    +

    + + +

    diff --git a/conifer/templates/item_metadata.xhtml b/conifer/templates/item_metadata.xhtml index 9441a22..e940575 100644 --- a/conifer/templates/item_metadata.xhtml +++ b/conifer/templates/item_metadata.xhtml @@ -22,7 +22,7 @@ title = item.title URL${item.url}
    -

    Download

    +

    Download

    diff --git a/conifer/templates/search_results.xhtml b/conifer/templates/search_results.xhtml index fd7aa6c..4bdfffb 100644 --- a/conifer/templates/search_results.xhtml +++ b/conifer/templates/search_results.xhtml @@ -72,8 +72,8 @@ courses = course_list - - + + ${pagetable(paginator, count, pagerow, pageheader)} -- 2.11.0
    Content type${item.fileobj_mimetype}
    Content length${item.fileobj.size}
    ${Markup(item.author_hl(norm_query))}${Markup(item.title_hl(norm_query))}${item.course.title}${Markup(item.title_hl(norm_query))}${item.course.title}