def test_signals(self): Handler = type( 'Handler', (object, ), { 'inc': lambda self: setattr(self, 'i', getattr(self, 'i') + 1), 'i': 0 }) user_handler = Handler() group_handler = Handler() def follow_handler(sender, user, target, instance, **kwargs): self.assertEqual(sender, User) self.assertEqual(self.lennon, user) self.assertEqual(self.hendrix, target) self.assertEqual(True, isinstance(instance, Follow)) user_handler.inc() def unfollow_handler(sender, user, target, instance, **kwargs): self.assertEqual(sender, User) self.assertEqual(self.lennon, user) self.assertEqual(self.hendrix, target) self.assertEqual(True, isinstance(instance, Follow)) user_handler.inc() def group_follow_handler(sender, **kwargs): self.assertEqual(sender, Group) group_handler.inc() def group_unfollow_handler(sender, **kwargs): self.assertEqual(sender, Group) group_handler.inc() signals.followed.connect(follow_handler, sender=User, dispatch_uid='userfollow') signals.unfollowed.connect(unfollow_handler, sender=User, dispatch_uid='userunfollow') signals.followed.connect(group_follow_handler, sender=Group, dispatch_uid='groupfollow') signals.unfollowed.connect(group_unfollow_handler, sender=Group, dispatch_uid='groupunfollow') utils.follow(self.lennon, self.hendrix) utils.unfollow(self.lennon, self.hendrix) self.assertEqual(2, user_handler.i) utils.follow(self.lennon, self.musicians) utils.unfollow(self.lennon, self.musicians) self.assertEqual(2, user_handler.i) self.assertEqual(2, group_handler.i)
def test_template_tags(self): follow_url = reverse('follow', args=['auth', 'user', self.hendrix.id]) unfollow_url = reverse('unfollow', args=['auth', 'user', self.hendrix.id]) request = type('Request', (object, ), {'user': self.lennon})() self.assertEqual(follow_url, utils.follow_link(self.hendrix)) self.assertEqual(unfollow_url, utils.unfollow_link(self.hendrix)) tpl = template.Template( """{% load follow_tags %}{% follow_url obj %}""") ctx = template.Context({'obj': self.hendrix, 'request': request}) self.assertEqual(follow_url, tpl.render(ctx)) utils.follow(self.lennon, self.hendrix) self.assertEqual(unfollow_url, tpl.render(ctx)) utils.unfollow(self.lennon, self.hendrix) self.assertEqual(follow_url, tpl.render(ctx)) tpl = template.Template( """{% load follow_tags %}{% follow_url obj user %}""") ctx2 = template.Context({ 'obj': self.lennon, 'user': self.hendrix, 'request': request }) self.assertEqual(utils.follow_url(self.hendrix, self.lennon), tpl.render(ctx2)) tpl = template.Template( """{% load follow_tags %}{% if request.user|is_following:obj %}True{% else %}False{% endif %}""" ) self.assertEqual("False", tpl.render(ctx)) utils.follow(self.lennon, self.hendrix) self.assertEqual("True", tpl.render(ctx)) tpl = template.Template( """{% load follow_tags %}{% follow_form obj %}""") self.assertEqual(True, isinstance(tpl.render(ctx), unicode)) tpl = template.Template( """{% load follow_tags %}{% follow_form obj "follow/form.html" %}""" ) self.assertEqual(True, isinstance(tpl.render(ctx), unicode))
def get(self, request, email): user = request.user following, created = User.objects.get_or_create(email=email) if created: following.username = email following.save() try: utils.unfollow(user, following) except: pass messages.success(request, "Unfollowed %s" % email) return redirect('/')
def process_following(self, request, obj, success_url): user = request.user kwargs = {} if not user == obj: is_following = Follow.objects.is_following(user, obj) follow_obj = unfollow(user, obj) if is_following else \ follow(user, obj) is_following = not is_following if follow_obj.target.__class__ is User: if is_following: mail_followee(request, follow_obj.target, user) title = user_display(follow_obj.target) else: title = unicode(follow_obj.target) key = "follow" if is_following else "unfollow" kwargs.update({"object": title}) else: key = "follow-warning" add_message(key, request, **kwargs) if request.is_ajax(): messages = render_messages(request) data = {"is_following": is_following, "messages": messages} return HttpResponse(json.dumps(data), mimetype="application/json") else: return HttpResponseRedirect(success_url)
def test_template_tags(self): follow_url = reverse('follow', args=['auth', 'user', self.hendrix.id]) unfollow_url = reverse('unfollow', args=['auth', 'user', self.hendrix.id]) request = type('Request', (object,), {'user': self.lennon})() self.assertEqual(follow_url, utils.follow_link(self.hendrix)) self.assertEqual(unfollow_url, utils.unfollow_link(self.hendrix)) tpl = template.Template("""{% load follow_tags %}{% follow_url obj %}""") ctx = template.Context({ 'obj':self.hendrix, 'request': request }) self.assertEqual(follow_url, tpl.render(ctx)) utils.follow(self.lennon, self.hendrix) self.assertEqual(unfollow_url, tpl.render(ctx)) utils.unfollow(self.lennon, self.hendrix) self.assertEqual(follow_url, tpl.render(ctx)) tpl = template.Template("""{% load follow_tags %}{% follow_url obj user %}""") ctx2 = template.Context({ 'obj': self.lennon, 'user': self.hendrix, 'request': request }) self.assertEqual(utils.follow_url(self.hendrix, self.lennon), tpl.render(ctx2)) tpl = template.Template("""{% load follow_tags %}{% if request.user|is_following:obj %}True{% else %}False{% endif %}""") self.assertEqual("False", tpl.render(ctx)) utils.follow(self.lennon, self.hendrix) self.assertEqual("True", tpl.render(ctx)) tpl = template.Template("""{% load follow_tags %}{% follow_form obj %}""") self.assertEqual(True, isinstance(tpl.render(ctx), str)) tpl = template.Template("""{% load follow_tags %}{% follow_form obj "follow/form.html" %}""") self.assertEqual(True, isinstance(tpl.render(ctx), str))
def test_signals(self): Handler = type('Handler', (object,), { 'inc': lambda self: setattr(self, 'i', getattr(self, 'i') + 1), 'i': 0 }) user_handler = Handler() group_handler = Handler() def follow_handler(sender, user, target, instance, **kwargs): self.assertEqual(sender, User) self.assertEqual(self.lennon, user) self.assertEqual(self.hendrix, target) self.assertEqual(True, isinstance(instance, Follow)) user_handler.inc() def unfollow_handler(sender, user, target, instance, **kwargs): self.assertEqual(sender, User) self.assertEqual(self.lennon, user) self.assertEqual(self.hendrix, target) self.assertEqual(True, isinstance(instance, Follow)) user_handler.inc() def group_follow_handler(sender, **kwargs): self.assertEqual(sender, Group) group_handler.inc() def group_unfollow_handler(sender, **kwargs): self.assertEqual(sender, Group) group_handler.inc() signals.followed.connect(follow_handler, sender=User, dispatch_uid='userfollow') signals.unfollowed.connect(unfollow_handler, sender=User, dispatch_uid='userunfollow') signals.followed.connect(group_follow_handler, sender=Group, dispatch_uid='groupfollow') signals.unfollowed.connect(group_unfollow_handler, sender=Group, dispatch_uid='groupunfollow') utils.follow(self.lennon, self.hendrix) utils.unfollow(self.lennon, self.hendrix) self.assertEqual(2, user_handler.i) utils.follow(self.lennon, self.musicians) utils.unfollow(self.lennon, self.musicians) self.assertEqual(2, user_handler.i) self.assertEqual(2, group_handler.i)
def test_template_tags(self): follow_url = reverse("follow", args=["auth", "user", self.hendrix.id]) unfollow_url = reverse("unfollow", args=["auth", "user", self.hendrix.id]) request = type("Request", (object,), {"user": self.lennon})() self.assertEqual(follow_url, utils.follow_link(self.hendrix)) self.assertEqual(unfollow_url, utils.unfollow_link(self.hendrix)) tpl = template.Template("""{% load follow_tags %}{% follow_url obj %}""") ctx = template.Context({"obj": self.hendrix, "request": request}) self.assertEqual(follow_url, tpl.render(ctx)) utils.follow(self.lennon, self.hendrix) self.assertEqual(unfollow_url, tpl.render(ctx)) utils.unfollow(self.lennon, self.hendrix) self.assertEqual(follow_url, tpl.render(ctx)) tpl = template.Template("""{% load follow_tags %}{% follow_url obj user %}""") ctx2 = template.Context({"obj": self.lennon, "user": self.hendrix, "request": request}) self.assertEqual(utils.follow_url(self.hendrix, self.lennon), tpl.render(ctx2)) tpl = template.Template( """{% load follow_tags %}{% if request.user|is_following:obj %}True{% else %}False{% endif %}""" ) self.assertEqual("False", tpl.render(ctx)) utils.follow(self.lennon, self.hendrix) self.assertEqual("True", tpl.render(ctx)) tpl = template.Template("""{% load follow_tags %}{% follow_form obj %}""") self.assertEqual(True, isinstance(tpl.render(ctx), unicode)) tpl = template.Template("""{% load follow_tags %}{% follow_form obj "follow/form.html" %}""") self.assertEqual(True, isinstance(tpl.render(ctx), unicode))