Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added forgotten managers.py file from previous commit

  • Loading branch information...
commit f2e4367f068831f3228a397e39a77aa6ed07c3cc 1 parent 9f6e1b2
@alex alex authored
Showing with 31 additions and 0 deletions.
  1. +31 −0 friends/managers.py
View
31 friends/managers.py
@@ -0,0 +1,31 @@
+from django.db import models
+
+
+class FriendshipManager(models.Manager):
+ def friends_for_user(self, user):
+ friends = []
+ for friendship in self.filter(from_user=user).select_related(depth=1):
+ friends.append({"friend": friendship.to_user, "friendship": friendship})
+ for friendship in self.filter(to_user=user).select_related(depth=1):
+ friends.append({"friend": friendship.from_user, "friendship": friendship})
+ return friends
+
+ def are_friends(self, user1, user2):
+ if self.filter(from_user=user1, to_user=user2).count() > 0:
+ return True
+ if self.filter(from_user=user2, to_user=user1).count() > 0:
+ return True
+ return False
+
+ def remove(self, user1, user2):
+ if self.filter(from_user=user1, to_user=user2):
+ friendship = self.filter(from_user=user1, to_user=user2)
+ elif self.filter(from_user=user2, to_user=user1):
+ friendship = self.filter(from_user=user2, to_user=user1)
+ friendship.delete()
+
+
+class FriendshipInvitationManager(models.Manager):
+ def invitations(self, *args, **kwargs):
+ return self.filter(*args, **kwargs).exclude(status__in=["6", "8"])
+
Please sign in to comment.
Something went wrong with that request. Please try again.