Allow to add a text to gift card transactions

This commit is contained in:
Raphael Michel
2020-03-02 12:47:39 +01:00
parent 1539eea664
commit 381fa5e1cd
6 changed files with 80 additions and 41 deletions

View File

@@ -168,16 +168,19 @@ class GiftCardViewSet(viewsets.ModelViewSet):
value = serializers.DecimalField(max_digits=10, decimal_places=2).to_internal_value(
request.data.get('value')
)
text = serializers.CharField(allow_blank=True, allow_null=True).to_internal_value(
request.data.get('text', '')
)
if gc.value + value < Decimal('0.00'):
return Response({
'value': ['The gift card does not have sufficient credit for this operation.']
}, status=status.HTTP_409_CONFLICT)
gc.transactions.create(value=value)
gc.transactions.create(value=value, text=text)
gc.log_action(
'pretix.giftcards.transaction.manual',
user=self.request.user,
auth=self.request.auth,
data={'value': value}
data={'value': value, 'text': text}
)
return Response(GiftCardSerializer(gc).data, status=status.HTTP_200_OK)

View File

@@ -0,0 +1,18 @@
# Generated by Django 2.2.4 on 2020-03-02 11:28
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('pretixbase', '0145_auto_20200210_1038'),
]
operations = [
migrations.AddField(
model_name='giftcardtransaction',
name='text',
field=models.TextField(null=True),
),
]

View File

@@ -119,6 +119,7 @@ class GiftCardTransaction(models.Model):
blank=True,
on_delete=models.PROTECT
)
text = models.TextField(blank=True, null=True)
class Meta:
ordering = ("datetime",)

View File

@@ -45,50 +45,53 @@
{% trans "Transactions" %}
</h3>
</div>
<table class="panel-body table">
<thead>
<tr>
<th>{% trans "Date" %}</th>
<th>{% trans "Order" %}</th>
<th class="text-right">{% trans "Value" %}</th>
</tr>
</thead>
<tbody>
{% for t in card.transactions.all %}
<form class="" method="post" action="">
{% csrf_token %}
<table class="panel-body table">
<thead>
<tr>
<td>{{ t.datetime|date:"SHORT_DATETIME_FORMAT" }}</td>
<td>
{% if t.order %}
<a href="{% url "control:event.order" event=t.order.event.slug organizer=t.order.event.organizer.slug code=t.order.code %}">
{{ t.order.full_code }}
</a>
{% else %}
<em>{% trans "Manual transaction" %}</em>
{% endif %}
</td>
<td class="text-right">
{{ t.value|money:card.currency }}
</td>
<th>{% trans "Date" %}</th>
<th>{% trans "Order" %}</th>
<th class="text-right">{% trans "Value" %}</th>
</tr>
{% endfor %}
</tbody>
<tfoot>
<tr>
<td></td>
<td></td>
<td class="text-right">
<form class="helper-display-inline form-inline" method="post" action="">
{% csrf_token %}
</thead>
<tbody>
{% for t in card.transactions.all %}
<tr>
<td>{{ t.datetime|date:"SHORT_DATETIME_FORMAT" }}</td>
<td>
{% if t.order %}
<a href="{% url "control:event.order" event=t.order.event.slug organizer=t.order.event.organizer.slug code=t.order.code %}">
{{ t.order.full_code }}
</a>
{% else %}
<em>{% trans "Manual transaction" %}{% if t.text %}: {{ t.text }}{% endif %}</em>
{% endif %}
</td>
<td class="text-right">
{{ t.value|money:card.currency }}
</td>
</tr>
{% endfor %}
</tbody>
<tfoot>
<tr>
<td></td>
<td>
<input type="text" class="form-control helper-display-block" placeholder="{% trans "Text" %}"
name="text">
</td>
<td class="text-right form-inline">
<input type="text" class="form-control input-sm" placeholder="{% trans "Value" %}" name="value">
<button class="btn btn-primary">
<span class="fa fa-plus"></span>
</button>
</form>
</td>
</td>
</tr>
</tfoot>
</table>
</tr>
</tfoot>
</table>
</form>
</div>
</div>
<div class="col-md-2 col-xs-12">

View File

@@ -1010,12 +1010,14 @@ class GiftCardDetailView(OrganizerDetailViewMixin, OrganizerPermissionRequiredMi
messages.error(request, _('Gift cards are not allowed to have negative values.'))
else:
self.object.transactions.create(
value=value
value=value,
text=request.POST.get('text') or None,
)
self.object.log_action(
'pretix.giftcards.transaction.manual',
data={
'value': value
'value': value,
'text': request.POST.get('text')
},
user=self.request.user,
)

View File

@@ -149,6 +149,18 @@ def test_giftcard_transact(token_client, organizer, event, giftcard):
assert resp.status_code == 200
giftcard.refresh_from_db()
assert giftcard.value == Decimal('33.00')
resp = token_client.post(
'/api/v1/organizers/{}/giftcards/{}/transact/'.format(organizer.slug, giftcard.pk),
{
'value': '10.00',
'text': 'bla'
},
format='json'
)
assert resp.status_code == 200
giftcard.refresh_from_db()
assert giftcard.value == Decimal('43.00')
assert giftcard.transactions.last().text == 'bla'
@pytest.mark.django_db