From: gfawcett Date: Mon, 19 Jul 2010 01:35:55 +0000 (+0000) Subject: reorg of site editing code; starting on smarter sakai-linktool integration X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=bbe3ca794bd38452dbe513ae057acfecab07e410;p=Syrup.git reorg of site editing code; starting on smarter sakai-linktool integration git-svn-id: svn://svn.open-ils.org/ILS-Contrib/servres/trunk@930 6d9bc8c9-1ec2-4278-b937-99fde70a366f --- diff --git a/conifer/integration/linktool/app.py b/conifer/integration/linktool/app.py index 6d00fe1..f2a1595 100644 --- a/conifer/integration/linktool/app.py +++ b/conifer/integration/linktool/app.py @@ -1,14 +1,25 @@ -from django.contrib.auth import authenticate, login -from django.http import HttpResponse, HttpResponseRedirect, HttpResponseForbidden +from conifer.syrup.views._common import * +from django.contrib.auth import authenticate, login +from conifer.syrup import models +from conifer.syrup.views import genshi_namespace +from conifer.plumbing.genshi_support import TemplateSet +g = TemplateSet([HERE('integration/linktool/templates'), + HERE('templates')], + genshi_namespace) -def linktool_welcome(request): + +def linktool_welcome(request, command=u''): + #return g.render('index.xhtml') user = authenticate(request=request) if user is None: return HttpResponseForbidden('You are not allowed here.') else: login(request, user) - request.session['clew-site'] = request.GET['site'] + extsite = request.session['clew-site'] = request.GET['site'] + extrole = request.session['clew-role'] = request.GET['role'] + + return HttpResponse("""""" """Redirecting to the library reserves system...""" % ( - request.META['SCRIPT_NAME'])) + request.META['SCRIPT_NAME'] or '/')) diff --git a/conifer/integration/linktool/templates/index.xhtml b/conifer/integration/linktool/templates/index.xhtml new file mode 100644 index 0000000..bd88c1e --- /dev/null +++ b/conifer/integration/linktool/templates/index.xhtml @@ -0,0 +1,11 @@ + + + + testing + + +

testing

