Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

cleaned up minor things

  • Loading branch information...
commit c330d3d9df1b80b05ebacb1a9844332e24cfdfe6 1 parent 75bfd1e
@brosner brosner authored
Showing with 22 additions and 10 deletions.
  1. +22 −10 friends/models.py
View
32 friends/models.py
@@ -1,14 +1,16 @@
import datetime
+
from random import random
+from django.conf import settings
+from django.core.urlresolvers import reverse
from django.db import models
+from django.db.models import signals
from django.template.loader import render_to_string
-from django.core.urlresolvers import reverse
-from django.contrib.auth.models import User
-from django.contrib.sites.models import Site
from django.utils.hashcompat import sha_constructor
-from django.db.models import signals
-from django.conf import settings
+
+from django.contrib.sites.models import Site
+from django.contrib.auth.models import User
# favour django-mailer but fall back to django.core.mail
if "mailer" in settings.INSTALLED_APPS:
@@ -26,6 +28,7 @@
else:
EmailAddress = None
+
class Contact(models.Model):
"""
A contact is a person known by a user who may or may not themselves
@@ -47,7 +50,7 @@ def __unicode__(self):
class FriendshipManager(models.Manager):
-
+
def friends_for_user(self, user):
friends = []
for friendship in self.filter(from_user=user).select_related(depth=1):
@@ -62,7 +65,7 @@ def are_friends(self, user1, user2):
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)
@@ -103,6 +106,7 @@ def friend_set_for(user):
("8", "Deleted")
)
+
class JoinInvitationManager(models.Manager):
def send_invitation(self, from_user, to_email, message):
@@ -161,10 +165,13 @@ def accept(self, new_user):
friends.append(user)
notification.send(friends, "friends_otherconnect", {"invitation": self, "to_user": new_user})
+
class FriendshipInvitationManager(models.Manager):
+
def invitations(self, *args, **kwargs):
return self.filter(*args, **kwargs).exclude(status__in=["6", "8"])
+
class FriendshipInvitation(models.Model):
"""
A frienship invite is an invitation from one user to another to be
@@ -178,7 +185,7 @@ class FriendshipInvitation(models.Model):
status = models.CharField(max_length=1, choices=INVITE_STATUS)
objects = FriendshipInvitationManager()
-
+
def accept(self):
if not Friendship.objects.are_friends(self.to_user, self.from_user):
friendship = Friendship(to_user=self.to_user, from_user=self.from_user)
@@ -191,12 +198,13 @@ def accept(self):
for user in friend_set_for(self.to_user) | friend_set_for(self.from_user):
if user != self.to_user and user != self.from_user:
notification.send([user], "friends_otherconnect", {"invitation": self, "to_user": self.to_user})
-
+
def decline(self):
if not Friendship.objects.are_friends(self.to_user, self.from_user):
self.status = "6"
self.save()
+
class FriendshipInvitationHistory(models.Model):
"""
History for friendship invitations
@@ -208,6 +216,7 @@ class FriendshipInvitationHistory(models.Model):
sent = models.DateField(default=datetime.date.today)
status = models.CharField(max_length=1, choices=INVITE_STATUS)
+
if EmailAddress:
def new_user(sender, instance, **kwargs):
if instance.verified:
@@ -219,7 +228,7 @@ def new_user(sender, instance, **kwargs):
for contact in Contact.objects.filter(email=instance.email):
contact.users.add(instance.user)
# @@@ send notification
-
+
# only if django-email-notification is installed
signals.post_save.connect(new_user, sender=EmailAddress)
@@ -230,8 +239,10 @@ def delete_friendship(sender, instance, **kwargs):
friendship_invitation.status = "8"
friendship_invitation.save()
+
signals.pre_delete.connect(delete_friendship, sender=Friendship)
+
# moves existing friendship invitation from user to user to FriendshipInvitationHistory before saving new invitation
def friendship_invitation(sender, instance, **kwargs):
friendship_invitations = FriendshipInvitation.objects.filter(to_user=instance.to_user, from_user=instance.from_user)
@@ -245,4 +256,5 @@ def friendship_invitation(sender, instance, **kwargs):
)
friendship_invitation.delete()
+
signals.pre_save.connect(friendship_invitation, sender=FriendshipInvitation)
Please sign in to comment.
Something went wrong with that request. Please try again.