Exemplo n.º 1
0
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))
        ]
Exemplo n.º 2
0
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')
Exemplo n.º 3
0
class Connection(Model, CRUDMixin, CreatedUpdatedMixin):
    id = db.Column(db.Integer, primary_key=True)
    from_person_id = db.Column(db.Integer, db.ForeignKey('person.id'))
    to_person_id = db.Column(db.Integer, db.ForeignKey('person.id'))
    connection_type = db.Column(db.Enum(ConnectionType), nullable=False)

    from_person = db.relationship("Person", foreign_keys=from_person_id)
    to_person = db.relationship("Person", foreign_keys=to_person_id)
Exemplo n.º 4
0
class Connection(Model, CRUDMixin, CreatedUpdatedMixin):
    id = db.Column(db.Integer, primary_key=True)
    from_person_id = db.Column(db.Integer, db.ForeignKey('person.id'))
    to_person_id = db.Column(db.Integer, db.ForeignKey('person.id'))
    connection_type = db.Column(db.Enum(ConnectionType), nullable=False)

    # Automagically get to/from people
    from_person = db.relationship('Person', foreign_keys=[from_person_id])
    to_person = db.relationship('Person', foreign_keys=[to_person_id])
Exemplo n.º 5
0
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])]
Exemplo n.º 6
0
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
Exemplo n.º 7
0
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
Exemplo n.º 8
0
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()