From 95895d23f1962fbced17660ad2e4a1b5121ac064 Mon Sep 17 00:00:00 2001 From: Klafyvel Date: Wed, 28 Feb 2018 21:25:44 +0100 Subject: [PATCH] Gestion des contenus. --- content/migrations/0001_initial.py | 6 +-- .../migrations/0002_content_school_owner.py | 22 ++++++++ content/models.py | 12 ++--- content/templates/content/content.html | 18 +++++++ content/templates/content/content_list.html | 13 ++--- content/urls.py | 21 +++++++- content/views.py | 50 +++++++++++++++++++ settings/admin.py | 3 +- settings/migrations/0001_initial.py | 12 +---- settings/models.py | 19 ------- settings/templates/settings/settings.html | 19 ------- settings/views.py | 3 +- templates/confirm_delete.html | 1 + users/migrations/0001_initial.py | 6 +-- users/models.py | 9 +++- users/templates/users/school.html | 21 ++++++++ users/urls.py | 6 +++ users/views.py | 13 ++++- 18 files changed, 175 insertions(+), 79 deletions(-) create mode 100644 content/migrations/0002_content_school_owner.py create mode 100644 content/templates/content/content.html create mode 100644 users/templates/users/school.html diff --git a/content/migrations/0001_initial.py b/content/migrations/0001_initial.py index ff3ec93..004b326 100644 --- a/content/migrations/0001_initial.py +++ b/content/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 2.0.1 on 2018-02-28 12:53 +# Generated by Django 2.0.1 on 2018-02-28 18:43 from django.db import migrations, models import django.db.models.deletion @@ -9,7 +9,6 @@ class Migration(migrations.Migration): initial = True dependencies = [ - ('auth', '0009_alter_user_last_name_max_length'), ] operations = [ @@ -27,9 +26,8 @@ class Migration(migrations.Migration): fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('name', models.CharField(max_length=255, verbose_name='Nom du contenu')), - ('content_url', models.URLField(null=True, verbose_name='URL du contenu')), + ('file', models.FileField(upload_to='', verbose_name='Fichier')), ('category', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='content.Category', verbose_name='Catégorie')), - ('group_owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='auth.Group')), ], ), ] diff --git a/content/migrations/0002_content_school_owner.py b/content/migrations/0002_content_school_owner.py new file mode 100644 index 0000000..affdd4d --- /dev/null +++ b/content/migrations/0002_content_school_owner.py @@ -0,0 +1,22 @@ +# Generated by Django 2.0.1 on 2018-02-28 18:43 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('content', '0001_initial'), + ('users', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='content', + name='school_owner', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='users.SchoolProfile'), + ), + ] diff --git a/content/models.py b/content/models.py index 2fefee0..99a61f4 100644 --- a/content/models.py +++ b/content/models.py @@ -3,6 +3,7 @@ from django.urls import reverse from django.contrib.auth.models import Group from django.conf import settings +from users.models import SchoolProfile class Category(models.Model): """Une catégorie de contenu.""" @@ -31,20 +32,19 @@ class Content(models.Model): max_length=255, verbose_name="Nom du contenu" ) - group_owner = models.ForeignKey( - Group, + school_owner = models.ForeignKey( + SchoolProfile, on_delete=models.CASCADE, ) - content_url = models.URLField( - verbose_name='URL du contenu', - null=True, - ) category = models.ForeignKey( Category, on_delete=models.SET_NULL, verbose_name="Catégorie", null=True ) + file = models.FileField( + verbose_name="Fichier" + ) def __str__(self): return self.name diff --git a/content/templates/content/content.html b/content/templates/content/content.html new file mode 100644 index 0000000..3f86de4 --- /dev/null +++ b/content/templates/content/content.html @@ -0,0 +1,18 @@ +
+
+ +
+

{{content.name}}

+

Contenu proposé par {{content.school_owner.group.name}}

+ +
+
+
+ diff --git a/content/templates/content/content_list.html b/content/templates/content/content_list.html index e74ab0b..d6fb175 100644 --- a/content/templates/content/content_list.html +++ b/content/templates/content/content_list.html @@ -27,18 +27,11 @@ $('html, body').animate({scrollTop: $('#category-content').offset().top}, 800);
- +
{% for content in contents %} -
-
-

{{content.name}}

-

Contenu proposé par {{content.group_owner.name}}

-
- -
+ {% include "content/content.html" %} {% endfor %} +


