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

Share amount and tokens #6927

Merged
merged 2 commits into from
Jun 23, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions app/assets/v2/js/cart-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,22 @@ class CartData {
const donations = this.loadCart();
let bulk_add_cart = 'https://gitcoin.co/grants/cart/bulk-add/';

let network = document.web3network;

if (!network) {
network = 'mainnet';
}

for (let i = 0; i < donations.length; i += 1) {
const donation = donations[i];
// eslint-disable-next-line no-loop-func
const token = tokens(network).filter(t => t.name === donation.grant_donation_currency);
let token_id = '';

bulk_add_cart += String(donation['grant_id']) + ',';
if (token.length) {
token_id = token[0].id;
}
bulk_add_cart += `${donation['grant_id']};${donation['grant_donation_amount']};${token_id},`;
}

if (document.contxt['github_handle']) {
Expand Down Expand Up @@ -50,19 +62,33 @@ class CartData {
let accptedTokenName;

try {
accptedTokenName = tokenAddressToDetailsByNetwork(grantData.grant_token_address, network).name;
const token = tokenAddressToDetailsByNetwork(grantData.grant_token_address, network);

grantData.token_local_id = token.id;
accptedTokenName = token.name;
} catch (e) {
// When numbers are too small toWei fails because there's too many decimal places
const dai = tokens(network).filter(t => t.name === 'DAI');

if (dai.length) {
grantData.token_local_id = dai[0].id;
}
accptedTokenName = 'DAI';
}

grantData.uuid = get_UUID();

if (acceptsAllTokens || 'DAI' == accptedTokenName) {
grantData.grant_donation_amount = 5;
grantData.grant_donation_currency = 'DAI';
if (!grantData.grant_donation_amount) {
grantData.grant_donation_amount = 5;
}
if (!grantData.grant_donation_currency) {
grantData.grant_donation_currency = 'DAI';
}
} else {
grantData.grant_donation_amount = 0.01;
if (!grantData.grant_donation_amount) {
grantData.grant_donation_amount = 0.01;
}
grantData.grant_donation_currency = 'ETH';
}

Expand Down
5 changes: 4 additions & 1 deletion app/assets/v2/js/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,10 @@ Vue.component('grants-cart', {

addComment(id, text) {
// Set comment at this index to an empty string to show textarea
this.comments.splice(id, 1, text ? text : ''); // we use splice to ensure it's reactive
this.grantData[id].grant_comments = text ? text : '';
CartData.setCart(this.grantData);
this.$forceUpdate();

$('input[type=textarea]').focus();
},

Expand Down
2 changes: 1 addition & 1 deletion app/economy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def __str__(self):

@property
def to_dict(self):
return {'addr': self.address, 'name': self.symbol, 'decimals': self.decimals, 'priority': self.priority}
return {'id': self.id, 'addr': self.address, 'name': self.symbol, 'decimals': self.decimals, 'priority': self.priority}

@property
def to_json(self):
Expand Down
2 changes: 1 addition & 1 deletion app/grants/templates/grants/bulk_add_to_cart.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

{% for grant in grants %}
<form class="js-addDetailToCart-form">
{% include 'grants/shared/hidden_inputs.html' %}
{% include 'grants/shared/hidden_inputs.html' with amount=grant.amount token=grant.token grant=grant.obj %}
</form>
{% endfor %}

Expand Down
48 changes: 34 additions & 14 deletions app/grants/templates/grants/cart-vue.html
Original file line number Diff line number Diff line change
Expand Up @@ -147,30 +147,40 @@ <h1 class="col-auto text-left font-bigger-2 black" style="font-weight: bold; mar
<div class="col-12 font-smaller-3" style="margin-top:1rem">
<div class="row">
<div class="comment-option col-auto" @click="addComment(index)">
<input :id="`comment-${index}-blank`" type="radio" class="radio-input" :name="`comment-${index}`">
<input :id="`comment-${index}-blank`" type="radio" class="radio-input" :name="`comment-${index}`"
:checked="grant.grant_comments === ''"
>
<label :for="`comment-${index}-blank`" class="radio-label">+ Add comment to owner</label>
</div>
<div class="comment-option col-auto" @click="addComment(index, '🙌 Great Job')">
<input :id="`comment-${index}-job`" type="radio" class="radio-input" :name="`comment-${index}`">
<input :id="`comment-${index}-job`" type="radio" class="radio-input" :name="`comment-${index}`"
:checked="grant.grant_comments === '🙌 Great Job'"
>
<label :for="`comment-${index}-job`" class="radio-label">🙌 Great Job</label>
</div>
<div class="comment-option col-auto" @click="addComment(index, '👍 i appreciate you')">
<input :id="`comment-${index}-appreciate`" type="radio" class="radio-input" :name="`comment-${index}`">
<input :id="`comment-${index}-appreciate`" type="radio" class="radio-input" :name="`comment-${index}`"
:checked="grant.grant_comments === '👍 i appreciate you'"
>
<label :for="`comment-${index}-appreciate`" class="radio-label">👍 i appreciate you</label>
</div>
<div class="comment-option col-auto" @click="addComment(index, '💪 keep up the great work')">
<input :id="`comment-${index}-work`" type="radio" class="radio-input" :name="`comment-${index}`">
<input :id="`comment-${index}-work`" type="radio" class="radio-input" :name="`comment-${index}`"
:checked="grant.grant_comments === '💪 keep up the great work'"
>
<label :for="`comment-${index}-work`" class="radio-label">💪 keep up the great work</label>
</div>
<div class="comment-option col-auto" @click="addComment(index, '😍 love the mission of your project')">
<input :id="`comment-${index}-love`" type="radio" class="radio-input" :name="`comment-${index}`">
<input :id="`comment-${index}-love`" type="radio" class="radio-input" :name="`comment-${index}`"
:checked="grant.grant_comments === '😍 love the mission of your project'"
>
<label :for="`comment-${index}-love`" class="radio-label">😍 love the mission of your project</label>
</div>
</div>
</div>
<div v-if="comments[index] !== undefined" class="col-12" style="margin-top:1rem;">
<div v-if="grant.grant_comments !== undefined" class="col-12" style="margin-top:1rem;">
<div class="row flex-nowrap justify-content-between">
<input class="form-control" style="margin-left:0.5rem" v-model="comments[index]"
<input class="form-control" style="margin-left:0.5rem" v-model="grant.grant_comments"
type="textarea" placeholder="Enter message">
</div>
</div>
Expand Down Expand Up @@ -227,28 +237,38 @@ <h1 class="col-auto text-left font-bigger-2 black" style="font-weight: bold; mar
</div>
<div class="row font-smaller-3 mt-3" style="margin-left:0.5rem">
<div class="comment-option col-auto" @click="addComment(index)">
<input :id="`comment-${index}-blank`" type="radio" class="radio-input" :name="`comment-${index}`">
<input :id="`comment-${index}-blank`" type="radio" class="radio-input" :name="`comment-${index}`"
:checked="grant.grant_comments === ''"
>
<label :for="`comment-${index}-blank`" class="radio-label">+ Add comment to owner</label>
</div>
<div class="comment-option col-auto" @click="addComment(index, '🙌 Great Job')">
<input :id="`comment-${index}-job`" type="radio" class="radio-input" :name="`comment-${index}`">
<input :id="`comment-${index}-job`" type="radio" class="radio-input" :name="`comment-${index}`"
:checked="grant.grant_comments === '🙌 Great Job'"
>
<label :for="`comment-${index}-job`" class="radio-label">🙌 Great Job</label>
</div>
<div class="comment-option col-auto" @click="addComment(index, '👍 i appreciate you')">
<input :id="`comment-${index}-appreciate`" type="radio" class="radio-input" :name="`comment-${index}`">
<input :id="`comment-${index}-appreciate`" type="radio" class="radio-input" :name="`comment-${index}`"
:checked="grant.grant_comments === '👍 i appreciate you'"
>
<label :for="`comment-${index}-appreciate`" class="radio-label">👍 i appreciate you</label>
</div>
<div class="comment-option col-auto" @click="addComment(index, '💪 keep up the great work')">
<input :id="`comment-${index}-work`" type="radio" class="radio-input" :name="`comment-${index}`">
<input :id="`comment-${index}-work`" type="radio" class="radio-input" :name="`comment-${index}`"
:checked="grant.grant_comments === '💪 keep up the great work'"
>
<label :for="`comment-${index}-work`" class="radio-label">💪 keep up the great work</label>
</div>
<div class="comment-option col-auto" @click="addComment(index, '😍 love the mission of your project')">
<input :id="`comment-${index}-love`" type="radio" class="radio-input" :name="`comment-${index}`">
<input :id="`comment-${index}-love`" type="radio" class="radio-input" :name="`comment-${index}`"
:checked="grant.grant_comments === '😍 love the mission of your project'"
>
<label :for="`comment-${index}-love`" class="radio-label">😍 love the mission of your project</label>
</div>
</div>
<input v-if="comments[index] !== undefined" style="margin-top:0.5rem; margin-left:0.5rem"
class="form-control" v-model.number="comments[index]" type="textarea" placeholder="Enter message">
<input v-if="grant.grant_comments !== undefined" style="margin-top:0.5rem; margin-left:0.5rem"
class="form-control" v-model="grant.grant_comments" type="textarea" placeholder="Enter message">
</div>
</div>

Expand Down
7 changes: 7 additions & 0 deletions app/grants/templates/grants/shared/hidden_inputs.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@
<input type="hidden" id="grant_clr_prediction_curve" name="grant_clr_prediction_curve" value="{{ grant.clr_prediction_curve }}">
<input type="hidden" id="grant_image_css" name="grant_image_css" value="{{ grant.image_css }}">
<input type="hidden" id="is_clr_eligible" name="is_clr_eligible" {% if grant.is_clr_eligible %} value="{{ grant.is_clr_eligible }}" {% endif %}>
{% if amount %}
<input type="hidden" id="grant_donation_amount" name="grant_donation_amount" value="{{ amount }}">
{% endif %}
{% if token %}
<input type="hidden" id="grant_donation_currency" name="grant_donation_currency" value="{{ token.symbol }}">
<input type="hidden" id="token_local_id" name="token_local_id" value="{{ token.id }}">
{% endif %}
39 changes: 31 additions & 8 deletions app/grants/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
from dashboard.tasks import increment_view_count
from dashboard.utils import get_web3, has_tx_mined
from economy.utils import convert_amount
from economy.models import Token as FTokens
from gas.utils import conf_time_spread, eth_usd_conv_rate, gas_advisories, recommend_min_gas_price_to_confirm_in_time
from grants.models import (
CartActivity, Contribution, Flag, Grant, GrantCategory, MatchPledge, PhantomFunding, Subscription,
Expand Down Expand Up @@ -244,7 +245,7 @@ def grants_stats_view(request):

def grants(request):
"""Handle grants explorer."""

_type = request.GET.get('type', 'all')
return grants_by_grant_type(request, _type)

Expand Down Expand Up @@ -922,26 +923,47 @@ def grants_cart_view(request):


def grants_bulk_add(request, grant_str):

grants = {}
redis = RedisService().redis
key = hashlib.md5(grant_str.encode('utf')).hexdigest()
views = redis.incr(key)

grant_ids = grant_str.split(':')[0].split(',')
grant_ids = [int(ele) for ele in grant_ids if ele and ele.isnumeric() ]
grants_data = grant_str.split(':')[0].split(',')

for ele in grants_data:
# new format will support amount and token in the URL separated by ;
grant_data = ele.split(';')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this backwards compatible with the old links that have already been shared out in the wild?

if len(grant_data) > 0 and grant_data[0].isnumeric():
grant_id = grant_data[0]
grants[grant_id] = {
'id': int(grant_id)
}

if len(grant_data) == 3: # backward compatibility
grants[grant_id]['amount'] = grant_data[1]
grants[grant_id]['token'] = FTokens.objects.filter(id=int(grant_data[2])).first()

by_whom = ""
prefix = ""
try:
by_whom = f"by {grant_str.split(':')[1]}"
prefix = f"{grant_str.split(':')[2]} : "
except:
pass
grants = Grant.objects.filter(pk__in=grant_ids)
grant_titles = ", ".join([grant.title for grant in grants])
title = f"{prefix}{grants.count()} Grants in Shared Cart {by_whom} : Viewed {views} times"

# search valid grants and associate with its amount and token
grants_info = grants.values()
grant_ids = [grant['id'] for grant in grants_info]
for grant in Grant.objects.filter(pk__in=grant_ids):
grants[str(grant.id)]['obj'] = grant

grants = [grant for grant in grants_info if grant.get('obj')]

grant_titles = ", ".join([grant['obj'].title for grant in grants])
title = f"{prefix}{len(grants)} Grants in Shared Cart {by_whom} : Viewed {views} times"

context = {
'grants': Grant.objects.filter(pk__in=grant_ids),
'grants': grants,
'avatar_url': request.build_absolute_uri(static('v2/images/twitter_cards/tw_cards-03.png')),
'title': title,
'card_desc': "Click to Add All to Cart: " + grant_titles
Expand All @@ -959,6 +981,7 @@ def profile(request):
handle = request.user.profile.handle
return redirect(f'/profile/{handle}/grants')


def quickstart(request):
"""Display quickstart guide."""
params = {
Expand Down