+ + diff --git a/conifer/plumbing/genshi_support.py b/conifer/plumbing/genshi_support.py index a0fe935..9b9235b 100644 --- a/conifer/plumbing/genshi_support.py +++ b/conifer/plumbing/genshi_support.py @@ -33,11 +33,18 @@ class TemplateSet(object): def __init__(self, basedir, namespace_module=None): self.basedir = basedir - self.dirs = [self.basedir] + if isinstance(basedir, list): + self.dirs = basedir + else: + self.dirs = [self.basedir] self.loader = TemplateLoader(self.dirs, auto_reload=True, callback=self.template_loaded) - self.namespace_module = namespace_module + if hasattr(namespace_module, '__dict__'): + self.namespace_module = namespace_module.__dict__ + else: + self.namespace_module = namespace_module + def file(self, name): fn = os.path.join(self.basedir, name) @@ -62,7 +69,7 @@ class TemplateSet(object): if not 'errors' in ns: ns['errors'] = None if self.namespace_module is not None: - ns.update(self.namespace_module.__dict__) + ns.update(self.namespace_module) def render(self, tname, **kwargs): request = get_request() diff --git a/conifer/syrup/models.py b/conifer/syrup/models.py index ec75f1c..9a7fb51 100644 --- a/conifer/syrup/models.py +++ b/conifer/syrup/models.py @@ -160,7 +160,7 @@ class Site(BaseModel): service_desk = m.ForeignKey(ServiceDesk) access = m.CharField(max_length=5, - default='LOGIN', + default='ANON', choices = [ ('ANON', _('World-accessible')), ('LOGIN', _('Accessible to all logged-in users')), @@ -169,7 +169,7 @@ class Site(BaseModel): class Meta: unique_together = (('course', 'term', 'owner')) - ordering = ['-term__start', 'course__code'] + ordering = ['-term__start', 'course__code', 'owner__last_name'] def save(self, *args, **kwargs): # Ensure there is always an internal Group. @@ -538,7 +538,7 @@ class Item(BaseModel): def parent_url(self, suffix=''): if self.parent_heading: - return self.parent_heading.item_url() + return self.parent_heading.item_url() + suffix else: return self.site.site_url() diff --git a/conifer/syrup/views/general.py b/conifer/syrup/views/general.py index 8b053cb..e350a57 100644 --- a/conifer/syrup/views/general.py +++ b/conifer/syrup/views/general.py @@ -111,9 +111,6 @@ def browse(request, browse_option=''): page_num=page_num, count=count) -@login_required -def my_sites(request): - return g.render('my_sites.xhtml') def instructor_detail(request, instructor_id): page_num = int(request.GET.get('page', 1)) diff --git a/conifer/syrup/views/items.py b/conifer/syrup/views/items.py index e88717e..d422441 100644 --- a/conifer/syrup/views/items.py +++ b/conifer/syrup/views/items.py @@ -123,10 +123,7 @@ def item_add(request, site_id, item_id): else: raise NotImplementedError - if parent_item: - return HttpResponseRedirect(parent_item.item_url('meta')) - else: - return HttpResponseRedirect(site.site_url()) + return HttpResponseRedirect(item.parent_url()) @instructors_only def item_add_cat_search(request, site_id, item_id): @@ -207,7 +204,8 @@ def item_add_cat_search(request, site_id, item_id): marcxml=raw_pickitem, **dct) item.save() - return HttpResponseRedirect('../../../%d/meta' % item.id) + + return HttpResponseRedirect(item.parent_url()) #------------------------------------------------------------ @@ -244,12 +242,9 @@ def item_delete(request, site_id, item_id): if 'yes' in request.POST: # I think Django's ON DELETE CASCADE-like behaviour will # take care of the sub-items. - if item.parent_heading: - redir = HttpResponseRedirect(item.parent_heading.item_url('meta')) - else: - redir = HttpResponseRedirect(site.site_url()) + redir = item.parent_url('meta') item.delete() - return redir + return HttpResponseRedirect(redir) else: return HttpResponseRedirect('../meta') diff --git a/conifer/syrup/views/search.py b/conifer/syrup/views/search.py index 517a770..9d49597 100644 --- a/conifer/syrup/views/search.py +++ b/conifer/syrup/views/search.py @@ -141,7 +141,8 @@ def search(request, in_site=None, for_owner=None): if not query_string: # empty query? if in_site: - return HttpResponseRedirect(reverse('site_detail', in_site)) + return HttpResponseRedirect(reverse('site_detail', + kwargs={'site_id': in_site})) else: return HttpResponseRedirect(reverse('browse')) else: diff --git a/conifer/syrup/views/sites.py b/conifer/syrup/views/sites.py index a29ec8a..072db11 100644 --- a/conifer/syrup/views/sites.py +++ b/conifer/syrup/views/sites.py @@ -9,18 +9,12 @@ class NewSiteForm(ModelForm): model = models.Site exclude = ('access',) - def clean_code(self): - v = (self.cleaned_data.get('code') or '').strip() - is_valid_func = gethook('course_code_is_valid') - if (not is_valid_func) or is_valid_func(v): - return v - else: - raise ValidationError, _('invalid course code') - def __init__(self, *args, **kwargs): owner = self.base_fields['owner'] - owner.label_from_instance = lambda u: u.get_list_name() - owner.queryset = User.objects.order_by('last_name', 'first_name', 'username') + owner.label_from_instance = lambda u: '%s (%s)' % ( + u.get_list_name(), u.username) + owner.queryset = User.objects.order_by('last_name', 'first_name', + 'username') super(NewSiteForm, self).__init__(*args, **kwargs) @@ -42,7 +36,6 @@ def _add_or_edit_site(request, instance=None): is_add = (instance is None) if is_add: instance = models.Site() - current_access_level = not is_add and instance.access or None if request.method != 'POST': form = NewSiteForm(instance=instance) return g.render('edit_site.xhtml', **locals()) @@ -55,7 +48,7 @@ def _add_or_edit_site(request, instance=None): site = form.instance assert site.id - if is_add or (current_access_level != site.access): + if is_add: # we need to configure permissions. return HttpResponseRedirect(site.site_url('edit/permission/')) else: @@ -74,94 +67,31 @@ def edit_site_permissions(request, site_id): ] choose_access = django.forms.RadioSelect(choices=choices) - + if request.method != 'POST': return g.render('edit_site_permissions.xhtml', **locals()) else: POST = request.POST + access = POST.get('access') + site.access = access + site.save() - if 'action_save_instructor' in POST: - # update instructor details ---------------------------------- - iname = POST.get('new_instructor_name','').strip() - irole = POST.get('new_instructor_role') - - def get_record_for(username): - instr = models.maybe_initialize_user(iname) - if instr: - try: - return models.Membership.objects.get(user=instr, site=site) - except models.Membership.DoesNotExist: - return models.Membership.objects.create(user=instr, site=site) - - # add a new instructor - if iname: - instr = get_record_for(iname) - if instr: # else? should have an error. - instr.role = irole - instr.save() - else: - instructor_error = 'No such user: %s' % iname - return g.render('edit_site_permissions.xhtml', **locals()) - - - # removing and changing roles of instructors - to_change_role = [(int(name.rsplit('_', 1)[-1]), POST[name]) \ - for name in POST if name.startswith('instructor_role_')] - to_remove = [int(name.rsplit('_', 1)[-1]) \ - for name in POST if name.startswith('instructor_remove_')] - for instr_id, newrole in to_change_role: - if not instr_id in to_remove: - instr = models.Membership.objects.get(pk=instr_id, site=site) - instr.role = newrole - instr.save() - for instr_id in to_remove: - # todo, should warn if deleting yourself! - instr = models.Membership.objects.get(pk=instr_id, site=site) - instr.delete() - # todo, should have some error-reporting. - return HttpResponseRedirect('.') - - elif 'action_save_student' in POST: - # update student details ------------------------------------ - access = POST.get('access') - site.access = access - # drop all provided users. fixme, this could be optimized to do add/drops. - #models.Membership.objects.filter(site=site, provided=True).delete() - if site.access == u'STUDT': - initial_sections = site.sections() - # add the 'new section' if any - new_sec = request.POST.get('add_section') - new_sec = models.section_decode_safe(new_sec) - if new_sec: - site.add_sections(new_sec) - # remove the sections to be dropped - to_remove = [models.section_decode_safe(name.rsplit('_',1)[1]) \ - for name in POST \ - if name.startswith('remove_section_')] - site.drop_sections(*to_remove) - student_names = models.campus.students_in(*site.sections()) - for name in student_names: - user = models.maybe_initialize_user(name) - if user: - if not models.Membership.objects.filter(site=site, user=user): - mbr = models.Membership.objects.create( - site=site, user=user, - role='STUDT', provided=True) - mbr.save() - else: - pass - site.save() - return HttpResponseRedirect('.#student_access') + return HttpResponseRedirect('../../') @instructors_only def delete_site(request, site_id): site = get_object_or_404(models.Site, pk=site_id) if request.POST.get('confirm_delete'): site.delete() - return HttpResponseRedirect(reverse('my_sites')) + return HttpResponseRedirect(reverse(my_sites)) else: return HttpResponseRedirect('../') + +@login_required +def my_sites(request): + return g.render('my_sites.xhtml') + #----------------------------------------------------------------------------- # Site Invitation Code handler @@ -188,13 +118,13 @@ def site_join(request, site_id): site = get_object_or_404(models.Site, pk=site_id) if not site.is_joinable_by(request.user): # user should never see this. - return simple_message(_('You cannot join this site.'), - _('Sorry, but you cannot join this site at this time.')) + return simple_message(_('You cannot join this site.'), + _('Sorry, but you cannot join this site ' + 'at this time.')) elif request.method != 'POST': return g.render('site_join.xhtml', site=site) else: - mbr = models.Membership.objects.create(user=request.user, site=site, role='STUDT') + mbr = models.Membership.objects.create(user=request.user, + site=site, role='STUDT') mbr.save() return HttpResponseRedirect(site.site_url()) - - diff --git a/conifer/templates/edit_site.xhtml b/conifer/templates/edit_site.xhtml index 0e4cc2f..ba75178 100644 --- a/conifer/templates/edit_site.xhtml +++ b/conifer/templates/edit_site.xhtml @@ -1,6 +1,6 @@ @@ -16,7 +16,6 @@ else:
${site_banner(instance)}

${title}

-

Edit site permissionsReturn to site page

  • ${err}
  • @@ -33,7 +32,6 @@ else: e.g., ${example} -

    General description

    ${field_row(form.owner)} ${field_row(form.term)} diff --git a/conifer/templates/edit_site_permissions.xhtml b/conifer/templates/edit_site_permissions.xhtml index 6090d27..9ce6469 100644 --- a/conifer/templates/edit_site_permissions.xhtml +++ b/conifer/templates/edit_site_permissions.xhtml @@ -1,5 +1,5 @@ ${site_banner(site)}

    ${title}

    -

    Edit site detailsReturn to site page

    -

    Site Access Level

    Who has permission to view resources in this site?

    ${Markup(choose_access.render('access', site.access, {'id':'id_access'}, []))}
    -

    +

    + ${go_back_link()}

    Current Membership

    diff --git a/conifer/templates/my_sites.xhtml b/conifer/templates/my_sites.xhtml index e12e5dc..71b7aba 100644 --- a/conifer/templates/my_sites.xhtml +++ b/conifer/templates/my_sites.xhtml @@ -18,12 +18,12 @@ title = _('My Reserves') ?>

    You are not part of any course-reserve sites at this time.

    - ${site.list_display()} + ${site}

    diff --git a/conifer/templates/site_detail.xhtml b/conifer/templates/site_detail.xhtml index dc7b0ca..7b06b6e 100644 --- a/conifer/templates/site_detail.xhtml +++ b/conifer/templates/site_detail.xhtml @@ -17,7 +17,8 @@ is_joinable = site.is_joinable_by(request.user) ${site_banner(site)}
    Join this site diff --git a/conifer/urls.py b/conifer/urls.py index f0ec87f..eeac6c3 100644 --- a/conifer/urls.py +++ b/conifer/urls.py @@ -42,5 +42,5 @@ if not settings.DEBUG: if settings.LINKTOOL_AUTHENTICATION: urlpatterns += patterns( 'conifer.integration.linktool.app', - (r'^linktool-welcome/$', 'linktool_welcome')) + (r'^linktool-welcome/(?P.*)$', 'linktool_welcome'))