Skip to content

Commit 1019939

Browse files
mikejmetsxispa
authored andcommitted
Allow year in any portal type's ID format string
* Added KeyError trap on GenerateUniqueId to provide more detail * Allow year in all portal types in ID Server config * Included a test for batch id with a year in it in IDServer Conflicts: bika/lims/docs/IDServer.rst bika/lims/idserver.py
1 parent 9e69137 commit 1019939

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

bika/lims/idserver.py

+20-10
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,13 @@ def getConfigByPortalType(config_map, portal_type):
9494
variables_map = {
9595
'sampleId': context.getSample().getId(),
9696
'sample': context.getSample(),
97+
'year': DateTime().strftime("%Y")[2:],
9798
}
9899
elif portal_type == "SamplePartition":
99100
variables_map = {
100101
'sampleId': context.aq_parent.getId(),
101102
'sample': context.aq_parent,
103+
'year': DateTime().strftime("%Y")[2:],
102104
}
103105
elif portal_type == "Sample" and parent:
104106
config = getConfigByPortalType(
@@ -107,6 +109,7 @@ def getConfigByPortalType(config_map, portal_type):
107109
variables_map = {
108110
'sampleId': context.getId(),
109111
'sample': context,
112+
'year': DateTime().strftime("%Y")[2:],
110113
}
111114
elif portal_type == "Sample":
112115
sampleDate = None
@@ -128,7 +131,9 @@ def getConfigByPortalType(config_map, portal_type):
128131
'sequence_type': 'generated',
129132
'prefix': '%s' % portal_type.lower(),
130133
}
131-
variables_map = {}
134+
variables_map = {
135+
'year': DateTime().strftime("%Y")[2:],
136+
}
132137

133138
# Actual id construction starts here
134139
form = config['form']
@@ -137,15 +142,20 @@ def getConfigByPortalType(config_map, portal_type):
137142
context=variables_map[config['context']],
138143
config=config)
139144
elif config['sequence_type'] == 'generated':
140-
if config.get('split_length', None) == 0:
141-
prefix_config = '-'.join(form.split('-')[:-1])
142-
prefix = prefix_config.format(**variables_map)
143-
elif config.get('split_length', 0) > 0:
144-
prefix_config = '-'.join(form.split('-')[:config['split_length']])
145-
prefix = prefix_config.format(**variables_map)
146-
else:
147-
prefix = config['prefix']
148-
new_seq = number_generator(key=prefix)
145+
try:
146+
if config.get('split_length', None) == 0:
147+
prefix_config = '-'.join(form.split('-')[:-1])
148+
prefix = prefix_config.format(**variables_map)
149+
elif config.get('split_length', 0) > 0:
150+
prefix_config = '-'.join(form.split('-')[:config['split_length']])
151+
prefix = prefix_config.format(**variables_map)
152+
else:
153+
prefix = config['prefix']
154+
new_seq = number_generator(key=prefix)
155+
except KeyError, e:
156+
msg = "KeyError in GenerateUniqueId on %s: %s" % (
157+
str(config), e)
158+
raise RuntimeError(msg)
149159
variables_map['seq'] = new_seq + 1
150160
result = form.format(**variables_map)
151161
return result

bika/lims/tests/doctests/IDServer.rst

+13
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Functional Helpers:
5151
Variables:
5252

5353
>>> date_now = timestamp()
54+
>>> year = date_now.split('-')[0][2:]
5455
>>> sample_date = DateTime(2017, 1, 31)
5556
>>> portal = self.portal
5657
>>> request = self.request
@@ -140,6 +141,12 @@ Set up `ID Server` configuration:
140141
... 'portal_type': 'SamplePartition',
141142
... 'sequence_type': 'counter',
142143
... 'value': ''},
144+
... {'form': 'B{year}-{seq:04d}',
145+
... 'portal_type': 'Batch',
146+
... 'prefix': 'batch',
147+
... 'sequence_type': 'generated',
148+
... 'split_length': 1,
149+
... 'value': ''},
143150
... ]
144151

145152
>>> bika_setup.setIDFormatting(values)
@@ -191,6 +198,12 @@ Create a third `AnalysisRequest` with existing sample:
191198
>>> ar
192199
<...water17-0002-R2>
193200

201+
Create a forth `Batch`::
202+
>>> batches = self.portal.batches
203+
>>> batch = api.create(batches, "Batch", ClientID="RB")
204+
>>> batch.getId() == "B{}-0001".format(year)
205+
True
206+
194207
Change ID formats and create new `AnalysisRequest`:
195208

196209
>>> values = [{'form': '{clientId}-{sampleDate:%Y%m%d}-{sampleType}-{seq:04d}',

0 commit comments

Comments
 (0)