Skip to content

Commit

Permalink
Merge pull request #2576 from gitcoinco/kudos-feed
Browse files Browse the repository at this point in the history
Create kudos in feed
  • Loading branch information
owocki authored Nov 5, 2018
2 parents 1608344 + 4a7bb9a commit 3a9514f
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 17 deletions.
2 changes: 1 addition & 1 deletion app/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
CSRF_TRUSTED_ORIGINS = env.list('CSRF_TRUSTED_ORIGINS', default=['localhost'])

# Notifications - Global on / off switch
ENABLE_NOTIFICATIONS_ON_NETWORK = env('ENABLE_NOTIFICATIONS_ON_NETWORK', default='mainnet')
ENABLE_NOTIFICATIONS_ON_NETWORK = env('ENABLE_NOTIFICATIONS_ON_NETWORK', default='mainnet')

# Application definition
INSTALLED_APPS = [
Expand Down
25 changes: 25 additions & 0 deletions app/dashboard/migrations/0111_auto_20181101_1922.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 2.1.2 on 2018-11-01 19:22

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('kudos', '0005_auto_20181101_1356'),
('dashboard', '0110_auto_20181027_1727'),
]

operations = [
migrations.AddField(
model_name='activity',
name='kudos',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='activities', to='kudos.KudosTransfer'),
),
migrations.AlterField(
model_name='activity',
name='activity_type',
field=models.CharField(blank=True, choices=[('new_bounty', 'New Bounty'), ('start_work', 'Work Started'), ('stop_work', 'Work Stopped'), ('work_submitted', 'Work Submitted'), ('work_done', 'Work Done'), ('worker_approved', 'Worker Approved'), ('worker_rejected', 'Worker Rejected'), ('worker_applied', 'Worker Applied'), ('increased_bounty', 'Increased Funding'), ('killed_bounty', 'Canceled Bounty'), ('new_tip', 'New Tip'), ('receive_tip', 'Tip Received'), ('bounty_abandonment_escalation_to_mods', 'Escalated for Abandonment of Bounty'), ('bounty_abandonment_warning', 'Warning for Abandonment of Bounty'), ('bounty_removed_slashed_by_staff', 'Dinged and Removed from Bounty by Staff'), ('bounty_removed_by_staff', 'Removed from Bounty by Staff'), ('bounty_removed_by_funder', 'Removed from Bounty by Funder'), ('new_crowdfund', 'New Crowdfund Contribution'), ('new_kudos', 'New Kudos')], max_length=50),
),
]
58 changes: 48 additions & 10 deletions app/dashboard/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1429,11 +1429,34 @@ class Activity(models.Model):
('bounty_removed_by_staff', 'Removed from Bounty by Staff'),
('bounty_removed_by_funder', 'Removed from Bounty by Funder'),
('new_crowdfund', 'New Crowdfund Contribution'),
('new_kudos', 'New Kudos'),
]

profile = models.ForeignKey('dashboard.Profile', related_name='activities', on_delete=models.CASCADE)
bounty = models.ForeignKey('dashboard.Bounty', related_name='activities', on_delete=models.CASCADE, blank=True, null=True)
tip = models.ForeignKey('dashboard.Tip', related_name='activities', on_delete=models.CASCADE, blank=True, null=True)
profile = models.ForeignKey(
'dashboard.Profile',
related_name='activities',
on_delete=models.CASCADE
)
bounty = models.ForeignKey(
'dashboard.Bounty',
related_name='activities',
on_delete=models.CASCADE,
blank=True,
null=True
)
tip = models.ForeignKey(
'dashboard.Tip',
related_name='activities',
on_delete=models.CASCADE,
blank=True,
null=True
)
kudos = models.ForeignKey(
'kudos.KudosTransfer',
related_name='activities',
on_delete=models.CASCADE,
blank=True, null=True
)
created = models.DateTimeField(auto_now_add=True, blank=True, null=True)
activity_type = models.CharField(max_length=50, choices=ACTIVITY_TYPES, blank=True)
metadata = JSONField(default=dict)
Expand All @@ -1453,15 +1476,19 @@ def i18n_name(self):
@property
def view_props(self):
from dashboard.tokens import token_by_name
from kudos.models import Token
icons = {
'new_tip': 'fa-thumbs-up',
'start_work': 'fa-lightbulb',
'new_bounty': 'fa-money-bill-alt',
'work_done': 'fa-check-circle',
'new_kudos': 'fa-thumbs-up',
}

