-
-
Notifications
You must be signed in to change notification settings - Fork 964
/
componentlist.py
117 lines (95 loc) · 3.45 KB
/
componentlist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# -*- coding: utf-8 -*-
#
# Copyright © 2012 - 2020 Michal Čihař <michal@cihar.com>
#
# This file is part of Weblate <https://weblate.org/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
"""Components list."""
import re
from django.db import models
from django.urls import reverse
from django.utils.translation import ugettext_lazy as _
from weblate.trans.fields import RegexField
from weblate.utils.stats import ComponentListStats
class ComponentListQuerySet(models.QuerySet):
def order(self):
return self.order_by('name')
class ComponentList(models.Model):
name = models.CharField(
verbose_name=_('Component list name'),
max_length=100,
unique=True,
help_text=_('Display name'),
)
slug = models.SlugField(
verbose_name=_('URL slug'),
db_index=True,
unique=True,
max_length=100,
help_text=_('Name used in URLs and filenames.'),
)
show_dashboard = models.BooleanField(
verbose_name=_('Show on dashboard'),
default=True,
db_index=True,
help_text=_(
'When enabled this component list will be shown as a tab on '
'the dashboard'
),
)
components = models.ManyToManyField('Component', blank=True)
objects = ComponentListQuerySet.as_manager()
class Meta(object):
verbose_name = _('Component list')
verbose_name_plural = _('Component lists')
def tab_slug(self):
return "list-" + self.slug
def __str__(self):
return self.name
def __init__(self, *args, **kwargs):
super(ComponentList, self).__init__(*args, **kwargs)
self.stats = ComponentListStats(self)
def get_absolute_url(self):
return reverse('component-list', kwargs={'name': self.slug})
class AutoComponentList(models.Model):
project_match = RegexField(
verbose_name=_('Project regular expression'),
max_length=200,
default='^$',
help_text=_('Regular expression which is used to match project slug.'),
)
component_match = RegexField(
verbose_name=_('Component regular expression'),
max_length=200,
default='^$',
help_text=_('Regular expression which is used to match component slug.'),
)
componentlist = models.ForeignKey(
ComponentList,
verbose_name=_('Component list to assign'),
on_delete=models.deletion.CASCADE,
)
def __str__(self):
return self.componentlist.name
def check_match(self, component):
if not re.match(self.project_match, component.project.slug):
return
if not re.match(self.component_match, component.slug):
return
self.componentlist.components.add(component)
class Meta(object):
verbose_name = _('Automatic component list assignment')
verbose_name_plural = _('Automatic component list assignments')