Skip to content

Commit

Permalink
the code to implement Bauble/bauble.classic#59
Browse files Browse the repository at this point in the history
still hard coded format, but not any more hard coded logic.
  • Loading branch information
mfrasca committed Jan 27, 2016
1 parent afc2d40 commit 35a52ae
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions bauble/plugins/garden/accession.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,21 @@ def get_next_code():
"""
# auto generate/increment the accession code
session = db.Session()
year = str(datetime.date.today().year)
start = '%s%s' % (year, Plant.get_delimiter())
format = '%Y%PD####'
format = format.replace('%PD', Plant.get_delimiter())
format = datetime.date.today().strftime(format)
start = format.rstrip('#')
digits = len(format) - len(start)
format = start + '%%0%dd' % digits
q = session.query(Accession.code).\
filter(Accession.code.startswith(start))
next = None
try:
if q.count() > 0:
codes = [int(code[0][len(start):]) for code in q]
next = '%s%s' % (start, str(max(codes)+1).zfill(4))
codes = [int(row[0][len(start):]) for row in q]
next = format % (max(codes)+1)
else:
next = '%s%s0001' % (datetime.date.today().year,
Plant.get_delimiter())
next = format % 1
except Exception, e:
logger.debug(e)
pass
Expand Down

0 comments on commit 35a52ae

Please sign in to comment.