diff --git a/content/urls.py b/content/urls.py index 6ce5612..ab53c2a 100644 --- a/content/urls.py +++ b/content/urls.py @@ -5,6 +5,9 @@ from .views import ( CreateCategory, DeleteCategory, EditCategory, + CreateContent, + DeleteContent, + EditContent, ) app_name = 'content' @@ -28,5 +31,21 @@ urlpatterns = [ 'category/edit/', EditCategory.as_view(), name='category-edit', - ) + ), + path( + 'new', + CreateContent.as_view(), + name='content-new', + ), + path( + '/delete', + DeleteContent.as_view(), + name="content-delete", + ), + path( + '/edit', + EditContent.as_view(), + name="content-edit", + ), + ] diff --git a/content/views.py b/content/views.py index 56ea358..601a2e7 100644 --- a/content/views.py +++ b/content/views.py @@ -54,3 +54,53 @@ class EditCategory(generic.UpdateView): return context +class CreateContent(generic.CreateView): + """Création de contenu.""" + model = Content + fields = [ + 'name', + 'category', + 'file' + ] + template_name = "edit.html" + extra_context = { + 'title' : 'Envoi de contenu', + 'validate' : 'Envoyer' + } + + def get_success_url(self): + return self.object.school_owner.get_absolute_url() + + def form_valid(self, form): + form.instance.school_owner = self.request.user.userprofile.school + r = super().form_valid(form) + return r + + +class DeleteContent(generic.DeleteView): + """Suppression de contenu""" + model = Content + template_name = "confirm_delete.html" + + def get_success_url(self): + return self.object.school_owner.get_absolute_url() + + +class EditContent(generic.UpdateView): + """Édition d'un contenu""" + model = Content + template_name = "edit.html" + fields = [ + 'name', + 'category', + 'file' + ] + template_name = "edit.html" + extra_context = { + 'title' : 'Édition de contenu', + 'validate' : 'Envoyer' + } + + def get_success_url(self): + return self.object.school_owner.get_absolute_url() + diff --git a/settings/admin.py b/settings/admin.py index 0395fe9..49005c3 100644 --- a/settings/admin.py +++ b/settings/admin.py @@ -1,6 +1,5 @@ from django.contrib import admin -from .models import SiteSettings, ContentSettings +from .models import SiteSettings admin.site.register(SiteSettings) -admin.site.register(ContentSettings) diff --git a/settings/migrations/0001_initial.py b/settings/migrations/0001_initial.py index d805f92..035ff9e 100644 --- a/settings/migrations/0001_initial.py +++ b/settings/migrations/0001_initial.py @@ -1,7 +1,6 @@ -# Generated by Django 2.0.1 on 2018-02-28 12:53 +# Generated by Django 2.0.1 on 2018-02-28 18:43 from django.db import migrations, models -import settings.aes_field class Migration(migrations.Migration): @@ -12,15 +11,6 @@ class Migration(migrations.Migration): ] operations = [ - migrations.CreateModel( - name='ContentSettings', - fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('ftp_url', models.URLField(default='', max_length=255, verbose_name='URL du FTP')), - ('ftp_id', models.CharField(default='', max_length=255, verbose_name='Identifiant sur le FTP')), - ('ftp_pass', settings.aes_field.AESEncryptedField(default='', max_length=255, verbose_name='Mot de passe')), - ], - ), migrations.CreateModel( name='SiteSettings', fields=[ diff --git a/settings/models.py b/settings/models.py index 0ab68cf..73ec1cd 100644 --- a/settings/models.py +++ b/settings/models.py @@ -3,25 +3,6 @@ from django.db import models from .aes_field import AESEncryptedField -class ContentSettings(models.Model): - PRETTY_NAME = "Réglages des contenus" - ftp_url = models.URLField( - max_length=255, - verbose_name="URL du FTP", - default="", - ) - ftp_id = models.CharField( - max_length=255, - verbose_name="Identifiant sur le FTP", - default="" - ) - ftp_pass = AESEncryptedField( - max_length=255, - verbose_name="Mot de passe", - default="" - ) - - class SiteSettings(models.Model): PRETTY_NAME = "Réglages du site" allow_upload = models.BooleanField( diff --git a/settings/templates/settings/settings.html b/settings/templates/settings/settings.html index 3bffb27..c42dd06 100644 --- a/settings/templates/settings/settings.html +++ b/settings/templates/settings/settings.html @@ -61,7 +61,6 @@ {% endfor %}

Réglages

-

Réglages du site

Éditer @@ -84,22 +83,4 @@ {{site_settings.home_message}} - -

Réglage du contenu

