class Person(Model, CRUDMixin, CreatedUpdatedMixin): id = db.Column(db.Integer, primary_key=True) first_name = db.Column(db.String(64), nullable=False) last_name = db.Column(db.String(64)) email = db.Column(db.String(145), unique=True, nullable=False) date_of_birth = db.Column(db.Date, nullable=False) connections = db.relationship('Connection', foreign_keys='Connection.from_person_id') def mutual_friends(self, contact): """Given another person *contact*, return a set of people who are friends with both *self* and *contact*.""" friends = [ conn.to_person_id for conn in self.connections if conn.connection_type == ConnectionType.friend ] contact_fiends = [ conn.to_person_id for conn in contact.connections if conn.connection_type == ConnectionType.friend ] return [ db.session.query(Person).filter(Person.id == friend).first() for friend in set(friends).intersection(set(contact_fiends)) ]
class Person(Model, CRUDMixin, CreatedUpdatedMixin): id = db.Column(db.Integer, primary_key=True) first_name = db.Column(db.String(64), nullable=False) last_name = db.Column(db.String(64)) email = db.Column(db.String(145), unique=True, nullable=False) connections = db.relationship('Connection', foreign_keys='Connection.from_person_id')
class Person(Model, CRUDMixin, CreatedUpdatedMixin): id = db.Column(db.Integer, primary_key=True) first_name = db.Column(db.String(64), nullable=False) last_name = db.Column(db.String(64)) email = db.Column(db.String(145), unique=True, nullable=False) connections = db.relationship('Connection', foreign_keys='Connection.from_person_id') def mutual_friends(self, target): friend_ids = [ i.to_person_id for i in self.connections if i.connection_type.value == 'friend' ] return [(db.session.query(Person).get(mutual)) for mutual in friend_ids if mutual in ([j.to_person_id for j in target.connections])]
class Person(Model, CRUDMixin, CreatedUpdatedMixin): id = db.Column(db.Integer, primary_key=True) first_name = db.Column(db.String(64), nullable=False) last_name = db.Column(db.String(64)) email = db.Column(db.String(145), unique=True, nullable=False) connections = db.relationship('Connection', foreign_keys='Connection.from_person_id') def mutual_friends(self, target): mutual_friends = [] for connection_target in target.connections: for connection_self in self.connections: if connection_target.connection_type == 'friend' \ and connection_self.connection_type == 'friend' \ and connection_target.to_person_id == connection_self.to_person_id: mutual_friends.append(Person.query.get(connection_target.to_person_id)) return mutual_friends
class Person(Model, CRUDMixin, CreatedUpdatedMixin): id = db.Column(db.Integer, primary_key=True) first_name = db.Column(db.String(64), nullable=False) last_name = db.Column(db.String(64)) email = db.Column(db.String(145), unique=True, nullable=False) connections = db.relationship('Connection', foreign_keys='Connection.from_person_id', lazy='dynamic') def mutual_friends(self, Person): self_friends = self.connections.filter_by( connection_type=ConnectionType.friend) other_friends = Person.connections.filter_by( connection_type=ConnectionType.friend) mutual_connections = self_friends.union(other_friends).group_by( Connection.to_person_id) result = [connection.to_person for connection in mutual_connections] return result
class Person(Model, CRUDMixin, CreatedUpdatedMixin): id = db.Column(db.Integer, primary_key=True) first_name = db.Column(db.String(64), nullable=False) last_name = db.Column(db.String(64)) email = db.Column(db.String(145), unique=True, nullable=False) connections = db.relationship('Connection', foreign_keys='Connection.from_person_id') def get_connections(self, target, type): return [ connection.to_person_id for connection in self.connections if connection.connection_type == type ] def mutual_friends(self, target): friend_id = self.get_connections(self, ConnectionType.friend) target_friend_id = self.get_connections(target, ConnectionType.friend) mutual_friends_id = list(set(friend_id) & set(target_friend_id)) return db.session.query(Person).filter( Person.id.in_(mutual_friends_id)).all()