diff --git a/refunding/admin.py b/refunding/admin.py index 44c5ab4..5b31152 100644 --- a/refunding/admin.py +++ b/refunding/admin.py @@ -1,5 +1,5 @@ from django.contrib import admin -from refunding.forms import RefundForm +from refunding.forms import RefundForm, RefundFormAdmin from refunding.models import Refund, Payment @@ -9,7 +9,7 @@ class RefundAdmin(admin.ModelAdmin): list_display_links = ('title',) search_fields = ('title',) date_hierarchy = 'date' - form = RefundForm + form = RefundFormAdmin readonly_fields = ('eur_value',) diff --git a/refunding/forms.py b/refunding/forms.py index 78583fc..d46f732 100644 --- a/refunding/forms.py +++ b/refunding/forms.py @@ -1,4 +1,6 @@ +from bootstrap3_datetime.widgets import DateTimePicker from django import forms +from django.db.models import Q from refunding.models import Refund, Payment @@ -7,18 +9,39 @@ class RefundForm(forms.ModelForm): model = Refund fields = '__all__' - payments = forms.ModelMultipleChoiceField(queryset=Payment.objects.all()) + payments = forms.ModelMultipleChoiceField(queryset=Payment.objects.none()) def __init__(self, *args, **kwargs): super(RefundForm, self).__init__(*args, **kwargs) if self.instance: self.fields['payments'].initial = self.instance.payment_set.all() + self.fields['payments'].queryset = Payment.objects.filter(Q(refund=None) | Q(refund=self.instance)) def save(self, *args, **kwargs): # Save the refund - instance = super(RefundForm, self).save(commit=False) + instance = super(RefundForm, self).save() # Remove the refund from payments it was previously assigned to self.fields['payments'].initial.update(refund=None) # Add the refund to the selected payments self.cleaned_data['payments'].update(refund=instance) return instance + + +class RefundFormAdmin(RefundForm): + class Meta: + model = Refund + fields = '__all__' + + +class RefundFormPublic(RefundForm): + class Meta: + model = Refund + exclude = ('user',) + + date = forms.DateField( + widget=DateTimePicker( + options={ + 'format': 'YYYY-MM-DD' + } + ) + ) diff --git a/refunding/migrations/0002_auto_20160604_0140.py b/refunding/migrations/0002_auto_20160604_0140.py new file mode 100644 index 0000000..2a2a242 --- /dev/null +++ b/refunding/migrations/0002_auto_20160604_0140.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.6 on 2016-06-04 01:40 +from __future__ import unicode_literals + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('refunding', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='refund', + name='user', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/refunding/models.py b/refunding/models.py index f6f4bd8..9470840 100644 --- a/refunding/models.py +++ b/refunding/models.py @@ -12,11 +12,16 @@ class Refund(models.Model): updated_at = models.DateTimeField(auto_now=True) user = models.ForeignKey( AUTH_USER_MODEL, - on_delete=models.PROTECT + on_delete=models.PROTECT, + null=True ) def eur_value(self) -> float: - return self.payment_set.all().aggregate(Sum('value')).get('value__sum') / 100 + value_sum = self.payment_set.all().aggregate(Sum('value')).get('value__sum') + if value_sum: + return value_sum / 100 + else: + return 0 def __str__(self) -> str: return "{0} on {1} for {2}".format(self.title, self.date, self.eur_value()) diff --git a/refunding/templates/refunding/new_refund.html b/refunding/templates/refunding/new_refund.html new file mode 100644 index 0000000..52e7164 --- /dev/null +++ b/refunding/templates/refunding/new_refund.html @@ -0,0 +1,27 @@ +{% extends 'base.html' %} +{% load crispy_forms_filters %} +{% load crispy_forms_field %} + +{% block javascript %} + {{ block.super }} + + +{% endblock %} + +{% block style %} + {{ block.super }} + +{% endblock %} + +{% block content %} +