-
-
Notifications
You must be signed in to change notification settings - Fork 775
/
models.py
2312 lines (2022 loc) · 88.9 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
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
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""Define the Grant models.
Copyright (C) 2021 Gitcoin Core
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import json
import logging
import traceback
from datetime import timedelta
from decimal import Decimal
from io import BytesIO
from django.conf import settings
from django.contrib.humanize.templatetags.humanize import naturaltime
from django.contrib.postgres.fields import ArrayField, JSONField
from django.contrib.postgres.indexes import GinIndex
from django.contrib.postgres.search import SearchVector, SearchVectorField
from django.core import serializers
from django.core.files.base import ContentFile
from django.core.files.uploadedfile import InMemoryUploadedFile
from django.db import models
from django.db.models import Q
from django.db.models.signals import post_save, pre_save
from django.dispatch import receiver
from django.templatetags.static import static
from django.urls import reverse
from django.utils import timezone
from django.utils.timezone import localtime
from django.utils.translation import gettext_lazy as _
import pytz
import requests
from django_extensions.db.fields import AutoSlugField
from economy.models import SuperModel
from economy.tx import check_for_replaced_tx
from economy.utils import ConversionRateNotFoundError, convert_amount
from gas.utils import eth_usd_conv_rate, recommend_min_gas_price_to_confirm_in_time
from grants.utils import generate_collection_thumbnail, get_upload_filename, is_grant_team_member
from townsquare.models import Comment, Favorite
from web3 import Web3
logger = logging.getLogger(__name__)
class GrantQuerySet(models.QuerySet):
"""Define the Grant default queryset and manager."""
def active(self):
"""Filter results down to active grants only."""
return self.filter(active=True)
def inactive(self):
"""Filter results down to inactive grants only."""
return self.filter(active=False)
def keyword(self, keyword):
"""Filter results to all Grant objects containing the keywords.
Args:
keyword (str): The keyword to search title, description, and reference URL by.
Returns:
dashboard.models.GrantQuerySet: The QuerySet of grants filtered by keyword.
"""
if not keyword:
return self
return self.filter(
Q(description__icontains=keyword) |
Q(title__icontains=keyword) |
Q(reference_url__icontains=keyword)
)
# TODO: REMOVE
class GrantCategory(SuperModel):
category = models.CharField(
max_length=50,
blank=False,
null=False,
help_text=_('Grant Category'),
)
def __str__(self):
"""Return the string representation of a Grant Category."""
return f"{self.category}"
class GrantTag(SuperModel):
name = models.CharField(
unique=True,
max_length=50,
blank=False,
null=False,
help_text=_('Grant Tag'),
)
def __str__(self):
"""Return the string representation of a GrantTag."""
return f"{self.name}"
class GrantType(SuperModel):
name = models.CharField(unique=True, max_length=15, help_text="Grant Type")
label = models.CharField(max_length=25, null=True, help_text="Display Name")
is_active = models.BooleanField(default=True, db_index=True, help_text="Is Grant Type currently active")
is_visible = models.BooleanField(default=True, db_index=True, help_text="Is visible on the Grant filters")
logo = models.ImageField(
upload_to=get_upload_filename,
null=True,
blank=True,
max_length=500,
help_text=_('The default category\'s marketing banner (aspect ratio = 10:3)'),
)
def __str__(self):
"""Return the string representation."""
return f"{self.name}"
@property
def clrs(self):
return GrantCLR.objects.filter(grant_filters__grant_type=str(self.pk))
@property
def active_clrs(self):
return GrantCLR.objects.filter(is_active=True, grant_filters__grant_type=str(self.pk))
@property
def active_clrs_sum(self):
return sum(self.active_clrs.values_list('total_pot', flat=True))
class GrantCLR(SuperModel):
class Meta:
unique_together = ('customer_name', 'round_num', 'sub_round_slug',)
customer_name = models.CharField(
max_length=15,
default='',
blank=True,
help_text="used to genrate customer_name/round_num/sub_round_slug"
)
round_num = models.PositiveIntegerField(
help_text="CLR Round Number. used to generate customer_name/round_num/sub_round_slug"
)
sub_round_slug = models.CharField(
max_length=25,
default='',
blank=True,
help_text="used to generate customer_name/round_num/sub_round_slug"
)
display_text = models.CharField(
max_length=25,
null=True,
blank=True,
help_text="sets the custom text in CLR banner on the landing page"
)
owner = models.ForeignKey(
'dashboard.Profile',
null=True,
blank=True,
on_delete=models.SET_NULL,
help_text='sets the owners profile photo in CLR banner on the landing page'
)
is_active = models.BooleanField(default=False, db_index=True, help_text="Is CLR Round currently active")
start_date = models.DateTimeField(help_text="CLR Round Start Date")
end_date = models.DateTimeField(help_text="CLR Round End Date")
grant_filters = JSONField(
default=dict,
null=True, blank=True,
help_text="Grants allowed in this CLR round"
)
subscription_filters = JSONField(
default=dict,
null=True, blank=True,
help_text="Grant Subscription to be allowed in this CLR round"
)
collection_filters = JSONField(
default=dict,
null=True, blank=True,
help_text="Grant Collections to be allowed in this CLR round"
)
verified_threshold = models.DecimalField(
help_text="This is the verfied CLR threshold. You can generally increase the saturation of the round / increase the CLR match by increasing this value, as it has a proportional relationship. However, depending on the pair totals by grant, it may reduce certain matches. In any case, please use the contribution multiplier first.",
default=25.0,
decimal_places=2,
max_digits=5
)
unverified_threshold = models.DecimalField(
help_text="This is the unverified CLR threshold. The relationship with the CLR match is the same as the verified threshold. If you would like to increase the saturation of round / increase the CLR match, increase this value, but please use the contribution multiplier first.",
default=5.0,
decimal_places=2,
max_digits=5
)
total_pot = models.DecimalField(
help_text="Total CLR Pot",
default=0,
decimal_places=2,
max_digits=10
)
contribution_multiplier = models.DecimalField(
help_text="This contribution multipler is applied to each contribution before running CLR calculations. In order to increase the saturation, please increase this value first, before modifying the thresholds.",
default=1.0,
decimal_places=4,
max_digits=10
)
logo = models.ImageField(
upload_to=get_upload_filename,
null=True,
blank=True,
max_length=500,
help_text=_('sets the background in CLR banner on the landing page'),
)
def __str__(self):
return f"{self.round_num}"
@property
def happening_now(self):
# returns true if we are within the time range for this round
now = timezone.now()
return now >= self.start_date and now <= self.end_date
@property
def happened_recently(self):
# returns true if we are within a week or 2 of this round
days = 14
now = timezone.now()
then = timezone.now() - timezone.timedelta(days=days)
return now >= self.start_date and then <= self.end_date
@property
def grants(self):
grants = Grant.objects.filter(hidden=False, active=True, is_clr_eligible=True, link_to_new_grant=None)
if self.collection_filters:
grant_ids = GrantCollection.objects.filter(**self.collection_filters).values_list('grants', flat=True)
grants = grants.filter(pk__in=grant_ids)
if self.grant_filters:
grants = grants.filter(**self.grant_filters)
if self.subscription_filters:
grants = grants.filter(**self.subscription_filters)
return grants
def record_clr_prediction_curve(self, grant, clr_prediction_curve):
for obj in self.clr_calculations.filter(grant=grant, latest=True):
obj.latest = False
obj.save()
GrantCLRCalculation.objects.create(
grantclr=self,
grant=grant,
clr_prediction_curve=clr_prediction_curve,
latest=True,
)
class GrantAPIKey(SuperModel):
"""Define the structure of a GrantAPIKey."""
key = models.CharField(
max_length=255,
blank=True,
db_index=True,
help_text="the api key"
)
secret = models.CharField(
max_length=255,
blank=True,
db_index=True,
help_text="the api secret"
)
profile = models.ForeignKey(
'dashboard.Profile',
related_name='grant_apikey',
on_delete=models.CASCADE,
help_text=_('The GrantAPI key\'s profile.'),
null=True,
)
class Grant(SuperModel):
"""Define the structure of a Grant."""
class Meta:
"""Define the metadata for Grant."""
ordering = ['-created_on']
indexes = (GinIndex(fields=["vector_column"]),)
REGIONS = [
('north_america', 'North America'),
('oceania', 'Oceania'),
('latin_america', 'Latin America'),
('europe', 'Europe'),
('africa', 'Africa'),
('middle_east', 'Middle East'),
('india', 'India'),
('east_asia', 'East Asia'),
('southeast_asia', 'Southeast Asia')
]
EXTERNAL_FUNDING = [
('yes', 'Yes'),
('no', 'No'),
('unknown', 'Unknown')
]
vector_column = SearchVectorField(null=True, help_text=_('Used for full text search. Generated using [title, description]'))
active = models.BooleanField(default=True, help_text=_('Whether or not the Grant is active.'), db_index=True)
grant_type = models.ForeignKey(GrantType, on_delete=models.CASCADE, null=True, help_text="Grant Type")
title = models.CharField(default='', max_length=255, help_text=_('The title of the Grant.'))
slug = AutoSlugField(populate_from='title')
description = models.TextField(default='', blank=True, help_text=_('The description of the Grant.'))
description_rich = models.TextField(default='', blank=True, help_text=_('HTML rich description.'))
reference_url = models.URLField(blank=True, help_text=_('The associated reference URL of the Grant.'))
github_project_url = models.URLField(blank=True, null=True, help_text=_('Grant Github Project URL'))
is_clr_eligible = models.BooleanField(default=True, help_text="Is grant eligible for CLR")
admin_message = models.TextField(default='', blank=True, help_text=_('An admin message that will be shown to visitors of this grant.'))
visible = models.BooleanField(default=True, help_text="Is grant visible on the site")
region = models.CharField(
max_length=30,
null=True,
blank=True,
choices=REGIONS,
help_text="region to which grant belongs to"
)
link_to_new_grant = models.ForeignKey(
'grants.Grant',
null=True,
on_delete=models.SET_NULL,
help_text=_('Link to new grant if migrated')
)
logo = models.ImageField(
upload_to=get_upload_filename,
null=True,
blank=True,
max_length=500,
help_text=_('The Grant logo image.'),
)
logo_svg = models.FileField(
upload_to=get_upload_filename,
null=True,
blank=True,
help_text=_('The Grant logo SVG.'),
)
# TODO-GRANTS: rename to eth_payout_address
admin_address = models.CharField(
max_length=255,
default='0x0',
null=True,
blank=True,
db_index=True,
help_text=_('The wallet address where subscription funds will be sent.'),
)
zcash_payout_address = models.CharField(
max_length=255,
default='0x0',
null=True,
blank=True,
help_text=_('The zcash wallet address where subscription funds will be sent.'),
)
celo_payout_address = models.CharField(
max_length=255,
default='0x0',
null=True,
blank=True,
help_text=_('The celo wallet address where subscription funds will be sent.'),
)
zil_payout_address = models.CharField(
max_length=255,
default='0x0',
null=True,
blank=True,
help_text=_('The zilliqa wallet address where subscription funds will be sent.'),
)
polkadot_payout_address = models.CharField(
max_length=255,
default='0x0',
null=True,
blank=True,
help_text=_('The polkadot wallet address where subscription funds will be sent.'),
)
kusama_payout_address = models.CharField(
max_length=255,
default='0x0',
null=True,
blank=True,
help_text=_('The kusama wallet address where subscription funds will be sent.'),
)
harmony_payout_address = models.CharField(
max_length=255,
default='0x0',
null=True,
blank=True,
help_text=_('The harmony wallet address where subscription funds will be sent.'),
)
binance_payout_address = models.CharField(
max_length=255,
default='0x0',
null=True,
blank=True,
help_text=_('The binance wallet address where subscription funds will be sent.'),
)
rsk_payout_address = models.CharField(
max_length=255,
default='0x0',
null=True,
blank=True,
help_text=_('The rsk wallet address where subscription funds will be sent.'),
)
algorand_payout_address = models.CharField(
max_length=255,
default='0x0',
null=True,
blank=True,
help_text=_('The algorand wallet address where subscription funds will be sent.'),
)
# TODO-GRANTS: remove
contract_owner_address = models.CharField(
max_length=255,
default='0x0',
help_text=_('The wallet address that owns the subscription contract and is able to call endContract()'),
)
amount_received_in_round = models.DecimalField(
default=0,
decimal_places=4,
max_digits=50,
help_text=_('The amount received in USD this round.'),
)
monthly_amount_subscribed = models.DecimalField(
default=0,
decimal_places=4,
max_digits=50,
help_text=_('The monthly subscribed to by contributors USD.'),
)
amount_received = models.DecimalField(
default=0,
decimal_places=4,
max_digits=50,
help_text=_('The total amount received for the Grant in USD.'),
)
# TODO-GRANTS: remove
token_address = models.CharField(
max_length=255,
default='0x0',
help_text=_('The token address to be used with the Grant.'),
)
token_symbol = models.CharField(
max_length=255,
default='',
help_text=_('The token symbol to be used with the Grant.'),
)
# TODO-GRANTS: remove
contract_address = models.CharField(
max_length=255,
default='0x0',
help_text=_('The contract address of the Grant.'),
)
# TODO-GRANTS: remove
deploy_tx_id = models.CharField(
max_length=255,
default='0x0',
help_text=_('The transaction id for contract deployment.'),
)
# TODO-GRANTS: remove
cancel_tx_id = models.CharField(
max_length=255,
default='0x0',
help_text=_('The transaction id for endContract.'),
blank=True,
)
contract_version = models.DecimalField(
default=0,
decimal_places=0,
max_digits=3,
help_text=_('The contract version the Grant.'),
)
metadata = JSONField(
default=dict,
blank=True,
help_text=_('The Grant metadata. Includes creation and last synced block numbers.'),
)
network = models.CharField(
max_length=8,
default='mainnet',
help_text=_('The network in which the Grant contract resides.'),
db_index=True
)
required_gas_price = models.DecimalField(
default='0',
decimal_places=0,
max_digits=50,
help_text=_('The required gas price for the Grant.'),
)
admin_profile = models.ForeignKey(
'dashboard.Profile',
related_name='grant_admin',
on_delete=models.CASCADE,
help_text=_('The Grant administrator\'s profile.'),
null=True,
)
team_members = models.ManyToManyField(
'dashboard.Profile',
related_name='grant_teams',
help_text=_('The team members contributing to this Grant.'),
)
image_css = models.CharField(default='', blank=True, max_length=255, help_text=_('additional CSS to attach to the grant-banner img.'))
amount_received_with_phantom_funds = models.DecimalField(
default=0,
decimal_places=2,
max_digits=20,
help_text=_('The fundingamount across all rounds with phantom funding'),
)
activeSubscriptions = ArrayField(models.CharField(max_length=200), blank=True, default=list)
hidden = models.BooleanField(default=False, help_text=_('Hide the grant from the /grants page?'), db_index=True)
random_shuffle = models.PositiveIntegerField(blank=True, null=True, db_index=True)
weighted_shuffle = models.PositiveIntegerField(blank=True, null=True, db_index=True)
contribution_count = models.PositiveIntegerField(blank=True, default=0)
contributor_count = models.PositiveIntegerField(blank=True, default=0)
# TODO-GRANTS: remove
positive_round_contributor_count = models.PositiveIntegerField(blank=True, default=0)
# TODO-GRANTS: remove
negative_round_contributor_count = models.PositiveIntegerField(blank=True, default=0)
defer_clr_to = models.ForeignKey(
'grants.Grant',
related_name='defered_clr_from',
on_delete=models.CASCADE,
help_text=_('The Grant that this grant defers it CLR contributions to (if any).'),
null=True,
)
# TODO-CROSS-GRANT: [{round: fk1, value: time}]
last_clr_calc_date = models.DateTimeField(
help_text=_('The last clr calculation date'),
null=True,
blank=True,
)
# TODO-CROSS-GRANT: [{round: fk1, value: time}]
next_clr_calc_date = models.DateTimeField(
help_text=_('The last clr calculation date'),
null=True,
blank=True,
)
# TODO-CROSS-GRANT: [{round: fk1, value: time}]
last_update = models.DateTimeField(
help_text=_('The last grant admin update date'),
null=True,
blank=True,
)
categories = models.ManyToManyField(GrantCategory, blank=True) # TODO: REMOVE
tags = models.ManyToManyField(GrantTag, blank=True)
twitter_handle_1 = models.CharField(default='', max_length=255, help_text=_('Grants twitter handle'), blank=True)
twitter_handle_2 = models.CharField(default='', max_length=255, help_text=_('Grants twitter handle'), blank=True)
twitter_handle_1_follower_count = models.PositiveIntegerField(blank=True, default=0)
twitter_handle_2_follower_count = models.PositiveIntegerField(blank=True, default=0)
sybil_score = models.DecimalField(
default=0,
decimal_places=4,
max_digits=50,
help_text=_('The Grants Sybil Score'),
)
# TODO-GRANTS: remove funding_info
funding_info = models.CharField(default='', blank=True, null=True, max_length=255, help_text=_('Is this grant VC funded?'))
has_external_funding = models.CharField(
max_length=8,
default='unknown',
choices=EXTERNAL_FUNDING,
help_text="Does this grant have external funding"
)
clr_prediction_curve = ArrayField(
ArrayField(
models.FloatField(),
size=2,
), blank=True, default=list, help_text=_('5 point curve to predict CLR donations.'))
weighted_risk_score = models.DecimalField(
default=0,
decimal_places=4,
max_digits=50,
help_text=_('The Grants Weighted Risk Score'),
)
in_active_clrs = models.ManyToManyField(
GrantCLR,
help_text="Active Grants CLR Round"
)
is_clr_active = models.BooleanField(default=False, help_text=_('CLR Round active or not? (auto computed)'))
clr_round_num = models.CharField(default='', max_length=255, help_text=_('the CLR round number thats active'), blank=True)
twitter_verified = models.BooleanField(default=False, help_text='The owner grant has verified the twitter account')
twitter_verified_by = models.ForeignKey('dashboard.Profile', null=True, blank=True, on_delete=models.SET_NULL, help_text='Team member who verified this grant')
twitter_verified_at = models.DateTimeField(blank=True, null=True, help_text='At what time and date what verified this grant')
# Grant Query Set used as manager.
objects = GrantQuerySet.as_manager()
def __str__(self):
"""Return the string representation of a Grant."""
return f"id: {self.pk}, active: {self.active}, title: {self.title}, type: {self.grant_type}"
def is_on_team(self, profile):
return is_grant_team_member(self, profile)
def calc_clr_round(self):
clr_round = None
# create_grant_active_clr_mapping
clr_rounds = GrantCLR.objects.all()
for this_clr_round in clr_rounds:
add_to_round = self.active and not self.hidden and this_clr_round.is_active and this_clr_round.happening_now and self.pk in this_clr_round.grants.all().values_list('pk', flat=True)
if add_to_round:
self.in_active_clrs.add(this_clr_round)
else:
if this_clr_round in self.in_active_clrs.all():
self.in_active_clrs.remove(this_clr_round)
# create_grant_clr_cache
if self.in_active_clrs.count() > 0 and self.is_clr_eligible:
clr_round = self.in_active_clrs.first()
if clr_round:
self.is_clr_active = True
self.clr_round_num = clr_round.round_num
else:
self.is_clr_active = False
self.clr_round_num = ''
@property
def tenants(self):
"""returns list of chains the grant can recieve contributions in"""
tenants = []
# TODO: rename to eth_payout_address
if self.admin_address and self.admin_address != '0x0':
tenants.append('ETH')
if self.zcash_payout_address and self.zcash_payout_address != '0x0':
tenants.append('ZCASH')
if self.celo_payout_address and self.celo_payout_address != '0x0':
tenants.append('CELO')
if self.zil_payout_address and self.zil_payout_address != '0x0':
tenants.append('ZIL')
if self.polkadot_payout_address and self.polkadot_payout_address != '0x0':
tenants.append('POLKADOT')
if self.kusama_payout_address and self.kusama_payout_address != '0x0':
tenants.append('KUSAMA')
if self.harmony_payout_address and self.harmony_payout_address != '0x0':
tenants.append('HARMONY')
if self.binance_payout_address and self.binance_payout_address != '0x0':
tenants.append('BINANCE')
if self.rsk_payout_address and self.rsk_payout_address != '0x0':
tenants.append('RSK')
if self.algorand_payout_address and self.algorand_payout_address != '0x0':
tenants.append('ALGORAND')
return tenants
@property
def calc_clr_round_nums(self):
"""Generates CLR rounds sub_round_slug seperated by comma"""
if self.pk:
round_nums = [ele for ele in self.in_active_clrs.values_list('sub_round_slug', flat=True)]
round_nums = list(filter(None, round_nums))
return ", ".join(round_nums)
return ''
@property
def calc_clr_round_label(self):
"""Generates CLR rounds display text seperated by comma"""
if self.pk:
round_nums = [ele for ele in self.in_active_clrs.values_list('display_text', flat=True)]
round_nums = list(filter(None, round_nums))
return ", ".join(round_nums)
return ''
@property
def calc_clr_prediction_curve(self):
# [amount_donated, match amount, bonus_from_match_amount ], etc..
# [0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]
_clr_prediction_curve = []
for insert_clr_calc in self.clr_calculations.using('default').filter(latest=True).order_by('-created_on'):
insert_clr_calc = insert_clr_calc.clr_prediction_curve
if not _clr_prediction_curve:
_clr_prediction_curve = insert_clr_calc
else:
for j in [1,2]:
for i in [0,1,2,3,4,5]:
# add the 1 and 2 index of each clr prediction cuve
_clr_prediction_curve[i][j] += insert_clr_calc[i][j]
if not _clr_prediction_curve:
_clr_prediction_curve = [[0.0, 0.0, 0.0] for x in range(0, 6)]
return _clr_prediction_curve
def updateActiveSubscriptions(self):
"""updates the active subscriptions list"""
handles = []
for handle in Subscription.objects.filter(grant=self, active=True, is_postive_vote=True).distinct('contributor_profile').values_list('contributor_profile__handle', flat=True):
handles.append(handle)
self.activeSubscriptions = handles
@property
def safe_next_clr_calc_date(self):
if self.next_clr_calc_date and self.next_clr_calc_date < timezone.now():
return timezone.now() + timezone.timedelta(minutes=5)
return self.next_clr_calc_date
@property
def recurring_funding_supported(self):
return self.contract_version < 2
@property
def related_grants(self):
pkg = self.metadata.get('related', [])
pks = [ele[0] for ele in pkg]
rg = Grant.objects.filter(pk__in=pks)
return_me = []
for ele in pkg:
grant = rg.get(pk=ele[0])
return_me.append([grant, ele[1]])
return return_me
@property
def configured_to_receieve_funding(self):
if self.contract_version == 2:
return True
return self.contract_address != '0x0'
@property
def clr_match_estimate_this_round(self):
try:
return self.clr_prediction_curve[0][1]
except:
return 0
@property
def contributions(self):
pks = []
for subscription in self.subscriptions.all():
pks += list(subscription.subscription_contribution.values_list('pk', flat=True))
return Contribution.objects.filter(pk__in=pks)
@property
def negative_voting_enabled(self):
return False
def is_on_team(self, profile):
if profile.pk == self.admin_profile.pk:
return True
if profile.grant_teams.filter(pk=self.pk).exists():
return True
return False
@property
def org_name(self):
from git.utils import org_name
try:
return org_name(self.reference_url)
except Exception:
return None
@property
def get_contribution_count(self):
num = 0
num += self.subscriptions.filter(is_postive_vote=True, subscription_contribution__success=True).count()
num += self.phantom_funding.all().count()
return num
@property
def contributors(self):
return_me = []
for sub in self.subscriptions.filter(is_postive_vote=True):
for contrib in sub.subscription_contribution.filter(success=True):
return_me.append(contrib.subscription.contributor_profile)
for pf in self.phantom_funding.all():
return_me.append(pf.profile)
return return_me
def get_contributor_count(self, since=None, is_postive_vote=True):
if not since:
since = timezone.datetime(1990, 1, 1)
num = self.subscriptions.filter(is_postive_vote=is_postive_vote, subscription_contribution__success=True, created_on__gt=since).distinct('contributor_profile').count()
if is_postive_vote:
num += self.phantom_funding.filter(created_on__gt=since).exclude(profile__in=self.subscriptions.values_list('contributor_profile')).all().count()
return num
@property
def org_profile(self):
from dashboard.models import Profile
profiles = Profile.objects.filter(handle=self.org_name.lower())
if profiles.count():
return profiles.first()
return None
@property
def history_by_month(self):
import math
# gets the history of contributions to this grant month over month so they can be shown o grant details
# returns [["", "Subscription Billing", "New Subscriptions", "One-Time Contributions", "CLR Matching Funds"], ["December 2017", 5534, 2011, 0, 0], ["January 2018", 10396, 0 , 0, 0 ], ... for each monnth in which this grant has contribution history];
CLR_PAYOUT_HANDLES = ['vs77bb', 'gitcoinbot', 'notscottmoore', 'owocki']
month_to_contribution_numbers = {}
subs = self.subscriptions.all().prefetch_related('subscription_contribution')
for sub in subs:
contribs = [sc for sc in sub.subscription_contribution.all() if sc.success]
for contrib in contribs:
#add all contributions
year = contrib.created_on.strftime("%Y")
quarter = math.ceil(int(contrib.created_on.strftime("%m"))/3.)
key = f"{year}/Q{quarter}"
subkey = 'One-Time'
if contrib.subscription.contributor_profile.handle in CLR_PAYOUT_HANDLES:
subkey = 'CLR'
if key not in month_to_contribution_numbers.keys():
month_to_contribution_numbers[key] = {"One-Time": 0, "Recurring-Recurring": 0, "New-Recurring": 0, 'CLR': 0}
if contrib.subscription.amount_per_period_usdt:
month_to_contribution_numbers[key][subkey] += float(contrib.subscription.amount_per_period_usdt)
# sort and return
return_me = [["", "Contributions", "CLR Matching Funds"]]
for key, val in (sorted(month_to_contribution_numbers.items(), key=lambda kv:(kv[0]))):
return_me.append([key, val['One-Time'], val['CLR']])
return return_me
@property
def history_by_month_max(self):
max_amount = 0
for ele in self.history_by_month:
if type(ele[1]) is float:
max_amount = max(max_amount, ele[1]+ele[2])
return max_amount
def get_amount_received_with_phantom_funds(self):
return float(self.amount_received) + float(sum([ele.value for ele in self.phantom_funding.all()]))
@property
def abi(self):
"""Return grants abi."""
if self.contract_version == 0:
from grants.abi import abi_v0
return abi_v0
elif self.contract_version == 1:
from grants.abi import abi_v1
return abi_v1
@property
def url(self):
"""Return grants url."""
from django.urls import reverse
slug = self.slug if self.slug else "-"
return reverse('grants:details', kwargs={'grant_id': self.pk, 'grant_slug': slug})
def get_absolute_url(self):
return self.url
@property
def contract(self):
"""Return grants contract."""
from dashboard.utils import get_web3
web3 = get_web3(self.network)
grant_contract = web3.eth.contract(Web3.toChecksumAddress(self.contract_address), abi=self.abi)
return grant_contract
def cart_payload(self, build_absolute_uri, user=None):
return {
'grant_id': str(self.id),
'grant_slug': self.slug,
'grant_url': self.url,
'grant_title': self.title,
'grant_contract_version': self.contract_version,
'grant_contract_address': self.contract_address,
'grant_token_symbol': self.token_symbol,
'grant_admin_address': self.admin_address,
'grant_token_address': self.token_address,
'grant_logo': self.logo.url if self.logo and self.logo.url else build_absolute_uri(static(f'v2/images/grants/logos/{self.id % 3}.png')),
'grant_clr_prediction_curve': self.clr_prediction_curve,
'grant_image_css': self.image_css,
'is_clr_eligible': self.is_clr_eligible,
'clr_round_num': self.clr_round_num,
'tenants': self.tenants,
'zcash_payout_address': self.zcash_payout_address,
'celo_payout_address': self.celo_payout_address,
'zil_payout_address': self.zil_payout_address,
'polkadot_payout_address': self.polkadot_payout_address,
'harmony_payout_address': self.harmony_payout_address,
'binance_payout_address': self.binance_payout_address,
'kusama_payout_address': self.kusama_payout_address,
'harmony_payout_address': self.harmony_payout_address,
'rsk_payout_address': self.rsk_payout_address,
'algorand_payout_address': self.algorand_payout_address,
'is_on_team': is_grant_team_member(self, user.profile) if user and user.is_authenticated else False,
}
def repr(self, user, build_absolute_uri):
team_members = serializers.serialize('json', self.team_members.all(),
fields=['handle', 'url', 'profile__lazy_avatar_url']
)
if self.grant_type:
grant_type = serializers.serialize('json', [self.grant_type], fields=['name', 'label'])
grant_tags = serializers.serialize('json', self.tags.all(),fields=['id', 'name'])
return {
'id': self.id,
'active': self.active,
'logo_url': self.logo.url if self.logo and self.logo.url else build_absolute_uri(static(f'v2/images/grants/logos/{self.id % 3}.png')),
'details_url': reverse('grants:details', args=(self.id, self.slug)),
'title': self.title,
'description': self.description,
'description_rich': self.description_rich,
'last_update': self.last_update,
'last_update_natural': naturaltime(self.last_update),
'sybil_score': self.sybil_score,
'weighted_risk_score': self.weighted_risk_score,
'is_clr_active': self.is_clr_active,
'clr_round_num': self.clr_round_num,
'admin_profile': {
'url': self.admin_profile.url,
'handle': self.admin_profile.handle,
'avatar_url': self.admin_profile.lazy_avatar_url
},
'favorite': self.favorite(user) if user.is_authenticated else False,
'is_on_team': is_grant_team_member(self, user.profile) if user.is_authenticated else False,
'clr_prediction_curve': self.clr_prediction_curve,
'last_clr_calc_date': naturaltime(self.last_clr_calc_date) if self.last_clr_calc_date else None,
'safe_next_clr_calc_date': naturaltime(self.safe_next_clr_calc_date) if self.safe_next_clr_calc_date else None,
'amount_received_in_round': self.amount_received_in_round,
'amount_received': self.amount_received,
'positive_round_contributor_count': self.positive_round_contributor_count,
'monthly_amount_subscribed': self.monthly_amount_subscribed,
'is_clr_eligible': self.is_clr_eligible,
'slug': self.slug,
'url': self.url,
'contract_version': self.contract_version,
'contract_address': self.contract_address,
'token_symbol': self.token_symbol,
'admin_address': self.admin_address,
'zcash_payout_address': self.zcash_payout_address or '',
'celo_payout_address': self.celo_payout_address,
'zil_payout_address': self.zil_payout_address,
'polkadot_payout_address': self.polkadot_payout_address,
'kusama_payout_address': self.kusama_payout_address,
'harmony_payout_address': self.harmony_payout_address,
'binance_payout_address': self.binance_payout_address,
'rsk_payout_address': self.rsk_payout_address,
'algorand_payout_address': self.algorand_payout_address,
'token_address': self.token_address,
'image_css': self.image_css,
'verified': self.twitter_verified,
'tenants': self.tenants,
'team_members': json.loads(team_members),
'metadata': self.metadata,
'grant_type': json.loads(grant_type) if grant_type else None,
'grant_tags': json.loads(grant_tags),
'twitter_handle_1': self.twitter_handle_1,
'twitter_handle_2': self.twitter_handle_2,
'reference_url': self.reference_url,
'github_project_url': self.github_project_url or '',
'funding_info': self.funding_info,
'admin_message': self.admin_message,
'link_to_new_grant': self.link_to_new_grant.url if self.link_to_new_grant else self.link_to_new_grant,
'region': {'name':self.region, 'label':self.get_region_display()} if self.region and self.region != 'null' else None,
'has_external_funding': self.has_external_funding
}
def favorite(self, user):
return Favorite.objects.filter(user=user, grant=self).exists()
def save(self, update=True, *args, **kwargs):
"""Override the Grant save to optionally handle modified_on logic."""