diff options
author | Jon Bergli Heier <snakebite@jvnv.net> | 2012-10-24 21:09:12 +0200 |
---|---|---|
committer | Jon Bergli Heier <snakebite@jvnv.net> | 2012-10-24 21:09:12 +0200 |
commit | d601b937bf6513ad171016c834a4bfc4e9605a46 (patch) | |
tree | 4129e25d4f98b13a6e6d3c300524622352ee00bc /modules | |
parent | 8d5bef90be1719a16b3bbe9ea01e51af82e38044 (diff) |
tracking: Properly update packages when the code changes.
This is done by looking for the previousPackageId, and setting the
previous_code attribute on the TrackingResult object. Both code and
previous_code is then queried, and the package code is updated to code
if it doesn't already match.
Diffstat (limited to 'modules')
-rw-r--r-- | modules/tracking.py | 30 |
1 files changed, 26 insertions, 4 deletions
diff --git a/modules/tracking.py b/modules/tracking.py index 4b308b0..aaa0304 100644 --- a/modules/tracking.py +++ b/modules/tracking.py @@ -79,8 +79,13 @@ class PackageError(Exception): pass class NoPackageFound(PackageError): pass class TrackingResult: - def __init__(self, code, date, desc, delivered = False): - self.code, self.date, self.desc, self.delivered = code, date, desc, delivered + def __init__(self, code, date, desc, delivered = False, previous_code = None): + self.code = code + self.date = date + self.desc = desc + self.delivered = delivered + # Set when a package changes tracking code. + self.previous_code = previous_code def __str__(self): return '\002{code}\002 {date} - {desc}'.format(code = self.code, date = self.date, desc = self.desc) @@ -130,6 +135,10 @@ class PostenModule(TrackingModule): results = [] for package in packages: code = package.attrib['packageId'] + if 'previousPackageId' in package.attrib: + previous_code = package.attrib['previousPackageId'] + else: + previous_code = None eventset = package.find('{%s}EventSet' % ns) if not len(eventset): continue @@ -143,7 +152,7 @@ class PostenModule(TrackingModule): if city: desc = '%s (%s)' % (desc, city.encode('utf8')) date = last.find('{%s}OccuredAtDisplayDate' % ns).text + ' ' + last.find('{%s}OccuredAtDisplayTime' % ns).text - results.append(TrackingResult(code, isodate, desc, status == 'DELIVERED')) + results.append(TrackingResult(code, isodate, desc, status == 'DELIVERED', previous_code = previous_code)) return results def code_split(code): @@ -428,7 +437,12 @@ class Module: package_results = [] for data in package_results: try: - package = session.query(Package).filter(and_(Package.consignment_id == consignment.id, Package.code == data.code)).one() + package = session.query(Package).filter(Package.consignment_id == consignment.id) + if data.previous_code: + package = package.filter(Package.code.in_((data.code, data.previous_code))) + else: + package = package.filter(Package.code == data.code) + package = package.one() except NoResultFound: # don't add delivered packages if data.delivered: @@ -445,6 +459,14 @@ class Module: break package = session.query(Package).filter(and_(Package.consignment_id == consignment.id, Package.code == data.code)).one() + # This happens when the package changes code. + if package.code != data.code: + # Always announce. + msg = '%s: %s has changed code to \002%s\002' % (consignment.nick.encode('utf-8'), package, data.code) + self.irc.msg(target, msg) + package.code = data.code + session.add(package) + if package.last == None or data.date > package.last: code = data.code last = data.date |