From b214ba448c14e1eb8348e1d7f365c2d665180092 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89lie=20Bouttier?= Date: Sat, 9 Jul 2016 18:16:41 +0200 Subject: [PATCH] Topic reviewers are User instead of Participation --- proposals/forms.py | 6 +- .../migrations/0005_auto_20160709_1457.py | 21 ++++++ proposals/models.py | 6 +- proposals/signals.py | 15 ++++- .../templates/proposals/topic_detail.html | 66 ------------------- proposals/templates/proposals/topic_list.html | 7 +- proposals/urls.py | 3 - proposals/utils.py | 2 +- proposals/views.py | 65 ++---------------- 9 files changed, 51 insertions(+), 140 deletions(-) create mode 100644 proposals/migrations/0005_auto_20160709_1457.py delete mode 100644 proposals/templates/proposals/topic_detail.html diff --git a/proposals/forms.py b/proposals/forms.py index dfeebf5..11a7de9 100644 --- a/proposals/forms.py +++ b/proposals/forms.py @@ -3,10 +3,12 @@ from django.forms.models import modelform_factory from proposals.models import Talk, Topic -__all__ = ['TalkForm', 'TopicForm'] +__all__ = ['TalkForm', 'TopicCreateForm', 'TopicUpdateForm'] TalkForm = modelform_factory(Talk, fields=['title', 'description', 'topics', 'event', 'speakers'], widgets={'topics': CheckboxSelectMultiple()}) -TopicForm = modelform_factory(Topic, fields=['name']) +TopicCreateForm = modelform_factory(Topic, fields=['name', 'reviewers']) + +TopicUpdateForm = modelform_factory(Topic, fields=['reviewers']) diff --git a/proposals/migrations/0005_auto_20160709_1457.py b/proposals/migrations/0005_auto_20160709_1457.py new file mode 100644 index 0000000..5848d3b --- /dev/null +++ b/proposals/migrations/0005_auto_20160709_1457.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.7 on 2016-07-09 14:57 +from __future__ import unicode_literals + +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('proposals', '0004_auto_20160706_1446'), + ] + + operations = [ + migrations.AlterField( + model_name='topic', + name='reviewers', + field=models.ManyToManyField(blank=True, to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/proposals/models.py b/proposals/models.py index 5d38801..1f2b257 100644 --- a/proposals/models.py +++ b/proposals/models.py @@ -25,7 +25,7 @@ class Topic(PonyConfModel): name = models.CharField(max_length=128, verbose_name='Name', unique=True) slug = AutoSlugField(populate_from='name', unique=True) - reviewers = models.ManyToManyField(Participation, blank=True) + reviewers = models.ManyToManyField(User, blank=True) objects = models.Manager() on_site = CurrentSiteManager() @@ -38,7 +38,7 @@ class Topic(PonyConfModel): return self.name def get_absolute_url(self): - return reverse('show-topic', kwargs={'slug': self.slug}) + return reverse('list-talks-by-topic', kwargs={'topic': self.slug}) class Talk(PonyConfModel): @@ -72,7 +72,7 @@ class Talk(PonyConfModel): participation = Participation.on_site.get(user=user) except Participation.DoesNotExists: return False - return participation.orga or self.topics.filter(reviewers=participation).exists() + return participation.orga or self.topics.filter(reviewers=participation.user).exists() def is_editable_by(self, user): return user == self.proposer or user in self.speakers.all() or self.is_moderable_by(user) diff --git a/proposals/signals.py b/proposals/signals.py index d5a653b..0677202 100644 --- a/proposals/signals.py +++ b/proposals/signals.py @@ -1,4 +1,9 @@ -from django.dispatch import Signal +from django.dispatch import Signal, receiver +from django.db.models.signals import m2m_changed + + +from .models import Topic +from accounts.models import Participation __all__ = [ 'talk_added', 'talk_edited' ] @@ -6,3 +11,11 @@ __all__ = [ 'talk_added', 'talk_edited' ] talk_added = Signal(providing_args=["sender", "instance", "author"]) talk_edited = Signal(providing_args=["sender", "instance", "author"]) + + +@receiver(m2m_changed, sender=Topic.reviewers.through, dispatch_uid="Create Participation for reviewers") +def create_participation_for_reviewers(sender, instance, action, reverse, model, pk_set, using, **kwargs): + if action != "pre_add": + pass + for reviewer in instance.reviewers.all(): + Participation.objects.get_or_create(user=reviewer, site=instance.site) diff --git a/proposals/templates/proposals/topic_detail.html b/proposals/templates/proposals/topic_detail.html deleted file mode 100644 index d051d83..0000000 --- a/proposals/templates/proposals/topic_detail.html +++ /dev/null @@ -1,66 +0,0 @@ -{% extends 'base.html' %} - -{% load staticfiles accounts_tags %} - -{% block topictab %} class="active"{% endblock %} - -{% block css %} -{{ block.super }} - - -{% endblock %} - -{% block content %} - -

{{ topic }}

- -{% if request.user.is_superuser %} -edit
-{% endif %} -{{ topic.talks.count }} talk{{ topic.talks.count|pluralize }}
- -Reviewers: - - -{% if request|orga %} -
- {% csrf_token %} -
-
- -
- -
-
-
-
-{% endif %} - - -{% endblock %} - -{% block js_end %} -{{ block.super }} - - - - - - -{% endblock %} diff --git a/proposals/templates/proposals/topic_list.html b/proposals/templates/proposals/topic_list.html index 514652b..0950f29 100644 --- a/proposals/templates/proposals/topic_list.html +++ b/proposals/templates/proposals/topic_list.html @@ -13,13 +13,12 @@
  • {% if request|staff %} {{ topic }}: - {{ topic.reviewers.count }} reviewer{{ topic.reviewers.count|pluralize }} + {{ topic.reviewers.count }} reviewer{{ topic.reviewers.count|pluralize }} and - {{ topic.talks.count }} talk{{ topic.talks.count|pluralize }} + {{ topic.talks.count }} talk{{ topic.talks.count|pluralize }} {% else %} - {{ topic }} + {{ topic }} {% endif %} - {% if request.user.is_superuser %} - edit{% endif %}
  • {% empty %}
  • No topics.
  • diff --git a/proposals/urls.py b/proposals/urls.py index dedae1e..d627693 100644 --- a/proposals/urls.py +++ b/proposals/urls.py @@ -12,10 +12,7 @@ urlpatterns = [ url(r'^talk/by-topic/(?P[-\w]+)$', views.talk_list_by_topic, name='list-talks-by-topic'), url(r'^topic/$', views.TopicList.as_view(), name='list-topics'), url(r'^topic/add/$', views.TopicCreate.as_view(), name='add-topic'), - url(r'^topic/(?P[-\w]+)/$', views.TopicDetail.as_view(), name='show-topic'), url(r'^topic/(?P[-\w]+)/edit/$', views.TopicUpdate.as_view(), name='edit-topic'), - url(r'^topic/(?P[-\w]+)/add-reviewer/$', views.topic_add_reviewer, name='add-reviewer'), - url(r'^topic/(?P[-\w]+)/remove-reviewer/(?P[\w.@+-]+)/$', views.topic_remove_reviewer, name='remove-reviewer'), url(r'^speakers/$', views.SpeakerList.as_view(), name='list-speakers'), url(r'^speaker/(?P[\w.@+-]+)$', views.user_details, name='show-speaker'), ] diff --git a/proposals/utils.py b/proposals/utils.py index f51889b..52f191b 100644 --- a/proposals/utils.py +++ b/proposals/utils.py @@ -11,5 +11,5 @@ def query_sum(queryset, field): def allowed_talks(talks, request): participation = Participation.on_site.get(user=request.user) if not participation.is_orga(): - talks = talks.filter(Q(topics__reviewers=participation) | Q(speakers=request.user) | Q(proposer=request.user)) + talks = talks.filter(Q(topics__reviewers=participation.user) | Q(speakers=request.user) | Q(proposer=request.user)) return talks.distinct() diff --git a/proposals/views.py b/proposals/views.py index 19d3ca6..59c773b 100644 --- a/proposals/views.py +++ b/proposals/views.py @@ -15,7 +15,7 @@ from accounts.mixins import OrgaRequiredMixin, StaffRequiredMixin, SuperuserRequ from accounts.models import Participation from accounts.utils import is_orga -from .forms import TalkForm, TopicForm +from .forms import TalkForm, TopicCreateForm, TopicUpdateForm from .models import Talk, Topic, Vote from .utils import allowed_talks from .signals import * @@ -86,7 +86,6 @@ class TalkDetail(LoginRequiredMixin, DetailView): class TopicMixin(object): model = Topic queryset = Topic.on_site.all() - form_class = TopicForm class TopicList(LoginRequiredMixin, TopicMixin, ListView): @@ -94,69 +93,15 @@ class TopicList(LoginRequiredMixin, TopicMixin, ListView): class TopicCreate(OrgaRequiredMixin, TopicMixin, CreateView): + form_class = TopicCreateForm def form_valid(self, form): form.instance.site = get_current_site(self.request) return super(TopicCreate, self).form_valid(form) -class TopicUpdate(SuperuserRequiredMixin, TopicMixin, UpdateView): - pass - - -class TopicDetail(StaffRequiredMixin, TopicMixin, DetailView): - pass - - -@login_required -def topic_add_reviewer(request, slug): - if not Participation.objects.get(user=request.user).is_orga(): - raise PermissionDenied() - - topic = get_object_or_404(Topic, slug=slug) - - if request.method == 'POST': - user = request.POST.get('user') - try: - user = User.objects.get(username=user) - except ObjectDoesNotExist: - messages.error(request, 'User not found.') - else: - participation, created = Participation.on_site.get_or_create(user=user, site=get_current_site(request)) - if participation in topic.reviewers.all(): - messages.info(request, '%s is already a reviewer of this topic.' % participation) - else: - topic.reviewers.add(participation) - topic.save() - messages.success(request, '%s added to reviewers!' % participation) - return redirect(topic.get_absolute_url()) - else: - term = request.GET.get('term') - if not term: - raise Http404() - query = Q(username__icontains=term) \ - | Q(first_name__icontains=term) \ - | Q(last_name__icontains=term) - users = User.objects \ - .exclude(id__in=topic.reviewers.values('user__id')) \ - .filter(query)[:10] - response = [] - for user in users: - response += [{ - 'label': str(user.profile), - 'value': user.username, - }] - return JsonResponse(response, safe=False) - - -@login_required -def topic_remove_reviewer(request, slug, username): - if not Participation.objects.get(user=request.user).is_orga(): - raise PermissionDenied() - topic = get_object_or_404(Topic, slug=slug) - participation = get_object_or_404(Participation, user__username=username) - topic.reviewers.remove(participation) - messages.success(request, '%s removed from reviewers!' % participation) - return redirect(topic.get_absolute_url()) +class TopicUpdate(OrgaRequiredMixin, TopicMixin, UpdateView): + def get_form_class(self): + return TopicCreateForm if self.request.user.is_superuser else TopicUpdateForm class SpeakerList(StaffRequiredMixin, ListView):