Start of inventory app page.
[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
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
11
12 import json
13
14 INVENTORY_CONTEXT_DEFAULTS = {
15     'app_name': 'Inventory Tracker',
16     'bcmagic': BarcodeMagicForm()
17 }
18
19 def data_items(request):
20     """
21     Returns items in json format
22     """
23     item_list = Item.objects.all()
24     d = { 'results': len(item_list) }
25     rows = []
26     
27     for item in item_list:
28         item_d = {}
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
34         rows.append(item_d)
35     
36     d['rows'] = rows
37     
38     j = json.JSONEncoder()
39     return HttpResponse(j.encode(d), content_type="application/javascript")
40
41 def index(request):
42     """
43     Inventory Index View
44     """
45     context_dict = {
46         'page_name': 'Inventory Index'
47     }
48     context_dict.update(INVENTORY_CONTEXT_DEFAULTS)
49     
50     return render_to_response('inventory_index.html',
51                               context_dict,
52                               context_instance=RequestContext(request))
53     
54
55 def link_flowcell_and_device(request, flowcell, serial):
56     """
57     Updates database records of a flowcell being archived on a device with a particular serial #
58     """
59     assert flowcell is not None
60     assert serial is not None
61     
62     LTS_UPDATED = False
63     SD_UPDATED = False
64     LIBRARY_UPDATED = False
65         
66     ###########################################
67     # Retrieve Storage Device
68     try:
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)
73     
74     ###########################################
75     # Retrieve FlowCell
76     try:    
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)
81     
82     ###########################################
83     # Retrieve or create LongTermStorage Object
84     count = fc.longtermstorage_set.count()
85     lts = None
86     if count > 1:
87         msg = "There really should only be one longtermstorage object per flowcell"
88         raise ValueError, msg
89     elif count == 1:
90         # lts already attached to flowcell
91         lts = fc.longtermstorage_set.all()[0]
92     else:
93         lts = LongTermStorage()
94         # Attach flowcell
95         lts.flowcell = fc
96         # Need a primary keey before linking to storage devices
97         lts.save()
98         LTS_UPDATED = True
99         
100         
101     ############################################
102     # Link Storage to Flowcell
103     
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)
107         SD_UPDATED = True
108     
109     ###########################################
110     # Add Library Links to LTS
111     
112     if fc.lane_1_library not in lts.libraries.all():
113         lts.libraries.add(fc.lane_1_library)
114         LIBRARY_UPDATED = True
115         print 1
116     
117     if fc.lane_2_library not in lts.libraries.all():
118         lts.libraries.add(fc.lane_2_library)
119         LIBRARY_UPDATED = True
120         print 2
121     
122     if fc.lane_3_library not in lts.libraries.all():
123         lts.libraries.add(fc.lane_3_library)
124         LIBRARY_UPDATED = True
125         print 3
126     
127     if fc.lane_4_library not in lts.libraries.all():
128         lts.libraries.add(fc.lane_4_library)
129         LIBRARY_UPDATED = True
130         print 4
131     
132     
133     if fc.lane_5_library not in lts.libraries.all():
134         lts.libraries.add(fc.lane_5_library)
135         LIBRARY_UPDATED = True
136         print 5
137     
138     if fc.lane_6_library not in lts.libraries.all():
139         lts.libraries.add(fc.lane_6_library)
140         LIBRARY_UPDATED = True
141         print 6
142     
143     if fc.lane_7_library not in lts.libraries.all():
144         lts.libraries.add(fc.lane_7_library)
145         LIBRARY_UPDATED = True
146         print 7
147     
148     if fc.lane_8_library not in lts.libraries.all():
149         lts.libraries.add(fc.lane_8_library)
150         LIBRARY_UPDATED = True
151         print 8
152         
153     # Save Changes
154     lts.save()
155     
156     msg = ['Success:']
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))
161     else:
162         msg.append('  No Updates Needed.')
163     
164     return HttpResponse('\n'.join(msg))