From 23b8d811251a1dcfaeaf2017981a38518f591350 Mon Sep 17 00:00:00 2001
From: gfawcett
Date: Tue, 28 Dec 2010 20:36:09 +0000
Subject: [PATCH] Admin function: add new staff member. Partly refactored
fuzzyFinder interface.
The refactored fuzzyFinder is ugly, though. Really there should be a
Genshi-side component.
git-svn-id: svn://svn.open-ils.org/ILS-Contrib/servres/trunk@1135 6d9bc8c9-1ec2-4278-b937-99fde70a366f
---
conifer/TODO | 5 ++-
conifer/static/edit_site.js | 78 +-------------------------------
conifer/static/fuzzyFinder.js | 79 +++++++++++++++++++++++++++++++++
conifer/syrup/urls.py | 4 +-
conifer/syrup/views/admin.py | 26 +++++++++++
conifer/templates/admin/staff_add.xhtml | 51 +++++++++++++++++++++
conifer/templates/edit_site.xhtml | 4 +-
7 files changed, 166 insertions(+), 81 deletions(-)
create mode 100644 conifer/static/fuzzyFinder.js
create mode 100644 conifer/templates/admin/staff_add.xhtml
diff --git a/conifer/TODO b/conifer/TODO
index ec8960f..73c9f2d 100644
--- a/conifer/TODO
+++ b/conifer/TODO
@@ -2,8 +2,6 @@ NEW:
* add URL form should have same metadata as ELEC form (except for copyright status)
-* add new staff member
-
* why are .focus() calls not working properly?
* edit URL item fails with error. KeyError: author2
@@ -22,6 +20,8 @@ IMPORTANT:
MAYBE:
+* refactor fuzzyFinder into a Genshi component.
+
* set up a proper issue-tracker?
* if someone has item checked out, show due date/time on item-about page.
@@ -75,3 +75,4 @@ RECENTLY DONE:
* factor out hardcoded references to the EG server.
+* add new staff member
diff --git a/conifer/static/edit_site.js b/conifer/static/edit_site.js
index 416d2c7..e925198 100644
--- a/conifer/static/edit_site.js
+++ b/conifer/static/edit_site.js
@@ -1,78 +1,4 @@
-var fuzzyLookup = {
-
- lastText: null,
- lastPress: null,
- waiting: false,
-
- lookup: function() {
- var text = $(this).val();
- if (text != fuzzyLookup.lastText) {
- fuzzyLookup.lastText = text;
- if (text.length < 3) {
- return;
- }
- fuzzyLookup.lastPress = new Date();
- }
- },
-
- minWait: 500,
-
- interval: function() {
- var now = new Date();
-
- if (fuzzyLookup.lastPress == null || (now - fuzzyLookup.lastPress) < fuzzyLookup.minWait) {
- return;
- }
-
- if (fuzzyLookup.waiting) {
- return;
- }
-
- fuzzyLookup.waiting = true;
- $('#fuzzyinput').css({backgroundColor: 'yellow'}); // debugging
- $.post('site_fuzzy_user_lookup', {'q': fuzzyLookup.lastText},
- function(data) {
- fuzzyLookup.waiting = false;
- fuzzyLookup.lastPress = null;
- $('#fuzzyinput').css({backgroundColor: 'white'}); // debugging
- $('#fuzzypanel').text('');
- if (data.results.length == 0) {
- $('#fuzzypanel').append('No matches.');
- }
- $.each(data.results, function(i,val) {
- var link = $('');
- link.text(val[1]);
- link.data('userid', val[0]);
- link.data('display', val[1]);
- link.click(fuzzyLookup.pick);
- $('#fuzzypanel').append(link);
- });
- if (data.notshown > 0) {
- $('#fuzzypanel').append('and ' + data.notshown + ' more.
');
- }
- }, 'json');
- },
-
- pick: function(uid) {
- $('#fuzzyedit').hide();
- $('#fuzzyview').show();
-
- var inp = $('#owner');
- inp.val($(this).data('userid'));
-
- $('#fuzzyname').text($(this).data('display'));
- },
-
- edit: function() {
- $('#fuzzyview').hide();
- $('#fuzzyedit').show();
- $('#fuzzyinput').focus();
- fuzzyLookup.lastText = $('#fuzzyinput').val();
- fuzzyLookup.lastPress = new Date(new Date() - 450);
- }
-}
-
$(function() {
- $('#fuzzyinput').keyup(fuzzyLookup.lookup);
- setInterval(fuzzyLookup.interval, 250);
+ var fuzzy = fuzzyFinder(
+ '#fuzzyinput', '#fuzzypanel', '#fuzzyedit', '#fuzzyview', '#fuzzyname', '#fuzzychange', '#owner');
});
diff --git a/conifer/static/fuzzyFinder.js b/conifer/static/fuzzyFinder.js
new file mode 100644
index 0000000..78ec8a4
--- /dev/null
+++ b/conifer/static/fuzzyFinder.js
@@ -0,0 +1,79 @@
+function fuzzyFinder(Search, Matches, EditPanel, ViewPanel, ViewName, Change, Owner) {
+ var here = {
+
+ lastText: null,
+ lastPress: null,
+ waiting: false,
+ minWait: 500,
+
+ lookup: function() {
+ var text = $(this).val();
+ if (text != here.lastText) {
+ here.lastText = text;
+ if (text.length < 3) {
+ return;
+ }
+ here.lastPress = new Date();
+ }
+ },
+
+ interval: function() {
+ var now = new Date();
+
+ if (here.lastPress == null || (now - here.lastPress) < here.minWait) {
+ return;
+ }
+
+ if (here.waiting) {
+ return;
+ }
+
+ here.waiting = true;
+ $(Search).css({backgroundColor: 'yellow'}); // debugging
+ $.post(ROOT + '/fuzzy_user_lookup', {'q': here.lastText},
+ function(data) {
+ here.waiting = false;
+ here.lastPress = null;
+ $(Search).css({backgroundColor: 'white'}); // debugging
+ $(Matches).text('');
+ if (data.results.length == 0) {
+ $(Matches).append('No matches.');
+ }
+ $.each(data.results, function(i,val) {
+ var link = $('');
+ link.text(val[1]);
+ link.data('userid', val[0]);
+ link.data('display', val[1]);
+ link.click(here.pick);
+ $(Matches).append(link);
+ });
+ if (data.notshown > 0) {
+ $(Matches).append('and ' + data.notshown + ' more.
');
+ }
+ }, 'json');
+ },
+
+ pick: function(uid) {
+ $(EditPanel).hide();
+ $(ViewPanel).show();
+
+ var inp = $(Owner);
+ inp.val($(this).data('userid'));
+
+ $(ViewName).text($(this).data('display'));
+ },
+
+ edit: function() {
+ $(ViewPanel).hide();
+ $(EditPanel).show();
+ $(Search).focus();
+ here.lastText = $(Search).val();
+ here.lastPress = new Date(new Date() - 450);
+ }
+ };
+ setInterval(here.interval, 250);
+ $(Search).keyup(here.lookup);
+ $(Change).click(here.edit);
+ return here;
+}
+
diff --git a/conifer/syrup/urls.py b/conifer/syrup/urls.py
index 62fcb2c..5026fec 100644
--- a/conifer/syrup/urls.py
+++ b/conifer/syrup/urls.py
@@ -34,7 +34,7 @@ urlpatterns = patterns('conifer.syrup.views',
(r'^site/(?P\d+)/edit/permission/delete_group/$', 'edit_site_permissions_delete_group'),
(r'^site/(?P\d+)/feeds/(?P.*)$', 'site_feeds'),
(r'^site/(?P\d+)/join/$', 'site_join'),
- (r'^site/.*fuzzy_user_lookup$', 'site_fuzzy_user_lookup'),
+ (r'^fuzzy_user_lookup$', 'site_fuzzy_user_lookup'),
(ITEM_PREFIX + r'$', 'item_detail'),
(ITEM_PREFIX + r'dl/(?P.*)$', 'item_download'),
(ITEM_PREFIX + r'meta$', 'item_metadata'),
@@ -53,6 +53,8 @@ urlpatterns = patterns('conifer.syrup.views',
(r'^admin/targets/' + GENERIC_REGEX, 'admin_targets'),
(r'^admin/update_depts_courses/$', 'admin_update_depts_courses'),
(r'^admin/update_terms/$', 'admin_update_terms'),
+ (r'^admin/staff/add/$', 'admin_staff_add'),
+
# (r'^phys/$', 'phys_index'),
# (r'^phys/checkout/$', 'phys_checkout'),
diff --git a/conifer/syrup/views/admin.py b/conifer/syrup/views/admin.py
index 314b375..141458e 100644
--- a/conifer/syrup/views/admin.py
+++ b/conifer/syrup/views/admin.py
@@ -149,3 +149,29 @@ def admin_update_terms(request):
code = tcode,
defaults = dict(name=tname, start=start, finish=finish))
return simple_message('Terms updated.', '')
+
+@admin_only
+def admin_staff_add(request):
+ if request.method != 'POST':
+ return g.render('admin/staff_add.xhtml', **locals())
+ else:
+ userid = request.POST.get('userid','').strip()
+ message_continue = True
+
+ try:
+ user = User.objects.get(username=userid)
+ except User.DoesNotExist:
+ user = User.objects.create(username=userid)
+ user.maybe_decorate()
+
+ user.is_staff = True
+ user.is_superuser = True # TODO: are we sure they should be superuser?
+ user.save()
+
+ if not userid:
+ message = 'No user selected.'
+ message_continue = False
+ else:
+ message = 'Staff user added: %s [%s].' % (user.get_full_name(), user.username)
+
+ return g.render('admin/staff_add.xhtml', **locals())
diff --git a/conifer/templates/admin/staff_add.xhtml b/conifer/templates/admin/staff_add.xhtml
new file mode 100644
index 0000000..6531e70
--- /dev/null
+++ b/conifer/templates/admin/staff_add.xhtml
@@ -0,0 +1,51 @@
+
+
+
+
+ ${title}
+
+
+
+
+
+
+ ${title}
+
+
${message}
+ ${go_back_link('../../', 'Continue')}
+
+
+
+
Type a partial name or userid into the box; then select one of the matches.
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/conifer/templates/edit_site.xhtml b/conifer/templates/edit_site.xhtml
index dcd6b06..b14e174 100644
--- a/conifer/templates/edit_site.xhtml
+++ b/conifer/templates/edit_site.xhtml
@@ -12,6 +12,7 @@ owner = instance.owner if instance.owner_id else None
${title}
+