|
| 1 | +""" |
| 2 | + The service for connecting to MemberSuite for SubscriptionService |
| 3 | +
|
| 4 | + http://api.docs.membersuite.com/#References/Objects/Subscription.htm |
| 5 | +
|
| 6 | + @todo |
| 7 | + let's define an Organization object, like this Subscription, |
| 8 | + for this interface |
| 9 | + @todo |
| 10 | + confirm owner field is actually the orgnization |
| 11 | + @todo |
| 12 | + set up fixtures in MemberSuite for integration testing |
| 13 | + @todo |
| 14 | + add date modified param for performance |
| 15 | + @todo |
| 16 | + additional method for getting all subscriptions for syncing purposes |
| 17 | +""" |
| 18 | + |
| 19 | +from .models import Subscription |
| 20 | + |
| 21 | + |
| 22 | +class SubscriptionService(object): |
| 23 | + |
| 24 | + def __init__(self, client): |
| 25 | + """ |
| 26 | + Accepts a ConciergeClient to connect with MemberSuite |
| 27 | + """ |
| 28 | + self.client = client |
| 29 | + |
| 30 | + def get_org_subscriptions(self, org_id, publication_id=None): |
| 31 | + """ |
| 32 | + Get all the subscriptions for a given organization |
| 33 | +
|
| 34 | + Returns a list of subscription objects |
| 35 | + """ |
| 36 | + query = "SELECT Objects() FROM Subscription" |
| 37 | + query += " WHERE owner = '%s'" % org_id |
| 38 | + |
| 39 | + if publication_id: |
| 40 | + query += "AND publication = '%s'" % publication_id |
| 41 | + |
| 42 | + result = self.client.runSQL(query) |
| 43 | + mysql_result = result['body']['ExecuteMSQLResult'] |
| 44 | + |
| 45 | + if not mysql_result['Errors']: |
| 46 | + obj_result = mysql_result['ResultValue']['ObjectSearchResult'] |
| 47 | + objects = obj_result['Objects']['MemberSuiteObject'] |
| 48 | + |
| 49 | + subscription_list = [] |
| 50 | + for obj in objects: |
| 51 | + sane_obj = self.client.convert_ms_object( |
| 52 | + obj['Fields']['KeyValueOfstringanyType']) |
| 53 | + subscription = Subscription( |
| 54 | + id=sane_obj['ID'], |
| 55 | + org=org_id, |
| 56 | + start=sane_obj['StartDate'], |
| 57 | + end=sane_obj['TerminationDate'], |
| 58 | + extra_data=sane_obj) |
| 59 | + subscription_list.append(subscription) |
| 60 | + |
| 61 | + return subscription_list |
| 62 | + |
| 63 | + else: |
| 64 | + return None |
0 commit comments