def write(self, ext_id, data): try: campaign = self.getresponse_api_session.update_campaign(ext_id, body=data) except NotFoundError as e: raise IDMissingInBackend(str(e.message) + ', ' + str(e.response)) if not campaign: raise ValueError( 'No data was written to GetResponse for campaign %s! Response: %s' '' % (ext_id, campaign)) return campaign
def write(self, external_id, data): try: tag = self.getresponse_api_session.update_tag(external_id, body=data) except NotFoundError as e: raise IDMissingInBackend(str(e.message) + ', ' + str(e.response)) if not tag: raise ValueError( 'No data was written to GetResponse for tag %s! Response: %s' '' % (external_id, tag)) # WARNING: !!! We return the tag object and not a dict !!! return tag
def read(self, ext_id, attributes=None): """ Returns the information of one record found by the external record id as a dict """ try: campaign = self.getresponse_api_session.get_campaign( ext_id, params=attributes) except NotFoundError as e: raise IDMissingInBackend(str(e.message) + ', ' + str(e.response)) if not campaign: raise ValueError( 'No data returned from GetResponse for campaign %s! Response: %s' '' % (ext_id, campaign)) # WARNING: A dict() is expected! Right now 'campaign' is a campaign object! return campaign.__dict__
def write(self, external_id, data): try: custom_field = self.getresponse_api_session.update_custom_field( external_id, body=data) except NotFoundError as e: raise IDMissingInBackend(str(e.message) + ', ' + str(e.response)) if not custom_field: raise ValueError( 'No data was written to GetResponse for custom field %s! Response: %s' '' % (external_id, custom_field)) # WARNING: !!! We return the custom_field object and not a dict !!! return custom_field
def delete(self, ext_id): """ Returns: bool: True for success, False otherwise """ try: result = self.getresponse_api_session.delete_contact(ext_id) except NotFoundError as e: raise IDMissingInBackend(str(e.message) + ', ' + str(e.response)) if not result: raise ValueError( 'Contact %s could not be deleted in GetResponse! Response: %s' '' % (ext_id, result)) return result
def write(self, ext_id, data): # Update the contact in GetResponse # ATTENTION: !!! This will replace the custom fields and the tags completely in GetResponse !!! try: contact = self.getresponse_api_session.update_contact(ext_id, body=data) except NotFoundError as e: raise IDMissingInBackend(str(e.message) + ', ' + str(e.response)) if not contact: raise ValueError( 'No data was written to GetResponse for contact %s! Response: %s' '' % (ext_id, contact)) # WARNING: !!! We return the contact object and not a dict !!! return contact
def _get_current_getresponse_record(self, bind_record): if not bind_record.getresponse_id: return None if not self.current_getresponse_record: try: exporter = self.unit_for(GetResponseExporter) self.current_getresponse_record = exporter.backend_adapter.read( bind_record.getresponse_id) except IDMissingInBackend: if bind_record.getresponse_id: raise IDMissingInBackend( 'GetResponse ID %s no longer exits!' % bind_record.getresponse_id) except Exception as e: raise e return self.current_getresponse_record
def _skip_export_for_updates(self): """ Return a message if the export must be skipped because e.g.: - odoo data has not changed since last sync or raise an exception to prevent the export e.g.: - no changes in odoo but write date in GetResponse is newer than the sync_date in odoo Returns: a message if the export must be skipped or 'False' to continue with the export """ # Skip checks for unbound records or records with prepared bindings because these would create new records binding = self.binding_record if not binding or not binding.getresponse_id: return False # GetResponse update-payload-data of the last sync last_sync_cmp_data = json.loads( binding.compare_data, encoding='utf8') if binding.compare_data else {} # Current odoo record export update data (=GetResponse update payload) # ATTENTION: The export mapper would merge the current getresponse data with the current odoo data! # !!! Therefore we need to set no_external_data=True !!! map_record = self.mapper.map_record(binding) current_odoo_export_data = map_record.values(no_external_data=True) # SKIPP EXPORT BECAUSE NO CHANGES WHERE MADE IN ODOO SINCE LAST EXPORT if cmp_payloads(last_sync_cmp_data, current_odoo_export_data) == 0: _logger.info( "SKIPP export of '%s'! Export data did not change since last sync for binding '%s', '%s'!" "" % ( self.getresponse_id, binding._name, binding.id, )) # Skip the export by returning a message return ( "SKIPP export of '%s'! Export data did not change since last export for binding '%s', '%s'!" "\n\ncurrent_odoo_export_data:\n%s,\n\nlast_sync_cmp_data:\n%s" "" % (self.getresponse_id, binding._name, binding.id, current_odoo_export_data, last_sync_cmp_data)) # CHECK FOR CHANGES IN GETRESPONSE SINCE THE LAST IMPORT # HINT: Right now this check will only trigger a log message and nothing else! The export will be done and may # override GetResponse data! This is less of a problem because the export mapper for contacts will # merge the current getresponse tags and custom fields - so no or very little data could be lost. if binding.sync_date: last_sync_date = datetime.strptime(binding.sync_date, DEFAULT_SERVER_DATETIME_FORMAT) try: getresponse_record = self.backend_adapter.read( self.getresponse_id) except IDMissingInBackend: raise IDMissingInBackend('GetResponse ID %s no longer exits!' % self.getresponse_id) except Exception as e: raise e getresponse_create_date = getresponse_record.get( 'created_on', None) getresponse_write_date = getresponse_record.get('changed_on', None) gr_last_change = getresponse_write_date or getresponse_create_date if gr_last_change: if gr_last_change.tzinfo is None or gr_last_change.tzinfo.utcoffset( gr_last_change) is None: gr_last_change = pytz.utc.localize(gr_last_change) if last_sync_date.tzinfo is None or last_sync_date.tzinfo.utcoffset( last_sync_date) is None: last_sync_date = pytz.utc.localize(last_sync_date) difference = gr_last_change - last_sync_date # If the last change in getresponse was 5 or more second later then the last sync we log a message if difference.total_seconds() >= 5: msg = ( "FORCE EXPORT of '%s' because data changed in both systems for binding '%s', '%s'!" " GetResponse data will be overwritten! gr_last_change: %s, last_sync_date: %s" "" % (self.getresponse_id, binding._name, binding.id, gr_last_change, last_sync_date)) # Do NOT skip the export but log an error message _logger.error(msg) # Continue with the export return False