Now have functional cmd_move_sample!
authorBrandon King <kingb@caltech.edu>
Thu, 30 Apr 2009 20:57:37 +0000 (20:57 +0000)
committerBrandon King <kingb@caltech.edu>
Thu, 30 Apr 2009 20:57:37 +0000 (20:57 +0000)
samplebc/samples/views.py
templates/app.html
www/js/magicbc.js

index 45d136fbe40a1494f9d927ef702763e5a36ce005..d1e6c3e8436a05dfc7657f56a2f543ed9a492b2d 100644 (file)
@@ -704,16 +704,14 @@ def user_profile(request):
 ################################################
 # Barcode Magic Commands
 ################################################
-@login_required
-def cmd_move_sample(request):
+
+def __cmd_move_sample_return(form, request, msg=''):
     """
-    Moves a sample to a target container
+    A small helper function since this code is reused multiple times
     """
-    form = CmdMoveSampleForm()
-    
     # Load form template
     c = Context({'form': form,
-                 'action_url': ''})
+                 'action_url': '.'})
     t = get_template('generic_form.html')
     
     return render_to_response('app.html', {
@@ -722,6 +720,54 @@ def cmd_move_sample(request):
                                 'media': '',
                                 'bcmagic': BarcodeMagicForm({'bcm_mode': 'cmd_move_sample'}),
                                 'select': 'samples',
+                                'msg': msg,
                                 'body': t.render(c)
                             },
-                              context_instance=RequestContext(request))
\ No newline at end of file
+                              context_instance=RequestContext(request))
+
+@login_required
+@revision.create_on_success
+def cmd_move_sample(request):
+    """
+    Moves a sample to a target container
+    """
+    form = CmdMoveSampleForm()
+    # Process form:
+    if request.method == 'POST':
+        form = CmdMoveSampleForm(request.POST)
+        
+        #If for some reason the form is not valid, return
+        if not form.is_valid():
+            msg = "Form is not valid."
+            return __cmd_move_sample_return(form, request, msg)
+        
+        try:
+            sample = models.Sample.objects.get(sampleid=form.cleaned_data['sampleid'])
+        except ObjectDoesNotExist, e:
+            msg = "Sample %s does not exist." % (form.cleaned_data['sampleid'])
+            return __cmd_move_sample_return(form, request, msg)
+            
+        try:
+            container = models.Container.objects.get(uuid=form.cleaned_data['container_id'])
+        except ObjectDoesNotExist, e:
+            msg = "Container %s does not exist." % (form.cleaned_data['container_id'])
+            return __cmd_move_sample_return(form, request, msg)
+        
+        # Move the sample to the new container
+        old_container = sample.container
+        sample.container = container
+        if old_container is None:
+            revision.comment = "Sample (%s) moved from Homeless to Container(%s; uuid: %s)" \
+                               % (str(sample), str(container), container.uuid)
+        else:
+            revision.comment = "Sample (%s) moved from Container(%s; uuid: %s) to Container(%s; uuid: %s)" \
+                               % (str(sample), str(old_container), old_container.uuid, str(container), container.uuid)
+        sample.save()
+        
+        return __cmd_move_sample_return(CmdMoveSampleForm(), request, revision.comment)
+        
+    return __cmd_move_sample_return(form, request)
+        
+        
+    
+    
\ No newline at end of file
index 41053f5e37b74b79b37c31258ea4e3f33a480281..41190aa07fbff640e9c41ebfa92d4cece72a8964 100644 (file)
@@ -66,6 +66,9 @@
         {% include "magic.html" %}
     </div>
     <div id="main" class="main">
+    {% block msg %}
+    <div class="msg">{{ msg }}</div>
+    {% endblock %}
     {% block content %}
         {{ body }}
     {% endblock %}
index b7cdae1b28298b07b1d928632cdf717a79d613b0..2044193f17e4fe14a8627325e0882da853aeadae 100644 (file)
@@ -126,6 +126,10 @@ var bcmagic_autofill = function(field, val)
     if (count == input_fields.length)
     {
         bcmagic_status('Form Full', 'Form is now full and ready to process');
+        form = $('form');
+        form.submit();
+        form.reset();
+    
     }
     else
     {