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

Bounty Requests: Move to django forms #2096

Merged
merged 4 commits into from
Oct 5, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 5 additions & 2 deletions app/assets/v2/js/pages/bounty_request_form.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var trigger_form_hooks = function() {
callFunctionWhenweb3Available(
function() {
const addr = web3.eth.coinbase;
const input = $('#eth_address');
const input = $('[name=eth_address]');

if (addr && !input.val()) {
input.val(addr);
Expand All @@ -14,7 +14,10 @@ var trigger_form_hooks = function() {
};

$(document).ready(function() {
$('#comment').bind('input propertychange', function() {
$('[for=comment]').append(
' (<span id="charcount">500</span> ' + gettext('characters left') + ')'
);
$('[name=comment]').bind('input propertychange', function() {
this.value = this.value.replace(/ +(?= )/g, '');

if (this.value.length > 500) {
Expand Down
56 changes: 56 additions & 0 deletions app/bounty_requests/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# -*- coding: utf-8 -*-
"""Define bounty request related forms.

Copyright (C) 2018 Gitcoin Core

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.

"""
from django import forms
from django.utils.html import escape, strip_tags
from django.utils.translation import gettext_lazy as _

from .models import BountyRequest


class BountyRequestForm(forms.ModelForm):
"""Define the BountyRequestForm handling."""
github_url = forms.URLField(label=_('The Github Issue Link'))

class Meta:
model = BountyRequest
fields = ['github_url', 'eth_address', 'amount', 'comment']
labels = {
'eth_address': _('Your ETH Address (Optional)'),
'amount': _('Proposed Funding Amount (USD)'),
'comment': _('Comment')
}

def __init__(self, *args, **kwargs):
super(BountyRequestForm, self).__init__(*args, **kwargs)

self.fields['eth_address'].widget.attrs['placeholder'] = '0x0'
self.fields['github_url'].widget.attrs['placeholder'] = 'https://github.com/gitcoinco/web/issues/2036'
self.fields['amount'].widget.attrs['placeholder'] = '1'
self.fields['amount'].widget.attrs['min'] = '1'
self.fields['comment'].widget.attrs['placeholder'] = _('Anything you want to tell us.')
self.fields['comment'].widget.attrs['rows'] = '4'
self.fields['comment'].widget.attrs['cols'] = '50'

for field in self.fields:
self.fields[field].widget.attrs['class'] = 'form__input'

def clean_comment(self):
comment = self.cleaned_data['comment'] or ''
return escape(strip_tags(comment))
19 changes: 19 additions & 0 deletions app/bounty_requests/migrations/0002_auto_20180824_2103.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 2.1 on 2018-08-24 21:03

import django.core.validators
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('bounty_requests', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='bountyrequest',
name='amount',
field=models.FloatField(validators=[django.core.validators.MinValueValidator(1.0)]),
),
]
3 changes: 2 additions & 1 deletion app/bounty_requests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"""
from __future__ import unicode_literals

from django.core.validators import MinValueValidator
from django.db import models

from economy.models import SuperModel
Expand Down Expand Up @@ -56,7 +57,7 @@ class BountyRequest(SuperModel):
eth_address = models.CharField(max_length=50, blank=True)
comment = models.TextField(max_length=500, default='')
comment_admin = models.TextField(max_length=500, blank=True)
amount = models.FloatField(default=0.0)
amount = models.FloatField(blank=False, validators=[MinValueValidator(1.0)])

objects = BountyQuerySet.as_manager()

Expand Down
50 changes: 20 additions & 30 deletions app/bounty_requests/templates/bounty_request_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,46 +38,36 @@ <h3>{% trans "Request a Bounty" %} <span id="alpha">{% trans "Alpha" %}</span></
Suggest it be given a bounty!
{% endblocktrans %}
</p>
<form id='bounty_request_form'>
<div class="mb-3">
<label for="requested_by" class="form__label">{% trans "Your Github Profile" %}
{% if not user.is_authenticated %}
(<a href="{% url 'social:begin' 'github' %}?next={{ request.get_full_path }}">{% trans "Login" %}</a>)
{% endif %}
</label>
<input name="requested_by" placeholder="{% trans "Please Login First" %}" id="requested_by" class="form__input" type="text" value="{% if github_handle %}{{ github_handle }}{% endif %}" required disabled />
</div>
<div class="mb-3">
<label for="github_url" class="form__label">{% trans "The Github Issue Link" %}</label>
<input name="github_url" placeholder="https://github.com/gitcoinco/web/issues/2036" id="github_url" class="form__input" type="text" required />
</div>
<div class="mb-3">
<label for="eth_address" class="form__label">{% trans "Your ETH Address (Optional)" %}</label>
<input name="eth_address" placeholder="0x0" id="eth_address" class="form__input" type="text" />
</div>
<div class="mb-3">
<label for="amount" class="form__label">{% trans "Proposed Funding Amount (USD)" %}</label>
<input name="amount" placeholder="1" id="amount" class="form__input" type="number" min="1" required />
</div>
<div class="mb-3">
<label for="comment" class="form__label"> {% trans "Comment" %} (<span id="charcount">500</span> {% trans "characters left" %}):</label>
<textarea name="comment" id="comment" class="form__input" rows="4" cols="50" required placeholder="{% trans "Anything you want to tell us." %}"></textarea>
</div>
<div class="mb-3">
<label for="requested_by" class="form__label">{% trans 'Your Github Profile' %}
{% if not user.is_authenticated %}
(<a href="{% url 'social:begin' 'github' %}?next={{ request.get_full_path }}">{% trans 'Login' %}</a>)
{% endif %}
</label>
<input name="requested_by" placeholder="{% trans "Please Login First" %}" id="requested_by" class="form__input" type="text" value="{% if github_handle %}{{ github_handle }}{% endif %}" required disabled />
</div>
<form id="bounty_request_form">
{% for field in form %}
<div class="mb-3">
<label for="{{ field.name }}" class="form__label">{{ field.label }}</label>
SaptakS marked this conversation as resolved.
Show resolved Hide resolved
{{ field }}
</div>
{% endfor %}
<div class="form__footer">
<button class="button button--primary btn-block js-submit" type="submit">{% trans "Proceed" %}</button>
<button class="button button--primary btn-block js-submit" type="submit">{% trans 'Proceed' %}</button>
</div>
</form>
</div>
</div>
<div id="success_container" class="text-center" style="display: none;">
<h3>{% trans "Bounty Request Received" %}</h3>
<h3>{% trans 'Bounty Request Received' %}</h3>
{% include 'svgs/success.svg' %}
<div>
<p class="font-header">{% trans "Hooray!" %}</p>
<p class="font-header">{% trans 'Hooray!' %}</p>
<p>
{% trans "Your request has been received." %}
{% trans 'Your request has been received.' %}
<br>
{% trans "Thank you for supporting open-source." %}
{% trans 'Thank you for supporting open-source.' %}
</p>
</div>
</div>
Expand Down
38 changes: 11 additions & 27 deletions app/bounty_requests/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@

from django.http import JsonResponse
from django.template.response import TemplateResponse
from django.utils.html import escape, strip_tags
from django.utils.translation import gettext_lazy as _
from django.views.decorators.csrf import csrf_exempt

from marketing.mails import new_bounty_request
from ratelimit.decorators import ratelimit

from .forms import BountyRequestForm
from .models import BountyRequest


Expand All @@ -44,37 +44,21 @@ def bounty_request(request):
status=401)

try:
params = json.loads(request.body)
except Exception:
result = BountyRequestForm(json.loads(request.body))
if not result.is_valid():
raise
except:
return JsonResponse({'error': 'Invalid JSON.'}, status=400)

requested_by = profile
github_url = params.get('github_url', '')
eth_address = params.get('eth_address', '')
comment = escape(strip_tags(params.get('comment', '')))
amount = params.get('amount', '')

try:
amount = int(amount)
except Exception as e:
return JsonResponse({'error': str(e)}, status=400)

if not requested_by or not github_url or not comment or not amount:
return JsonResponse({'error': _('Missing required attributes.')}, status=400)

result = BountyRequest.objects.create(
requested_by=requested_by,
github_url=github_url,
eth_address=eth_address,
comment=comment,
amount=amount
)

new_bounty_request(result)

model = result.save(commit=False)
model.requested_by = profile
model.save()
new_bounty_request(model)
return JsonResponse({'msg': _('Bounty Request received.')}, status=200)

form = BountyRequestForm()
params = {
'form': form,
'title': _('Request a Bounty'),
'card_title': _('Gitcoin Requests'),
'card_desc': _('Have an open-source issue that you think would benefit the community? '
Expand Down
2 changes: 1 addition & 1 deletion app/marketing/mails.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ def new_bounty_request(model):
try:
setup_lang(to_email)
subject = _("New Bounty Request")
body_str = _(f"New Bounty Request from")
body_str = _("New Bounty Request from")
body = f"{body_str} {model.requested_by}: "\
f"{settings.BASE_URL}_administrationbounty_requests/bountyrequest/{model.pk}/change"
send_mail(from_email, to_email, subject, body, from_name=_("No Reply from Gitcoin.co"))
Expand Down