Tom Evans
unread,Feb 8, 2013, 12:50:22 PM2/8/13Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to django...@googlegroups.com
Hi all
I have a curious problem with a complex data replication issue.
Basically, we use SalesForce as a CRM system. We also have a bunch of
users who aren't allowed a SF license due to cost reasons, and they
interact with SF via our own Django website that communicates to SF
via an API.
With this API, we can CRUD all salesforce objects. We can also fetch
all changes since (specific time), so we maintain a local copy of all
our SF data in django models, synchronizing changes every 10 minutes
throughout the day. This actually works quite well!
The problem comes with using django idioms like get_or_create(), when
an object is created directly on SF in the period since the last
synchronization. In this example, we want to get_or_create a Contact
with a specific email address. There is no Contact locally with the
specified email address, so get_or_create tries to create a new one.
Using the API, this issues the appropriate query to SF, but since the
Contact already exists on SF, an exception is raised.
get_or_create(), create() all work via save(), so my idea is to catch
this specific error in save, re-synchronise the database against the
remote end, pull the freshly synced record out of the database, and
replace self.__dict__ with new_item.__dict__, looking something like
this:
def save(self, *args, **kwargs):
try:
super(Contact, self).save(*args, **kwargs)
except ValueError, e:
if 'Contact already exists' in unicode(e):
# Synchronize Contact objects with SF
run_log = SyncRunLog(start_time=datetime.now())
run_log.save()
Contact.update(run_log)
# The contact should now be available locally
try:
synched_contact = Contact.objects.get(email=self.email)
self.__dict__ = synched_contact.__dict__
except Contact.DoesNotExist:
# Still not there, raise a new ValueError
raise ValueError(
'Failed to find contact %s after resyncing '
'Contact following this error: %s'
% (self.email, unicode(e)))
else:
raise e
Is there an obvious issue with doing this? Can I simply replace
self.__dict__ like that without bad consequences?
Cheers
Tom