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
|
||||
Ernestophone/settings/secret.py
|
||||
__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
|
||||
|
||||
|
||||
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))
|
||||
|
|
|
@ -2,7 +2,8 @@
|
|||
|
||||
{% 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 %}
|
||||
|
@ -11,32 +12,23 @@
|
|||
<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 %}
|
||||
|
|
|
@ -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
|
||||
|
||||
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:
|
||||
|
|
Loading…
Add table
Reference in a new issue