Commit 30c7569c authored by delanoe's avatar delanoe

[FEAT] Collaborative: Adding friends requests.

parent e99f2815
......@@ -221,9 +221,11 @@ class NodeUser(Base):
"""
__tablename__ = 'node_user'
node_id = Column(Integer, ForeignKey(Node.id, ondelete='CASCADE'), primary_key=True)
user_id = Column(Integer, ForeignKey(User.id, ondelete='CASCADE'))
group_id = Column(Integer, ForeignKey(User.id, ondelete='CASCADE'))
mode = Column(Integer)
node_id = Column(Integer, ForeignKey(Node.id, ondelete='CASCADE'), primary_key=True)
user_id = Column(Integer, ForeignKey(User.id, ondelete='CASCADE'))
group_id = Column(Integer, ForeignKey(User.id, ondelete='CASCADE'))
rules_user = Column(Integer)
rules_group = Column(Integer)
rules_others = Column(Integer)
......@@ -20,15 +20,8 @@ class User(Base):
last_login = Column(DateTime(timezone=False))
date_joined = Column(DateTime(timezone=False), default=datetime.now)
def contacts(self):
"""get all contacts in relation with the user"""
Friend = aliased(User)
query = (session
.query(Friend)
.join(Contact, Contact.user2_id == Friend.id)
.filter(Contact.user1_id == self.id)
)
return query.all()
def get_params(self, username=None):
return self.__dict__.items()
def nodes(self, typename=None, order=None):
"""get all nodes belonging to the user
......@@ -46,26 +39,112 @@ class User(Base):
query = query.order_by(Node.name)
return query
def invite(self, username=None, block=False, notification=True):
if username is None:
raise ValueError('if you contact someone give his username please.')
if username is self.username:
raise ValueError('Why self contact should be implemented ?')
maybeFriend = session.query(User).filter(User.username == username).first()
if maybeFriend is None:
raise ValueError('username unknown in database.')
relation = (session.query(Contact)
.filter( user1_id == self.id
, user2_id == maybeFriend.id
)
.first()
)
if relation is not None:
if relation.is_blocked != block:
relation.is_blocked = block
session.add(relation)
session.commit()
print('Link does exist already and updated')
else:
print('Link does exist already and not updated')
else :
relation = Contact()
relation.user1_id = self.id
relation.user2_id = maybeFriend.id
relation.is_blocked = False
session.add(relation)
session.commit()
print('Relation is created in one direction only')
if notification is True:
print('TODO Create notification')
return relation
def accept(self, username=None, notification=False):
self.invite(username=username, block=False, notification=notification)
def refuse(self, username=None, notification=False):
self.invite(username=username, block=True, notification=notification)
def contacts(self):
"""get all contacts in one-relation with the user"""
Friend = aliased(User)
query = (session
.query(Friend)
.join(Contact, Contact.user2_id == Friend.id)
.filter(Contact.user1_id == self.id, Contact.is_blocked==False)
)
return query.all()
def friends(self):
"""get all contacts in bidirectional-relation
(both parties accepted to be linked) with the user"""
Friend = aliased(User)
Contact1 = aliased(Contact)
Contact2 = aliased(Contact)
query = (session
.query(Friend)
.join(Contact1, Contact1.user2_id == Friend.id)
.join(Contact2, Contact2.user1_id == Friend.id)
.filter(Contact1.user1_id == self.id, Contact1.is_blocked == False)
.filter(Contact2.user2_id == self.id, Contact2.is_blocked == False)
)
return query.all()
########################################################################
def contacts_nodes(self, typename=None):
from .nodes import Node
for contact in self.contacts():
contact_nodes = (session
.query(Node)
.filter(Node.user_id == contact.id)
.filter(Node.typename == typename)
.order_by(Node.date)
).all()
yield contact, contact_nodes
def owns(self, node):
"""check if a given node is owned by the user"""
return (node.user_id == self.id) or \
node.id in (contact.id for contact in self.contacts())
return (node.user_id == self.id)
# def owns(self, node):
# """check if a given node is owned by the user"""
# return (node.user_id == self.id) or \
# node.id in (contact.id for contact in self.friends())
# Deprecated
# def contacts_nodes(self, typename=None):
# from .nodes import Node
# for contact in self.contacts():
# contact_nodes = (session
# .query(Node)
# .filter(Node.user_id == contact.id)
# .filter(Node.typename == typename)
# .order_by(Node.date)
# ).all()
# yield contact, contact_nodes
def get_params(self, username=None):
print(self.__dict__.items())
return self.hyperdata
class Contact(Base):
__tablename__ = 'contacts'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment