Merge branch 'categories' into 'master'
Catégories de partitions éditables See merge request !5
This commit is contained in:
commit
8e50dffcce
7 changed files with 140 additions and 63 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -4,3 +4,4 @@ media/
|
||||||
db.sqlite3
|
db.sqlite3
|
||||||
Ernestophone/settings/secret.py
|
Ernestophone/settings/secret.py
|
||||||
__pycache__
|
__pycache__
|
||||||
|
venv
|
||||||
|
|
9
partitions/admin.py
Normal file
9
partitions/admin.py
Normal 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"]
|
80
partitions/migrations/0002_category.py
Normal file
80
partitions/migrations/0002_category.py
Normal 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"),
|
||||||
|
]
|
|
@ -4,12 +4,16 @@ from django.conf import settings
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
|
|
||||||
PARTITION_TYPES = (
|
class Category(models.Model):
|
||||||
("active", "Actif"),
|
name = models.CharField(max_length=127)
|
||||||
("incoming", "À venir"),
|
order = models.IntegerField(verbose_name="order")
|
||||||
("old", "Archive"),
|
|
||||||
("optional", "Optionnel"),
|
def __str__(self):
|
||||||
)
|
return self.name
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
verbose_name = "catégorie"
|
||||||
|
verbose_name_plural = "catégories"
|
||||||
|
|
||||||
|
|
||||||
class Partition(models.Model):
|
class Partition(models.Model):
|
||||||
|
@ -28,10 +32,12 @@ class Partition(models.Model):
|
||||||
class PartitionSet(models.Model):
|
class PartitionSet(models.Model):
|
||||||
nom = models.CharField(max_length=100)
|
nom = models.CharField(max_length=100)
|
||||||
auteur = models.CharField(max_length=100)
|
auteur = models.CharField(max_length=100)
|
||||||
category = models.CharField('Types de partitions', max_length=8,
|
category = models.ForeignKey(
|
||||||
choices=PARTITION_TYPES, default="incoming")
|
Category,
|
||||||
|
on_delete=models.PROTECT,
|
||||||
|
verbose_name="Type de partition"
|
||||||
|
)
|
||||||
infos = models.TextField("Infos utiles", null=True, blank=True)
|
infos = models.TextField("Infos utiles", null=True, blank=True)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return("%s - %s [%s]" % (self.nom, self.auteur,
|
return("%s - %s [%s]" % (self.nom, self.auteur, self.category))
|
||||||
self.get_category_display()))
|
|
||||||
|
|
|
@ -2,41 +2,33 @@
|
||||||
|
|
||||||
{% block titre %}Liste des partitions{% endblock %}
|
{% block titre %}Liste des partitions{% endblock %}
|
||||||
|
|
||||||
{% block content %}<h1>Liste des partitions</h1>
|
{% block content %}
|
||||||
{% if suppression %}
|
<h1>Liste des partitions</h1>
|
||||||
<p>{{ suppression }}</p>
|
{% if suppression %}
|
||||||
{% endif %}
|
<p>{{ suppression }}</p>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
{% if user.is_authenticated %}
|
{% 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 %}
|
{% endif %}
|
||||||
|
|
||||||
|
{% for category in categories %}
|
||||||
<h3 class="part">Partitions actives</h3>
|
<h3 class="part">{{ category }}</h3>
|
||||||
<ul class="filelist">
|
<ul class="filelist">
|
||||||
{% for part in active_partitions %}
|
{% for partition in category.partitionset_set.all %}
|
||||||
{% include "partitions/liste_item.html" with part=part %}
|
<li>
|
||||||
{% endfor %}
|
{% if not user.is_authenticated %}
|
||||||
</ul>
|
{{ partition.nom }} - {{ partition.auteur }}
|
||||||
|
{% else %}
|
||||||
<h3 class="part">Partitions optionnelles</h3>
|
<a href="{% url "partitions-listepart" partition.nom partition.auteur %}"
|
||||||
<ul class="filelist">
|
class="fichier">{{ partition.nom }} - {{ partition.auteur }}</a>
|
||||||
{% for part in optional_partitions %}
|
{% endif %}
|
||||||
{% include "partitions/liste_item.html" with part=part %}
|
{% if user.profile.is_chef %}
|
||||||
{% endfor %}
|
<a href="{% url "partitions-conf_delete_morc" partition.nom partition.auteur %}"
|
||||||
</ul>
|
class="supprimer">Supprimer</a>
|
||||||
|
{% endif %}
|
||||||
<h3 class="part">Partitions à venir</h3>
|
</li>
|
||||||
<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 %}
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
|
{% endfor %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
@ -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>
|
|
|
@ -9,20 +9,13 @@ from django.http import Http404
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from partitions.forms import UploadFileForm, UploadMorceauForm
|
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
|
from partitions.decorators import chef_required
|
||||||
|
|
||||||
|
|
||||||
def liste(request):
|
def liste(request):
|
||||||
partitions = PartitionSet.objects.order_by("nom")
|
categories = Category.objects.prefetch_related("partitionset_set")
|
||||||
context = {
|
return render(request, 'partitions/liste.html', {"categories": categories})
|
||||||
"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)
|
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
|
@ -106,6 +99,13 @@ def ajouter_morceau(request):
|
||||||
auteur=partitionset.auteur)
|
auteur=partitionset.auteur)
|
||||||
error = "Un morceau du même nom existe déjà"
|
error = "Un morceau du même nom existe déjà"
|
||||||
except PartitionSet.DoesNotExist:
|
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()
|
partitionset.save()
|
||||||
sauvegarde = True
|
sauvegarde = True
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Add table
Reference in a new issue