-
-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathmodels.py
More file actions
734 lines (632 loc) · 24.2 KB
/
Copy pathmodels.py
File metadata and controls
734 lines (632 loc) · 24.2 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
#
# Copyright © 2012–2021 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 datetime import timedelta
from uuid import uuid4
import html2text
import requests
from django.conf import settings
from django.contrib.auth.models import User
from django.db import models
from django.db.models import Q
from django.urls import reverse
from django.utils import timezone
from django.utils.crypto import get_random_string
from django.utils.functional import cached_property
from django.utils.translation import gettext as _
from django.utils.translation import override, ugettext_lazy
from markupfield.fields import MarkupField
from paramiko.client import SSHClient
from payments.models import Payment, get_period_delta
from payments.utils import send_notification
PAYMENTS_ORIGIN = "https://weblate.org/donate/process/"
REWARDS = (
(0, ugettext_lazy("No reward")),
(1, ugettext_lazy("Name in the list of supporters")),
(2, ugettext_lazy("Link in the list of supporters")),
(3, ugettext_lazy("Logo and link on the Weblate website")),
)
TOPICS = (
("release", ugettext_lazy("Release")),
("feature", ugettext_lazy("Features")),
("announce", ugettext_lazy("Announcement")),
("conferences", ugettext_lazy("Conferences")),
("hosting", ugettext_lazy("Hosted Weblate")),
("development", ugettext_lazy("Development")),
("localization", ugettext_lazy("Localization")),
)
TOPIC_DICT = dict(TOPICS)
def create_backup_repository(service):
"""
Configure backup repository.
- create filesystem folders
- store ssh key
- create subaccount
"""
# Create folder and SSH key
client = SSHClient()
client.load_system_host_keys()
client.connect(**settings.STORAGE_SERVER)
ftp = client.open_sftp()
dirname = str(uuid4())
ftp.mkdir(dirname)
ftp.chdir(dirname)
ftp.mkdir(".ssh")
ftp.chdir(".ssh")
with ftp.open("authorized_keys", "w") as handle:
handle.write(service.last_report.ssh_key)
# Create account on the service
url = "https://robot-ws.your-server.de/storagebox/{}/subaccount".format(
settings.STORAGE_BOX
)
response = requests.post(
url,
data={
"homedirectory": f"weblate/{dirname}",
"ssh": "1",
"external_reachability": "1",
"comment": f"Weblate backup service {service.pk}",
},
auth=(settings.STORAGE_USER, settings.STORAGE_PASSWORD),
)
data = response.json()
return "ssh://{}@{}:23/./backups".format(
data["subaccount"]["username"], data["subaccount"]["server"]
)
class Donation(models.Model):
user = models.ForeignKey(User, on_delete=models.deletion.CASCADE)
payment = models.UUIDField(blank=True, null=True) # noqa: DJ01
reward = models.IntegerField(choices=REWARDS, default=0)
link_text = models.CharField(
verbose_name=ugettext_lazy("Link text"), max_length=200, blank=True
)
link_url = models.URLField(verbose_name=ugettext_lazy("Link URL"), blank=True)
link_image = models.ImageField(
verbose_name=ugettext_lazy("Link image"), blank=True, upload_to="donations/"
)
created = models.DateTimeField(auto_now_add=True)
expires = models.DateTimeField()
active = models.BooleanField(blank=True, db_index=True)
class Meta:
verbose_name = "Donation"
verbose_name_plural = "Donations"
def __str__(self):
return f"{self.user}:{self.reward}"
def get_absolute_url(self):
return reverse("donate-edit", kwargs={"pk": self.pk})
@cached_property
def payment_obj(self):
if not self.payment:
return None
return Payment.objects.get(pk=self.payment)
def list_payments(self):
past = set(self.pastpayments_set.values_list("payment", flat=True))
query = Q(pk=self.payment)
if past:
query |= Q(pk__in=past)
query |= Q(repeat__pk__in=past)
if self.payment:
query |= Q(repeat__pk=self.payment)
return Payment.objects.filter(query).distinct()
def get_amount(self):
if not self.payment:
return 0
return self.payment_obj.amount
def get_payment_description(self):
if self.reward:
return f"Weblate donation: {self.get_reward_display()}"
return "Weblate donation"
def send_notification(self, notification):
send_notification(
notification,
[self.user.email],
donation=self,
)
def process_donation(payment):
if payment.state != Payment.ACCEPTED:
raise ValueError("Can not process not accepted payment")
if payment.repeat:
# Update existing
donation = Donation.objects.get(payment=payment.repeat.pk)
payment.start = donation.expires
donation.expires += get_period_delta(payment.repeat.recurring)
payment.end = donation.expires
donation.save()
elif "donation" in payment.extra:
donation = Donation.objects.get(pk=payment.extra["donation"])
if donation.payment:
donation.pastpayments_set.create(payment=donation.payment)
payment.start = donation.expires
donation.expires += get_period_delta(payment.recurring)
payment.end = donation.expires
donation.payment = payment.pk
donation.save()
else:
user = User.objects.get(pk=payment.customer.user_id)
reward = payment.extra.get("reward", 0)
# Calculate expiry
expires = timezone.now()
if payment.recurring:
payment.start = expires
expires += get_period_delta(payment.recurring)
payment.end = expires
elif reward:
payment.start = expires
expires += get_period_delta("y")
payment.end = expires
# Create new
donation = Donation.objects.create(
user=user,
payment=payment.pk,
reward=int(reward),
expires=expires,
active=True,
)
# Flag payment as processed
payment.state = Payment.PROCESSED
payment.save()
return donation
def get_service(payment, user):
try:
return user.service_set.get(pk=payment.extra["service"])
except Service.DoesNotExist:
try:
return user.service_set.get()
except (Service.MultipleObjectsReturned, Service.DoesNotExist):
service = user.service_set.create()
service.was_created = True
return service
def process_subscription(payment):
if payment.state != Payment.ACCEPTED:
raise ValueError("Can not process not accepted payment")
if payment.repeat:
# Update existing
subscription = Subscription.objects.get(payment=payment.repeat.pk)
payment.start = subscription.expires
subscription.expires += get_period_delta(payment.repeat.recurring)
payment.end = subscription.expires
subscription.save()
elif isinstance(payment.extra["subscription"], int):
subscription = Subscription.objects.get(pk=payment.extra["subscription"])
if subscription.payment:
subscription.pastpayments_set.create(payment=subscription.payment)
payment.start = subscription.expires
subscription.expires += get_period_delta(subscription.get_repeat())
payment.end = subscription.expires
subscription.payment = payment.pk
subscription.save()
else:
user = User.objects.get(pk=payment.customer.user_id)
package = Package.objects.get(name=payment.extra["subscription"])
# Calculate expiry
repeat = package.get_repeat()
if repeat:
expires = timezone.now()
payment.start = expires
expires += get_period_delta(repeat)
payment.end = expires
else:
expires = timezone.now()
# Create new
service = get_service(payment, user)
subscription = Subscription.objects.create(
service=service,
payment=payment.pk,
package=package.name,
expires=expires,
)
with override("en"):
send_notification(
"new_subscription",
settings.NOTIFY_SUBSCRIPTION,
subscription=subscription,
service=subscription.service,
)
if service.was_created and service.needs_token:
subscription.send_notification("subscription_intro")
# Flag payment as processed
payment.state = Payment.PROCESSED
payment.save()
return subscription
class Image(models.Model):
name = models.CharField(max_length=100, unique=True)
image = models.ImageField(
upload_to="images/", help_text="Article image, 1200x630 pixels"
)
class Meta:
verbose_name = "Image"
verbose_name_plural = "Images"
def __str__(self):
return self.name
class Post(models.Model):
title = models.CharField(max_length=100)
slug = models.SlugField(unique=True)
timestamp = models.DateTimeField(db_index=True)
author = models.ForeignKey(
User, editable=False, on_delete=models.deletion.SET_NULL, null=True
)
topic = models.CharField(max_length=100, db_index=True, choices=TOPICS, default="")
body = MarkupField(default_markup_type="markdown")
summary = models.TextField(
blank=True, help_text="Will be generated from first body paragraph if empty"
)
image = models.ForeignKey(
Image, on_delete=models.deletion.SET_NULL, blank=True, null=True
)
milestone = models.BooleanField(
blank=True,
db_index=True,
default=False,
help_text="This is an important milestone, shown on milestones archive",
)
class Meta:
verbose_name = "Blog post"
verbose_name_plural = "Blog posts"
def __str__(self):
return self.title
def save(
self, force_insert=False, force_update=False, using=None, update_fields=None
):
# Need to save first as rendered value is available only then
super().save(force_insert, force_update, using, update_fields)
if not self.summary:
h2t = html2text.HTML2Text()
h2t.body_width = 0
h2t.ignore_images = True
h2t.ignore_links = True
h2t.ignore_emphasis = True
text = h2t.handle(self.body.rendered) # pylint: disable=no-member
self.summary = text.splitlines()[0]
if self.summary:
super().save(update_fields=["summary"])
def get_absolute_url(self):
return reverse("post", kwargs={"slug": self.slug})
def generate_secret():
return get_random_string(64)
class Package(models.Model):
name = models.CharField(max_length=150, unique=True)
verbose = models.CharField(max_length=400)
price = models.IntegerField()
limit_projects = models.IntegerField(default=0)
limit_languages = models.IntegerField(default=0)
limit_source_strings = models.IntegerField(default=0)
class Meta:
verbose_name = "Service package"
verbose_name_plural = "Service packages"
def __str__(self):
return self.verbose
def get_repeat(self):
if self.name in ("basic", "extended", "premium", "backup"):
return "y"
if self.name.startswith("hosted:") or self.name.startswith("shared:"):
if self.name.endswith("-m"):
return "m"
return "y"
return ""
class Service(models.Model):
secret = models.CharField(max_length=100, default=generate_secret, db_index=True)
users = models.ManyToManyField(User)
status = models.CharField(
max_length=150,
choices=(
("community", ugettext_lazy("Expired service")),
("hosted", ugettext_lazy("Dedicated hosted service")),
("shared", ugettext_lazy("Hosted service")),
("basic", ugettext_lazy("Basic self-hosted support")),
("extended", ugettext_lazy("Extended self-hosted support")),
("premium", ugettext_lazy("Premium self-hosted support")),
),
default="community",
)
backup_repository = models.CharField(max_length=500, default="", blank=True)
limit_languages = models.IntegerField(default=0)
limit_projects = models.IntegerField(default=0)
limit_source_strings = models.IntegerField(default=0)
created = models.DateTimeField(auto_now_add=True)
note = models.TextField(blank=True)
hosted_billing = models.IntegerField(default=0, db_index=True)
class Meta:
verbose_name = "Customer service"
verbose_name_plural = "Customer services"
def __str__(self):
if self.last_report:
url = self.last_report.site_url
else:
url = ""
return f"{self.get_status_display()}: {self.user_emails}: {url}"
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.was_created = False
@property
def needs_token(self):
return self.status not in ("hosted", "shared", "community")
@cached_property
def site_title(self):
if self.last_report:
return self.last_report.site_title
return "Weblate"
@cached_property
def site_url(self):
if self.last_report:
return self.last_report.site_url
return ""
@cached_property
def site_version(self):
if self.last_report:
return self.last_report.version
return ""
def projects_limit(self):
report = self.last_report
if report:
if self.limit_projects:
return f"{report.projects}/{self.limit_projects}"
return f"{report.projects}"
return "0"
projects_limit.short_description = "Projects"
def languages_limit(self):
report = self.last_report
if report:
if self.limit_languages:
return f"{report.languages}/{self.limit_languages}"
return f"{report.languages}"
return "0"
languages_limit.short_description = "Languages"
def source_strings_limit(self):
report = self.last_report
if report:
if self.limit_source_strings:
return f"{report.source_strings}/{self.limit_source_strings}"
return f"{report.source_strings}"
return "0"
source_strings_limit.short_description = "Source strings"
@cached_property
def user_emails(self):
return ", ".join(self.users.values_list("email", flat=True))
@cached_property
def last_report(self):
try:
return self.report_set.latest("timestamp")
except Report.DoesNotExist:
return None
@cached_property
def hosted_subscriptions(self):
return self.subscription_set.filter(package__startswith="hosted:")
@cached_property
def shared_subscriptions(self):
return self.subscription_set.filter(package__startswith="shared:")
@cached_property
def basic_subscriptions(self):
return self.subscription_set.filter(package="basic")
@cached_property
def extended_subscriptions(self):
return self.subscription_set.filter(package="extended")
@cached_property
def premium_subscriptions(self):
return self.subscription_set.filter(package="premium")
@cached_property
def support_subscriptions(self):
return (
self.hosted_subscriptions
| self.shared_subscriptions
| self.basic_subscriptions
| self.extended_subscriptions
| self.premium_subscriptions
)
@cached_property
def backup_subscriptions(self):
return self.subscription_set.filter(package="backup")
@cached_property
def expires(self):
try:
return self.support_subscriptions.latest("expires").expires
except Subscription.DoesNotExist:
return timezone.now()
def get_suggestions(self):
if not self.support_subscriptions.exists():
yield (
"basic",
_("Basic support"),
_(
"This will give you more of this and that. "
"You can't resist, because it is a huge deal."
),
"img/Support-Basic.svg",
_("Get more support"),
)
if (
not self.hosted_subscriptions.exists()
and not self.shared_subscriptions.exists()
):
if not self.premium_subscriptions.exists():
yield (
"premium",
_("Premium support"),
_(
"This will give you more of this and that. "
"You can't resist, because it is a huge deal."
),
"img/Support-Plus.svg",
_("Get more support"),
)
if not self.extended_subscriptions.exists():
yield (
"extended",
_("Extended support"),
_(
"This will give you more of this and that. "
"You can't resist, because it is a huge deal."
),
"img/Support-Premium.svg",
_("Get more support"),
)
if not self.backup_subscriptions.exists():
yield (
"backup",
_("Backup service"),
_(
"This will give you more of this and that. "
"You can't resist, because it is a huge deal."
),
"img/Support-Backup.svg",
_("Get more support"),
)
def update_status(self):
status = "community"
package = "community"
if self.hosted_subscriptions.filter(expires__gt=timezone.now()).exists():
status = "hosted"
package = self.hosted_subscriptions.latest("expires").package
elif self.shared_subscriptions.filter(expires__gt=timezone.now()).exists():
status = "shared"
package = self.shared_subscriptions.latest("expires").package
elif self.premium_subscriptions.filter(expires__gt=timezone.now()).exists():
status = "premium"
elif self.extended_subscriptions.filter(expires__gt=timezone.now()).exists():
status = "extended"
elif self.basic_subscriptions.filter(expires__gt=timezone.now()).exists():
status = "basic"
package_obj = Package.objects.get(name=package)
if (
status != self.status
or package_obj.limit_source_strings != self.limit_source_strings
):
self.status = status
self.limit_source_strings = package_obj.limit_source_strings
self.limit_languages = package_obj.limit_languages
self.limit_projects = package_obj.limit_projects
self.save()
def create_backup(self):
backup = False
if self.hosted_subscriptions.filter(expires__gt=timezone.now()).exists():
backup = True
if self.backup_subscriptions.filter(expires__gt=timezone.now()).exists():
backup = True
if backup and not self.backup_repository and self.report_set.exists():
self.backup_repository = create_backup_repository(self)
self.save(update_fields=["backup_repository"])
def check_in_limits(self):
if (
self.limit_source_strings
and self.last_report.source_strings > self.limit_source_strings
):
return False
if self.limit_projects and self.last_report.projects > self.limit_projects:
return False
if self.limit_languages and self.last_report.languages > self.limit_languages:
return False
return True
def regenerate(self):
self.secret = generate_secret()
self.save(update_fields=["secret"])
class Subscription(models.Model):
service = models.ForeignKey(Service, on_delete=models.deletion.CASCADE)
payment = models.UUIDField(blank=True, null=True) # noqa: DJ01
package = models.CharField(max_length=150)
created = models.DateTimeField(auto_now_add=True)
expires = models.DateTimeField()
class Meta:
verbose_name = "Customer subscription"
verbose_name_plural = "Customer subscription"
def __str__(self):
return f"{self.get_package_display()}: {self.service}"
def save(
self, force_insert=False, force_update=False, using=None, update_fields=None
):
super().save(force_insert, force_update, using, update_fields)
self.service.update_status()
def get_absolute_url(self):
return reverse("subscription-view", kwargs={"pk": self.pk})
@cached_property
def yearly_package(self):
if self.package.endswith("-m"):
return self.package[:-2]
return None
@cached_property
def package_obj(self):
return Package.objects.get(name=self.package)
def get_package_display(self):
return _(self.package_obj.verbose)
def get_repeat(self):
return self.package_obj.get_repeat()
def active(self):
return self.expires >= timezone.now()
def get_amount(self):
return self.package_obj.price
@cached_property
def payment_obj(self):
return Payment.objects.get(pk=self.payment)
def list_payments(self):
# pylint: disable=no-member
past = set(self.pastpayments_set.values_list("payment", flat=True))
query = Q(pk=self.payment)
if past:
query |= Q(pk__in=past)
query |= Q(repeat__pk__in=past)
if self.payment:
query |= Q(repeat__pk=self.payment)
return Payment.objects.filter(query).distinct()
def send_notification(self, notification):
send_notification(
notification,
[user.email for user in self.service.users.all()],
subscription=self,
)
with override("en"):
send_notification(
notification,
settings.NOTIFY_SUBSCRIPTION,
subscription=self,
)
def could_be_obsolete(self):
expires = timezone.now() + timedelta(days=3)
return (
self.package in ("basic", "extended", "premium")
and self.service.support_subscriptions.exclude(pk=self.pk)
.filter(expires__gt=expires)
.exists()
)
class PastPayments(models.Model):
subscription = models.ForeignKey(
Subscription, on_delete=models.deletion.CASCADE, null=True, blank=True
)
donation = models.ForeignKey(
Donation, on_delete=models.deletion.CASCADE, null=True, blank=True
)
payment = models.UUIDField()
class Meta:
verbose_name = "Past payment"
verbose_name_plural = "Past payments"
def __str__(self):
return f"{self.subscription}: {self.payment}"
class Report(models.Model):
service = models.ForeignKey(Service, on_delete=models.deletion.CASCADE)
site_url = models.URLField(default="")
site_title = models.TextField(default="")
version = models.TextField(default="")
ssh_key = models.TextField(default="")
users = models.IntegerField(default=0)
projects = models.IntegerField(default=0)
components = models.IntegerField(default=0)
languages = models.IntegerField(default=0)
source_strings = models.IntegerField(default=0)
timestamp = models.DateTimeField(auto_now_add=True)
class Meta:
verbose_name = "Weblate report"
verbose_name_plural = "Weblate reports"
def __str__(self):
return self.site_url