diff --git a/apps/dashboard/tests.py b/apps/dashboard/tests.py index 00fdc2e..a6ea0cf 100644 --- a/apps/dashboard/tests.py +++ b/apps/dashboard/tests.py @@ -17,17 +17,13 @@ class DashboardTests(TestCase): def test_unauthorized_request(self): """Unauthorized requests should get a signin template.""" response = self.client.get('/%s/' % (self.locale,)) - - # NOTE: because we are using jinja2, we can't use the - # assertTemplateUsed method, so we have check for the - # username element in the response body instead. - self.assertContains(response, 'id_username', status_code=200) + self.assertTemplateUsed(response, 'dashboard/splash.html') def test_authorized_request(self): """Authorized requests should land on a personalized dashboard.""" self.client.login(username=self.test_username, password=self.test_password) response = self.client.get('/%s/' % (self.locale,)) - self.assertContains(response, 'status_update', status_code=200) + self.assertTemplateUsed(response, 'dashboard/dashboard.html') diff --git a/apps/l10n/tests.py b/apps/l10n/tests.py index 1635355..eb7aeb6 100644 --- a/apps/l10n/tests.py +++ b/apps/l10n/tests.py @@ -59,7 +59,7 @@ class TestLocaleURLs(TestCase): user = User.objects.create_user( 'testuser', 'test@mozilla.com', 'testpass' ) - response = self.client.get('/de/') + response = self.client.get('/de/login/') self.assertContains(response, 'csrfmiddlewaretoken') response = self.client.post('/de/login/', { 'username': user.username, diff --git a/apps/projects/views.py b/apps/projects/views.py index c769bb6..589022e 100644 --- a/apps/projects/views.py +++ b/apps/projects/views.py @@ -1,5 +1,7 @@ import re +from django.contrib.auth.decorators import login_required +from django.contrib.contenttypes.models import ContentType from django.core.urlresolvers import reverse from django.http import HttpResponseRedirect from django.shortcuts import render_to_response, get_object_or_404 @@ -8,11 +10,14 @@ from django.template import RequestContext from projects.models import Project from projects.forms import ProjectForm - def show(request, slug): project = get_object_or_404(Project, slug=slug) + following = (request.user.is_authenticated() and + request.user.is_following(project) or False) return render_to_response('projects/project.html', { - 'project': project + 'project': project, + 'type': ContentType.objects.get_for_model(project), + 'following': following, }, context_instance=RequestContext(request)) def gallery(request): @@ -20,6 +25,7 @@ def gallery(request): 'projects': Project.objects.all() }, context_instance=RequestContext(request)) +@login_required def create(request): form = ProjectForm() if request.method == 'POST': diff --git a/apps/relationships/models.py b/apps/relationships/models.py index f10d4bd..f347f98 100644 --- a/apps/relationships/models.py +++ b/apps/relationships/models.py @@ -18,12 +18,14 @@ class Relationship(models.Model): target = generic.GenericForeignKey('target_content_type', 'target_object_id') def save(self, *args, **kwargs): - if self.source.pk == self.target.pk: + if (self.source_content_type == self.target_content_type) and ( + self.source.pk == self.target.pk): raise ValidationError(_('Cannot create self referencing relationship.')) super(Relationship, self).save(*args, **kwargs) class Meta: - unique_together = (('source_object_id', 'target_object_id'),) + unique_together = ('source_content_type', 'target_content_type', + 'source_object_id', 'target_object_id',) def __unicode__(self): return "%(from)s => %(to)s" % { @@ -41,8 +43,16 @@ def following(self): source_object_id=self.id).filter( source_content_type=ContentType.objects.get_for_model(self))] +def is_following(self, model): + obj_type_id = ContentType.objects.get_for_model(model) + return len(Relationship.objects.filter( + source_object_id=self.id, + target_object_id=model.pk, + target_content_type=obj_type_id)) > 0 + User.followers = followers User.following = following +User.is_following = is_following def follow_handler(sender, **kwargs): rel = kwargs.get('instance', None) diff --git a/apps/relationships/views.py b/apps/relationships/views.py index c1523ab..d54a887 100644 --- a/apps/relationships/views.py +++ b/apps/relationships/views.py @@ -1,5 +1,6 @@ from django.contrib.auth.models import User from django.contrib.auth.decorators import login_required +from django.contrib.contenttypes.models import ContentType from django.core.urlresolvers import reverse from django.http import HttpResponse, HttpResponseRedirect from django.utils.translation import ugettext as _ @@ -30,25 +31,33 @@ def followers(request): @login_required @require_http_methods(['POST']) def follow(request): - if 'user' not in request.POST: + + if 'object_id' not in request.POST or 'object_type_id' not in request.POST: # todo - report error usefully return HttpResponse("error") - user = User.objects.get(id=int(request.POST['user'])) - rel = Relationship(source=request.user, target=user) + + obj_type = ContentType.objects.get(id=int(request.POST['object_type_id'])) + target = obj_type.get_object_for_this_type(id=int(request.POST['object_id'])) + rel = Relationship(source=request.user, target=target) rel.save() - # todo - redirect user to whatever page they were on before. - return HttpResponseRedirect(reverse('users_user_list')) + + return HttpResponseRedirect(request.META['HTTP_REFERER']) @login_required @require_http_methods(['POST']) def unfollow(request): - if 'user' not in request.POST: + + if 'object_id' not in request.POST or 'object_type_id' not in request.POST: # todo - report error usefully return HttpResponse("error") - user = User.objects.get(id=int(request.POST['user'])) + + obj_type = ContentType.objects.get(id=int(request.POST['object_type_id'])) + target = obj_type.get_object_for_this_type(id=int(request.POST['object_id'])) + rel = Relationship.objects.get( source_object_id__exact=request.user.id, - target_object_id__exact=user.id) + target_object_id__exact=target.pk, + target_content_type__exact=obj_type) rel.delete() - # todo - redirect user to whatever page they were on before. - return HttpResponseRedirect(reverse('users_user_list')) + + return HttpResponseRedirect(request.META['HTTP_REFERER']) diff --git a/apps/users/views.py b/apps/users/views.py index 8c3ef7a..6438d50 100644 --- a/apps/users/views.py +++ b/apps/users/views.py @@ -3,6 +3,7 @@ from django.contrib import auth from django.contrib import messages from django.contrib.auth.decorators import login_required from django.contrib.auth.models import User +from django.contrib.contenttypes.models import ContentType from django.core.urlresolvers import reverse from django.http import HttpResponseRedirect, Http404 from django.utils.translation import ugettext as _ @@ -138,7 +139,8 @@ def user_list(request): return render_to_response('users/user_list.html', { 'heading' : _('Users'), 'users' : users, - 'following' : [user.id for user in request.user.following()] + 'following' : [user.id for user in request.user.following()], + 'type': ContentType.objects.get_for_model(request.user), }, context_instance=RequestContext(request)) @anonymous_only diff --git a/settings.py b/settings.py index 540801f..e685b33 100644 --- a/settings.py +++ b/settings.py @@ -115,7 +115,7 @@ WELLKNOWN_HOSTMETA_HOSTS = ('localhost:8000',) MOZILLA_AMCD_HREF = '/meta/amcd.json' # Auth settings -LOGIN_URL = '/' +LOGIN_URL = '/login/' LOGIN_REDIRECT_URL = '/' ACCOUNT_ACTIVATION_DAYS = 7 diff --git a/templates/projects/project.html b/templates/projects/project.html index 43aa441..0581c92 100644 --- a/templates/projects/project.html +++ b/templates/projects/project.html @@ -6,4 +6,19 @@
{{ project.description }}
+{% if following %} + +{% else %} + +{% endif %} {% endblock %} diff --git a/templates/users/user_list.html b/templates/users/user_list.html index 2021fb2..18fe946 100644 --- a/templates/users/user_list.html +++ b/templates/users/user_list.html @@ -16,13 +16,15 @@ {% if user.id in following %} {% else %} {% endif %}