forked from WeblateOrg/weblate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
models: add component categorization
- add model for storing categories - support maximal depth of 3 categories rigth now Fixes #263 TODO: - add UI to: - add a category - delete a category - move component between categories - add documentation - add tests - add API - document adjusted API lookups (category in component, %2F)
- Loading branch information
Showing
11 changed files
with
257 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
weblate/trans/migrations/0182_category_component_category.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# Copyright © Michal Čihař <[email protected]> | ||
# | ||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
# Generated by Django 4.2.3 on 2023-08-12 04:26 | ||
|
||
import django.db.models.deletion | ||
from django.db import migrations, models | ||
|
||
import weblate.trans.mixins | ||
import weblate.utils.validators | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("trans", "0181_change_trans_chang_user_id_b1b554_idx"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="Category", | ||
fields=[ | ||
( | ||
"id", | ||
models.AutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
( | ||
"name", | ||
models.CharField( | ||
help_text="Display name", | ||
max_length=100, | ||
verbose_name="Category name", | ||
), | ||
), | ||
( | ||
"slug", | ||
models.SlugField( | ||
help_text="Name used in URLs and filenames.", | ||
max_length=100, | ||
validators=[weblate.utils.validators.validate_slug], | ||
verbose_name="URL slug", | ||
), | ||
), | ||
( | ||
"category", | ||
models.ForeignKey( | ||
blank=True, | ||
null=True, | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="subcategory_set", | ||
to="trans.category", | ||
verbose_name="Category", | ||
), | ||
), | ||
( | ||
"project", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
to="trans.project", | ||
verbose_name="Project", | ||
), | ||
), | ||
], | ||
bases=( | ||
models.Model, | ||
weblate.trans.mixins.PathMixin, | ||
weblate.trans.mixins.CacheKeyMixin, | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="component", | ||
name="category", | ||
field=models.ForeignKey( | ||
blank=True, | ||
null=True, | ||
on_delete=django.db.models.deletion.CASCADE, | ||
to="trans.category", | ||
verbose_name="Category", | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Copyright © Michal Čihař <[email protected]> | ||
# | ||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
from __future__ import annotations | ||
|
||
from django.core.exceptions import ValidationError | ||
from django.db import models | ||
from django.utils.translation import gettext, gettext_lazy | ||
|
||
from weblate.trans.defines import CATEGORY_DEPTH, COMPONENT_NAME_LENGTH | ||
from weblate.trans.mixins import CacheKeyMixin, ComponentCategoryMixin, PathMixin | ||
from weblate.utils.validators import validate_slug | ||
|
||
|
||
class Category(models.Model, PathMixin, CacheKeyMixin, ComponentCategoryMixin): | ||
name = models.CharField( | ||
verbose_name=gettext_lazy("Category name"), | ||
max_length=COMPONENT_NAME_LENGTH, | ||
help_text=gettext_lazy("Display name"), | ||
) | ||
slug = models.SlugField( | ||
verbose_name=gettext_lazy("URL slug"), | ||
max_length=COMPONENT_NAME_LENGTH, | ||
help_text=gettext_lazy("Name used in URLs and filenames."), | ||
validators=[validate_slug], | ||
) | ||
project = models.ForeignKey( | ||
"Project", | ||
verbose_name=gettext_lazy("Project"), | ||
on_delete=models.deletion.CASCADE, | ||
) | ||
category = models.ForeignKey( | ||
"Category", | ||
verbose_name=gettext_lazy("Category"), | ||
on_delete=models.deletion.CASCADE, | ||
null=True, | ||
blank=True, | ||
related_name="subcategory_set", | ||
) | ||
|
||
def __str__(self): | ||
return f"{self.category or self.project}/{self.name}" | ||
|
||
def _get_childs_depth(self): | ||
return 1 + max( | ||
(child._get_childs_depth() for child in self.subcategory_set.all()), | ||
default=0, | ||
) | ||
|
||
def clean(self): | ||
# Validate maximal nesting depth | ||
depth = self._get_childs_depth() | ||
current = self | ||
while current.category: | ||
depth += 1 | ||
current = current.category | ||
|
||
if depth > CATEGORY_DEPTH: | ||
raise ValidationError(gettext("Too deep nesting of categories!")) | ||
|
||
# Validate category/component name uniqueness at given level | ||
self.clean_unique_together() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.