1 from htsworkflow.frontend.inventory.models import Item, LongTermStorage
2 from htsworkflow.frontend.experiments.models import FlowCell
3 from htsworkflow.frontend.bcmagic.forms import BarcodeMagicForm
5 from django.core.exceptions import ObjectDoesNotExist
6 from django.http import HttpResponse, HttpResponseRedirect
7 from django.shortcuts import render_to_response
8 from django.template import RequestContext
9 from django.template.loader import get_template
10 from django.contrib.auth.decorators import login_required
14 INVENTORY_CONTEXT_DEFAULTS = {
15 'app_name': 'Inventory Tracker',
16 'bcmagic': BarcodeMagicForm()
19 def data_items(request):
21 Returns items in json format
23 item_list = Item.objects.all()
24 d = { 'results': len(item_list) }
27 for item in item_list:
29 item_d['uuid'] = item.uuid
30 item_d['barcode_id'] = item.barcode_id
31 item_d['creation_date'] = item.creation_date.strftime('%Y-%m-%d %H:%M:%S')
32 item_d['modified_date'] = item.modified_date.strftime('%Y-%m-%d %H:%M:%S')
33 item_d['type'] = item.item_type.name
38 j = json.JSONEncoder()
39 return HttpResponse(j.encode(d), content_type="application/javascript")
46 'page_name': 'Inventory Index'
48 context_dict.update(INVENTORY_CONTEXT_DEFAULTS)
50 return render_to_response('inventory_index.html',
52 context_instance=RequestContext(request))
55 def link_flowcell_and_device(request, flowcell, serial):
57 Updates database records of a flowcell being archived on a device with a particular serial #
59 assert flowcell is not None
60 assert serial is not None
64 LIBRARY_UPDATED = False
66 ###########################################
67 # Retrieve Storage Device
69 sd = Item.objects.get(barcode_id=serial)
70 except ObjectDoesNotExist, e:
71 msg = "Item with barcode_id of %s not found." % (serial)
72 raise ObjectDoesNotExist(msg)
74 ###########################################
77 fc = FlowCell.objects.get(flowcell_id=flowcell)
78 except ObjectDoesNotExist, e:
79 msg = "FlowCell with flowcell_id of %s not found." % (flowcell)
80 raise ObjectDoesNotExist(msg)
82 ###########################################
83 # Retrieve or create LongTermStorage Object
84 count = fc.longtermstorage_set.count()
87 msg = "There really should only be one longtermstorage object per flowcell"
90 # lts already attached to flowcell
91 lts = fc.longtermstorage_set.all()[0]
93 lts = LongTermStorage()
96 # Need a primary keey before linking to storage devices
101 ############################################
102 # Link Storage to Flowcell
104 # Add a link to this storage device if it is not already linked.
105 if sd not in lts.storage_devices.all():
106 lts.storage_devices.add(sd)
109 ###########################################
110 # Add Library Links to LTS
112 if fc.lane_1_library not in lts.libraries.all():
113 lts.libraries.add(fc.lane_1_library)
114 LIBRARY_UPDATED = True
117 if fc.lane_2_library not in lts.libraries.all():
118 lts.libraries.add(fc.lane_2_library)
119 LIBRARY_UPDATED = True
122 if fc.lane_3_library not in lts.libraries.all():
123 lts.libraries.add(fc.lane_3_library)
124 LIBRARY_UPDATED = True
127 if fc.lane_4_library not in lts.libraries.all():
128 lts.libraries.add(fc.lane_4_library)
129 LIBRARY_UPDATED = True
133 if fc.lane_5_library not in lts.libraries.all():
134 lts.libraries.add(fc.lane_5_library)
135 LIBRARY_UPDATED = True
138 if fc.lane_6_library not in lts.libraries.all():
139 lts.libraries.add(fc.lane_6_library)
140 LIBRARY_UPDATED = True
143 if fc.lane_7_library not in lts.libraries.all():
144 lts.libraries.add(fc.lane_7_library)
145 LIBRARY_UPDATED = True
148 if fc.lane_8_library not in lts.libraries.all():
149 lts.libraries.add(fc.lane_8_library)
150 LIBRARY_UPDATED = True
157 if LTS_UPDATED or SD_UPDATED or LIBRARY_UPDATED:
158 msg.append(' LongTermStorage (LTS) Created: %s' % (LTS_UPDATED))
159 msg.append(' Storage Device Linked to LTS: %s' % (SD_UPDATED))
160 msg.append(' Libraries updated in LTS: %s' % (LIBRARY_UPDATED))
162 msg.append(' No Updates Needed.')
164 return HttpResponse('\n'.join(msg))