Import of sampletracker.bzr revno 42! What a great revision number to migrate to...
[htsworkflow.git] / samplebc / samples / containers.py
1 from samplebc.samples.models import Container, Sample
2 import types
3
4 def __get_container(string_or_container):
5     """
6     returns a container object given container object or
7     container name string.
8     """
9     if isinstance(string_or_container, Container):
10         return string_or_container
11     elif isinstance(string_or_container, types.StringType):
12         return Container.objects.get(name=string_or_container
13 )
14     raise ValueError, 'Invalid type: %s' % (string_or_container)
15     
16
17 def item_count(string_or_container):
18     """
19     Returns the number of items in a container given container
20     object or string.
21     """
22     container_obj = __get_container(string_or_container)   
23     return len(Sample.objects.filter(container__name=container_obj.name))
24     
25     
26 def available_space(string_or_container):
27     """
28     Returns the available space in a container by passing in either
29     the container object or the name of the container.
30     """
31     count = item_count(string_or_container)
32     container_obj = __get_container(string_or_container)
33     
34     return (container_obj.max_items - count)