def save_image(self, original, large, small, image_type): with self.db.get_cursor() as c: oids = {} for size in self.IMAGE_SIZES: lobject = c.connection.lobject(getattr(self, 'image_oid_' + size), mode='wb') lobject.write(locals()[size]) oids[size] = lobject.oid lobject.close() c.run( """UPDATE teams SET image_oid_original=%s, image_oid_large=%s, image_oid_small=%s , image_type=%s WHERE id=%s""", (oids['original'], oids['large'], oids['small'], image_type, self.id)) add_event(c, 'team', dict(action='upsert_image', id=self.id, **oids)) self.set_attributes( image_type=image_type, **{'image_oid_' + size: oids[size] for size in oids}) return oids
def retrieve_identity_info(self, country_id): """Return the participant's national identity information for a given country. :param int country_id: an ``id`` from the ``countries`` table :returns: a dictionary of identity information, or ``None`` """ with self.db.get_cursor() as cursor: identity_id, info = cursor.one(""" SELECT id, info FROM participant_identities WHERE participant_id=%s AND country_id=%s """, (self.id, country_id), default=(None, None)) if info is not None: info = bytes( info) # psycopg2 returns bytea as buffer; we want bytes info = self.encrypting_packer.unpack(info) payload = dict(id=self.id, identity_id=identity_id, country_id=country_id, action='retrieve identity') add_event(cursor, 'participant', payload) return info
def update(self, **kw): updateable = frozenset([ 'name', 'product_or_service', 'homepage', 'onboarding_url', 'todo_url' ]) cols, vals = zip(*kw.items()) assert set(cols).issubset(updateable) old_value = {} for col in cols: old_value[col] = getattr(self, col) cols = ', '.join(cols) placeholders = ', '.join(['%s'] * len(vals)) with self.db.get_cursor() as c: c.run( """ UPDATE teams SET ({0}) = ({1}) WHERE id = %s """.format(cols, placeholders), vals + (self.id, )) add_event(c, 'team', dict(action='update', id=self.id, **old_value)) self.set_attributes(**kw)
def _add_event(action): payload = dict( id=self.id , country_id=country_id , identity_id=identity_id , action=action + ' identity' ) add_event(cursor, 'participant', payload)
def update(self, **kw): updateable = frozenset(['name', 'product_or_service', 'homepage', 'onboarding_url', 'todo_url']) cols, vals = zip(*kw.items()) assert set(cols).issubset(updateable) old_value = {} for col in cols: old_value[col] = getattr(self, col) cols = ', '.join(cols) placeholders = ', '.join(['%s']*len(vals)) with self.db.get_cursor() as c: c.run(""" UPDATE teams SET ({0}) = ({1}) WHERE id = %s """.format(cols, placeholders), vals+(self.id,) ) add_event(c, 'team', dict( action='update' , id=self.id , **old_value )) self.set_attributes(**kw)
def retrieve_identity_info(self, country_id): """Return the participant's national identity information for a given country. :param int country_id: an ``id`` from the ``countries`` table :returns: a dictionary of identity information, or ``None`` """ with self.db.get_cursor() as cursor: identity_id, info = cursor.one(""" SELECT id, info FROM participant_identities WHERE participant_id=%s AND country_id=%s """, (self.id, country_id), default=(None, None)) if info is not None: info = bytes(info) # psycopg2 returns bytea as buffer; we want bytes info = self.encrypting_packer.unpack(info) payload = dict( id=self.id , identity_id=identity_id , country_id=country_id , action='retrieve identity' ) add_event(cursor, 'participant', payload) return info
def close(self): """Close the team account. """ with self.db.get_cursor() as cursor: cursor.run("UPDATE teams SET is_closed=true WHERE id=%s", (self.id, )) add_event( cursor, 'team', dict(id=self.id, action='set', values=dict(is_closed=True))) self.set_attributes(is_closed=True)
def set_identity_verification(self, country_id, is_verified): """Set the verification status of the participant's national identity for a given country. :param int country_id: an ``id`` from the ``countries`` table :param bool is_verified: whether the information has been verified or not This is a no-op if the participant has no identity on file for the given ``country_id``. """ is_verified = bool(is_verified) action = 'verify' if is_verified else 'unverify' with self.db.get_cursor() as cursor: old = cursor.one( """ SELECT id, is_verified FROM participant_identities WHERE participant_id=%(participant_id)s AND country_id=%(country_id)s """, dict(locals(), participant_id=self.id)) cursor.run( """ UPDATE participant_identities SET is_verified=%(is_verified)s WHERE participant_id=%(participant_id)s AND country_id=%(country_id)s """, dict(locals(), participant_id=self.id)) payload = dict(id=self.id, identity_id=old.id if old else None, country_id=country_id, new_value=is_verified, old_value=old.is_verified if old else None, action=action + ' identity') add_event(cursor, 'participant', payload) self._update_has_verified_identity(cursor)
def save_image(self, original, large, small, image_type): with self.db.get_cursor() as c: oids = {} for size in self.IMAGE_SIZES: lobject = c.connection.lobject(getattr(self, "image_oid_" + size), mode="wb") lobject.write(locals()[size]) oids[size] = lobject.oid lobject.close() c.run( """UPDATE teams SET image_oid_original=%s, image_oid_large=%s, image_oid_small=%s , image_type=%s WHERE id=%s""", (oids["original"], oids["large"], oids["small"], image_type, self.id), ) add_event(c, "team", dict(action="upsert_image", id=self.id, **oids)) self.set_attributes(image_type=image_type, **{"image_oid_" + size: oids[size] for size in oids}) return oids
def set_identity_verification(self, country_id, is_verified): """Set the verification status of the participant's national identity for a given country. :param int country_id: an ``id`` from the ``countries`` table :param bool is_verified: whether the information has been verified or not This is a no-op if the participant has no identity on file for the given ``country_id``. """ is_verified = bool(is_verified) action = 'verify' if is_verified else 'unverify' with self.db.get_cursor() as cursor: old = cursor.one(""" SELECT id, is_verified FROM participant_identities WHERE participant_id=%(participant_id)s AND country_id=%(country_id)s """, dict(locals(), participant_id=self.id)) cursor.run(""" UPDATE participant_identities SET is_verified=%(is_verified)s WHERE participant_id=%(participant_id)s AND country_id=%(country_id)s """, dict(locals(), participant_id=self.id)) payload = dict( id=self.id , identity_id=old.id if old else None , country_id=country_id , new_value=is_verified , old_value=old.is_verified if old else None , action=action + ' identity' ) add_event(cursor, 'participant', payload) self._update_has_verified_identity(cursor)
def save_image(self, original, large, small, image_type): with self.db.get_cursor() as c: oids = {} for size in self.IMAGE_SIZES: lobject = c.connection.lobject(getattr(self, 'image_oid_'+size), mode='wb') lobject.write(locals()[size]) oids[size] = lobject.oid lobject.close() c.run("""UPDATE teams SET image_oid_original=%s, image_oid_large=%s, image_oid_small=%s , image_type=%s WHERE id=%s""" , (oids['original'], oids['large'], oids['small'], image_type, self.id) ) add_event(c, 'team', dict( action='upsert_image' , id=self.id , **oids )) self.set_attributes( image_type=image_type , **{'image_oid_'+size: oids[size] for size in oids} ) return oids
def clear_identity(self, country_id): """Clear the participant's national identity record for a given country. :param int country_id: an ``id`` from the ``countries`` table """ with self.db.get_cursor() as cursor: identity_id = cursor.one( """ DELETE FROM participant_identities WHERE participant_id=%(participant_id)s AND country_id=%(country_id)s RETURNING id """, dict(locals(), participant_id=self.id)) payload = dict(id=self.id, identity_id=identity_id, country_id=country_id, action='clear identity') add_event(cursor, 'participant', payload) self._update_has_verified_identity(cursor)
def clear_identity(self, country_id): """Clear the participant's national identity record for a given country. :param int country_id: an ``id`` from the ``countries`` table """ with self.db.get_cursor() as cursor: identity_id = cursor.one(""" DELETE FROM participant_identities WHERE participant_id=%(participant_id)s AND country_id=%(country_id)s RETURNING id """, dict(locals(), participant_id=self.id)) payload = dict( id=self.id , identity_id=identity_id , country_id=country_id , action='clear identity' ) add_event(cursor, 'participant', payload) self._update_has_verified_identity(cursor)
def _add_event(action): payload = dict(id=self.id, country_id=country_id, identity_id=identity_id, action=action + ' identity') add_event(cursor, 'participant', payload)