def add_friend(self, from_user, to_user, message=None): """ Create a friendship request """ if from_user == to_user: raise ValidationError("Users cannot be friends with themselves") if self.are_friends(from_user, to_user): raise AlreadyFriendsError("Users are already friends") if self.can_request_send(from_user, to_user): raise AlreadyExistsError("Friendship already requested") if message is None: message = "" request, created = FriendshipRequest.objects.get_or_create( from_user=from_user, to_user=to_user) if created is False: raise AlreadyExistsError("Friendship already requested") if message: request.message = message request.save() bust_cache("requests", to_user.pk) bust_cache("sent_requests", from_user.pk) friendship_request_created.send(sender=request) return request
def add_friend(self, from_user, to_user, message=None): """ Create a friendship request """ if from_user == to_user: raise ValidationError("Users cannot be friends with themselves") if self.are_friends(from_user, to_user): raise AlreadyFriendsError("Users are already friends") if message is None: message = '' request, created = FriendshipRequest.objects.get_or_create( from_user=from_user, to_user=to_user, ) if created is False: raise AlreadyExistsError("Friendship already requested") if message: request.message = message request.save() bust_cache('requests', to_user.pk) bust_cache('sent_requests', from_user.pk) friendship_request_created.send(sender=request) return request
def add_friend(self, from_user, to_user): """ Create a friendship request """ if from_user == to_user: raise ValidationError("Users cannot be friends with themselves") # If the reverse friendship request exists, do not create the # request: the user will have to accept the other request instead if FriendshipRequest.objects.filter( from_user=to_user, to_user=from_user).exists(): raise AlreadyExistsError("This user already requested friendship") request, created = FriendshipRequest.objects.get_or_create( from_user=from_user, to_user=to_user ) if created is False: raise AlreadyExistsError("Friendship already requested") bust_cache('requests', to_user.pk) bust_cache('sent_requests', from_user.pk) friendship_request_created.send(sender=request) return request
def add_friend(self, from_user, to_user): """ Create a friendship request """ request = FriendshipRequest.objects.create(from_user=from_user, to_user=to_user) bust_cache("requests", to_user.pk) friendship_request_created.send(sender=self) return request
def add_friend(self, from_user, to_user): """ Create a friendship request """ request = FriendshipRequest.objects.create(from_user=from_user, to_user=to_user) bust_cache('requests', to_user.pk) friendship_request_created.send(sender=self) return request
def add_friend(self, from_user, to_user): """ Create a friendship request """ if from_user == to_user: raise ValidationError("Users cannot be friends with themselves") request = FriendshipRequest.objects.create( from_user=from_user, to_user=to_user ) bust_cache('requests', to_user.pk) friendship_request_created.send(sender=request) return request
def add_friend(self, from_user, to_user): """ Create a friendship request """ if from_user == to_user: raise ValidationError("Users cannot be friends with themselves") request, created = FriendshipRequest.objects.get_or_create(from_user=from_user, to_user=to_user) if created is False: raise AlreadyExistsError("Friendship already requested") bust_cache("requests", to_user.pk) friendship_request_created.send(sender=request) return request
def save(self, *args, **kwargs): if self.pk is None or self.pk == u'0': send = True super(FriendshipRequest, self).save(*args, **kwargs) # Bust requests cache - request is deleted bust_cache('requests', self.to_user.pk) bust_cache('sent_requests', self.from_user.pk) # Bust reverse requests cache - reverse request might be deleted bust_cache('requests', self.from_user.pk) bust_cache('sent_requests', self.to_user.pk) # Bust friends cache - new friends added bust_cache('friends', self.to_user.pk) bust_cache('friends', self.from_user.pk) if send: print(self.pk) friendship_request_created.send(sender=self)
def add_friend(self, from_user, to_user): """ Create a friendship request """ if from_user == to_user: raise ValidationError("Users cannot be friends with themselves") request, created = FriendshipRequest.objects.get_or_create( from_user=from_user, to_user=to_user) if created is False: raise AlreadyExistsError("Friendship already requested") bust_cache('requests', to_user.pk) bust_cache('sent_requests', from_user.pk) friendship_request_created.send(sender=request) return request
def add_friend(self, from_user, to_user): """ Create a friendship request """ if from_user == to_user: raise ValidationError("You cannot add yourself as a friend") request, created = FriendshipRequest.objects.get_or_create( from_user=from_user, to_user=to_user ) if created is False: raise AlreadyExistsError("Friendship already requested") bust_cache('requests', to_user.pk) bust_cache('sent_requests', from_user.pk) friendship_request_created.send(sender=request) return request