activity = self
activity.icon = icons.get(activity.activity_type, 'fa-check-circle')
if activity.kudos:
activity.kudos_data = Token.objects.get(pk=activity.kudos.kudos_token_cloned_from_id)
obj = activity.metadata
if 'new_bounty' in activity.metadata:
obj = activity.metadata['new_bounty']
Expand Down Expand Up @@ -1568,27 +1595,38 @@ class Profile(SuperModel):
@property
def get_my_kudos(self):
from kudos.models import KudosTransfer
kt_owner_address = KudosTransfer.objects.filter(kudos_token_cloned_from__owner_address__iexact=self.preferred_payout_address)
kt_owner_address = KudosTransfer.objects.filter(
kudos_token_cloned_from__owner_address__iexact=self.preferred_payout_address
)
kt_profile = KudosTransfer.objects.filter(recipient_profile=self)

kudos_transfers = kt_profile | kt_owner_address
kudos_transfers = kudos_transfers.filter(kudos_token_cloned_from__contract__network=settings.KUDOS_NETWORK)
kudos_transfers = kudos_transfers.filter(
kudos_token_cloned_from__contract__network=settings.KUDOS_NETWORK
)
kudos_transfers = kudos_transfers.exclude(txid='')
kudos_transfers = kudos_transfers.distinct('id') # remove this line IFF we ever move to showing multiple kudos transfers on a profile

return kudos_transfers
# remove this line IFF we ever move to showing multiple kudos transfers on a profile
kudos_transfers = kudos_transfers.distinct('id')

return kudos_transfers

@property
def get_sent_kudos(self):
from kudos.models import KudosTransfer
kt_address = KudosTransfer.objects.filter(from_address__iexact=self.preferred_payout_address)
kt_address = KudosTransfer.objects.filter(
from_address__iexact=self.preferred_payout_address
)
kt_sender_profile = KudosTransfer.objects.filter(sender_profile=self)

kudos_transfers = kt_address | kt_sender_profile
kudos_transfers = kudos_transfers.exclude(txid='')
kudos_transfers = kudos_transfers.filter(kudos_token_cloned_from__contract__network=settings.KUDOS_NETWORK)
kudos_transfers = kudos_transfers.distinct('id') # remove this line IFF we ever move to showing multiple kudos transfers on a profile
kudos_transfers = kudos_transfers.filter(
kudos_token_cloned_from__contract__network=settings.KUDOS_NETWORK
)

# remove this line IFF we ever move to showing multiple kudos transfers on a profile
kudos_transfers = kudos_transfers.distinct('id')

return kudos_transfers

Expand Down
51 changes: 48 additions & 3 deletions app/kudos/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from django.contrib import messages
from django.contrib.postgres.search import SearchVector
from django.contrib.staticfiles.templatetags.staticfiles import static
from django.db.models import Q
from django.http import Http404, HttpResponse, JsonResponse
from django.shortcuts import get_object_or_404, redirect
from django.template.response import TemplateResponse
Expand Down Expand Up @@ -404,7 +405,11 @@ def send_4(request):
# notifications
maybe_market_kudos_to_email(kudos_transfer)
# record_user_action(kudos_transfer.from_username, 'send_kudos', kudos_transfer)
# record_kudos_activity(kudos_transfer, kudos_transfer.from_username, 'new_kudos' if kudos_transfer.username else 'new_crowdfund')
record_kudos_activity(
kudos_transfer,
kudos_transfer.from_username,
'new_kudos' if kudos_transfer.username else 'new_crowdfund'
)
return JsonResponse(response)


Expand Down Expand Up @@ -442,6 +447,39 @@ def record_kudos_email_activity(kudos_transfer, github_handle, event_name):
logger.error(f"error in record_kudos_email_activity: {e} - {event_name} - {kudos_transfer} - {github_handle}")


