Custom limeobject¶
Custom limeobjects allows for custom logic to be executed when limeobjects are added, updated or deleted in CRM.
Running the following:
cd cool-stuff
lime-project generate limeobject
Information
If you already have a limeobject_classes
folder in your solution or package. Copy one of the existing LimeObject
files instead of running above command.
Information
It's a good practice to name the limeobject_class.py
file to what limetype it handles. i.e. person.py
or limeobject_person.py
If you want to create a custom limeobject for another limetype do the following:
- Change the name of the class:
class Person(LimeObject):
- Change what limetype to override in the
register_limeobject_classes
function in the file:
def register_limeobject_classes(register_class):
register_class('person', Person)
register_class('person', Person)
yields the following code in
cool-stuff/cool_stuff/limeobject_classes/limeobject_class.py
:
class Person(LimeObject):
"""Summarize the function of a person object here"""
def before_update(self, uow, **kwargs):
"""
This is called on all new and updated objects. All changes
made to the object here will be persisted.
All other objects that are changed or created here must be
added to the unit of work.
"""
self.properties.name.value = '{} {}'.format(
self.properties.firstname.value,
self.properties.lastname.value)
def before_delete(self, uow, **kwargs):
"""
This is called on objects that are about to be deleted,
before relations are detached. All changes made to
the object here will be persisted.
All other objects that are changed or created here must be
added to the unit of work.
"""
pass
def after_update(self, unsaved_self, **kwargs):
"""
This is called after the object has been saved, but before the
transaction has been committed. IDs on new records will be set,
and values that has changed are available on the unsaved_self
object.
Changes made to the object here will NOT be persisted.
This is the place to publish custom events.
"""
if unsaved_self.properties.name.is_dirty():
self.publish('custom.person.renamed', {
'old_name': unsaved_self.properties.name.original_value,
'new_name': self.properties.name.value})
def register_limeobject_classes(register_class):
register_class('person', Person)
it also creates the following files:
cool-stuff/cool_stuff/limeobject_classes/limeobject_class_test.py
(Example of tests)cool-stuff/cool_stuff/limeobject_classes/__init__.py
(For registering the custom lime objects)
This generates a custom limeobject and registers it to handle objects of
type person
in Lime CRM. This means that whenever you create a
limeobject of type person, or whenever you retrieve a person from the
database, this class will be instantiated instead of the default
LimeObject
The code generated for our package adds hooks for before and after a person is about to be saved to the database.
In before_updated
we set the property name to be the concatenation of
the person's first and last name.
In after_update
, we check to see if a person's name has changed and
publishes an event called 'custom.person.renamed' containing the old
and the new name that others can listen to.
Information
If you want to execute something only when a limeobject has been created as a copy of another limeobject you can check for the is_copy
property of the limeobject itself.
class Person(LimeObject):
def before_update(self, uow, **kwargs):
if self.is_copy:
# Perform custom logic if this Person is a copy of another Person
...
A limeobject is only marked as a copy of another limeobject when the copy has been created with the copy()
function on a limeobject instance.
copy_of_person = person.copy()