def update(self, vcard, account_name, href='', etag='', status=OK): """insert a new or update an existing card in the db :param vcard: vcard to be inserted or updated :type vcard: model.VCard() or unicode() (an actual vcard) :param href: href of the card on the server, if this href already exists in the db the card gets updated. If no href is given, a random href is chosen and it is implied that this card does not yet exist on the server, but will be uploaded there on next sync. :type href: str() :param etag: the etga of the vcard, if this etag does not match the remote etag on next sync, this card will be updated from the server. For locally created vcards this should not be set :type etag: str() :param status: status of the vcard * OK: card is in sync with remote server * NEW: card is not yet on the server, this needs to be set for locally created vcards * CHANGED: card locally changed, will be updated on the server on next sync (if remote card has not changed since last sync) * DELETED: card locally delete, will also be deleted on one the server on next sync (if remote card has not changed) :type status: one of backend.OK, backend.NEW, backend.CHANGED, BACKEND.DELETED """ if isinstance(vcard, (str, unicode)): # unicode for py2, str for py3 try: vcard_s = vcard.decode('utf-8') except UnicodeEncodeError: vcard_s = vcard # incase it's already unicode and py2 vcard = model.vcard_from_string(vcard) else: vcard_s = vcard.vcf if self.href_exists(href, account_name): # existing card stuple = (etag, vcard.name, vcard.fname, vcard_s, status, href) sql_s = 'UPDATE {0} SET etag = ?, name = ?, fname = ?, vcard = ?, \ status = ? WHERE href = ?;'.format(account_name) self.sql_ex(sql_s, stuple) else: if href == '': for _ in range(10): href = get_random_href() if self.href_exists(href, account_name) is False: break # could not find a (random) href that's not yet in the db # broken random number generator? #TODO: what's happens now? exception? stuple = (href, etag, vcard.name, vcard.fname, vcard_s, status) sql_s = ('INSERT INTO {0} ' '(href, etag, name, fname, vcard, status) ' 'VALUES (?,?,?,?,?,?);'.format(account_name)) self.sql_ex(sql_s, stuple)
def get_vcard_from_db(self, href, account_name): """returns a VCard() """ sql_s = 'SELECT vcard FROM {0} WHERE href=(?)'.format(account_name) result = self.sql_ex(sql_s, (href, )) vcard = model.vcard_from_string(result[0][0]) vcard.href = href return vcard
def get_vcard_from_data(self, account_name, href, vcard, etag): """returns a VCard() """ vcard = model.vcard_from_string(vcard) vcard.href = href vcard.account = account_name vcard.etag = etag return vcard
def update(self, vcard, account_name, href='', etag='', status=OK): """insert a new or update an existing card in the db :param vcard: vcard to be inserted or updated :type vcard: model.VCard() or unicode() (an actual vcard) :param href: href of the card on the server, if this href already exists in the db the card gets updated. If no href is given, a random href is chosen and it is implied that this card does not yet exist on the server, but will be uploaded there on next sync. :type href: str() :param etag: the etga of the vcard, if this etag does not match the remote etag on next sync, this card will be updated from the server. For locally created vcards this should not be set :type etag: str() :param status: status of the vcard * OK: card is in sync with remote server * NEW: card is not yet on the server, this needs to be set for locally created vcards * CHANGED: card locally changed, will be updated on the server on next sync (if remote card has not changed since last sync) * DELETED: card locally delete, will also be deleted on one the server on next sync (if remote card has not changed) :type status: one of backend.OK, backend.NEW, backend.CHANGED, BACKEND.DELETED """ if isinstance(vcard, (str, unicode)): # unicode for py2, str for py3 try: vcard_s = vcard.decode('utf-8') except UnicodeEncodeError: vcard_s = vcard # incase it's already unicode and py2 try: vcard = model.vcard_from_string(vcard) except: logging.error('VCard {0} could not be inserted into the ' 'db'.format(href)) if self.debug: logging.error('could not be converted to vcard') logging.error(vcard) return else: vcard_s = vcard.vcf if href == '': href = get_random_href() stuple = (etag, vcard.name, vcard.fname, vcard_s, status, href, href) sql_s = ('INSERT OR REPLACE INTO {0} ' '(etag, name, fname, vcard, status, href) ' 'VALUES (?, ?, ?, ?, ?, ' 'COALESCE((SELECT href FROM {0} WHERE href = ?), ?)' ');'.format(account_name)) self.sql_ex(sql_s, stuple)
def update(self, vcard, account_name, href='', etag='', status=OK): """insert a new or update an existing card in the db :param vcard: vcard to be inserted or updated :type vcard: model.VCard() or unicode() (an actual vcard) :param href: href of the card on the server, if this href already exists in the db the card gets updated. If no href is given, a random href is chosen and it is implied that this card does not yet exist on the server, but will be uploaded there on next sync. :type href: str() :param etag: the etga of the vcard, if this etag does not match the remote etag on next sync, this card will be updated from the server. For locally created vcards this should not be set :type etag: str() :param status: status of the vcard * OK: card is in sync with remote server * NEW: card is not yet on the server, this needs to be set for locally created vcards * CHANGED: card locally changed, will be updated on the server on next sync (if remote card has not changed since last sync) * DELETED: card locally delete, will also be deleted on one the server on next sync (if remote card has not changed) :type status: one of backend.OK, backend.NEW, backend.CHANGED, BACKEND.DELETED """ if isinstance(vcard, (str, unicode)): # unicode for py2, str for py3 try: vcard_s = vcard.decode('utf-8') except UnicodeEncodeError: vcard_s = vcard # incase it's already unicode and py2 vcard = model.vcard_from_string(vcard) else: vcard_s = vcard.vcf if href == '': href = get_random_href() stuple = (etag, vcard.name, vcard.fname, vcard_s, status, href, href) sql_s = ('INSERT OR REPLACE INTO {0} ' '(etag, name, fname, vcard, status, href) ' 'VALUES (?, ?, ?, ?, ?, ' 'COALESCE((SELECT href FROM {0} WHERE href = ?), ?)' ');'.format(account_name)) self.sql_ex(sql_s, stuple)