From 488fa4b9acc1f1ca6b9b5a74d02c0ba1e98b6fb5 Mon Sep 17 00:00:00 2001 From: Miguel Angel G Date: Fri, 19 Jun 2020 01:25:07 -0500 Subject: [PATCH 1/2] Add support for share amount and tokens --- app/assets/v2/js/cart-data.js | 36 ++++++++++++++--- app/economy/models.py | 2 +- .../templates/grants/bulk_add_to_cart.html | 2 +- .../grants/shared/hidden_inputs.html | 7 ++++ app/grants/views.py | 39 +++++++++++++++---- 5 files changed, 71 insertions(+), 15 deletions(-) diff --git a/app/assets/v2/js/cart-data.js b/app/assets/v2/js/cart-data.js index 6aabcccd10b..450007cacfa 100644 --- a/app/assets/v2/js/cart-data.js +++ b/app/assets/v2/js/cart-data.js @@ -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']) { @@ -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'; } diff --git a/app/economy/models.py b/app/economy/models.py index 28109420b7f..810c27dc71b 100644 --- a/app/economy/models.py +++ b/app/economy/models.py @@ -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): diff --git a/app/grants/templates/grants/bulk_add_to_cart.html b/app/grants/templates/grants/bulk_add_to_cart.html index ce1566a7497..f6266650a11 100644 --- a/app/grants/templates/grants/bulk_add_to_cart.html +++ b/app/grants/templates/grants/bulk_add_to_cart.html @@ -5,7 +5,7 @@ {% for grant in grants %}
- {% include 'grants/shared/hidden_inputs.html' %} + {% include 'grants/shared/hidden_inputs.html' with amount=grant.amount token=grant.token grant=grant.obj %}
{% endfor %} diff --git a/app/grants/templates/grants/shared/hidden_inputs.html b/app/grants/templates/grants/shared/hidden_inputs.html index ba1226a5539..736c87d96f1 100644 --- a/app/grants/templates/grants/shared/hidden_inputs.html +++ b/app/grants/templates/grants/shared/hidden_inputs.html @@ -14,3 +14,10 @@ +{% if amount %} + +{% endif %} +{% if token %} + + +{% endif %} diff --git a/app/grants/views.py b/app/grants/views.py index 07c50c0c8bc..fd6c55dce86 100644 --- a/app/grants/views.py +++ b/app/grants/views.py @@ -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, @@ -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) @@ -922,13 +923,26 @@ 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(';') + 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: @@ -936,12 +950,20 @@ def grants_bulk_add(request, grant_str): 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 @@ -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 = { From d049be9b7069a1036de6ea9a1218a4fc84c07489 Mon Sep 17 00:00:00 2001 From: Miguel Angel G Date: Fri, 19 Jun 2020 01:57:37 -0500 Subject: [PATCH 2/2] Persist comments on grants --- app/assets/v2/js/cart.js | 5 ++- app/grants/templates/grants/cart-vue.html | 48 ++++++++++++++++------- 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/app/assets/v2/js/cart.js b/app/assets/v2/js/cart.js index 2844a1a215d..57b8c1c4bd5 100644 --- a/app/assets/v2/js/cart.js +++ b/app/assets/v2/js/cart.js @@ -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(); }, diff --git a/app/grants/templates/grants/cart-vue.html b/app/grants/templates/grants/cart-vue.html index 02f8f19c6b8..ee3b29e9409 100644 --- a/app/grants/templates/grants/cart-vue.html +++ b/app/grants/templates/grants/cart-vue.html @@ -147,30 +147,40 @@

- +
- +
- +
- +
- +
-
+
-
@@ -227,28 +237,38 @@

- +
- +
- +
- +
- +

- +