Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Records UserAction events for Tip, Start/Stop Work, and Bounty related things #664

Merged
merged 2 commits into from Mar 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion app/assets/v2/js/pages/bounty_details.js
Expand Up @@ -194,7 +194,6 @@ var showWarningMessage = function(txid) {
clearInterval(interval);
var link_url = etherscan_tx_url(txid);

$('#pending_changes').attr('href', link_url);
$('#transaction_url').attr('href', link_url);
}

Expand Down
28 changes: 27 additions & 1 deletion app/dashboard/helpers.py
Expand Up @@ -30,7 +30,7 @@

import requests
from bs4 import BeautifulSoup
from dashboard.models import Bounty, BountyFulfillment, BountySyncRequest
from dashboard.models import Bounty, BountyFulfillment, BountySyncRequest, UserAction
from dashboard.notifications import (
maybe_market_to_email, maybe_market_to_github, maybe_market_to_slack, maybe_market_to_twitter,
)
Expand Down Expand Up @@ -450,6 +450,29 @@ def process_bounty_details(bounty_details):
return (did_change, latest_old_bounty, latest_old_bounty)


def record_user_action(event_name, old_bounty, new_bounty):
user_profile = None
fulfillment = None
try:
user_profile = Profile.objects.filter(handle__iexact=new_bounty.bounty_owner_github_username).first()
fulfillment = new_bounty.fulfillments.order_by('pk').first()

except Exception as e:
logging.error(f'{e} during record_user_action for {new_bounty}')
# TODO: create a profile if one does not exist already?

if user_profile:
UserAction.objects.create(
profile=user_profile,
action=event_name,
metadata={
'new_bounty': new_bounty.pk if new_bounty else None,
'old_bounty': old_bounty.pk if old_bounty else None,
'fulfillment': fulfillment,
},
)


def process_bounty_changes(old_bounty, new_bounty):
"""Process Bounty changes.

Expand Down Expand Up @@ -491,6 +514,9 @@ def process_bounty_changes(old_bounty, new_bounty):

print(f"- {event_name} event; diff => {json_diff}")

# record a useraction for this
record_user_action(event_name, old_bounty, new_bounty)

# Build profile pairs list
if new_bounty.fulfillments.exists():
profile_pairs = build_profile_pairs(new_bounty)
Expand Down
3 changes: 0 additions & 3 deletions app/dashboard/templates/bounty_details.html
Expand Up @@ -23,9 +23,6 @@
<div style='width:100%; font-size: 16px;'>
<strong style="font-size: 22px;">Your transaction has been posted to web3</strong>
<br>
This funded issue has recently been updated and while the blockchain syncs it has
<a target="_blank" href="javascript:void(0)" id="pending_changes">pending changes</a>.
<br>
Please wait a minute or two for web3 to sync
<a target="_blank" href="javascript:void(0)" id="transaction_url">this transaction</a>.
(You may close the browser tab. This page will automatically refresh as soon as the blockchain is updated.)
Expand Down
21 changes: 20 additions & 1 deletion app/dashboard/views.py
Expand Up @@ -34,7 +34,7 @@

from app.utils import ellipses, sync_profile
from dashboard.models import (
Bounty, CoinRedemption, CoinRedemptionRequest, Interest, Profile, ProfileSerializer, Subscription, Tip,
Bounty, CoinRedemption, CoinRedemptionRequest, Interest, Profile, ProfileSerializer, Subscription, Tip, UserAction,
)
from dashboard.notifications import (
maybe_market_tip_to_email, maybe_market_tip_to_github, maybe_market_tip_to_slack, maybe_market_to_slack,
Expand Down Expand Up @@ -66,6 +66,21 @@ def send_tip(request):
return TemplateResponse(request, 'yge/send1.html', params)


def record_user_action(profile_handle, event_name, instance):
instance_class = instance.__class__.__name__.lower()

try:
user_profile = Profile.objects.filter(handle__iexact=profile_handle).first()
UserAction.objects.create(
profile=user_profile,
action=event_name,
metadata={
f'{instance_class}_pk': instance.pk,
})
except Exception as e:
# TODO: sync_profile?
logging.error(f"error in record_action: {e} - {event_name} - {instance}")

@require_POST
@csrf_exempt
def new_interest(request, bounty_id):
Expand Down Expand Up @@ -100,6 +115,7 @@ def new_interest(request, bounty_id):
except Interest.DoesNotExist:
interest = Interest.objects.create(profile_id=profile_id)
bounty.interested.add(interest)
record_user_action(Profile.objects.get(pk=profile_id).handle, 'start_work', interest)
maybe_market_to_slack(bounty, 'start_work')
except Interest.MultipleObjectsReturned:
bounty_ids = bounty.interested \
Expand Down Expand Up @@ -144,6 +160,7 @@ def remove_interest(request, bounty_id):

try:
interest = Interest.objects.get(profile_id=profile_id, bounty=bounty)
record_user_action(Profile.objects.get(pk=profile_id).handle, 'stop_work', interest)
bounty.interested.remove(interest)
maybe_market_to_slack(bounty, 'stop_work')
interest.delete()
Expand Down Expand Up @@ -240,6 +257,7 @@ def receive_tip(request):
tip.receive_txid = params['receive_txid']
tip.received_on = timezone.now()
tip.save()
record_user_action(tip.username, 'receive_tip', tip)
except Exception as e:
status = 'error'
message = str(e)
Expand Down Expand Up @@ -334,6 +352,7 @@ def send_tip_2(request):
maybe_market_tip_to_github(tip)
maybe_market_tip_to_slack(tip, 'new_tip')
maybe_market_tip_to_email(tip, to_emails)
record_user_action(tip.username, 'send_tip', tip)
if not to_emails:
response['status'] = 'error'
response['message'] = 'Uh oh! No email addresses for this user were found via Github API. Youll have to let the tipee know manually about their tip.'
Expand Down