-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSSOT.py
91 lines (81 loc) · 3.69 KB
/
SSOT.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from HSBCBusiness import HSBCBusiness
from GoCardless import GoCardless
from TransactionGatewayAbstract import TransactionGatewayAbstract
from TransactionGatewayAbstract import PartnerGatewayAbstract
class SSOT(TransactionGatewayAbstract, PartnerGatewayAbstract):
""" Single Source Of Truth (SSOT) transaction gateway.
Merges other gateway transactions, and partner gateways
"""
def __init__(self, target_gateways=None, refresh=False, **kwargs):
self.target_gateways = target_gateways
self.loaded_gateways = []
self.transactions = []
self.partners = []
self.init(refresh=refresh)
@staticmethod
def get_name():
return "Single Source of Truth"
@staticmethod
def get_short_name():
return "SSOT"
def init(self, **kwargs):
if self.target_gateways is None:
self.HSBC = HSBCBusiness()
self.HSBC.fetchTransactions()
self.HSBC.fetchPartners()
self.loaded_gateways.append('HSBC')
self.GC = GoCardless()
self.GC.fetchTransactions()
self.GC.fetchPartners()
self.loaded_gateways.append('GC')
else:
for gateway in self.target_gateways:
gatewayName = gateway['name']
gatewayModule = __import__(gatewayName)
gatewayClass = getattr(gatewayModule,gatewayName)
shortName = gatewayClass.get_short_name()
gatewayInstance = gatewayClass(gateway['construct'])
gatewayInstance.fetchTransactions(refresh=kwargs.pop('refresh',False))
gatewayInstance.fetchPartners(refresh=kwargs.pop('refresh', False))
# Dynamically runs: from <gatewayName> import <gatewayClass>
# and than adds <gatewayClass> instance as property to SSOT
setattr(self, shortName, gatewayInstance)
self.loaded_gateways.append(shortName)
# Finally Join SSOT Partners & Transactions from all loaded gateways
self.fetchTransactions()
self.fetchPartners()
def fetchTransactions(self):
if self.target_gateways is None:
self.transactions = self.HSBC.transactions + self.GC.transactions
else:
for gateway in self.loaded_gateways:
gatewayTransactions = self.__getattribute__(gateway).transactions
self.transactions = self.transactions + gatewayTransactions
def fetchPartners(self):
if self.target_gateways is None:
self.partners = self.HSBC.partners + self.GC.partners
else:
for gateway in self.loaded_gateways:
gatewayPartners = self.__getattribute__(gateway).partners
self.partners = self.partners + gatewayPartners
def filterby(self, source_gateway=None, source_id=None, reference=None, *args, **kwargs):
if source_gateway:
try:
recordType = kwargs['recordType']
if recordType is "Partner":
return filter(lambda x: source_gateway in x.source_gateway, self.partners)
except KeyError:
pass
return filter(lambda x: source_gateway in x.source_gateway, self.transactions)
if reference:
return filter(lambda x: reference in x.reference, self.transactions)
#Fallback return all
return self.transactions
def fuzzygroup(self):
matches = {}
for transaction in self.transactions:
if transaction.reference in matches:
matches[transaction.reference].append(transaction)
else:
matches[transaction.reference] = [transaction]
return matches