-
- - Éditer - -
-
- - - - - - - - - -
URL du FTP{{content_settings.ftp_url}}
Identifiant du FTP{{content_settings.ftp_id}}
{% endblock %} diff --git a/settings/views.py b/settings/views.py index a841f4a..47b5c1e 100644 --- a/settings/views.py +++ b/settings/views.py @@ -2,7 +2,7 @@ from django.views.generic import TemplateView, UpdateView from django.urls import reverse_lazy from content.models import Category from users.models import SchoolProfile -from .models import ContentSettings, SiteSettings +from .models import SiteSettings class SettingsView(TemplateView): @@ -12,7 +12,6 @@ class SettingsView(TemplateView): context = super().get_context_data(**kwargs) context['categories'] = Category.objects.all() context['site_settings'], _ = SiteSettings.objects.get_or_create() - context['content_settings'], _ = ContentSettings.objects.get_or_create() context['schools'] = SchoolProfile.objects.all() context['settings'] = True return context diff --git a/templates/confirm_delete.html b/templates/confirm_delete.html index 524de5b..6d7994c 100644 --- a/templates/confirm_delete.html +++ b/templates/confirm_delete.html @@ -1,5 +1,6 @@ {% extends 'base.html' %} {% block content %} +

Suppression

{% csrf_token %}

Êtes-vous certain de vouloir supprimer "{{ object }}"?

diff --git a/users/migrations/0001_initial.py b/users/migrations/0001_initial.py index 97de584..f355ca7 100644 --- a/users/migrations/0001_initial.py +++ b/users/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 2.0.1 on 2018-02-28 12:53 +# Generated by Django 2.0.1 on 2018-02-28 18:43 from django.conf import settings from django.db import migrations, models @@ -10,8 +10,8 @@ class Migration(migrations.Migration): initial = True dependencies = [ - migrations.swappable_dependency(settings.AUTH_USER_MODEL), ('auth', '0009_alter_user_last_name_max_length'), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), ] operations = [ @@ -19,7 +19,7 @@ class Migration(migrations.Migration): name='SchoolProfile', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('group', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to='auth.Group')), + ('group', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='students', to='auth.Group')), ], ), migrations.CreateModel( diff --git a/users/models.py b/users/models.py index 496bc27..079d0cf 100644 --- a/users/models.py +++ b/users/models.py @@ -1,16 +1,23 @@ from django.db import models from django.contrib.auth.models import User, Group from django.db.models.signals import post_save +from django.urls import reverse from django.dispatch import receiver class SchoolProfile(models.Model): """Ajoute un champ pour distinguer les groupes écoles des autres.""" - group = models.OneToOneField(Group, on_delete=models.CASCADE) + group = models.OneToOneField( + Group, + on_delete=models.CASCADE, + related_name="students" + ) def __str__(self): return self.group.name + def get_absolute_url(self): + return reverse("users:school", kwargs={'pk':self.pk}) class UserProfile(models.Model): """Profil d'un utilisateur""" diff --git a/users/templates/users/school.html b/users/templates/users/school.html new file mode 100644 index 0000000..8165751 --- /dev/null +++ b/users/templates/users/school.html @@ -0,0 +1,21 @@ +{% extends 'base.html' %} +{% load bootstrap4 %} + +{% block content %} +

{{object.group.name}}

+ + + Éditer + + + + Ajouter un contenu + +
+
+
+{% for content in contents %} +{% include "content/content.html" %} +{%endfor%} +
+{% endblock %} diff --git a/users/urls.py b/users/urls.py index 9941870..e4b147c 100644 --- a/users/urls.py +++ b/users/urls.py @@ -9,6 +9,7 @@ from .views import ( Logout, PasswordChange, Profile, + School, ) app_name = 'users' @@ -48,6 +49,11 @@ urlpatterns = [ CreateSchool.as_view(), name='new-school' ), + path( + 'school/', + School.as_view(), + name='school' + ), path( 'school//edit', EditSchool.as_view(), diff --git a/users/views.py b/users/views.py index f2d0318..3757875 100644 --- a/users/views.py +++ b/users/views.py @@ -1,5 +1,5 @@ from django.contrib.auth.models import User, Group -from django.views.generic import CreateView, UpdateView, DeleteView +from django.views.generic import CreateView, UpdateView, DeleteView, DetailView from django.contrib.auth.views import LoginView, LogoutView, PasswordChangeView from django.contrib.auth.hashers import make_password from django.contrib.messages.views import SuccessMessageMixin @@ -7,6 +7,7 @@ from django.urls import reverse, reverse_lazy from django.shortcuts import get_object_or_404 from .models import UserProfile, SchoolProfile +from content.models import Content class CreateUser(CreateView): @@ -116,6 +117,16 @@ class DeleteSchool(DeleteView): model = Group +class School(DetailView): + model = SchoolProfile + template_name = "users/school.html" + + def get_context_data(self, **kwargs): + context = super().get_context_data() + context['contents'] = Content.objects.filter(school_owner=self.object) + return context + + class Logout(SuccessMessageMixin, LogoutView): success_message = "Vous vous êtes bien déconnecté."