Remove category tree

This commit is contained in:
Edgar P. Burkhart 2022-05-20 21:33:06 +02:00
parent f85c67bb49
commit 25482790dc
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
5 changed files with 89 additions and 33 deletions

View file

@ -0,0 +1,56 @@
# Generated by Django 4.0.4 on 2022-05-20 19:25
import django.core.validators
from django.db import migrations, models
import re
class Migration(migrations.Migration):
dependencies = [
("main", "0006_alter_category_options_transaction_trader_and_more"),
]
operations = [
migrations.AlterModelOptions(
name="category",
options={},
),
migrations.AlterModelOptions(
name="transaction",
options={"ordering": ["-date"]},
),
migrations.RemoveField(
model_name="category",
name="full_name",
),
migrations.RemoveField(
model_name="category",
name="parent",
),
migrations.AlterField(
model_name="category",
name="name",
field=models.CharField(
default="New Category",
max_length=64,
validators=[
django.core.validators.RegexValidator(
re.compile("^[-\\w]+\\Z"),
"Enter a valid “slug” consisting of Unicode letters, numbers, underscores, or hyphens.",
"invalid",
)
],
),
),
migrations.AlterField(
model_name="invoice",
name="name",
field=models.CharField(default="New Invoice", max_length=256),
),
migrations.AlterField(
model_name="transaction",
name="name",
field=models.CharField(default="New Transaction", max_length=256),
),
]

View file

@ -7,35 +7,21 @@ from django.core.validators import validate_unicode_slug, FileExtensionValidator
class Category(models.Model): class Category(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField(max_length=64, validators=[validate_unicode_slug], default="New Category") name = models.CharField(
full_name = models.CharField(max_length=512, editable=False, default="") max_length=64, validators=[validate_unicode_slug], default="New Category"
parent = models.ForeignKey("self", on_delete=models.SET_NULL, blank=True, null=True) )
def __str__(self): def __str__(self):
if self.parent is None or self.parent == self:
return self.name return self.name
return f"{self.parent}>{self.name}"
def save(self, *args, **kwargs):
self.full_name = str(self)
super().save(*args, **kwargs)
for child in self.__class__.objects.filter(parent=self):
child.save()
@property
def tree(self):
if self.parent is None or self.parent == self:
return [self.name]
return self.parent.tree + [self.name]
class Meta: class Meta:
ordering = ["full_name"] ordering = ["name"]
class CategoryForm(ModelForm): class CategoryForm(ModelForm):
class Meta: class Meta:
model = Category model = Category
fields = ["name", "parent"] fields = ["name"]
class Transaction(models.Model): class Transaction(models.Model):

View file

@ -47,9 +47,6 @@ h1 {
text-overflow: ellipsis; text-overflow: ellipsis;
} }
ul#categories { #categories > a {
list-style-type: " "; margin-right: 1em;
} }
ul#categories li.cat2 {margin-left: 1em}
ul#categories li.cat3 {margin-left: 2em}
ul#categories li.cat4 {margin-left: 3em}

View file

@ -18,8 +18,18 @@ body {
--text-inv: #ffffffde; --text-inv: #ffffffde;
--bg-01: #dedede; --bg-01: #dedede;
--text-link: #0066ff;
} }
h1 { h1 {
margin: 0; margin: 0;
} }
a {
color: var(--text-link);
text-decoration: none;
}
a:hover {
text-decoration: underline;
}

View file

@ -25,7 +25,15 @@
<span class="name text"><a href="{% url 'transaction' trans.id %}">{{ trans.name }}</a></span> <span class="name text"><a href="{% url 'transaction' trans.id %}">{{ trans.name }}</a></span>
<span class="value num">{{ trans.value|floatformat:"2g" }} €</span> <span class="value num">{{ trans.value|floatformat:"2g" }} €</span>
<span class="trader text center">{{ trans.trader|default_if_none:"" }}</span> <span class="trader text center">{{ trans.trader|default_if_none:"" }}</span>
<span class="category text center">{% if trans.category %}{{ trans.category.tree|join:" → " }}{% else %}{% endif %}</span> <span class="category text center">
{% if trans.category %}
<a href="{% url 'category' trans.category.id %}">
{{ trans.category }}
</a>
{% else %}
{% endif %}
</span>
<span class="description text">{{ trans.description }}</span> <span class="description text">{{ trans.description }}</span>
</div> </div>
{% endfor %} {% endfor %}
@ -33,15 +41,14 @@
{% endif %} {% endif %}
{% if categories %} {% if categories %}
<ul id="categories"> <h2>Catégories</h2>
<div id="categories">
{% spaceless %}
{% for cat in categories %} {% for cat in categories %}
<li class="{% if cat.tree|length > 3 %}cat4{% else %}cat{{ cat.tree|length }}{% endif %}"> <a href="{% url 'category' cat.id %}">{{ cat }}</a>
<a href="{% url 'category' cat.id %}">
{% if cat.tree|length > 3 %}{{ cat.tree|slice:"3:"|join:" → " }}{% else %}{{ cat.name }}{% endif %}
</a>
</li>
{% endfor %} {% endfor %}
</ul> {% endspaceless %}
</div>
{% endif %} {% endif %}
{% endblock %} {% endblock %}