def unplace_sticker(self, id): id = get_id_bytes(id) self.connection.execute( self.sticker_placements.delete().where( self.sticker_placements.c.id == id ) )
def revoke_sticker(self, id): id = get_id_bytes(id) self.connection.execute( self.collected_stickers.delete().where( self.collected_stickers.c.id == id, ) )
def unplace_by_user(self, user_id): try: user_id = get_id_bytes(user_id) #TODO narrow catch except: return self.connection.execute( self.sticker_placements.delete().where( self.sticker_placements.c.user_id == user_id ) )
def anonymize_id(self, id, new_id=None): id = get_id_bytes(id) if not new_id: new_id = uuid.uuid4().bytes self.connection.execute(self.logs.update().values( subject_id=new_id).where(self.logs.c.subject_id == id, )) self.connection.execute(self.logs.update().values( object_id=new_id).where(self.logs.c.object_id == id, )) return new_id
def get_user_by_identifier(self, identifier): try: user_id = get_id_bytes(identifier) except: pass else: user = self.get_user(user_id) if user: return user users = self.search_users(filter={'names': identifier}) if 0 == len(users): return None return users.values()[0]
def grant_sticker(self, sticker_id, user_id, receive_time=None): sticker_id = get_id_bytes(sticker_id) user_id = get_id_bytes(user_id) collected_stickers = self.search_collected_stickers( filter={'user_ids': user_id, 'sticker_ids': sticker_id}, ) if 0 < len(collected_stickers): raise ValueError('Specified user already has the specified sticker') collected_sticker = CollectedSticker( user_id=user_id, sticker_id=sticker_id, receive_time=receive_time, ) if self.count_collected_stickers(filter={'ids': collected_sticker.id_bytes}): raise ValueError('Collected sticker ID collision') self.connection.execute( self.collected_stickers.insert(), id=collected_sticker.id_bytes, receive_time=int(collected_sticker.receive_time), user_id=collected_sticker.user_id_bytes, sticker_id=collected_sticker.sticker_id_bytes, ) return collected_sticker
def prune_user_sticker_placements(self, subject_id, user_id, maximum_stickers): try: subject_id = get_id_bytes(subject_id) #TODO narrow catch except: return try: user_id = get_id_bytes(user_id) #TODO narrow catch except: return placements = self.search_sticker_placements( filter={ 'subject_ids': subject_id, 'user_ids': user_id, }, sort='placement_time', order='desc', ) conditions = [] i = 0 for placement in placements.values(): i += 1 if i < maximum_stickers: continue conditions.append(self.sticker_placements.c.id == placement.id_bytes) if not conditions: return statement = self.sticker_placements.delete().where( and_( self.sticker_placements.c.subject_id == subject_id, self.sticker_placements.c.user_id == user_id, or_(*conditions), ) ) self.connection.execute(statement)
def anonymize_id(self, id, new_id=None): id = get_id_bytes(id) if not new_id: new_id = uuid.uuid4().bytes self.connection.execute( self.collected_stickers.update().values(user_id=new_id).where( self.collected_stickers.c.user_id == id, ) ) self.connection.execute( self.sticker_placements.update().values(user_id=new_id).where( self.sticker_placements.c.user_id == id, ) ) return new_id
def get_user_unique_sticker_placement_counts(self, user_id): user_id = get_id_bytes(user_id) statement = self.sticker_placements.select().where( self.sticker_placements.c.user_id == user_id ).with_only_columns( [ self.sticker_placements.c.sticker_id, func.count(distinct(self.sticker_placements.c.subject_id)), ] ).group_by( self.sticker_placements.c.sticker_id ) result = self.connection.execute(statement).fetchall() unique_sticker_placement_counts = {} for row in result: sticker_id, count = row sticker_id, sticker_id_bytes = parse_id(sticker_id) unique_sticker_placement_counts[sticker_id] = count return unique_sticker_placement_counts
def delete_log(self, id): id = get_id_bytes(id) self.connection.execute(self.logs.delete().where(self.logs.c.id == id))