Skip to content

Commit

Permalink
Update pythoncivicrm.py
Browse files Browse the repository at this point in the history
  • Loading branch information
hvelarde committed Jun 19, 2015
1 parent 4b33dd8 commit 9c7edc4
Showing 1 changed file with 13 additions and 34 deletions.
47 changes: 13 additions & 34 deletions src/collective/civicrm/pythoncivicrm.py 100755 → 100644
Expand Up @@ -150,11 +150,11 @@ def _get(self, action, entity, parameters=None):

if not parameters:
parameters = {}
payload = self._construct_url_payload(action, entity, parameters)
payload = self._construct_payload('get', action, entity, parameters)
api_call = requests.get(self.url, params=payload, timeout=self.timeout)
if api_call.status_code != 200:
raise CivicrmError('request to %s failed with status code %s'
% (self.url, api_call.status_code))
raise CivicrmError('request to %s failed with status code %s'
% (self.url, api_call.status_code))
results = json.loads(api_call.content)
return self._check_results(results)

Expand All @@ -163,7 +163,7 @@ def _post(self, action, entity, parameters=None):

if not parameters:
parameters = {}
postdata = self._construct_post_data(action, entity, parameters)
postdata = self._construct_payload('post', action, entity, parameters)
api_call = requests.post(
self.url, data=postdata, timeout=self.timeout
)
Expand Down Expand Up @@ -196,15 +196,6 @@ def _payload_template(self, action, entity):
}
return payload

# def _add_options(self, params, **kwargs):
# """Adds limit and offset etc in form required by REST API
# Takes key=value pairs and/or a dictionary(kwlist)
# in addition to a parameter dictionary to extend."""
# 'action': action,
# 'fnName': "civicrm/%s/%s" % (entity, action)
# }
# return payload

def _filter_merge_payload(self, parameters, payload, notparams):
"""Some parameters should be set explicitly, or not present,
so remove them.
Expand All @@ -226,11 +217,13 @@ def _filter_merge_payload(self, parameters, payload, notparams):
payload['sequential'] = 1
return payload

def _construct_url_payload(self, action, entity, parameters):
def _construct_payload(self, use, action, entity, parameters):
"""Takes action, entity, parameters returns payload suitable for a URL.
body_html and body_text parameters are removed as being very likely
to exceed the maximum URL length limits. Use POST instead.
: use: Http method to use ['get, 'post', 'put', 'delete'] only get and
post are used here.
:param action: What to do with the payload '
- such as create, delete, getsingle etc.
:param entity: Which CiviCRM module to reference.
Expand All @@ -249,23 +242,10 @@ def _construct_url_payload(self, action, entity, parameters):
'body_html',
'body_text'
]
if use.lower() == 'post':
notparams.extend(['body_html', 'body_text'])
return self._filter_merge_payload(parameters, payload, notparams)

def _construct_post_data(self, action, entity, parameters):
"""Takes action, entity, parameters returns a payload suitable
for a POST.
:param action: What to do with the payload
- such as create, delete, getsingle etc.
:param entity: Which CiviCRM module to reference.
:param parameters: An dictionary of key-value pairs to include
in the request.
:return: A dictionary of k-v pairs to send to the server
in the request.
"""
payload = self._payload_template(action, entity)
notparams = ['site_key', 'api_key', 'entity', 'action', 'json']
return self._filter_merge_payload(parameters, payload, notparams)

def _add_options(self, params, **kwargs):
"""Adds limit and offset etc. keys in form required by REST API
Expand Down Expand Up @@ -371,7 +351,7 @@ def setvalue(self, entity, db_id, field, value):
'id': db_id,
'field': field,
'value': value
}
}
)

def delete(self, entity, db_id, skip_undelete=False):
Expand Down Expand Up @@ -535,7 +515,6 @@ def add_contribution(self, contact_id, total_amount,
This can be obtained with
self.getoptions('Contribution', 'financial_type_id').
"""

if type(financial_type) is not int:
financial_type = self.is_valid_option(
'Contribution',
Expand Down Expand Up @@ -581,9 +560,9 @@ def add_tag(self, name, **kwargs):
def add_entity_tag(self, entity_id, tag_id,
entity_table="civicrm_contact"):
"""Tag an entity_id (a contact id by default) by tag id.
Note returns a dict with "is_error,not_added, added, total_count
It's not an error to tag an entity with a tag, it just won't
get added Iand added and not_added will reflect this.
Note returns a dict with "is_error, not_added, added, total_count
It's not an error to tag an entity with an invalid tag, it just won't
get added, and added and not_added will reflect this.
See also notes under delete."""

return self.create('EntityTag', entity_id=entity_id,
Expand Down

0 comments on commit 9c7edc4

Please sign in to comment.