Commits

Luke Plant committed 74bf34d Merge

Merged from default

Comments (0)

Files changed (3)

cciw/officers/views.py

 
     return camps.distinct()
 
+def close_window_and_update_ref(ref_id):
+    """
+    HttpResponse that closes the current window, and updates the reference
+    in the parent window. Applies to popup from manage_references view.
+    """
+    return HttpResponse("""<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
+            "http://www.w3.org/TR/html4/loose.dtd"><html><head><title>Close</title><script type="text/javascript">window.opener.refreshReferenceSection(%s); window.close()</script></head><body></body></html>""" % ref_id)
+
 
 # /officers/
 @staff_member_required
 @camp_admin_required # we don't care which camp they are admin for.
 @never_cache
 def manage_references(request, year=None, number=None):
-    camp = _get_camp_or_404(year, number)
+    c = template.RequestContext(request)
 
-    c = template.RequestContext(request)
-    c['camp'] = camp
+    # If ref_id is set, we just want to update part of the page.
+    ref_id = request.GET.get('ref_id')
 
-    apps = camp.application_set.filter(finished=True)
-    # force creation of Reference objects.
-    if Reference.objects.filter(application__finished=True,
-                                application__camp=camp).count() < apps.count() * 2:
-        [a.references for a in apps]
+    if ref_id is None:
+        camp = _get_camp_or_404(year, number)
+        c['camp'] = camp
 
-    # TODO - check for case where user has submitted multiple application forms.
-    # User.objects.all().filter(application__camp=camp).annotate(num_applications=models.Count('application')).filter(num_applications__gt=1)
+        apps = camp.application_set.filter(finished=True)
+        # force creation of Reference objects.
+        if Reference.objects.filter(application__finished=True,
+                                    application__camp=camp).count() < apps.count() * 2:
+            [a.references for a in apps]
 
-    refinfo = Reference.objects\
-              .filter(application__camp=camp, application__finished=True)\
-              .order_by('application__officer__first_name', 'application__officer__last_name',
-                        'referee_number')
+            # TODO - check for case where user has submitted multiple application forms.
+            # User.objects.all().filter(application__camp=camp).annotate(num_applications=models.Count('application')).filter(num_applications__gt=1)
+
+        refinfo = Reference.objects\
+            .filter(application__camp=camp, application__finished=True)\
+            .order_by('application__officer__first_name', 'application__officer__last_name',
+                      'referee_number')
+
+    else:
+        refinfo = Reference.objects.filter(pk=ref_id).order_by()
+
     received = refinfo.filter(received=True)
     requested = refinfo.filter(received=False, requested=True)
     notrequested = refinfo.filter(received=False, requested=False)
             else:
                 curref.possible_previous_references = prev
 
-    c['notrequested'] = notrequested
-    c['requested'] = requested
-    c['received'] = received
+    if ref_id is None:
+        c['notrequested'] = notrequested
+        c['requested'] = requested
+        c['received'] = received
+        template_name = 'cciw/officers/manage_references.html'
+    else:
+        c['mode'] = 'notrequested' if notrequested else \
+            ('requested' if requested else 'received')
+        c['ref'] = refinfo[0]
+        template_name = 'cciw/officers/manage_reference.html'
 
-    return render_to_response('cciw/officers/manage_references.html',
-                              context_instance=c)
+    return render_to_response(template_name, context_instance=c)
 
 
 def email_sending_failed_response():
                 ref.requested = True
                 ref.log_request_made(request.user, datetime.datetime.now())
                 ref.save()
-                return close_window_response()
+                return close_window_and_update_ref(ref_id)
         elif 'setemail' in request.POST:
             emailform = SetEmailForm(request.POST)
             if emailform.is_valid():
             form = ReferenceEditForm(request.POST, instance=ref)
             if form.is_valid():
                 form.save()
-                return close_window_response()
+                return close_window_and_update_ref(ref.id)
         else:
             return close_window_response()
     else:

templates/cciw/officers/manage_reference.html

 {% load url from future %}
+<div id="officerref{{ ref.id }}">
 <h3 class="referencestart">{{ ref.application.officer.first_name }} {{ ref.application.officer.last_name }}</h3>
 <h4>From: {{ ref.referee.name }}</h4>
