Skip to content

Commit

Permalink
Extract grants models into individual files (#9341)
Browse files Browse the repository at this point in the history
* create new directory for models, copy over Contribution model

* extract grants models to individual files

* rename relocated_models directory, remove original models directory, add imports, resolve circular dependencies

* extract CLRMatch into separate file

* extract Flag into separate file

* extract MatchPledge to separate file

* extract Donation and PhantomFunding

* extract GrantStat into separate file

* refactor

* extract GrantBrandingRoutingPolicy to separate file

* update migration

* add missing import to MatchPledge, remove imports from __init__.py

* add missing import

* decouple GrantCLRCalculation and move to own file

* extract GrantType to own file

* extract GrantCLR to own file

* add missing import

* refactor, add missing imports

* remove whitespace

* resolve circular dependency

* run 'make fix'

* import changes from #9314

* add try/except to migration file instead of editing migration directly

* refactor
  • Loading branch information
Jeremy Schuurmans committed Sep 22, 2021
1 parent 6f736d0 commit 337a72e
Show file tree
Hide file tree
Showing 26 changed files with 2,487 additions and 38 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ jobs:
- npm run eslint
- npm run stylelint
- (cd app; python3 ./manage.py collectstatic --noinput --disable-collectfast)
# Run migrations
- python3 app/manage.py migrate
# Run the tests
- pytest -p no:ethereum -p no:warnings
# Run cypress tests
- bin/ci/cypress-run
Expand Down
4 changes: 3 additions & 1 deletion app/app/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ def preprocess(request):
session_key = request.session._session_key
utm = _get_utm_from_cookie(request)
# record the visit as a celery task
record_visit.delay(request.user.pk, profile.pk, ip_address, visitorId, useragent, referrer, path, session_key, utm)
record_visit.delay(
request.user.pk, profile.pk, ip_address, visitorId, useragent, referrer, path, session_key, utm
)

if should_record_join:
# record the joined action as a celery task
Expand Down
5 changes: 3 additions & 2 deletions app/app/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


class PrimaryDBRouter:

def db_for_read(self, model, **hints):
"""
Reads go to a randomly-chosen replica if backend node
Expand All @@ -26,12 +27,12 @@ def allow_relation(self, obj1, obj2, **hints):
db_set = {'default', 'read_replica_1', 'read_replica_2', 'read_replica_3'}
if obj1._state.db in db_set and obj2._state.db in db_set:
return True
return True # TODO: be more stringent about this IFF we ever have a situation in which diff tables are on diff DBs
return True # TODO: be more stringent about this IFF we ever have a situation in which diff tables are on diff DBs

def allow_migrate(self, db, app_label, model_name=None, **hints):
"""
All non-auth models end up in this pool.
"""
if db == 'default':
return True
return True
return False
53 changes: 34 additions & 19 deletions app/app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,21 @@
),

# verification services
url(r'^api/v0.1/profile/(?P<handle>.*)/request_user_sms/?$', dashboard.views.send_verification, name='request_verification'),
url(r'^api/v0.1/profile/(?P<handle>.*)/verify_user_sms/?$', dashboard.views.validate_verification, name='request_verification'),
url(r'^api/v0.1/profile/(?P<handle>.*)/disconnect_user_sms/?$', dashboard.views.disconnect_sms, name='disconnect_sms'),
url(
r'^api/v0.1/profile/(?P<handle>.*)/request_user_sms/?$',
dashboard.views.send_verification,
name='request_verification'
),
url(
r'^api/v0.1/profile/(?P<handle>.*)/verify_user_sms/?$',
dashboard.views.validate_verification,
name='request_verification'
),
url(
r'^api/v0.1/profile/(?P<handle>.*)/disconnect_user_sms/?$',
dashboard.views.disconnect_sms,
name='disconnect_sms'
),
url(
r'^api/v0.1/profile/(?P<handle>.*)/start_session_idena',
dashboard.views.start_session_idena,
Expand Down Expand Up @@ -187,19 +199,26 @@
r'^api/v0.1/profile/(?P<handle>.*)/verify_user_poap', dashboard.views.verify_user_poap, name='verify_user_poap'
),
url(
r'^api/v0.1/profile/(?P<handle>.*)/disconnect_user_poap', dashboard.views.disconnect_user_poap, name='disconnect_user_poap'
),
url(
r'^api/v0.1/profile/(?P<handle>.*)/verify_user_brightid', dashboard.views.verify_user_brightid, name='verify_user_brightid'
r'^api/v0.1/profile/(?P<handle>.*)/disconnect_user_poap',
dashboard.views.disconnect_user_poap,
name='disconnect_user_poap'
),
url(
r'^api/v0.1/profile/(?P<handle>.*)/disconnect_user_brightid', dashboard.views.disconnect_user_brightid, name='disconnect_user_brightid'
r'^api/v0.1/profile/(?P<handle>.*)/verify_user_brightid',
dashboard.views.verify_user_brightid,
name='verify_user_brightid'
),
url(
r'^api/v0.1/profile/(?P<handle>.*)/verify_user_poh', dashboard.views.verify_user_poh, name='verify_user_poh',
r'^api/v0.1/profile/(?P<handle>.*)/disconnect_user_brightid',
dashboard.views.disconnect_user_brightid,
name='disconnect_user_brightid'
),
url(r'^api/v0.1/profile/(?P<handle>.*)/verify_user_poh', dashboard.views.verify_user_poh, name='verify_user_poh',
),
url(
r'^api/v0.1/profile/(?P<handle>.*)/disconnect_user_poh', dashboard.views.disconnect_user_poh, name='disconnect_user_poh'
r'^api/v0.1/profile/(?P<handle>.*)/disconnect_user_poh',
dashboard.views.disconnect_user_poh,
name='disconnect_user_poh'
),
url(
r'^api/v0.1/profile/(?P<handle>.*)/request_verify_google',
Expand All @@ -223,11 +242,11 @@
dashboard.views.disconnect_user_facebook,
name='disconnect_user_facebook'
),
url(r'api/v0.1/profile/(?P<handle>.*)/verify_user_ens', dashboard.views.verify_user_ens, name='verify_user_ens'),
url(
r'api/v0.1/profile/(?P<handle>.*)/verify_user_ens', dashboard.views.verify_user_ens, name='verify_user_ens'
),
url(
r'api/v0.1/profile/(?P<handle>.*)/disconnect_user_ens', dashboard.views.disconnect_user_ens, name='disconnect_user_ens'
r'api/v0.1/profile/(?P<handle>.*)/disconnect_user_ens',
dashboard.views.disconnect_user_ens,
name='disconnect_user_ens'
),
# url(
# r'^api/v0.1/profile/verify_user_duniter',
Expand Down Expand Up @@ -295,7 +314,6 @@
name='funder_dashboard_bounty_info'
),


# quests
re_path(r'^quests/?$', quests.views.index, name='quests_index'),
re_path(r'^quests/next?$', quests.views.next_quest, name='next_quest'),
Expand All @@ -312,7 +330,6 @@
path(
'contributor_dashboard/<str:bounty_type>/', dashboard.views.contributor_dashboard, name='contributor_dashboard'
),

path('revenue/attestations/new', revenue.views.new_attestation, name='revenue_new_attestation'),

# Hackathons / special events
Expand Down Expand Up @@ -596,7 +613,6 @@
re_path(r'^web3/?$', retail.views.web3, name='web3'),
re_path(r'^support/?$', retail.views.support, name='support'),


# increase funding limit
re_path(r'^requestincrease/?', retail.views.increase_funding_limit_request, name='increase_funding_limit_request'),

Expand Down Expand Up @@ -751,7 +767,7 @@
re_path(r'^_administration/email/match_distribution$', retail.emails.match_distribution, name='match_distribution'),

# docs
re_path(r'^_administration/docs/',include('django.contrib.admindocs.urls')),
re_path(r'^_administration/docs/', include('django.contrib.admindocs.urls')),

# settings
re_path(r'^settings/email/(.*)', marketing.views.email_settings, name='email_settings'),
Expand Down Expand Up @@ -833,7 +849,6 @@
url(r'^api/v0.1/users_csv/', dashboard.views.output_users_to_csv, name='users_csv'),
url(r'^api/v0.1/bounty_mentor/', dashboard.views.bounty_mentor, name='bounty_mentor'),
url(r'^api/v0.1/users_fetch/', dashboard.views.users_fetch, name='users_fetch'),

]

if settings.ENABLE_SILK:
Expand Down
8 changes: 1 addition & 7 deletions app/app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,13 +395,7 @@ def notion_write(database_id='', payload=None):
# write to the pages api (https://developers.notion.com/reference/post-page)
url = "https://api.notion.com/v1/pages"
# define the parent (database) that we're writing to and set the properties (row content)
body = {
"parent": {
"type": "database_id",
"database_id": database_id
},
"properties": payload
}
body = {"parent": {"type": "database_id", "database_id": database_id}, "properties": payload}

# return success as dict
return notion_api_call(url, body)
Expand Down
1 change: 1 addition & 0 deletions app/avatar/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ def determine_response(self, use_svg=True):

return None, None


class CustomAvatar(BaseAvatar):
recommended_by_staff = models.BooleanField(default=False)
style = models.CharField(max_length=10, default='original')
Expand Down
2 changes: 1 addition & 1 deletion app/dataviz/d3_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ def mesh_network_viz(request, ):
"theme": theme,
"themes": ['light', 'dark'],
"show_labels_options": ['1', '0'],
'trim_pct_options': range(0,100),
'trim_pct_options': range(0, 100),
'trim_pct': trim_pct,
"day": day,
"to_year": to_year,
Expand Down
24 changes: 16 additions & 8 deletions app/grants/migrations/0017_matchpledge_end_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@ class Migration(migrations.Migration):
dependencies = [
('grants', '0016_merge_20190305_1435'),
]

operations = [
migrations.AddField(
model_name='matchpledge',
name='end_date',
field=models.DateTimeField(default=grants.models.next_month),
),
]
try:
operations = [
migrations.AddField(
model_name='matchpledge',
name='end_date',
field=models.DateTimeField(default=grants.models.next_month),
),
]
except AttributeError:
operations = [
migrations.AddField(
model_name='matchpledge',
name='end_date',
field=models.DateTimeField(default=grants.models.match_pledge.next_month),
),
]
37 changes: 37 additions & 0 deletions app/grants/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# -*- 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/>.
"""

from .cart_activity import CartActivity
from .clr_match import CLRMatch
from .contribution import Contribution
from .donation import Donation
from .flag import Flag
from .grant import Grant, GrantCLR
from .grant_api_key import GrantAPIKey
from .grant_branding_routing_policy import GrantBrandingRoutingPolicy
from .grant_category import GrantCategory
from .grant_clr_calculation import GrantCLRCalculation
from .grant_collection import GrantCollection
from .grant_stat import GrantStat
from .grant_tag import GrantTag
from .grant_type import GrantType
from .match_pledge import MatchPledge
from .phantom_funding import PhantomFunding
from .subscription import Subscription
24 changes: 24 additions & 0 deletions app/grants/models/cart_activity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from django.contrib.postgres.fields import JSONField
from django.db import models
from django.utils.translation import gettext_lazy as _

from economy.models import SuperModel


class CartActivity(SuperModel):
ACTIONS = (
('ADD_ITEM', 'Add item to cart'),
('REMOVE_ITEM', 'Remove item to cart'),
('CLEAR_CART', 'Clear cart')
)
grant = models.ForeignKey('Grant', null=True, on_delete=models.CASCADE, related_name='cart_actions',
help_text=_('Related Grant Activity '))
profile = models.ForeignKey('dashboard.Profile', on_delete=models.CASCADE, related_name='cart_activity',
help_text=_('User Cart Activity'))
action = models.CharField(max_length=20, choices=ACTIONS, help_text=_('Type of activity'))
metadata = JSONField(default=dict, blank=True, help_text=_('Related data to the action'))
bulk = models.BooleanField(default=False)
latest = models.BooleanField(default=False)

def __str__(self):
return f'{self.action} {self.grant.id if self.grant else "bulk"} from the cart {self.profile.handle}'
56 changes: 56 additions & 0 deletions app/grants/models/clr_match.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from django.db import models
from django.utils.translation import gettext_lazy as _

from economy.models import SuperModel


class CLRMatch(SuperModel):
"""Define the structure of a CLR Match amount."""

round_number = models.PositiveIntegerField(blank=True, null=True)
amount = models.FloatField()
grant = models.ForeignKey(
'grants.Grant',
related_name='clr_matches',
on_delete=models.CASCADE,
null=False,
help_text=_('The associated Grant.'),
)
has_passed_kyc = models.BooleanField(default=False, help_text=_('Has this grant gone through KYC?'))
ready_for_test_payout = models.BooleanField(default=False, help_text=_('Ready for test payout or not'))
test_payout_tx = models.CharField(
max_length=255,
blank=True,
help_text=_('The test payout txid'),
)
test_payout_tx_date = models.DateTimeField(null=True, blank=True)
test_payout_contribution = models.ForeignKey(
'grants.Contribution',
related_name='test_clr_match_payouts',
null=True,
blank=True,
on_delete=models.SET_NULL,
help_text=_('Contribution for the test payout')
)

ready_for_payout = models.BooleanField(default=False, help_text=_('Ready for regular payout or not'))
payout_tx = models.CharField(
max_length=255,
blank=True,
help_text=_('The test payout txid'),
)
payout_tx_date = models.DateTimeField(null=True, blank=True)
payout_contribution = models.ForeignKey(
'grants.Contribution',
related_name='clr_match_payouts',
null=True,
blank=True,
on_delete=models.SET_NULL,
help_text=_('Contribution for the payout')
)
comments = models.TextField(default='', blank=True, help_text=_('The comments.'))


def __str__(self):
"""Return the string representation of a Grant."""
return f"id: {self.pk}, grant: {self.grant.pk}, round: {self.round_number}, amount: {self.amount}"
Loading

0 comments on commit 337a72e

Please sign in to comment.