Improve Matmat, still needs a profile form

This commit is contained in:
Skia 2017-05-10 19:14:52 +02:00
parent b3bc33a319
commit e00c948da9
6 changed files with 123 additions and 13 deletions

View File

@ -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,
),
]

View File

@ -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',
),
]

View File

@ -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)

View File

@ -14,10 +14,15 @@
</form>
{% else %}
<p>{% trans matmat = user.matmat_user.matmat %}You are subscribed to the Matmatronch {{ matmat }}{% endtrans %}</p>
{% for u in user.matmat_user.matmat.users.all() %}
{% for u in user.matmat_user.matmat.users.exclude(id=user.matmat_user.id) %}
<div class="ib">
<div>{{ u.user.get_display_name() }}</div>
<a>Comment</a>
{% set comment = u.received_comments.filter(author__id=user.matmat_user.id).first() %}
{% if comment %}
<a href="{{ url("matmat:edit_comment", comment_id=comment.id) }}">Edit comment</a>
{% else %}
<a href="{{ url("matmat:new_comment", user_id=u.id) }}">Comment</a>
{% endif %}
</div>
{% endfor %}
{% endif %}

View File

@ -31,6 +31,8 @@ urlpatterns = [
url(r'^(?P<matmat_id>[0-9]+)/edit$', MatmatEditView.as_view(), name='edit'),
url(r'^(?P<matmat_id>[0-9]+)/delete/(?P<user_id>[0-9]+)$', MatmatDeleteUserView.as_view(), name='delete_user'),
url(r'^(?P<matmat_id>[0-9]+)$', MatmatDetailView.as_view(), name='detail'),
url(r'^tools$', UserMatmatView.as_view(), name='user_tools'),
url(r'^(?P<user_id>[0-9]+)/new_comment$', MatmatCommentCreateView.as_view(), name='new_comment'),
url(r'^comment/(?P<comment_id>[0-9]+)/edit$', MatmatCommentEditView.as_view(), name='edit_comment'),
url(r'^tools$', UserMatmatToolsView.as_view(), name='user_tools'),
]

View File

@ -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'