Skip to content

Commit

Permalink
django-relationships doesn't work with Django 1.7 + custom user model (
Browse files Browse the repository at this point in the history
…coleifer#40). Fortunately, we don't use any of their hacky things (`user.relationships` like stuff), so I just commented out all of that jazz.
  • Loading branch information
Aniruddha Maru committed Feb 25, 2015
1 parent bcfb22c commit 4b6abbc
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
10 changes: 5 additions & 5 deletions relationships/admin.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from django.contrib import admin
from django.contrib import admin, auth
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User as DefaultUserModel

from .compat import User
from .compat import AUTH_USER_MODEL
from .forms import RelationshipStatusAdminForm
from .models import Relationship, RelationshipStatus

Expand All @@ -22,11 +22,11 @@ class RelationshipStatusAdmin(admin.ModelAdmin):
form = RelationshipStatusAdminForm


if User == DefaultUserModel:
if AUTH_USER_MODEL == 'auth.User':
class UserRelationshipAdmin(UserRelationshipAdminMixin, UserAdmin):
pass

admin.site.unregister(User)
admin.site.register(User, UserRelationshipAdmin)
admin.site.unregister(auth.models.User)
admin.site.register(auth.models.User, UserRelationshipAdmin)

admin.site.register(RelationshipStatus, RelationshipStatusAdmin)
6 changes: 4 additions & 2 deletions relationships/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@


# Django 1.5 add support for custom auth user model
from django.conf import settings

if django.VERSION >= (1, 5):
from django.contrib.auth import get_user_model
User = get_user_model()
AUTH_USER_MODEL = settings.AUTH_USER_MODEL
else:
try:
from django.contrib.auth.models import User
AUTH_USER_MODEL = 'auth.User'
except ImportError:
raise ImportError(u"User model is not to be found.")

Expand Down
34 changes: 18 additions & 16 deletions relationships/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import django
import jsonfield
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.models import UserManager
from django.contrib.sites.models import Site
from django.db import models, connection
from django.db.models.fields.related import create_many_related_manager, ManyToManyRel
from django.utils.translation import ugettext_lazy as _

from .compat import User
from .compat import AUTH_USER_MODEL
from south.modelsinspector import add_introspection_rules


Expand Down Expand Up @@ -104,9 +106,9 @@ def __unicode__(self):

class Relationship(models.Model):
id = SIDField()
from_user = models.ForeignKey(User,
from_user = models.ForeignKey(AUTH_USER_MODEL,
related_name='from_relationships', verbose_name=_('from user'))
to_user = models.ForeignKey(User,
to_user = models.ForeignKey(AUTH_USER_MODEL,
related_name='to_relationships', verbose_name=_('to user'),
null=True, blank=True)
status = models.ForeignKey(RelationshipStatus, verbose_name=_('status'))
Expand Down Expand Up @@ -136,11 +138,11 @@ def is_owner(self, user):
return user.pk in (self.to_user_id, self.from_user_id)


field = models.ManyToManyField(User, through=Relationship,
field = models.ManyToManyField(AUTH_USER_MODEL, through=Relationship,
symmetrical=False, related_name='related_to')


class RelationshipManager(User._default_manager.__class__):
class RelationshipManager(UserManager.__class__):
def __init__(self, instance=None, *args, **kwargs):
super(RelationshipManager, self).__init__(*args, **kwargs)
self.instance = instance
Expand Down Expand Up @@ -227,14 +229,14 @@ def get_relationships(self, status, symmetrical=False):
if symmetrical:
query.update(self._get_to_query(status))

return User.objects.filter(**query)
return get_user_model().objects.filter(**query)

def get_related_to(self, status):
"""
Returns a QuerySet of user objects which have created a relationship to
the given user.
"""
return User.objects.filter(**self._get_to_query(status))
return get_user_model().objects.filter(**self._get_to_query(status))

def only_to(self, status):
"""
Expand Down Expand Up @@ -278,7 +280,7 @@ def exists(self, user, status=None, symmetrical=False):
if status:
query.update(from_relationships__status=status)

return User.objects.filter(**query).exists()
return get_user_model().objects.filter(**query).exists()

# some defaults
def following(self):
Expand All @@ -305,7 +307,7 @@ class RelationshipsDescriptor(object):
def __get__(self, instance, instance_type=None):
qn = connection.ops.quote_name
manager = RelatedManager(
model=User,
model=get_user_model(),
core_filters={'related_to__pk': instance._get_pk_val()},
instance=instance,
symmetrical=False,
Expand All @@ -318,15 +320,15 @@ def __get__(self, instance, instance_type=None):
elif django.VERSION > (1, 2) and django.VERSION < (1, 4):

fake_rel = ManyToManyRel(
to=User,
to=AUTH_USER_MODEL,
through=Relationship)

RelatedManager = create_many_related_manager(RelationshipManager, fake_rel)

class RelationshipsDescriptor(object):
def __get__(self, instance, instance_type=None):
manager = RelatedManager(
model=User,
model=get_user_model(),
core_filters={'related_to__pk': instance._get_pk_val()},
instance=instance,
symmetrical=False,
Expand All @@ -338,15 +340,15 @@ def __get__(self, instance, instance_type=None):
else:

fake_rel = ManyToManyRel(
to=User,
to=AUTH_USER_MODEL,
through=Relationship)

RelatedManager = create_many_related_manager(RelationshipManager, fake_rel)

class RelationshipsDescriptor(object):
def __get__(self, instance, instance_type=None):
manager = RelatedManager(
model=User,
model=get_user_model(),
query_field_name='related_to',
instance=instance,
symmetrical=False,
Expand All @@ -356,6 +358,6 @@ def __get__(self, instance, instance_type=None):
)
return manager

#HACK
field.contribute_to_class(User, 'relationships')
setattr(User, 'relationships', RelationshipsDescriptor())
# HACK
#field.contribute_to_class(User, 'relationships')
#setattr(User, 'relationships', RelationshipsDescriptor())
1 change: 0 additions & 1 deletion relationships/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
User = get_user_model()


from .compat import User
from .models import RelationshipStatus


Expand Down

0 comments on commit 4b6abbc

Please sign in to comment.