|
4 | 4 | http://api.docs.membersuite.com/#References/Objects/Subscription.htm |
5 | 5 |
|
6 | 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 |
| 7 | + set up fixtures in MemberSuite sandbox for integration testing |
13 | 8 | @todo |
14 | 9 | add date modified param for performance |
15 | | - @todo |
16 | | - additional method for getting all subscriptions for syncing purposes |
17 | 10 | """ |
18 | 11 |
|
19 | 12 | from .models import Subscription |
| 13 | +from ..exceptions import ExecuteMSQLError |
| 14 | +from ..mixins import ChunkQueryMixin |
20 | 15 | from ..utils import convert_ms_object |
21 | 16 |
|
| 17 | +import datetime |
22 | 18 |
|
23 | | -class SubscriptionService(object): |
| 19 | + |
| 20 | +class SubscriptionService(ChunkQueryMixin, object): |
24 | 21 |
|
25 | 22 | def __init__(self, client): |
26 | 23 | """ |
27 | 24 | Accepts a ConciergeClient to connect with MemberSuite |
28 | 25 | """ |
29 | 26 | self.client = client |
30 | 27 |
|
31 | | - def get_org_subscriptions(self, org_id, publication_id=None): |
| 28 | + def get_subscriptions( |
| 29 | + self, publication_id=None, org_id=None, since_when=None, |
| 30 | + retry_attempts=2, limit_to=200, max_calls=None): |
32 | 31 | """ |
33 | | - Get all the subscriptions for a given organization |
34 | | -
|
35 | | - Returns a list of subscription objects |
| 32 | + Fetches all subscriptions from Membersuite of a particular |
| 33 | + `publication_id` if set. |
36 | 34 | """ |
37 | 35 | query = "SELECT Objects() FROM Subscription" |
38 | | - query += " WHERE owner = '%s'" % org_id |
39 | | - |
| 36 | + if org_id: |
| 37 | + query += " WHERE owner = '%s'" % org_id |
40 | 38 | if publication_id: |
41 | | - query += "AND publication = '%s'" % publication_id |
| 39 | + query += " AND publication = '%s'" % publication_id |
| 40 | + if since_when: |
| 41 | + query += " AND LastModifiedDate > '{since_when} 00:00:00'" \ |
| 42 | + .format(since_when=datetime.date.today() - |
| 43 | + datetime.timedelta(days=since_when)) |
42 | 44 |
|
43 | | - result = self.client.runSQL(query) |
| 45 | + # note, get_long_query is overkill when just looking at |
| 46 | + # one org, but it still only executes once |
| 47 | + # `get_long_query` uses `result_to_models` to return Subscriptions |
| 48 | + subscription_list = self.get_long_query( |
| 49 | + query, retry_attempts=retry_attempts, limit_to=limit_to, |
| 50 | + max_calls=max_calls) |
| 51 | + |
| 52 | + return subscription_list |
| 53 | + |
| 54 | + def result_to_models(self, result): |
| 55 | + """ |
| 56 | + this is the 'transorm' part of ETL: |
| 57 | + converts the result of the SQL to Subscription objects |
| 58 | + """ |
44 | 59 | mysql_result = result['body']['ExecuteMSQLResult'] |
45 | 60 |
|
46 | 61 | if not mysql_result['Errors']: |
47 | 62 | obj_result = mysql_result['ResultValue']['ObjectSearchResult'] |
| 63 | + if not obj_result['Objects']: |
| 64 | + return [] |
48 | 65 | objects = obj_result['Objects']['MemberSuiteObject'] |
49 | 66 |
|
50 | 67 | subscription_list = [] |
51 | 68 | for obj in objects: |
52 | | - sane_obj = convert_ms_object( |
53 | | - obj['Fields']['KeyValueOfstringanyType']) |
54 | | - subscription = Subscription( |
55 | | - id=sane_obj['ID'], |
56 | | - org=org_id, |
57 | | - start=sane_obj['StartDate'], |
58 | | - end=sane_obj['TerminationDate'], |
59 | | - extra_data=sane_obj) |
| 69 | + subscription = self.ms_object_to_model(obj) |
60 | 70 | subscription_list.append(subscription) |
61 | 71 |
|
62 | 72 | return subscription_list |
63 | 73 |
|
64 | 74 | else: |
65 | | - return None |
| 75 | + raise ExecuteMSQLError(result) |
| 76 | + |
| 77 | + def ms_object_to_model(self, ms_obj): |
| 78 | + " Converts an individual result to a Subscription Model " |
| 79 | + sane_obj = convert_ms_object( |
| 80 | + ms_obj['Fields']['KeyValueOfstringanyType']) |
| 81 | + subscription = Subscription( |
| 82 | + id=sane_obj['ID'], |
| 83 | + org_id=sane_obj['Owner'], |
| 84 | + start=sane_obj['StartDate'], |
| 85 | + end=sane_obj['TerminationDate'], |
| 86 | + extra_data=sane_obj) |
| 87 | + return subscription |
0 commit comments