Commit 708a260d authored by ryanss's avatar ryanss
Browse files

Improve documentation

parent f0bc6490
......@@ -14,43 +14,43 @@ Example Usage
# Connect
url = "http://your-sugarcrm-domain/service/v4/rest.php"
sugar = sugarcrm.API(url, username, password)
session = sugarcrm.Session(url, username, password)
# Create a new note
note = sugarcrm.Note(name="Test Note")
# Save note
sugar.set_entry(note)
session.set_entry(note)
# Add attachment to note
sugar.set_note_attachment(note, "sugarcrm.py")
session.set_note_attachment(note, "sugarcrm.py")
# Query for all notes that have a name that begins with "Test"
note_query = sugarcrm.Note(name="Test%")
results = sugar.get_entry_list(note_query)
results = session.get_entry_list(note_query)
# Query for all contacts with the first name "Mylee"
contact_query = sugarcrm.Contact(first_name="Mylee")
results = sugar.get_entry_list(contact_query)
results = session.get_entry_list(contact_query)
# Get the email address for the user assigned to an Opportunity
op = sugar.get_entry("Opportunities", "82f72939-735e-53a2-0944-5418c4edae2a")
user = sugar.get_entry("Users", op.assigned_user_id)
op = session.get_entry("Opportunities", "82f72939-735e-53a2-0944-5418c4edae2a")
user = session.get_entry("Users", op.assigned_user_id)
print user.email1
# Change the status of an Opportunity
op = sugarcrm.Opportunity(id="82f72939-735e-53a2-0944-5418c4edae2a")
op.sales_stage = "Approved"
sugar.set_entry(op)
session.set_entry(op)
# Extract all non-empty email fields from all Contacts in SugarCRM
emails = set()
contact_query = sugarcrm.Contact() # No filters provider finds all objects
contact_count = sugar.get_entries_count(contact_query, deleted=True)
contact_count = session.get_entries_count(contact_query, deleted=True)
print "Extracting emails from %d Contacts" % contact_count
# Grab 100 Contact objects at a time from SugarCRM
for offset in range(0, count, 100):
contacts = sugar.get_entry_list(contact_query, deleted=True,
contacts = session.get_entry_list(contact_query, deleted=True,
max_results=100, offset=offset)
for contact in contacts:
for field in dir(contact):
......@@ -76,38 +76,20 @@ If the above fails, please use easy_install instead:
$ easy_install sugarcrm
SugarCRM Objects
----------------
.. code-block:: python
>>> contact = sugarcrm.Contact()
>>> print contact.module
"Contacts"
>>> note = sugarcrm.Note()
>>> print note.module
"Notes"
>>> opportunity = sugarcrm.Opportunity()
>>> print opportunity.module
"Opportunities"
Session Object
--------------
API Object
----------
class sugarcrm.API(url, username, password, app="Python", lang="en_us")
The main class used to connect to the SugarCRM API and make quests with.
class sugarcrm.Session(url, username, password, app="Python", lang="en_us")
The main class used to connect to the SugarCRM API and make requests with.
.. code-block:: python
url = "http://your-sugarcrm-domain/service/v4/rest.php"
sugar = sugarcrm.API(url, username, password)
session = sugarcrm.Session(url, username, password)
API Methods
-----------
Available Methods
-----------------
get_available_modules(filter="default")
Retrieves a list of available modules in the system.
......@@ -115,7 +97,7 @@ get_available_modules(filter="default")
.. code-block:: python
modules = sugar.get_available_modules()
modules = session.get_available_modules()
for m in modules:
print m.module_key
......@@ -124,12 +106,12 @@ get_entry(module, object_id, links={}, track_view=False)
.. code-block:: python
note = sugar.get_entry("Notes", "f0c78aab-e051-174a-12aa-5439a7146977")
note = session.get_entry("Notes", "f0c78aab-e051-174a-12aa-5439a7146977")
print note.name
# Get a lead and specific fields from linked contacts in one query
links = {'Contacts': ['id', 'first_name', 'last_name']}
lead = sugar.get_entry("Leads", "d7dac88d-ce33-d98a-da8b-5418bba9e664",
lead = session.get_entry("Leads", "d7dac88d-ce33-d98a-da8b-5418bba9e664",
links=links)
for c in lead.contacts:
print c.id, c.first_name, c.last_name
......@@ -144,7 +126,7 @@ get_entries(module, object_ids, track_view=False)
"32f02fj2-4ggn-4nnf-fs33-f3fh3f93n333",
"82f72939-735e-53a2-0944-5418c4edae2a",
]
notes = sugar.get_entries("Notes", ids)
notes = session.get_entries("Notes", ids)
for note in notes:
print note.name
......@@ -156,7 +138,7 @@ get_entries_count(query_object, deleted=False)
# Get a count of all Contacts with a first name of "Fred"
# and include Contacts that have been deleted
contact_query = sugarcrm.Contact(first_name="Fred")
contacts = sugar.get_entries_count(contact_query, deleted=True)
contacts = session.get_entries_count(contact_query, deleted=True)
for contact in contacts:
print contact.first_name, contact.last_name
......@@ -167,7 +149,7 @@ get_entry_list(query_object, fields=[], links={}, order_by="", max_results=0, of
# Get a list of all Notes with a name that begins with "Test"
note_query = sugarcrm.Note(name="Test%")
notes = sugar.get_entry_list(note_query)
notes = session.get_entry_list(note_query)
for note in notes:
print note.name
......@@ -176,7 +158,7 @@ get_entry_list(query_object, fields=[], links={}, order_by="", max_results=0, of
q = sugarcrm.Opportunity()
q.query = "opportunities.date_entered > '2014-09-01'"
links = {'Contacts': ['id', 'first_name', 'last_name']}
results = sugar.get_entry_list(q, links=links)
results = session.get_entry_list(q, links=links)
for o in results:
for c in o.contacts:
print o.id, c.id, c.first_name, c.last_name
......@@ -190,8 +172,8 @@ set_document_revision(document, file)
.. code-block:: python
doc = sugarcrm.Document(document_name="Test Doc", revision=1)
sugar.set_entry(doc)
sugar.set_document_revision(doc, "/path/to/test.pdf")
session.set_entry(doc)
session.set_document_revision(doc, "/path/to/test.pdf")
set_entry(sugar_object)
......@@ -202,7 +184,7 @@ set_entry(sugar_object)
note = sugarcrm.Note()
note.name = "Test Note"
note.assigned_user_id = "82f72939-735e-53a2-0944-5418c4edae2a"
sugar.set_entry(note)
session.set_entry(note)
print note.id
set_note_attachment(note, attachment)
......@@ -211,8 +193,8 @@ set_note_attachment(note, attachment)
.. code-block:: python
with open("test1.pdf") as pdf_file:
sugar.set_note_attachment(note1, pdf_file)
sugar.set_note_attachment(note2, "test2.pdf")
session.set_note_attachment(note1, pdf_file)
session.set_note_attachment(note2, "test2.pdf")
print note1.filename, note2.filename
set_relationship(parent, child, delete=False)
......@@ -221,10 +203,20 @@ set_relationship(parent, child, delete=False)
.. code-block:: python
doc = sugarcrm.Document(document_name="Test Doc", revision=1)
sugar.set_entry(doc)
sugar.set_document_revision(doc, "/path/to/test.pdf")
opportunity = sugar.get_entry("Opportunities", "5b671886-cfe4-36f5-fa9d-5418a24e4aca")
sugar.set_relationship(opportunity, doc)
session.set_entry(doc)
session.set_document_revision(doc, "/path/to/test.pdf")
opportunity = session.get_entry("Opportunities", "5b671886-cfe4-36f5-fa9d-5418a24e4aca")
session.set_relationship(opportunity, doc)
Unavailable Methods
-------------------
.. _issue: https://github.com/ryanss/sugarcrm/issues
The following lesser-used SugarCRM API methods have not been included in this
library yet. Please open an issue_ if you require any of these methods and I
would be more than happy to implement them!
get_document_revision()
Method not implemented yet.
......@@ -311,6 +303,68 @@ snip_update_contacts()
Method not implemented yet.
SugarCRM Objects
----------------
.. code-block:: python
>>> call = sugarcrm.Call()
>>> print call.module
"Calls"
>>> campaign = sugarcrm.Campaign()
>>> print campaign.module
"Campaigns"
>>> contact = sugarcrm.Contact()
>>> print contact.module
"Contacts"
>>> document = sugarcrm.Document()
>>> print document.module
"Documents"
>>> email = sugarcrm.Email()
>>> print email.module
"Emails"
>>> lead = sugarcrm.Lead()
>>> print lead.module
"Leads"
>>> module = sugarcrm.Module()
>>> print module.module
"Modules"
>>> note = sugarcrm.Note()
>>> print note.module
"Notes"
>>> opportunity = sugarcrm.Opportunity()
>>> print opportunity.module
"Opportunities"
>>> product = sugarcrm.Product()
>>> print product.module
"Products"
>>> prospect = sugarcrm.Prospect()
>>> print prospect.module
"Prospects"
>>> prospect_list = sugarcrm.ProspectList()
>>> print prospect_list.module
"ProspectLists"
>>> quote = sugarcrm.Quote()
>>> print quote.module
"Quotes"
>>> report = sugarcrm.Report()
>>> print report.module
"Reports"
Development Version
-------------------
......
......@@ -18,7 +18,7 @@ import sys
import requests
class API:
class Session:
def __init__(self, url, username, password, app="Python", lang="en_us"):
self.url = url
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment