From e00c948da978d867ca6fa6c271d5cde5ad2be7ea Mon Sep 17 00:00:00 2001 From: Skia Date: Wed, 10 May 2017 19:14:52 +0200 Subject: [PATCH] Improve Matmat, still needs a profile form --- matmat/migrations/0002_auto_20170510_1754.py | 41 +++++++++++++++ .../0003_remove_matmatcomment_is_moderated.py | 18 +++++++ matmat/models.py | 14 ++++-- .../{user_matmat.jinja => user_tools.jinja} | 9 +++- matmat/urls.py | 4 +- matmat/views.py | 50 ++++++++++++++++--- 6 files changed, 123 insertions(+), 13 deletions(-) create mode 100644 matmat/migrations/0002_auto_20170510_1754.py create mode 100644 matmat/migrations/0003_remove_matmatcomment_is_moderated.py rename matmat/templates/matmat/{user_matmat.jinja => user_tools.jinja} (62%) diff --git a/matmat/migrations/0002_auto_20170510_1754.py b/matmat/migrations/0002_auto_20170510_1754.py new file mode 100644 index 00000000..0a8a6f45 --- /dev/null +++ b/matmat/migrations/0002_auto_20170510_1754.py @@ -0,0 +1,41 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('matmat', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='matmat', + name='max_chars', + field=models.IntegerField(help_text='maximum number of characters allowed in a comment', default=400, verbose_name='maximum characters'), + ), + migrations.AddField( + model_name='matmatcomment', + name='content', + field=models.TextField(default='', verbose_name='content'), + ), + migrations.AddField( + model_name='matmatcomment', + name='is_moderated', + field=models.BooleanField(default=False, verbose_name='is moderated'), + ), + migrations.AddField( + model_name='matmatcomment', + name='target', + field=models.ForeignKey(verbose_name='target', to='matmat.MatmatUser', related_name='received_comments', default=0), + preserve_default=False, + ), + migrations.AlterField( + model_name='matmatcomment', + name='author', + field=models.ForeignKey(verbose_name='author', to='matmat.MatmatUser', related_name='given_comments', default=0), + preserve_default=False, + ), + ] diff --git a/matmat/migrations/0003_remove_matmatcomment_is_moderated.py b/matmat/migrations/0003_remove_matmatcomment_is_moderated.py new file mode 100644 index 00000000..09f32bfa --- /dev/null +++ b/matmat/migrations/0003_remove_matmatcomment_is_moderated.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('matmat', '0002_auto_20170510_1754'), + ] + + operations = [ + migrations.RemoveField( + model_name='matmatcomment', + name='is_moderated', + ), + ] diff --git a/matmat/models.py b/matmat/models.py index 460b3e93..e56d0bc0 100644 --- a/matmat/models.py +++ b/matmat/models.py @@ -56,6 +56,8 @@ class Matmat(models.Model): comments_deadline = models.DateField(_('comments deadline'), default=timezone.now, help_text=_("After this date, users won't be " "able to make comments anymore")) + max_chars = models.IntegerField(_('maximum characters'), default=400, + help_text=_('maximum number of characters allowed in a comment')) club = models.OneToOneField(Club, related_name='matmat') objects = MatmatManager() @@ -75,9 +77,12 @@ class Matmat(models.Model): def is_owned_by(self, user): return user.is_owner(self.club) - def can_be_viewed_by(self, user): + def can_be_edited_by(self, user): return user.can_edit(self.club) + def can_be_viewed_by(self, user): + return user.id in [u.user.id for u in self.users.all()] + class MatmatUser(models.Model): """ This class is only here to avoid cross references between the core, club, @@ -94,6 +99,9 @@ class MatmatComment(models.Model): """ author = models.ForeignKey(MatmatUser, verbose_name=_("author"), related_name='given_comments') target = models.ForeignKey(MatmatUser, verbose_name=_("target"), related_name='received_comments') - content = models.TextField(_("content"), default="", max_length=400) - is_moderated = models.BooleanField(_("is moderated"), default=False) + content = models.TextField(_("content"), default="") + def can_be_viewed_by(self, user): + if user.id == self.target.user.id: + return False + return user.id == self.author.user.id or user.can_edit(self.author.matmat) diff --git a/matmat/templates/matmat/user_matmat.jinja b/matmat/templates/matmat/user_tools.jinja similarity index 62% rename from matmat/templates/matmat/user_matmat.jinja rename to matmat/templates/matmat/user_tools.jinja index d368cf6a..2278665d 100644 --- a/matmat/templates/matmat/user_matmat.jinja +++ b/matmat/templates/matmat/user_tools.jinja @@ -14,10 +14,15 @@ {% else %}

{% trans matmat = user.matmat_user.matmat %}You are subscribed to the Matmatronch {{ matmat }}{% endtrans %}

-{% for u in user.matmat_user.matmat.users.all() %} +{% for u in user.matmat_user.matmat.users.exclude(id=user.matmat_user.id) %}
{{ u.user.get_display_name() }}
- Comment + {% set comment = u.received_comments.filter(author__id=user.matmat_user.id).first() %} + {% if comment %} + Edit comment + {% else %} + Comment + {% endif %}
{% endfor %} {% endif %} diff --git a/matmat/urls.py b/matmat/urls.py index 072dfa18..cc10b39e 100644 --- a/matmat/urls.py +++ b/matmat/urls.py @@ -31,6 +31,8 @@ urlpatterns = [ url(r'^(?P[0-9]+)/edit$', MatmatEditView.as_view(), name='edit'), url(r'^(?P[0-9]+)/delete/(?P[0-9]+)$', MatmatDeleteUserView.as_view(), name='delete_user'), url(r'^(?P[0-9]+)$', MatmatDetailView.as_view(), name='detail'), - url(r'^tools$', UserMatmatView.as_view(), name='user_tools'), + url(r'^(?P[0-9]+)/new_comment$', MatmatCommentCreateView.as_view(), name='new_comment'), + url(r'^comment/(?P[0-9]+)/edit$', MatmatCommentEditView.as_view(), name='edit_comment'), + url(r'^tools$', UserMatmatToolsView.as_view(), name='user_tools'), ] diff --git a/matmat/views.py b/matmat/views.py index 484905e2..9a8c736d 100644 --- a/matmat/views.py +++ b/matmat/views.py @@ -23,12 +23,14 @@ # from django.shortcuts import render, get_object_or_404, redirect +from django.core.urlresolvers import reverse_lazy, reverse from django.views.generic import ListView, DetailView, RedirectView, TemplateView from django.views.generic.edit import UpdateView, CreateView, DeleteView, FormView, SingleObjectMixin from django.utils.translation import ugettext_lazy as _ from django import forms +from django.forms.models import modelform_factory -from matmat.models import Matmat, MatmatUser +from matmat.models import Matmat, MatmatUser, MatmatComment from core.views.forms import SelectFile, SelectDate from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, TabedViewMixin, CanCreateMixin, QuickNotifMixin from core.models import User @@ -37,7 +39,7 @@ from club.models import Club class MatmatForm(forms.ModelForm): class Meta: model = Matmat - fields = ['subscription_deadline', 'comments_deadline'] + fields = ['subscription_deadline', 'comments_deadline', 'max_chars'] widgets = { 'subscription_deadline': SelectDate, 'comments_deadline': SelectDate, @@ -70,7 +72,7 @@ class MatmatEditView(CanEditPropMixin, UpdateView): template_name = 'core/edit.jinja' pk_url_kwarg = 'matmat_id' -class MatmatDetailView(CanViewMixin, DetailView): +class MatmatDetailView(CanEditMixin, DetailView): model = Matmat template_name = 'matmat/detail.jinja' pk_url_kwarg = 'matmat_id' @@ -94,11 +96,11 @@ class UserMatmatForm(forms.Form): "Be aware that you can subscribe only once, so don't play with that, " "or you will expose yourself to the admins' wrath!")) -class UserMatmatView(QuickNotifMixin, TemplateView): +class UserMatmatToolsView(QuickNotifMixin, TemplateView): """ Display a user's matmat tools """ - template_name = "core/user_matmat.jinja" + template_name = "matmat/user_tools.jinja" def post(self, request, *args, **kwargs): self.form = UserMatmatForm(request.POST) @@ -107,12 +109,46 @@ class UserMatmatView(QuickNotifMixin, TemplateView): matmat=self.form.cleaned_data['matmat']) matmat_user.save() self.quick_notif_list += ['qn_success'] - return super(UserMatmatView, self).get(request, *args, **kwargs) + return super(UserMatmatToolsView, self).get(request, *args, **kwargs) def get_context_data(self, **kwargs): - kwargs = super(UserMatmatView, self).get_context_data(**kwargs) + kwargs = super(UserMatmatToolsView, self).get_context_data(**kwargs) kwargs['user'] = self.request.user if not hasattr(self.request.user, 'matmat_user'): kwargs['subscribe_form'] = UserMatmatForm() return kwargs +class MatmatCommentFormView(): + """ + Create/edit a matmat comment + """ + model = MatmatComment + fields = ['content'] + + def get_form_class(self): + self.matmat = self.request.user.matmat_user.matmat + return modelform_factory(self.model, fields=self.fields, + widgets={ + 'content': forms.widgets.Textarea(attrs={'maxlength': self.matmat.max_chars}) + }, + help_texts={ + 'content': _("Maximum characters: %(max_length)s") % {'max_length': self.matmat.max_chars} + }) + + def get_success_url(self): + return reverse('matmat:user_tools') + +class MatmatCommentCreateView(MatmatCommentFormView, CreateView): + template_name = 'core/create.jinja' + + def form_valid(self, form): + target = get_object_or_404(MatmatUser, id=self.kwargs['user_id']) + form.instance.author = self.request.user.matmat_user + form.instance.target = target + return super(MatmatCommentCreateView, self).form_valid(form) + +class MatmatCommentEditView(MatmatCommentFormView, CanViewMixin, UpdateView): + pk_url_kwarg = "comment_id" + template_name = 'core/edit.jinja' + +