-
-
Notifications
You must be signed in to change notification settings - Fork 964
/
models.py
252 lines (207 loc) · 8.76 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# -*- 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/>.
#
from collections import defaultdict
from appconf import AppConf
from django.db import models
from django.db.models import Q
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.urls import reverse
from django.utils.functional import cached_property
from weblate.addons.events import (
EVENT_CHOICES,
EVENT_COMPONENT_UPDATE,
EVENT_POST_ADD,
EVENT_POST_COMMIT,
EVENT_POST_PUSH,
EVENT_POST_UPDATE,
EVENT_PRE_COMMIT,
EVENT_PRE_PUSH,
EVENT_PRE_UPDATE,
EVENT_STORE_POST_LOAD,
EVENT_UNIT_POST_SAVE,
EVENT_UNIT_PRE_CREATE,
)
from weblate.trans.models import Component, Unit
from weblate.trans.signals import (
component_post_update,
store_post_load,
translation_post_add,
unit_pre_create,
vcs_post_commit,
vcs_post_push,
vcs_post_update,
vcs_pre_commit,
vcs_pre_push,
vcs_pre_update,
)
from weblate.utils.classloader import ClassLoader
from weblate.utils.decorators import disable_for_loaddata
from weblate.utils.fields import JSONField
# Initialize addons registry
ADDONS = ClassLoader('WEBLATE_ADDONS', False)
class AddonQuerySet(models.QuerySet):
def filter_component(self, component):
return self.prefetch_related('event_set').filter(
(Q(component=component) & Q(project_scope=False))
| (Q(component__project=component.project) & Q(project_scope=True))
| (Q(component__linked_component=component) & Q(repo_scope=True))
| (Q(component=component.linked_component) & Q(repo_scope=True))
)
def filter_event(self, component, event):
if component.addons_cache is None:
component.addons_cache = defaultdict(list)
for addon in self.filter_component(component):
for installed in addon.event_set.all():
component.addons_cache[installed.event].append(addon)
return component.addons_cache[event]
class Addon(models.Model):
component = models.ForeignKey(Component, on_delete=models.deletion.CASCADE)
name = models.CharField(max_length=100)
configuration = JSONField()
state = JSONField()
project_scope = models.BooleanField(default=False, db_index=True)
repo_scope = models.BooleanField(default=False, db_index=True)
objects = AddonQuerySet.as_manager()
def __str__(self):
return '{}: {}'.format(self.addon.verbose, self.component)
def configure_events(self, events):
for event in events:
Event.objects.get_or_create(addon=self, event=event)
self.event_set.exclude(event__in=events).delete()
@cached_property
def addon(self):
return ADDONS[self.name](self)
def get_absolute_url(self):
return reverse(
'addon-detail',
kwargs={
'project': self.component.project.slug,
'component': self.component.slug,
'pk': self.pk,
},
)
def delete(self, *args, **kwargs):
# Delete any addon alerts
if self.addon.alert:
self.component.alert_set.filter(name=self.addon.alert).delete()
super(Addon, self).delete(*args, **kwargs)
class Event(models.Model):
addon = models.ForeignKey(Addon, on_delete=models.deletion.CASCADE)
event = models.IntegerField(choices=EVENT_CHOICES)
class Meta:
unique_together = ('addon', 'event')
def __str__(self):
return '{}: {}'.format(self.addon, self.get_event_display())
class AddonsConf(AppConf):
ADDONS = (
'weblate.addons.gettext.GenerateMoAddon',
'weblate.addons.gettext.UpdateLinguasAddon',
'weblate.addons.gettext.UpdateConfigureAddon',
'weblate.addons.gettext.MsgmergeAddon',
'weblate.addons.gettext.GettextCustomizeAddon',
'weblate.addons.gettext.GettextAuthorComments',
'weblate.addons.cleanup.CleanupAddon',
'weblate.addons.consistency.LangaugeConsistencyAddon',
'weblate.addons.discovery.DiscoveryAddon',
'weblate.addons.autotranslate.AutoTranslateAddon',
'weblate.addons.flags.SourceEditAddon',
'weblate.addons.flags.TargetEditAddon',
'weblate.addons.flags.SameEditAddon',
'weblate.addons.flags.BulkEditAddon',
'weblate.addons.generate.GenerateFileAddon',
'weblate.addons.json.JSONCustomizeAddon',
'weblate.addons.properties.PropertiesSortAddon',
'weblate.addons.git.GitSquashAddon',
'weblate.addons.removal.RemoveComments',
'weblate.addons.removal.RemoveSuggestions',
'weblate.addons.resx.ResxUpdateAddon',
'weblate.addons.yaml.YAMLCustomizeAddon',
)
class Meta:
prefix = 'WEBLATE'
@receiver(vcs_pre_push)
def pre_push(sender, component, **kwargs):
for addon in Addon.objects.filter_event(component, EVENT_PRE_PUSH):
component.log_debug('running pre_push addon: %s', addon.name)
addon.addon.pre_push(component)
@receiver(vcs_post_push)
def post_push(sender, component, **kwargs):
for addon in Addon.objects.filter_event(component, EVENT_POST_PUSH):
component.log_debug('running post_push addon: %s', addon.name)
addon.addon.post_push(component)
@receiver(vcs_post_update)
def post_update(sender, component, previous_head, child=False, **kwargs):
for addon in Addon.objects.filter_event(component, EVENT_POST_UPDATE):
if child and addon.repo_scope:
continue
component.log_debug('running post_update addon: %s', addon.name)
addon.addon.post_update(component, previous_head)
@receiver(component_post_update)
def component_update(sender, component, **kwargs):
for addon in Addon.objects.filter_event(component, EVENT_COMPONENT_UPDATE):
component.log_debug('running component_update addon: %s', addon.name)
addon.addon.component_update(component)
@receiver(vcs_pre_update)
def pre_update(sender, component, **kwargs):
for addon in Addon.objects.filter_event(component, EVENT_PRE_UPDATE):
component.log_debug('running pre_update addon: %s', addon.name)
addon.addon.pre_update(component)
@receiver(vcs_pre_commit)
def pre_commit(sender, translation, author, **kwargs):
addons = Addon.objects.filter_event(translation.component, EVENT_PRE_COMMIT)
for addon in addons:
translation.log_debug('running pre_commit addon: %s', addon.name)
addon.addon.pre_commit(translation, author)
@receiver(vcs_post_commit)
def post_commit(sender, component, translation=None, **kwargs):
addons = Addon.objects.filter_event(component, EVENT_POST_COMMIT)
for addon in addons:
component.log_debug('running post_commit addon: %s', addon.name)
addon.addon.post_commit(component, translation)
@receiver(translation_post_add)
def post_add(sender, translation, **kwargs):
addons = Addon.objects.filter_event(translation.component, EVENT_POST_ADD)
for addon in addons:
translation.log_debug('running post_add addon: %s', addon.name)
addon.addon.post_add(translation)
@receiver(unit_pre_create)
def unit_pre_create_handler(sender, unit, **kwargs):
addons = Addon.objects.filter_event(
unit.translation.component, EVENT_UNIT_PRE_CREATE
)
for addon in addons:
unit.translation.log_debug('running unit_pre_create addon: %s', addon.name)
addon.addon.unit_pre_create(unit)
@receiver(post_save, sender=Unit)
@disable_for_loaddata
def unit_post_save_handler(sender, instance, created, **kwargs):
addons = Addon.objects.filter_event(
instance.translation.component, EVENT_UNIT_POST_SAVE
)
for addon in addons:
instance.translation.log_debug('running unit_post_save addon: %s', addon.name)
addon.addon.unit_post_save(instance, created)
@receiver(store_post_load)
def store_post_load_handler(sender, translation, store, **kwargs):
addons = Addon.objects.filter_event(translation.component, EVENT_STORE_POST_LOAD)
for addon in addons:
translation.log_debug('running store_post_load addon: %s', addon.name)
addon.addon.store_post_load(translation, store)