Upload de contenus
This commit is contained in:
parent
c606570a99
commit
821a337582
10 changed files with 159 additions and 7 deletions
20
content/forms.py
Normal file
20
content/forms.py
Normal file
|
@ -0,0 +1,20 @@
|
|||
from django import forms
|
||||
|
||||
from .models import Content, Category
|
||||
|
||||
|
||||
class CreateContent(forms.ModelForm):
|
||||
class Meta:
|
||||
model = Content
|
||||
fields = [
|
||||
'name',
|
||||
'category',
|
||||
'file',
|
||||
]
|
||||
|
||||
def __init__(self, school, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.instance.school = school
|
||||
|
||||
already_created = map(lambda x:x.category.pk, school.content_set.select_related('category'))
|
||||
self.fields['category'].queryset = Category.objects.exclude(pk__in=already_created)
|
19
content/migrations/0003_auto_20180309_1233.py
Normal file
19
content/migrations/0003_auto_20180309_1233.py
Normal file
|
@ -0,0 +1,19 @@
|
|||
# Generated by Django 2.0.1 on 2018-03-09 11:33
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('content', '0002_auto_20180309_1116'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='content',
|
||||
name='school_owner',
|
||||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='users.School'),
|
||||
),
|
||||
]
|
19
content/migrations/0004_auto_20180309_1235.py
Normal file
19
content/migrations/0004_auto_20180309_1235.py
Normal file
|
@ -0,0 +1,19 @@
|
|||
# Generated by Django 2.0.1 on 2018-03-09 11:35
|
||||
|
||||
import content.models
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('content', '0003_auto_20180309_1233'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='content',
|
||||
name='file',
|
||||
field=models.FileField(upload_to=content.models.get_upload_to, verbose_name='Fichier'),
|
||||
),
|
||||
]
|
18
content/migrations/0005_auto_20180309_1255.py
Normal file
18
content/migrations/0005_auto_20180309_1255.py
Normal file
|
@ -0,0 +1,18 @@
|
|||
# Generated by Django 2.0.1 on 2018-03-09 11:55
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('content', '0004_auto_20180309_1235'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='content',
|
||||
name='file',
|
||||
field=models.FileField(upload_to='', verbose_name='Fichier'),
|
||||
),
|
||||
]
|
19
content/migrations/0006_auto_20180309_1257.py
Normal file
19
content/migrations/0006_auto_20180309_1257.py
Normal file
|
@ -0,0 +1,19 @@
|
|||
# Generated by Django 2.0.1 on 2018-03-09 11:57
|
||||
|
||||
import content.models
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('content', '0005_auto_20180309_1255'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='content',
|
||||
name='file',
|
||||
field=models.FileField(upload_to=content.models.get_upload_to, verbose_name='Fichier'),
|
||||
),
|
||||
]
|
25
content/migrations/0007_auto_20180309_1315.py
Normal file
25
content/migrations/0007_auto_20180309_1315.py
Normal file
|
@ -0,0 +1,25 @@
|
|||
# Generated by Django 2.0.1 on 2018-03-09 12:15
|
||||
|
||||
import content.models
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('content', '0006_auto_20180309_1257'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='content',
|
||||
name='file',
|
||||
field=models.FileField(upload_to=content.models.get_upload_to, validators=[content.models.validate_file_extension], verbose_name='Fichier'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='content',
|
||||
name='school_owner',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='users.School'),
|
||||
),
|
||||
]
|
|
@ -35,14 +35,16 @@ class Category(models.Model):
|
|||
|
||||
def get_upload_to(instance, filename):
|
||||
extension = filename.split('.')[-1]
|
||||
return "static/media/"+instance.school.name+"/"+instance.category.name+'.'+extension
|
||||
proper_school = ''.join(e for e in instance.school_owner.name if e.isalnum() and ord(e)<128)
|
||||
proper_name = ''.join(e for e in instance.category.name if e.isalnum() and ord(e)<128)
|
||||
return "static/media/"+proper_school+"/"+proper_name+'.'+extension
|
||||
|
||||
|
||||
def validate_file_extension(value):
|
||||
ext = os.path.splitext(value.name)[1] # [0] returns path+filename
|
||||
valid_extensions = ['mp4', 'avi', 'mov']
|
||||
valid_extensions = ['.mp4', '.avi', '.mov']
|
||||
if not ext.lower() in valid_extensions:
|
||||
raise ValidationError(u'Unsupported file extension.')
|
||||
raise ValidationError(u'Format non supporté : {}'.format(ext))
|
||||
|
||||
|
||||
class Content(models.Model):
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
from django.urls import path
|
||||
|
||||
from . import views
|
||||
|
||||
from .views import (
|
||||
CreateCategory,
|
||||
ViewCategory,
|
||||
|
@ -33,8 +35,8 @@ urlpatterns = [
|
|||
name='category-edit',
|
||||
),
|
||||
path(
|
||||
'new',
|
||||
CreateContent.as_view(),
|
||||
'new/<int:school_pk>',
|
||||
views.create_content,
|
||||
name='content-new',
|
||||
),
|
||||
path(
|
||||
|
|
|
@ -1,10 +1,13 @@
|
|||
from django.views import generic
|
||||
from django.urls import reverse, reverse_lazy
|
||||
from django.shortcuts import get_object_or_404, redirect
|
||||
from django.shortcuts import get_object_or_404, redirect, render
|
||||
from django.contrib.auth.mixins import PermissionRequiredMixin, LoginRequiredMixin
|
||||
from django.contrib.admin.views.decorators import staff_member_required
|
||||
from django.contrib import messages
|
||||
|
||||
from .models import Content, Category
|
||||
from users.models import School
|
||||
from . import forms
|
||||
from settings.models import SiteSettings
|
||||
|
||||
|
||||
|
@ -121,3 +124,28 @@ class EditContent(PermissionRequiredMixin, generic.UpdateView):
|
|||
messages.error(request, "Le téléversement de contenu n'est pas autorisé actuellement.")
|
||||
return redirect(reverse("home"))
|
||||
return super().dispatch(request, *args, **kwargs)
|
||||
|
||||
|
||||
def create_content(request, school_pk):
|
||||
settings,_ = SiteSettings.objects.get_or_create()
|
||||
if not settings.allow_upload :
|
||||
messages.error(request, "Le téléversement de contenu n'est pas autorisé actuellement.")
|
||||
return redirect(reverse("home"))
|
||||
school = get_object_or_404(School, pk=school_pk)
|
||||
can = request.user.is_staff or request.user == school.admin
|
||||
if not can:
|
||||
messages.error(request, 'Vous ne pouvez pas accéder à cette page')
|
||||
return redirect(reverse('home'))
|
||||
|
||||
content_form = forms.CreateContent(school, request.POST or None, request.FILES or None)
|
||||
content_form.instance.school_owner = school
|
||||
|
||||
if content_form.is_valid():
|
||||
content_form.save()
|
||||
messages.success(request, "Contenu ajouté.")
|
||||
return redirect(school.get_absolute_url())
|
||||
return render(request, 'edit.html', {
|
||||
'form' : content_form,
|
||||
'title' : 'Ajout de contenu',
|
||||
'validate' : 'Ajouter'
|
||||
})
|
||||
|
|
|
@ -147,7 +147,7 @@ Inscription validée
|
|||
</a>
|
||||
|
||||
<h2>Contenus</h2>
|
||||
<a class="btn btn-success btn-sm" href="{% url 'content:content-new' %}">
|
||||
<a class="btn btn-success btn-sm" href="{% url 'content:content-new' school.pk %}">
|
||||
<i class="fa fa-plus"></i>
|
||||
Ajouter un contenu
|
||||
</a>
|
||||
|
|
Loading…
Reference in a new issue