Skip to content

Commit e57bb1e

Browse files
committed
Add example for matching owner and tag from holvi transaction data
The generic transaction matcher is still more useful for matching transactions created via API from recurrigtransaction callbacks.
1 parent 9a207e6 commit e57bb1e

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

project/examples/handlers.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ def __init__(self, *args, **kwargs):
7878
self.memberclass = Member
7979
self.try_methods = [
8080
self.import_generic_transaction,
81+
self.import_holvi_transaction,
8182
self.import_tmatch_transaction,
8283
]
8384
super().__init__(*args, **kwargs)
@@ -111,6 +112,40 @@ def import_transaction(self, at):
111112
# Nothing worked, return None
112113
return None
113114

115+
def import_holvi_transaction(self, at, lt):
116+
"""Try to find suitable owner and tag based on the invoice/order info"""
117+
if not at.email:
118+
# Not from holvi, those always have email
119+
msg = "No email set, cannot be from holvi"
120+
logger.info(msg)
121+
print(msg)
122+
return None
123+
try:
124+
lt.owner = self.memberclass.objects.get(email=at.email)
125+
except self.memberclass.DoesNotExist:
126+
msg = "No member with email %s" % at.email
127+
logger.info(msg)
128+
print(msg)
129+
return None
130+
131+
try:
132+
lt.tag = TransactionTag.objects.get(holvi_code=at.holvi_invoice.items[0].category.code)
133+
except (AttributeError, TransactionTag.DoesNotExist) as e:
134+
msg = "Got %s when trying to look for at.invoice.items[0].category.code" % repr(e)
135+
logger.info(msg)
136+
print(msg)
137+
try:
138+
# Triggers loading of the full product object so we can get the category, can be remove when https://github.com/rambo/python-holviapi/issues/14 is fixed
139+
tmp = at.holvi_order.purchases[0].product.name
140+
lt.tag = TransactionTag.objects.get(holvi_code=at.holvi_order.purchases[0].product.category.code)
141+
except (AttributeError, TransactionTag.DoesNotExist) as e:
142+
msg = "Got %s when trying to look for at.order.purchases[0].product.category.code" % repr(e)
143+
logger.info(msg)
144+
print(msg)
145+
return None
146+
lt.save()
147+
return lt
148+
114149
def import_generic_transaction(self, at, lt):
115150
"""Look for a transaction with same reference but oppsite value. If found use that for owner and tag"""
116151
qs = Transaction.objects.filter(reference=at.reference, amount=-at.amount).order_by('-stamp')

0 commit comments

Comments
 (0)