Skip to content

Commit

Permalink
Displays all tokens in user profile
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafal Kowalski committed Jun 8, 2019
1 parent b17efb5 commit b091b2c
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 2 deletions.
42 changes: 42 additions & 0 deletions app/dashboard/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2563,6 +2563,44 @@ def get_eth_sum(self, sum_type='collected', network='mainnet', bounties=None):

return eth_sum

def get_all_tokens_sum(self, sum_type='collected', network='mainnet', bounties=None):
"""Get the sum of collected or funded tokens based on the provided type.
Args:
sum_type (str): The sum to lookup. Defaults to: collected.
network (str): The network to query results for.
Defaults to: mainnet.
bounties (dashboard.models.BountyQuerySet): Override the BountyQuerySet this function processes.
Defaults to: None.
Returns:
query: Grouped query by token_name and sum all token value
"""
all_tokens_sum = None
if not bounties:
if sum_type == 'funded':
bounties = self.get_funded_bounties(network=network)
elif sum_type == 'collected':
bounties = self.get_fulfilled_bounties(network=network)
elif sum_type == 'org':
bounties = self.get_orgs_bounties(network=network)

if sum_type == 'funded':
bounties = bounties.has_funds()

try:
if bounties.exists():
all_tokens_sum = bounties.values(
'token_name'
).annotate(
value_in_token=Sum('value_in_token') / 10**18,
).order_by('token_name')

except Exception:
pass

return all_tokens_sum

def get_who_works_with(self, work_type='collected', network='mainnet', bounties=None):
"""Get an array of profiles that this user works with.
Expand Down Expand Up @@ -2704,6 +2742,8 @@ def to_dict(self, activities=True, leaderboards=True, network=None, tips=True):
works_with_funded = self.get_who_works_with(work_type='funded', bounties=funded_bounties)
works_with_collected = self.get_who_works_with(work_type='collected', bounties=fulfilled_bounties)

sum_all_funded_tokens = self.get_all_tokens_sum(sum_type='funded', bounties=funded_bounties)
sum_all_collected_tokens = self.get_all_tokens_sum(sum_type='collected', bounties=fulfilled_bounties)
# org only
count_bounties_on_repo = 0
sum_eth_on_repos = 0
Expand Down Expand Up @@ -2736,6 +2776,8 @@ def to_dict(self, activities=True, leaderboards=True, network=None, tips=True):
'sum_eth_on_repos': sum_eth_on_repos,
'works_with_org': works_with_org,
'count_bounties_on_repo': count_bounties_on_repo,
'sum_all_funded_tokens': sum_all_funded_tokens,
'sum_all_collected_tokens': sum_all_collected_tokens
}

if activities:
Expand Down
14 changes: 12 additions & 2 deletions app/dashboard/templates/profiles/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,12 @@ <h1 class="profile-header__handle">
<div class="card-body">
<ul>
<li><b>{{ count_bounties_completed }}</b> {% trans "bounties completed" %}</li>
<li><b>{{ sum_eth_collected|floatformat:2 }} ETH</b> {% trans "collected" %}</li>
{% for row in sum_all_collected_tokens %}
<li><b>{{ row.value_in_token|floatformat:2 }} {{ row.token_name }}</b> {% trans "collected" %}</li>
{% endfor %}
{% if not sum_all_collected_tokens %}
<li><b>{{ sum_eth_collected|floatformat:2 }} ETH</b> {% trans "collected" %}
{% endif %}
{% if no_times_been_removed %}
<li> - {% trans "removed from" %} <b>{{ no_times_been_removed }}</b> {% trans "bounties" %}</li>
{% endif %}
Expand Down Expand Up @@ -170,7 +175,12 @@ <h1 class="profile-header__handle">
<div class="card-body font-subheader">
<ul>
<li><b>{{ funded_bounties_count }}</b> {% trans "bounties funded" %}</li>
<li><b>{{ sum_eth_funded|floatformat:2 }} ETH</b> {% trans "funded" %}</li>
{% for row in sum_all_funded_tokens %}
<li><b>{{ row.value_in_token|floatformat:2 }} {{ row.token_name }}</b> {% trans "funded" %}</li>
{% endfor %}
{% if not sum_all_funded_tokens %}
<li><b>{{ sum_eth_funded|floatformat:2 }} ETH</b> {% trans "funded" %}
{% endif %}
<li class="works_with_list">
{% if works_with_funded|length %}
<span class="font-body"><i class="fab fa-ethereum"></i> {% trans "Funds" %}:</span>
Expand Down
38 changes: 38 additions & 0 deletions app/dashboard/tests/test_dashboard_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,3 +483,41 @@ def test_bounty_clean_gh_url_on_save():
)
assert bounty.github_url == 'https://github.com/gitcoinco/web/issues/305'
bounty.delete()

@staticmethod
def get_all_tokens_sum():
"""Test all users funded tokens."""
token_names = ['ETH', 'ETH', 'DAI']
for name in token_names:
Bounty.objects.create(
title='foo',
value_in_token=3,
token_name=name,
web3_created=datetime(2008, 10, 31, tzinfo=pytz.UTC),
github_url='https://github.com/gitcoinco/web/issues/305#issuecomment-999999999',
token_address='0x0',
issue_description='hello world',
bounty_owner_github_username='gitcoinco',
is_open=True,
expires_date=datetime(2008, 11, 30, tzinfo=pytz.UTC),
idx_project_length=5,
project_length='Months',
bounty_type='Feature',
experience_level='Intermediate',
raw_data={},
current_bounty=True,
network='mainnet',

)

profile = Profile(
handle='gitcoinco',
data={'type': 'Organization'},
)
query = profile.to_dict()['sum_all_funded_tokens']
assert query[0]['token_name'] == 'DAI'
assert query[0]['value_in_token'] == 3

assert query[1]['token_name'] == 'ETH'
assert query[1]['value_in_token'] == 6

0 comments on commit b091b2c

Please sign in to comment.