Exemplo n.º 1
0
def get_object_followers(obj):
    """returns query set of users following the object"""
    from followit.models import FollowRecord
    ct = ContentType.objects.get_for_model(obj)
    fr_set = FollowRecord.objects.filter(content_type=ct, object_id=obj.pk)
    uids = fr_set.values_list('user', flat=True)
    User = get_user_model()
    return User.objects.filter(pk__in=uids)
Exemplo n.º 2
0
def get_object_followers(obj):
    """returns query set of users following the object"""
    from followit.models import FollowRecord
    ct = ContentType.objects.get_for_model(obj)
    fr_set = FollowRecord.objects.filter(content_type=ct, object_id=obj.pk)
    uids = fr_set.values_list('user', flat=True)
    User = get_user_model()
    return User.objects.filter(pk__in=uids)
Exemplo n.º 3
0
def register(model):
    """returns model class that connects
    User with the followed object

    ``model`` - is the model class to follow

    The ``model`` class gets new method - ``get_followers``
    and the User class - a method - ``get_followed_Xs``, where
    the ``X`` is the name of the model
    and ``is_following(something)``
    
    Note, that proper pluralization of the model name is not supported,
    just "s" is added
    """
    from followit import models as followit_models
    from django.db import models as django_models
    from django.db.models.fields.related import ForeignKey

    User = get_user_model()

    model_name = get_model_name(model)
    if model in REGISTRY:
        return
    REGISTRY[model_name] = model

    #1) patch ``model`` with method ``get_followers()``
    model.add_to_class('get_followers', get_object_followers)

    #2) patch ``User`` with method ``get_followed_Xs``
    method_name = 'get_followed_' + model_name + 's'
    getter_method = make_followed_objects_getter(model)
    User.add_to_class(method_name, getter_method)

    #3) patch ``User with method ``is_following()``
    if not hasattr(User, 'is_following'):
        User.add_to_class('is_following', test_follow_method)

    #4) patch ``User`` with method ``follow_X``
    follow_method = make_follow_method(model)
    User.add_to_class('follow_' + model_name, follow_method)

    #5) patch ``User`` with method ``unfollow_X``
    unfollow_method = make_unfollow_method(model)
    User.add_to_class('unfollow_' + model_name, unfollow_method)
Exemplo n.º 4
0
def register(model):
    """returns model class that connects
    User with the followed object

    ``model`` - is the model class to follow

    The ``model`` class gets new method - ``get_followers``
    and the User class - a method - ``get_followed_Xs``, where
    the ``X`` is the name of the model
    and ``is_following(something)``
    
    Note, that proper pluralization of the model name is not supported,
    just "s" is added
    """
    from followit import models as followit_models
    from django.db import models as django_models
    from django.db.models.fields.related import ForeignKey

    User = get_user_model()

    model_name = get_model_name(model)
    if model in REGISTRY:
        return
    REGISTRY[model_name] = model

    #1) patch ``model`` with method ``get_followers()``
    model.add_to_class('get_followers', get_object_followers)

    #2) patch ``User`` with method ``get_followed_Xs``
    method_name = 'get_followed_' + model_name + 's'
    getter_method = make_followed_objects_getter(model)
    User.add_to_class(method_name, getter_method)

    #3) patch ``User with method ``is_following()``
    if not hasattr(User, 'is_following'):
        User.add_to_class('is_following', test_follow_method)

    #4) patch ``User`` with method ``follow_X``
    follow_method = make_follow_method(model)
    User.add_to_class('follow_' + model_name, follow_method)

    #5) patch ``User`` with method ``unfollow_X``
    unfollow_method = make_unfollow_method(model)
    User.add_to_class('unfollow_' + model_name, unfollow_method)
Exemplo n.º 5
0
 def setUp(self):
     User = get_user_model()
     self.u1 = User.objects.create_user('user1', '*****@*****.**')
     self.u2 = User.objects.create_user('user2', '*****@*****.**')
     self.u3 = User.objects.create_user('user3', '*****@*****.**')
Exemplo n.º 6
0
from django.db import models
from followit.compat import get_user_model

class Car(models.Model):
    yeah = models.BooleanField(default = True)

class Alligator(models.Model):
    yeah = models.BooleanField(default = True)

import followit
followit.register(Car)
followit.register(Alligator)
followit.register(get_user_model())#to test following users