Add user-to-user credit transfers [#21784207]

pull/1/head
eric 2012-08-13 21:00:28 -04:00
parent 0d13fe9ba4
commit 403420a283
3 changed files with 62 additions and 3 deletions

View File

@ -177,6 +177,25 @@ class OpenCampaignForm(forms.ModelForm):
fields = 'name', 'work', 'managers'
widgets = { 'work': forms.HiddenInput }
def getTransferCreditForm(maximum, data=None, *args, **kwargs ):
class TransferCreditForm(forms.Form):
recipient = AutoCompleteSelectField(
OwnerLookup,
label='Recipient',
widget=AutoCompleteSelectWidget(OwnerLookup),
required=True,
error_messages={'required': 'Please ensure the recipient is a valid Unglue.it account.'},
)
amount = forms.IntegerField(
required=True,
min_value=1,
max_value=maximum,
label="Transfer Amount",
error_messages={'min_value': 'Transfer amount must be positive', 'max_value': 'You only have %(limit_value)s available for transfer'},
)
return TransferCreditForm( data=data )
class EditManagersForm(forms.ModelForm):
managers = AutoCompleteSelectMultipleField(
OwnerLookup,

View File

@ -4,8 +4,12 @@
{% block title %}Donations{% endblock %}
{% block extra_extra_head %}
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/themes/ui-lightness/jquery-ui.css" type="text/css" media="screen">
<link type="text/css" rel="stylesheet" href="/static/css/pledge.css" />
{{ transfer_form.media.css }}
<script type="text/javascript" src="{{ jquery_home }}"></script>
<script type="text/javascript" src="{{ jquery_ui_home }}" ></script>
{{ transfer_form.media.js }}
{% endblock %}
{% block doccontent %}
@ -15,6 +19,23 @@ You have a balance of {{ user.credit.balance }} donation credits. <br />
You have pledged {{ user.credit.pledged }} donation credits to ungluing campaigns.<br />
You have {{ user.credit.available }} donation credits available to pledge or transfer.<br />
</p>
<h2>Donation Credit Transfers</h2>
{% if transfer_message %}
<p>{{ transfer_message }}
{% if transfer_amount %}
<br /> Recipient: <a href="{% url supporter recipient %}">{{ recipient }}</a>
<br /> Amount: {{ transfer_amount }} donation credits
{% endif %}
</p>
{% endif %}
<p>
You may transfer up to {{ user.credit.available }} donation credits to another Unglue.it user.<br />
</p>
<form action="#" method="POST">
{% csrf_token %}
{{ transfer_form.as_p }}
<input id="transfer_submit" type="submit" name="transfer" value="Transfer Credits" />
</form>
{% endblock %}

View File

@ -47,6 +47,7 @@ from regluit.frontend.forms import UserData, UserEmail, ProfileForm, CampaignPle
from regluit.frontend.forms import RightsHolderForm, UserClaimForm, LibraryThingForm, OpenCampaignForm
from regluit.frontend.forms import getManageCampaignForm, DonateForm, CampaignAdminForm, EmailShareForm, FeedbackForm
from regluit.frontend.forms import EbookForm, CustomPremiumForm, EditManagersForm, EditionForm, PledgeCancelForm
from regluit.frontend.forms import getTransferCreditForm
from regluit.payment.manager import PaymentManager
from regluit.payment.models import Transaction
from regluit.payment.parameters import TARGET_TYPE_CAMPAIGN, TARGET_TYPE_DONATION, PAYMENT_TYPE_AUTHORIZATION
@ -552,10 +553,28 @@ class DonationView(TemplateView):
template_name = "donation.html"
def get(self, request, *args, **kwargs):
return self.render_to_response(self.get_context_data())
context = self.get_context_data()
context['transfer_form']=getTransferCreditForm(self.request.user.credit.available)
return self.render_to_response(context)
def post(self, request, *args, **kwargs):
return self.render_to_response(self.get_context_data())
context = self.get_context_data()
transfer_form=getTransferCreditForm(self.request.user.credit.available, data=self.request.POST)
if transfer_form.is_valid():
if self.request.user.credit.transfer_to(transfer_form.cleaned_data['recipient'], transfer_form.cleaned_data['amount']):
#successful transfer
context['transfer_message'] = 'Your transfer has been successfully executed.'
context['recipient']= transfer_form.cleaned_data['recipient']
context['transfer_amount'] = transfer_form.cleaned_data['amount']
context['transfer_form']=getTransferCreditForm(self.request.user.credit.available)
else:
#unsuccessful transfer
context['transfer_message'] = 'Your transfer was not successful.'
context['transfer_form']=transfer_form
else:
#not valid
context['transfer_form']=transfer_form
return self.render_to_response(context)
def get_context_data(self, *args, **kwargs):
context = {'user' : self.request.user}