Admin function: add new staff member. Partly refactored fuzzyFinder interface.
authorgfawcett <gfawcett@6d9bc8c9-1ec2-4278-b937-99fde70a366f>
Tue, 28 Dec 2010 20:36:09 +0000 (20:36 +0000)
committergfawcett <gfawcett@6d9bc8c9-1ec2-4278-b937-99fde70a366f>
Tue, 28 Dec 2010 20:36:09 +0000 (20:36 +0000)
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
conifer/static/edit_site.js
conifer/static/fuzzyFinder.js [new file with mode: 0644]
conifer/syrup/urls.py
conifer/syrup/views/admin.py
conifer/templates/admin/staff_add.xhtml [new file with mode: 0644]
conifer/templates/edit_site.xhtml

index ec8960f..73c9f2d 100644 (file)
@@ -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
index 416d2c7..e925198 100644 (file)
@@ -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 = $('<a class="fuzzychoice" href="#"/>');
-                                          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('<div>and ' + data.notshown + ' more.</div>');
-                                  }
-                          }, '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 (file)
index 0000000..78ec8a4
--- /dev/null
@@ -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 = $('<a class="fuzzychoice" href="#"/>');
+                          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('<div>and ' + data.notshown + ' more.</div>');
+                      }
+                  }, '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;
+}
+    
index 62fcb2c..5026fec 100644 (file)
@@ -34,7 +34,7 @@ urlpatterns = patterns('conifer.syrup.views',
     (r'^site/(?P<site_id>\d+)/edit/permission/delete_group/$', 'edit_site_permissions_delete_group'),
     (r'^site/(?P<site_id>\d+)/feeds/(?P<feed_type>.*)$', 'site_feeds'),
     (r'^site/(?P<site_id>\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<filename>.*)$', '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'),
index 314b375..141458e 100644 (file)
@@ -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 (file)
index 0000000..6531e70
--- /dev/null
@@ -0,0 +1,51 @@
+<?python
+title = _('Add new staff user')
+?>
+<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"/>
+<head>
+  <title>${title}</title>
+  <script type="text/javascript" src="${ROOT}/static/fuzzyFinder.js"/>
+  <script>
+    $(function() {
+    var fuzzy = fuzzyFinder(
+    '#fuzzyinput', '#fuzzypanel', '#fuzzyedit', '#fuzzyview', '#fuzzyname', '#fuzzychange', '#userid');
+    });
+  </script>
+  <style>
+       .fuzzychoice { display: block; margin: 0.5em 0;  font-size: 90%; }
+       #fuzzyview { display: block; margin: 0.5em 0;  font-size: 90%; }
+       #message { background-color: #fdd; padding: 12; display: inline-block; }
+  </style>
+  <style type="text/css">
+    ul { margin-bottom: 2em; }
+  </style>
+</head>
+<body>
+  <h1>${title}</h1>
+  <p py:if="defined('message')">
+    <div id="message">${message}</div>
+    <span py:if="message_continue">${go_back_link('../../', 'Continue')}</span>
+  </p>
+  <div>
+    <div id="fuzzyedit" style="display: block">
+      <div style="font-size: 80%; margin: 0.5em 0;">Type a partial name or userid into the box; then select one of the matches.</div>
+      <input type="text" id="fuzzyinput" autocomplete="off" value=""/>
+      <div id="fuzzypanel">
+      </div>
+    </div>
+    <div id="fuzzyview" style="display: none;">
+      <span id="fuzzyname">
+      </span>
+      <input id="fuzzychange" type="button" value="change" onclick="fuzzyLookup.edit();"
+            style="margin-left: 1em;"/>
+      <form action="." method="POST">
+       <input type="hidden" id="userid" name="userid" value=""/>
+       <p><input type="submit" value="Add this user"/></p>
+      </form>
+    </div>
+  </div>
+</body>
+</html>
\ No newline at end of file
index dcd6b06..b14e174 100644 (file)
@@ -12,6 +12,7 @@ owner = instance.owner if instance.owner_id else None
 <xi:include href="components/site.xhtml"/>
 <head>
   <title>${title}</title>
+  <script type="text/javascript" src="${ROOT}/static/fuzzyFinder.js"/>
   <script type="text/javascript" src="${ROOT}/static/edit_site.js"/>
   <style>
        .fuzzychoice { display: block; margin: 0.5em 0;  font-size: 90%; }
@@ -58,8 +59,7 @@ owner = instance.owner if instance.owner_id else None
                          ${owner.get_full_name()} [${owner}]
                        </span>
                        </span>
-                       <input type="button" value="change" onclick="fuzzyLookup.edit();"
-                                        style="margin-left: 1em;"/>
+                       <input type="button" value="change" id="fuzzychange" style="margin-left: 1em;"/>
                  </div>
                </td>
          </tr>