Merge branch 'master' into django2

This commit is contained in:
Martin Pépin 2018-05-01 10:09:50 +02:00
commit 1ea12ed3f9
7 changed files with 191 additions and 128 deletions

View file

@ -45,7 +45,7 @@ def calendar(request, pYear, pMonth):
lPreviousMonth = lMonth - 1
if lPreviousMonth == 0:
lPreviousMonth = 12
lPreviousYear - 1
lPreviousYear -= 1
lNextYear = lYear
lNextMonth = lMonth + 1
if lNextMonth == 13:
@ -105,6 +105,7 @@ def view_eventbis(request, id):
return render(request, 'calendrier/view_event.html', locals())
# XXX: Horrible nasty code duplication. Go to hell RikM
@chef_required
def resend(request, id):
if request.method == "POST":
@ -113,7 +114,6 @@ def resend(request, id):
ev = get_object_or_404(Event, id=id)
users = ErnestoUser.objects.filter(is_ernesto=True)
message = form.cleaned_data["message"]
try:
for user in users:
if user.mails:
Subject = "[Pouet-Pouet] %s, le %s" % (
@ -131,24 +131,18 @@ def resend(request, id):
'Pour répondre non, allez sur %s%s\n'
'Pour répondre peut-être, allez sur %s%s\n') \
% (domain,
reverse_lazy('view-event', args=[obj.id]),
reverse_lazy('view-event', args=[ev.id]),
domain,
reverse_lazy(
'calendrier.reponse_event',
args=[user.slug, obj.slug, "oui"]),
reverse_lazy('calendrier.reponse_event', args=[user.slug, ev.slug, "oui"]),
domain,
reverse_lazy(
'calendrier.reponse_event',
args=[user.slug, obj.slug, "non"]),
reverse_lazy('calendrier.reponse_event', args=[user.slug, ev.slug, "non"]),
domain,
reverse_lazy(
'calendrier.reponse_event',
args=[user.slug, obj.slug, "pe"]))
reverse_lazy('calendrier.reponse_event', args=[user.slug, ev.slug, "pe"]))
try:
send_mail(Subject, text, sender, receivers)
return redirect("calendrier.views.view_eventbis", id)
except:
erreur = "Une erreur est survenue, le mail n'a pas pu être " \
+ "envoyé."
erreur = "Une erreur est survenue, le mail n'a pas pu être envoyé."
return redirect("calendrier.views.view_eventbis", id)
else:
form = SendMail()
return render(request, "calendrier/resend.html", locals())
@ -194,7 +188,6 @@ def create_event(request):
users = ErnestoUser.objects.filter(is_ernesto=True)
for user in users:
if user.mails:
try:
Subject = "[Pouet-Pouet] %s, le %s" % (nom, date)
sender = 'fanfare@ens.fr'
receivers = [user.user.email]
@ -211,21 +204,15 @@ def create_event(request):
% (domain,
reverse('view-event', args=[obj.id]),
domain,
reverse(
'calendrier.reponse_event',
args=[user.slug, obj.slug, "oui"]),
reverse('calendrier.reponse_event', args=[user.slug, obj.slug, "oui"]),
domain,
reverse(
'calendrier.reponse_event',
args=[user.slug, obj.slug, "non"]),
reverse('calendrier.reponse_event', args=[user.slug, obj.slug, "non"]),
domain,
reverse(
'calendrier.reponse_event',
args=[user.slug, obj.slug, "pe"]))
reverse('calendrier.reponse_event', args=[user.slug, obj.slug, "pe"]))
try:
send_mail(Subject, text, sender, receivers)
except:
erreur = "Une erreur est survenue, le mail n'a " \
+ "pas pu être envoyé."
erreur = "Une erreur est survenue, le mail n'a pas pu être envoyé."
else:
form = EventForm()

9
partitions/admin.py Normal file
View file

@ -0,0 +1,9 @@
from django.contrib import admin
from .models import Category
@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
list_display = ["name", "order"]
list_editable = ["order"]
ordering = ["order"]

View file

@ -0,0 +1,80 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
def create_categories(apps, schema_editor):
# Insert the previously hardcoded categories in the database
Category = apps.get_model("partitions", "Category")
Category.objects.bulk_create([
Category(name="Partitions actives", order=1),
Category(name="Partitions optionnelles", order=2),
Category(name="Partitions à venir", order=3),
Category(name="Archives", order=4)
])
def set_new_categories(apps, schema_editor):
Category = apps.get_model("partitions", "Category")
PartitionSet = apps.get_model("partitions", "PartitionSet")
assoc_list = Category.objects.order_by("order")
mapping = {
"active": assoc_list[0],
"incoming": assoc_list[2],
"old": assoc_list[3],
"optional": assoc_list[1]
}
for par_set in PartitionSet.objects.all():
par_set.category = mapping[par_set.category_old]
par_set.save()
def reset_old_categories(apps, schema_editor):
Category = apps.get_model("partitions", "Category")
PartitionSet = apps.get_model("partition", "PartitionSet")
assoc_list = Category.values_list("order", "id").order_by("order")
mapping = {
assoc_list[0][1]: "active",
assoc_list[2][1]: "incoming",
assoc_list[3][1]: "old",
assoc_list[1][1]: "optional"
}
for par_set in PartitionSet.objects.all():
par_set.category_old = mapping[par_set.category]
par_set.save()
class Migration(migrations.Migration):
dependencies = [
('partitions', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='Category',
fields=[
('id', models.AutoField(verbose_name='ID', primary_key=True, serialize=False, auto_created=True)),
('name', models.CharField(max_length=127)),
('order', models.IntegerField(verbose_name='order')),
],
options={'verbose_name': 'catégorie', 'verbose_name_plural': 'catégories'},
),
migrations.RunPython(create_categories, migrations.RunPython.noop),
migrations.RenameField(model_name="partitionset", old_name="category", new_name="category_old"),
migrations.AddField(
model_name='partitionset',
name='category',
field=models.ForeignKey(
verbose_name='Type de partition',
to='partitions.Category',
on_delete=django.db.models.deletion.PROTECT,
default=1 # Dummy, will be set by the RunPython operation
),
preserve_default=False,
),
migrations.RunPython(set_new_categories, migrations.RunPython.noop),
migrations.RemoveField(model_name="partitionset", name="category_old"),
]