def record_kudos_activity(kudos_transfer, github_handle, event_name):
logger.debug(kudos_transfer)
kwargs = {
'activity_type': event_name,
'kudos': kudos_transfer,
'metadata': {
'amount': str(kudos_transfer.amount),
'token_name': kudos_transfer.tokenName,
'value_in_eth': str(kudos_transfer.value_in_eth),
'value_in_usdt_now': str(kudos_transfer.value_in_usdt_now),
'github_url': kudos_transfer.github_url,
'to_username': kudos_transfer.username,
'from_name': kudos_transfer.from_name,
'received_on': str(kudos_transfer.received_on) if kudos_transfer.received_on else None
}
}
try:
kwargs['profile'] = Profile.objects.get(handle=github_handle)
except Profile.MultipleObjectsReturned:
kwargs['profile'] = Profile.objects.filter(handle__iexact=github_handle).first()
except Profile.DoesNotExist:
logging.error(f"error in record_kudos_activity: profile with github name {github_handle} not found")
return
try:
kwargs['bounty'] = kudos_transfer.bounty
except:
pass
try:
Activity.objects.create(**kwargs)
except Exception as e:
logging.error(f"error in record_kudos_activity: {e} - {event_name} - {kudos_transfer} - {github_handle}")


def receive(request, key, txid, network):
"""Handle the receiving of a kudos (the POST).
Expand All @@ -450,8 +488,10 @@ def receive(request, key, txid, network):
"""
these_kudos_transfers = KudosTransfer.objects.filter(web3_type='v3', txid=txid, network=network)
kudos_transfers = these_kudos_transfers.filter(metadata__reference_hash_for_receipient=key) | these_kudos_transfers.filter(
metadata__reference_hash_for_funder=key)
kudos_transfers = these_kudos_transfers.filter(
Q(metadata__reference_hash_for_receipient=key) |
Q(metadata__reference_hash_for_funder=key)
)
kudos_transfer = kudos_transfers.first()
if not kudos_transfer:
raise Http404
Expand Down Expand Up @@ -502,6 +542,11 @@ def receive(request, key, txid, network):
kudos_transfer.save()
record_user_action(kudos_transfer.from_username, 'receive_kudos', kudos_transfer)
record_kudos_email_activity(kudos_transfer, kudos_transfer.username, 'receive_kudos')
record_kudos_activity(
kudos_transfer,
kudos_transfer.from_username,
'new_kudos' if kudos_transfer.username else 'new_crowdfund'
)
messages.success(request, _('This kudos has been received'))
except Exception as e:
messages.error(request, str(e))
Expand Down
2 changes: 1 addition & 1 deletion app/marketing/management/commands/assemble_leaderboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ def handle(self, *args, **options):
continue
index_terms = tip_index_terms(t)
sum_tips(t, index_terms)

# kudos'
for kt in KudosTransfer.objects.exclude(txid='').filter(network='mainnet'):
sum_kudos(kt)
Expand Down
21 changes: 19 additions & 2 deletions app/retail/templates/shared/activity.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
<div class="col-12 col-md-1">
<div class="activity-avatar">
<img class="avatar" src="/dynamic/avatar/{{ row.profile.handle }}"/>
{% if row.metadata.to_username %}
<img class="avatar" src="/dynamic/avatar/{{ row.metadata.to_username|cut:'@' }}" style="
width: 30px; height: 30px; bottom: 0; right: 0; position: absolute;"/>
{% endif %}
</div>
</div>
<div class="col-12 col-md-10 activity-info">
Expand All @@ -32,6 +36,13 @@
<a href="https://gitcoin.co/profile/{{ row.metadata.to_username }}" target="_blank">
@{{ row.metadata.to_username }}
</a>
{% elif row.activity_type == 'new_kudos' %}
{% trans "send a" %}
{{ row.i18n_name|lower }}
{% trans "to" %}
<a href="/profile/{{ row.metadata.to_username|cut:'@' }}">
{{ row.metadata.to_username }}
</a>
{% elif row.activity_type == 'new_bounty' %}
<span>{% trans "funded a new issue: " %}</span>{{ row.urled_title | safe }}
{% elif row.activity_type == 'start_work' %}
Expand Down Expand Up @@ -73,7 +84,13 @@
</div>
</div>
</div>
<div class="col-12 col-md-1">
<i class="far {{ row.icon }} last-icon"></i>
<div class="col-12 col-md-1 text-center">
{% if row.kudos %}
<a href="/kudos/{{row.kudos.kudos_token_cloned_from_id}}/{{row.kudos.kudos_token_cloned_from.name}}">
<img src="{% static row.kudos_data.image %}" alt="" class="w-100">
</a>
{% else %}
<i class="far {{ row.icon }} last-icon"></i>
{% endif %}
</div>
</div>

0 comments on commit 3a9514f

Please sign in to comment.