3c72d207590e4e03c1b3a22bd41097870730b962
[htsworkflow.git] / htsworkflow / frontend / inventory / views.py
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
4 from htsworkflow.util.jsonutil import encode_json
5
6 from django.core.exceptions import ObjectDoesNotExist
7 from django.http import HttpResponse, HttpResponseRedirect
8 from django.shortcuts import render_to_response
9 from django.template import RequestContext
10 from django.template.loader import get_template
11 from django.contrib.auth.decorators import login_required
12
13
14
15 INVENTORY_CONTEXT_DEFAULTS = {
16     'app_name': 'Inventory Tracker',
17     'bcmagic': BarcodeMagicForm()
18 }
19
20 def data_items(request):
21     """
22     Returns items in json format
23     """
24     item_list = Item.objects.all()
25     d = { 'results': len(item_list) }
26     rows = []
27     
28     for item in item_list:
29         item_d = {}
30         item_d['uuid'] = item.uuid
31         item_d['barcode_id'] = item.barcode_id
32         item_d['model_id'] = item.item_info.model_id
33         item_d['part_number'] = item.item_info.part_number
34         item_d['lot_number'] = item.item_info.lot_number
35         item_d['vendor'] = item.item_info.vendor.name
36         item_d['creation_date'] = item.creation_date.strftime('%Y-%m-%d %H:%M:%S')
37         item_d['modified_date'] = item.modified_date.strftime('%Y-%m-%d %H:%M:%S')
38         item_d['location'] = item.location.name
39         
40         # Item status if exists
41         if item.status is None:
42             item_d['status'] = ''
43         else:
44             item_d['status'] = item.status.name
45             
46         # Stored flowcells on device
47         if item.longtermstorage_set.count() > 0:
48             item_d['flowcells'] = ','.join([ lts.flowcell.flowcell_id for lts in item.longtermstorage_set.all() ])
49         else:
50             item_d['flowcells'] = ''
51         
52         item_d['type'] = item.item_type.name
53         rows.append(item_d)
54     
55     d['rows'] = rows
56     
57     return HttpResponse(encode_json(d), content_type="application/javascript")
58
59
60 def index(request):
61     """
62     Inventory Index View
63     """
64     context_dict = {
65         'page_name': 'Inventory Index'
66     }
67     context_dict.update(INVENTORY_CONTEXT_DEFAULTS)
68     
69     return render_to_response('inventory_index.html',
70                               context_dict,
71                               context_instance=RequestContext(request))
72     
73
74 def link_flowcell_and_device(request, flowcell, serial):
75     """
76     Updates database records of a flowcell being archived on a device with a particular serial #
77     """
78     assert flowcell is not None
79     assert serial is not None
80     
81     LTS_UPDATED = False
82     SD_UPDATED = False
83     LIBRARY_UPDATED = False
84         
85     ###########################################
86     # Retrieve Storage Device
87     try:
88         sd = Item.objects.get(barcode_id=serial)
89     except ObjectDoesNotExist, e:
90         msg = "Item with barcode_id of %s not found." % (serial)
91         raise ObjectDoesNotExist(msg)
92     
93     ###########################################
94     # Retrieve FlowCell
95     try:    
96         fc = FlowCell.objects.get(flowcell_id=flowcell)
97     except ObjectDoesNotExist, e:
98         msg = "FlowCell with flowcell_id of %s not found." % (flowcell)
99         raise ObjectDoesNotExist(msg)
100     
101     ###########################################
102     # Retrieve or create LongTermStorage Object
103     count = fc.longtermstorage_set.count()
104     lts = None
105     if count > 1:
106         msg = "There really should only be one longtermstorage object per flowcell"
107         raise ValueError, msg
108     elif count == 1:
109         # lts already attached to flowcell
110         lts = fc.longtermstorage_set.all()[0]
111     else:
112         lts = LongTermStorage()
113         # Attach flowcell
114         lts.flowcell = fc
115         # Need a primary keey before linking to storage devices
116         lts.save()
117         LTS_UPDATED = True
118         
119         
120     ############################################
121     # Link Storage to Flowcell
122     
123     # Add a link to this storage device if it is not already linked.
124     if sd not in lts.storage_devices.all():
125         lts.storage_devices.add(sd)
126         SD_UPDATED = True
127     
128     ###########################################
129     # Add Library Links to LTS
130     
131     if fc.lane_1_library not in lts.libraries.all():
132         lts.libraries.add(fc.lane_1_library)
133         LIBRARY_UPDATED = True
134         print 1
135     
136     if fc.lane_2_library not in lts.libraries.all():
137         lts.libraries.add(fc.lane_2_library)
138         LIBRARY_UPDATED = True
139         print 2
140     
141     if fc.lane_3_library not in lts.libraries.all():
142         lts.libraries.add(fc.lane_3_library)
143         LIBRARY_UPDATED = True
144         print 3
145     
146     if fc.lane_4_library not in lts.libraries.all():
147         lts.libraries.add(fc.lane_4_library)
148         LIBRARY_UPDATED = True
149         print 4
150     
151     
152     if fc.lane_5_library not in lts.libraries.all():
153         lts.libraries.add(fc.lane_5_library)
154         LIBRARY_UPDATED = True
155         print 5
156     
157     if fc.lane_6_library not in lts.libraries.all():
158         lts.libraries.add(fc.lane_6_library)
159         LIBRARY_UPDATED = True
160         print 6
161     
162     if fc.lane_7_library not in lts.libraries.all():
163         lts.libraries.add(fc.lane_7_library)
164         LIBRARY_UPDATED = True
165         print 7
166     
167     if fc.lane_8_library not in lts.libraries.all():
168         lts.libraries.add(fc.lane_8_library)
169         LIBRARY_UPDATED = True
170         print 8
171         
172     # Save Changes
173     lts.save()
174     
175     msg = ['Success:']
176     if LTS_UPDATED or SD_UPDATED or LIBRARY_UPDATED:
177         msg.append('  LongTermStorage (LTS) Created: %s' % (LTS_UPDATED))
178         msg.append('   Storage Device Linked to LTS: %s' % (SD_UPDATED))
179         msg.append('       Libraries updated in LTS: %s' % (LIBRARY_UPDATED))
180     else:
181         msg.append('  No Updates Needed.')
182     
183     return HttpResponse('\n'.join(msg))