-
-
Notifications
You must be signed in to change notification settings - Fork 964
/
models.py
190 lines (163 loc) · 6.3 KB
/
models.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# -*- 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/>.
#
import json
from appconf import AppConf
from django.db import IntegrityError, models, transaction
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.utils.functional import cached_property
from weblate.checks import CHECKS
from weblate.utils.decorators import disable_for_loaddata
class WeblateChecksConf(AppConf):
# List of quality checks
CHECK_LIST = (
'weblate.checks.same.SameCheck',
'weblate.checks.chars.BeginNewlineCheck',
'weblate.checks.chars.EndNewlineCheck',
'weblate.checks.chars.BeginSpaceCheck',
'weblate.checks.chars.EndSpaceCheck',
'weblate.checks.chars.DoubleSpaceCheck',
'weblate.checks.chars.EndStopCheck',
'weblate.checks.chars.EndColonCheck',
'weblate.checks.chars.EndQuestionCheck',
'weblate.checks.chars.EndExclamationCheck',
'weblate.checks.chars.EndEllipsisCheck',
'weblate.checks.chars.EndSemicolonCheck',
'weblate.checks.chars.MaxLengthCheck',
'weblate.checks.chars.KashidaCheck',
'weblate.checks.chars.PuctuationSpacingCheck',
'weblate.checks.format.PythonFormatCheck',
'weblate.checks.format.PythonBraceFormatCheck',
'weblate.checks.format.PHPFormatCheck',
'weblate.checks.format.CFormatCheck',
'weblate.checks.format.PerlFormatCheck',
'weblate.checks.format.JavaScriptFormatCheck',
'weblate.checks.format.CSharpFormatCheck',
'weblate.checks.format.JavaFormatCheck',
'weblate.checks.format.JavaMessageFormatCheck',
'weblate.checks.angularjs.AngularJSInterpolationCheck',
'weblate.checks.qt.QtFormatCheck',
'weblate.checks.qt.QtPluralCheck',
'weblate.checks.ruby.RubyFormatCheck',
'weblate.checks.consistency.PluralsCheck',
'weblate.checks.consistency.SamePluralsCheck',
'weblate.checks.consistency.ConsistencyCheck',
'weblate.checks.consistency.TranslatedCheck',
'weblate.checks.chars.EscapedNewlineCountingCheck',
'weblate.checks.chars.NewLineCountCheck',
'weblate.checks.markup.BBCodeCheck',
'weblate.checks.chars.ZeroWidthSpaceCheck',
'weblate.checks.render.MaxSizeCheck',
'weblate.checks.markup.XMLValidityCheck',
'weblate.checks.markup.XMLTagsCheck',
'weblate.checks.markup.MarkdownRefLinkCheck',
'weblate.checks.markup.MarkdownLinkCheck',
'weblate.checks.markup.MarkdownSyntaxCheck',
'weblate.checks.markup.URLCheck',
'weblate.checks.markup.SafeHTMLCheck',
'weblate.checks.placeholders.PlaceholderCheck',
'weblate.checks.placeholders.RegexCheck',
'weblate.checks.source.OptionalPluralCheck',
'weblate.checks.source.EllipsisCheck',
'weblate.checks.source.MultipleFailingCheck',
)
class Meta(object):
prefix = ''
class CheckManager(models.Manager):
# pylint: disable=no-init
def bulk_create_ignore(self, objs):
"""Wrapper to bulk_create to ignore existing entries.
Once we require Django 2.2 this can be replaced with
bulk_create(ignore_conflicts=True).
"""
try:
with transaction.atomic():
self.bulk_create(objs, batch_size=500)
except IntegrityError:
for obj in objs:
try:
with transaction.atomic():
obj.save()
except IntegrityError:
continue
class Check(models.Model):
unit = models.ForeignKey("trans.Unit", on_delete=models.deletion.CASCADE)
check = models.CharField(max_length=50, choices=CHECKS.get_choices())
ignore = models.BooleanField(db_index=True, default=False)
objects = CheckManager()
@cached_property
def check_obj(self):
try:
return CHECKS[self.check]
except KeyError:
return None
class Meta(object):
unique_together = ('unit', 'check')
def __str__(self):
return '{0}: {1}'.format(self.unit, self.check)
def is_enforced(self):
return self.check in self.unit.translation.component.enforced_checks
def get_description(self):
if self.check_obj:
try:
return self.check_obj.get_description(self)
except IndexError:
return self.check_obj.description
return self.check
def get_fixup(self):
if self.check_obj:
try:
return self.check_obj.get_fixup(self.unit)
except IndexError:
return None
return None
def get_fixup_json(self):
fixup = self.get_fixup()
if not fixup:
return None
return json.dumps(fixup)
def get_name(self):
if self.check_obj:
return self.check_obj.name
return self.check
def get_severity(self):
if self.check_obj:
return self.check_obj.severity
return 'info'
def get_doc_url(self):
if self.check_obj:
return self.check_obj.get_doc_url()
return ''
def set_ignore(self, state=True):
"""Set ignore flag."""
self.ignore = state
self.save()
@receiver(post_save, sender=Check)
@disable_for_loaddata
def update_failed_check_flag(sender, instance, created, **kwargs):
"""Update related unit failed check flag."""
if created:
return
try:
instance.unit.update_has_failing_check(
has_checks=None if instance.ignore else True, invalidate=True
)
except IndexError:
return