Skip to content

Commit

Permalink
Merge remote branch 'coleifer/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewryanscott committed Dec 31, 2010
2 parents af51540 + b80f271 commit d29a0ba
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ include TODO.rst
recursive-include relationships/fixtures *.json
recursive-include relationships/locale *.po *.mo
recursive-include relationships/templates *.html
recursive-include relationships/tests/fixtures *.json
35 changes: 34 additions & 1 deletion relationships/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,24 @@
from django.core.urlresolvers import reverse
from django.template import Template, Context
from django.test import TestCase

from relationships.forms import RelationshipStatusAdminForm
from relationships.models import Relationship, RelationshipStatus
from relationships.utils import extract_user_field, positive_filter, negative_filter
from relationships.utils import (relationship_exists, extract_user_field,
positive_filter, negative_filter)

class BaseRelationshipsTestCase(TestCase):
"""
The fixture data defines:
- 4 users, The Walrus, John, Paul and Yoko
- 2 relationship status, Following & Blocking
- 4 relationships
- John is following Yoko
- John is following Paul
- Yoko is following John
- Paul is blocking John
"""
fixtures = ['relationships.json']

def setUp(self):
Expand Down Expand Up @@ -617,3 +630,23 @@ def test_negative_filter(self):
self.paul.relationships.blocking(),
'user')
self.assertQuerysetEqual(paul_blocking_groups, [beatles, characters, john_yoko])

def test_relationship_exists(self):
self.assertTrue(relationship_exists(self.john, self.yoko, 'following'))
self.assertTrue(relationship_exists(self.john, self.yoko, 'followers'))
self.assertTrue(relationship_exists(self.john, self.yoko, 'friends'))

self.assertTrue(relationship_exists(self.yoko, self.john, 'following'))
self.assertTrue(relationship_exists(self.yoko, self.john, 'followers'))
self.assertTrue(relationship_exists(self.yoko, self.john, 'friends'))

self.assertTrue(relationship_exists(self.john, self.paul, 'following'))
self.assertFalse(relationship_exists(self.john, self.paul, 'followers'))
self.assertFalse(relationship_exists(self.john, self.paul, 'friends'))

self.assertFalse(relationship_exists(self.paul, self.john, 'following'))
self.assertTrue(relationship_exists(self.paul, self.john, 'followers'))
self.assertFalse(relationship_exists(self.paul, self.john, 'friends'))

self.assertTrue(relationship_exists(self.paul, self.john, 'blocking'))
self.assertFalse(relationship_exists(self.paul, self.john, 'blockers'))
12 changes: 12 additions & 0 deletions relationships/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.contrib.auth.models import User

from relationships.models import RelationshipStatus


def relationship_exists(from_user, to_user, status_slug='following'):
status = RelationshipStatus.objects.by_slug(status_slug)
if status.from_slug == status_slug:
return from_user.relationships.exists(to_user, status)
elif status.to_slug == status_slug:
return to_user.relationships.exists(from_user, status)
else:
return from_user.relationships.symmetrical_exists(to_user, status)

def extract_user_field(model):
for field in model._meta.fields + model._meta.many_to_many:
if field.rel and field.rel.to == User:
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
author='Charles Leifer',
author_email='coleifer@gmail.com',
url='http://github.com/coleifer/django-relationships/tree/master',
packages=find_packages(exclude=['example']),
packages=find_packages(),
package_data = {
'relationships': [
'fixtures/*.json',
'templates/*.html',
'templates/*/*.html',
'locale/*/LC_MESSAGES/*',
'tests/fixtures/*.json',
],
},
classifiers=[
Expand Down

0 comments on commit d29a0ba

Please sign in to comment.