From 685b595f7eda6451324d3951ac51b23d89f985e5 Mon Sep 17 00:00:00 2001 From: Brandon King Date: Thu, 30 Apr 2009 20:57:37 +0000 Subject: [PATCH] Now have functional cmd_move_sample! --- samplebc/samples/views.py | 60 ++++++++++++++++++++++++++++++++++----- templates/app.html | 3 ++ www/js/magicbc.js | 4 +++ 3 files changed, 60 insertions(+), 7 deletions(-) diff --git a/samplebc/samples/views.py b/samplebc/samples/views.py index 45d136f..d1e6c3e 100644 --- a/samplebc/samples/views.py +++ b/samplebc/samples/views.py @@ -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 diff --git a/templates/app.html b/templates/app.html index 41053f5..41190aa 100644 --- a/templates/app.html +++ b/templates/app.html @@ -66,6 +66,9 @@ {% include "magic.html" %}
+ {% block msg %} +
{{ msg }}
+ {% endblock %} {% block content %} {{ body }} {% endblock %} diff --git a/www/js/magicbc.js b/www/js/magicbc.js index b7cdae1..2044193 100644 --- a/www/js/magicbc.js +++ b/www/js/magicbc.js @@ -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 { -- 2.30.2