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