View file

@ -4,12 +4,16 @@ from django.conf import settings
from django.db import models
PARTITION_TYPES = (
("active", "Actif"),
("incoming", "À venir"),
("old", "Archive"),
("optional", "Optionnel"),
)
class Category(models.Model):
name = models.CharField(max_length=127)
order = models.IntegerField(verbose_name="order")
def __str__(self):
return self.name
class Meta:
verbose_name = "catégorie"
verbose_name_plural = "catégories"
class Partition(models.Model):
@ -28,10 +32,12 @@ class Partition(models.Model):
class PartitionSet(models.Model):
nom = models.CharField(max_length=100)
auteur = models.CharField(max_length=100)
category = models.CharField('Types de partitions', max_length=8,
choices=PARTITION_TYPES, default="incoming")
category = models.ForeignKey(
Category,
on_delete=models.PROTECT,
verbose_name="Type de partition"
)
infos = models.TextField("Infos utiles", null=True, blank=True)
def __str__(self):
return("%s - %s [%s]" % (self.nom, self.auteur,
self.get_category_display()))
return("%s - %s [%s]" % (self.nom, self.auteur, self.category))

View file

@ -2,41 +2,33 @@
{% block titre %}Liste des partitions{% endblock %}
{% block content %}<h1>Liste des partitions</h1>
{% block content %}
<h1>Liste des partitions</h1>
{% if suppression %}
<p>{{ suppression }}</p>
{% endif %}
{% if user.is_authenticated %}
<h3><a href="{% url "partitions:ajouter_morceau" %}">Ajouter un morceau</a></h3>
<h3><a href="{% url "partitions-ajouter_morceau" %}">Ajouter un morceau</a></h3>
{% endif %}
<h3 class="part">Partitions actives</h3>
{% for category in categories %}
<h3 class="part">{{ category }}</h3>
<ul class="filelist">
{% for part in active_partitions %}
{% include "partitions/liste_item.html" with part=part %}
{% for partition in category.partitionset_set.all %}
<li>
{% if not user.is_authenticated %}
{{ partition.nom }} - {{ partition.auteur }}
{% else %}
<a href="{% url "partitions-listepart" partition.nom partition.auteur %}"
class="fichier">{{ partition.nom }} - {{ partition.auteur }}</a>
{% endif %}
{% if user.profile.is_chef %}
<a href="{% url "partitions-conf_delete_morc" partition.nom partition.auteur %}"
class="supprimer">Supprimer</a>
{% endif %}
</li>
{% endfor %}
</ul>
<h3 class="part">Partitions optionnelles</h3>
<ul class="filelist">
{% for part in optional_partitions %}
{% include "partitions/liste_item.html" with part=part %}
{% endfor %}
</ul>
<h3 class="part">Partitions à venir</h3>
<ul class="filelist">
{% for part in incoming_partitions %}
{% include "partitions/liste_item.html" with part=part %}
{% endfor %}
</ul>
<h3 class="part">Archives</h3>
<ul class="filelist">
{% for part in old_partitions %}
{% include "partitions/liste_item.html" with part=part %}
{% endfor %}
</ul>
{% endblock %}

View file

@ -1,11 +0,0 @@
<li>
{% if not user.is_authenticated %}
{{ part.nom }} - {{ part.auteur }}
{% endif %}
{% if user.is_authenticated %}
<a href="{% url "partitions:listepart" part.nom part.auteur %}" class="fichier">{{ part.nom }} - {{ part.auteur }}</a>
{% endif %}
{% if user.profile.is_chef %}
<a href="{% url "partitions:conf_delete_morc" part.nom part.auteur %}" class="supprimer">Supprimer</a>
{% endif %}
</li>

View file

@ -9,20 +9,13 @@ from django.http import Http404
import os
from partitions.forms import UploadFileForm, UploadMorceauForm
from partitions.models import Partition, PartitionSet
from partitions.models import Partition, PartitionSet, Category
from partitions.decorators import chef_required
def liste(request):
partitions = PartitionSet.objects.order_by("nom")
context = {
"request": request,
"active_partitions": partitions.filter(category="active").all(),
"optional_partitions": partitions.filter(category="optional").all(),
"incoming_partitions": partitions.filter(category="incoming").all(),
"old_partitions": partitions.filter(category="old").all()
}
return render(request, 'partitions/liste.html', context)
categories = Category.objects.prefetch_related("partitionset_set")
return render(request, 'partitions/liste.html', {"categories": categories})
@login_required
@ -106,6 +99,13 @@ def ajouter_morceau(request):
auteur=partitionset.auteur)
error = "Un morceau du même nom existe déjà"
except PartitionSet.DoesNotExist:
# XXX. Hideous
cat = Category.objects.first()
try:
cat = Category.objects.get(name="Partitions à venir")
except Category.DoesNotExist:
pass
partitionset.category = cat
partitionset.save()
sauvegarde = True
else: