From 5bf5d0277cf79d27bdbf3ce6092f39a96ef0c2d9 Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Sat, 15 Jun 2019 17:01:25 +0200 Subject: [PATCH] pedagogy: create view and form for UV WARNING: A new group has been created, to be set by the infra team at deployment !!! --- core/management/commands/populate.py | 1 + pedagogy/forms.py | 70 ++++++++++++++++++++++++++++ pedagogy/migrations/0001_initial.py | 28 +++++------ pedagogy/models.py | 24 ++++++---- pedagogy/views.py | 26 +++++++++-- sith/settings.py | 5 +- 6 files changed, 124 insertions(+), 30 deletions(-) create mode 100644 pedagogy/forms.py diff --git a/core/management/commands/populate.py b/core/management/commands/populate.py index 312744c6..96aa1a8e 100644 --- a/core/management/commands/populate.py +++ b/core/management/commands/populate.py @@ -84,6 +84,7 @@ class Command(BaseCommand): Group(name="Banned to subscribe").save() Group(name="SAS admin").save() Group(name="Forum admin").save() + Group(name="Pedagogy admin").save() self.reset_index("core", "auth") root = User( id=0, diff --git a/pedagogy/forms.py b/pedagogy/forms.py new file mode 100644 index 00000000..57b75385 --- /dev/null +++ b/pedagogy/forms.py @@ -0,0 +1,70 @@ +# -*- coding:utf-8 -* +# +# Copyright 2016,2017 +# - Skia +# +# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM, +# http://ae.utbm.fr. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License a published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +# details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple +# Place - Suite 330, Boston, MA 02111-1307, USA. +# +# + +from django import forms + +from core.views.forms import MarkdownInput +from core.models import User + +from pedagogy.models import UV + + +class UVForm(forms.ModelForm): + """ + Form handeling creation and edit of an UV + """ + + class Meta: + model = UV + fields = ( + "code", + "author", + "credit_type", + "semester", + "language", + "credits", + "hours_CM", + "hours_TD", + "hours_TP", + "hours_THE", + "hours_TE", + "manager", + "title", + "objectives", + "program", + "skills", + "key_concepts", + ) + widgets = { + "objectives": MarkdownInput, + "program": MarkdownInput, + "skills": MarkdownInput, + "key_concepts": MarkdownInput, + "author": forms.HiddenInput, + } + + def __init__(self, author_id, *args, **kwargs): + super(UVForm, self).__init__(*args, **kwargs) + self.fields["author"].queryset = User.objects.filter(id=author_id).all() + self.fields["author"].initial = author_id diff --git a/pedagogy/migrations/0001_initial.py b/pedagogy/migrations/0001_initial.py index f5a5b5bf..9f8f8e76 100644 --- a/pedagogy/migrations/0001_initial.py +++ b/pedagogy/migrations/0001_initial.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Generated by Django 1.11.20 on 2019-06-15 12:13 +# Generated by Django 1.11.20 on 2019-06-15 15:00 from __future__ import unicode_literals from django.conf import settings @@ -76,9 +76,11 @@ class Migration(migrations.Migration): ("FREE", "Free"), ("CS", "CS"), ("TM", "TM"), + ("OM", "OM"), + ("QC", "QC"), ("EC", "EC"), - ("CG", "CG"), ("RN", "RN"), + ("ST", "ST"), ("EXT", "EXT"), ], default="FREE", @@ -96,7 +98,7 @@ class Migration(migrations.Migration): ("AUTOMN_AND_SPRING", "Autumn and spring"), ], default="CLOSED", - max_length=10, + max_length=20, verbose_name="semester", ), ), @@ -129,10 +131,11 @@ class Migration(migrations.Migration): ("objectives", models.TextField(verbose_name="objectives")), ("program", models.TextField(verbose_name="program")), ("skills", models.TextField(verbose_name="skills")), - ("key_concepts", models.TextField(verbose_name="key_concepts")), + ("key_concepts", models.TextField(verbose_name="key concepts")), ( "hours_CM", models.IntegerField( + default=0, validators=[django.core.validators.MinValueValidator(0)], verbose_name="hours CM", ), @@ -140,6 +143,7 @@ class Migration(migrations.Migration): ( "hours_TD", models.IntegerField( + default=0, validators=[django.core.validators.MinValueValidator(0)], verbose_name="hours TD", ), @@ -147,6 +151,7 @@ class Migration(migrations.Migration): ( "hours_TP", models.IntegerField( + default=0, validators=[django.core.validators.MinValueValidator(0)], verbose_name="hours TP", ), @@ -154,6 +159,7 @@ class Migration(migrations.Migration): ( "hours_THE", models.IntegerField( + default=0, validators=[django.core.validators.MinValueValidator(0)], verbose_name="hours THE", ), @@ -161,6 +167,7 @@ class Migration(migrations.Migration): ( "hours_TE", models.IntegerField( + default=0, validators=[django.core.validators.MinValueValidator(0)], verbose_name="hours TE", ), @@ -171,18 +178,7 @@ class Migration(migrations.Migration): on_delete=django.db.models.deletion.CASCADE, related_name="created_UVs", to=settings.AUTH_USER_MODEL, - verbose_name="created UVs", - ), - ), - ( - "moderator", - models.ForeignKey( - blank=True, - null=True, - on_delete=django.db.models.deletion.CASCADE, - related_name="moderated_UVs", - to=settings.AUTH_USER_MODEL, - verbose_name="moderated UVs", + verbose_name="author", ), ), ], diff --git a/pedagogy/models.py b/pedagogy/models.py index 3385337e..92474722 100644 --- a/pedagogy/models.py +++ b/pedagogy/models.py @@ -37,6 +37,12 @@ class UV(models.Model): Contains infos about an UV (course) """ + def is_owned_by(self, user): + """ + Can be created by superuser, root or pedagogy admin user + """ + return user.is_in_group(settings.SITH_GROUP_PEDAGOGY_ADMIN_ID) + code = models.CharField( _("code"), max_length=10, @@ -50,17 +56,10 @@ class UV(models.Model): ) ], ) - moderator = models.ForeignKey( - User, - related_name="moderated_UVs", - verbose_name=_("moderated UVs"), - null=True, - blank=True, - ) author = models.ForeignKey( User, related_name="created_UVs", - verbose_name=_("created UVs"), + verbose_name=_("author"), null=False, blank=False, ) @@ -73,7 +72,7 @@ class UV(models.Model): manager = models.CharField(_("uv manager"), max_length=300) semester = models.CharField( _("semester"), - max_length=10, + max_length=20, choices=settings.SITH_PEDAGOGY_UV_SEMESTER, default=settings.SITH_PEDAGOGY_UV_SEMESTER[0][0], ) @@ -98,7 +97,7 @@ class UV(models.Model): objectives = models.TextField(_("objectives")) program = models.TextField(_("program")) skills = models.TextField(_("skills")) - key_concepts = models.TextField(_("key_concepts")) + key_concepts = models.TextField(_("key concepts")) # Hours types CM, TD, TP, THE and TE # Kind of dirty but I have nothing else in mind for now @@ -107,30 +106,35 @@ class UV(models.Model): validators=[validators.MinValueValidator(0)], blank=False, null=False, + default=0, ) hours_TD = models.IntegerField( _("hours TD"), validators=[validators.MinValueValidator(0)], blank=False, null=False, + default=0, ) hours_TP = models.IntegerField( _("hours TP"), validators=[validators.MinValueValidator(0)], blank=False, null=False, + default=0, ) hours_THE = models.IntegerField( _("hours THE"), validators=[validators.MinValueValidator(0)], blank=False, null=False, + default=0, ) hours_TE = models.IntegerField( _("hours TE"), validators=[validators.MinValueValidator(0)], blank=False, null=False, + default=0, ) diff --git a/pedagogy/views.py b/pedagogy/views.py index 41cd6c08..e1e2f93d 100644 --- a/pedagogy/views.py +++ b/pedagogy/views.py @@ -23,8 +23,18 @@ # from django.views.generic import CreateView, DeleteView, DetailView, ListView, FormView +from django.core.urlresolvers import reverse_lazy -from core.views import DetailFormView +from core.views import ( + DetailFormView, + CanCreateMixin, + CanEditMixin, + CanViewMixin, + CanEditPropMixin, +) + +from pedagogy.forms import UVForm +from pedagogy.models import UV class UVDetailFormView(DetailFormView): @@ -76,12 +86,22 @@ class UVModerationFormView(FormView): pass -class UVCreateView(CreateView): +class UVCreateView(CanCreateMixin, CreateView): """ Add a new UV (Privileged) """ - pass + model = UV + form_class = UVForm + template_name = "core/edit.jinja" + + def get_form_kwargs(self): + kwargs = super(UVCreateView, self).get_form_kwargs() + kwargs["author_id"] = self.request.user.id + return kwargs + + def get_success_url(self): + return reverse_lazy("pedagogy:uv_detail", kwargs={"uv_id": self.object.id}) class UVDeleteView(DeleteView): diff --git a/sith/settings.py b/sith/settings.py index 39e0e517..643d01ca 100644 --- a/sith/settings.py +++ b/sith/settings.py @@ -322,6 +322,7 @@ SITH_GROUP_BANNED_COUNTER_ID = 9 SITH_GROUP_BANNED_SUBSCRIPTION_ID = 10 SITH_GROUP_SAS_ADMIN_ID = 11 SITH_GROUP_FORUM_ADMIN_ID = 12 +SITH_GROUP_PEDAGOGY_ADMIN_ID = 13 SITH_CLUB_REFOUND_ID = 89 @@ -408,9 +409,11 @@ SITH_PEDAGOGY_UV_TYPE = [ ("FREE", _("Free")), ("CS", _("CS")), ("TM", _("TM")), + ("OM", _("OM")), + ("QC", _("QC")), ("EC", _("EC")), - ("CG", _("CG")), ("RN", _("RN")), + ("ST", _("ST")), ("EXT", _("EXT")), ]