+
+<div class="referencemode" style="display:none;">{{ mode }}</div>
 <div class="reference">
 {% if ref.received %}
-<div class="good"><strong>Received</strong></div>
+  <div class="good"><strong>Received</strong></div>
 {% else %}
-{% if ref.requested %}
-<div class="ok"><strong>Requested</strong></div>
-  {% if ref.last_requested %}
-  <div>Last requested: <b> {{ ref.last_requested|timesince }} ago</b></div>
+  {% if ref.requested %}
+    <div class="ok"><strong>Requested</strong></div>
+    {% if ref.last_requested %}
+      <div>Last requested: <b> {{ ref.last_requested|timesince }} ago</b></div>
+    {% endif %}
+  {% else %}
+    <div><strong>Not requested</strong></div>
   {% endif %}
-{% else %}
-<div><strong>Not requested</strong></div>
-{% endif %}
 {% endif %}
 
 {% if mode == "received" %}
 
 </div>
 <p>If you need to amend details of the referee, <a href="/admin/officers/application/{{ ref.application_id }}/" rel="external">view/edit officer's application form (in new window)</a></p>
+</div>

templates/cciw/officers/manage_references.html

 {% block title %}Manage references :: CCIW{% endblock %}
 {% block extrastyle %}{{ block.super }}
 <script type="text/javascript">
-<!--
+//<![CDATA[
 
 function requestGeneric(query) {
     window.open('{% url "cciw.officers.views.request_reference" %}?' + query,
                 '_blank',"toolbar=yes,height=600,width=900,location=yes,menubar=yes,scrollbars=yes,resizable=yes");
 }
 
+function refreshReferenceSection(ref_id) {
+    jQuery.ajax({
+        type: "GET",
+        url: "?ref_id=" + ref_id.toString(),
+        dataType: 'text',
+        success: function(data, textStatus, xhr) {
+                     // Replace existing div and move to correct place,
+                     // which might be under a different 'mode'
+                     var section = jQuery('#officerref' + ref_id.toString());
+                     var officerName = section.find("h3.referencestart").text();
+                     var mode = jQuery(data).find('div.referencemode').text();
+
+                     // Look through other officer names in same section
+                     var allOfficerNames = jQuery('#' + mode + 'refs h3.referencestart');
+                     var dest = null;
+                     allOfficerNames.each(function(i, elem) {
+                         if (officerName < elem.innerHTML) {
+                             // parent() depends on 'h3' being immediate child
+                             // of the element we want:
+                             dest = jQuery(elem).parent();
+                             return false;
+                         };
+                     });
+                     if (!dest) {
+                         dest = jQuery('#' + mode + 'end');
+                     }
+
+                     section.fadeOut('slow', function() {
+                         section.insertBefore(dest);
+                         // Don't use 'replaceWith' so we can control animation.
+                         section.html(jQuery(data).html());
+                         section.fadeIn('slow');
+                    });
+                }
+    });
+}
+
 function requestReference(ref_id) {
     requestGeneric('ref_id=' + ref_id.toString());
 }
 }
 
 
-//-->
+//]]>
 </script>
 {% endblock %}
 
 
 <h2>References to be requested</h2>
 
+<div id="notrequestedrefs">
 {% for ref in notrequested %}
 
 {% with "notrequested" as mode %}
 {% empty %}
 <p>No outstanding references to be requested.</p>
 {% endfor %}
+<div id="notrequestedend"></div>
+</div>
 
 <h2 class="ok">References requested</h2>
 
+<div id="requestedrefs">
 {% for ref in requested %}
 
 {% with "requested" as mode %}
 {% empty %}
 <p>No references requested.</p>
 {% endfor %}
+<div id="requestedend"></div>
+</div>
 
 <h2 class="good">References received</h2>
 
+<div id="receivedrefs">
 {% for ref in received %}
 
 {% with "received" as mode %}
 {% empty %}
 <p>No references received.</p>
 {% endfor %}
+<div id="receivedend"></div>
+</div>
 
 </div>
 {% endblock %}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.