Browse Source

More work on the integration of a web interface to import the CSV

Alexandre Aubin 6 years ago
parent
commit
4b02f7db9b

+ 20 - 9
coin/billing/admin.py

@@ -17,6 +17,9 @@ from django.core.urlresolvers import reverse
 import autocomplete_light
 from functools import update_wrapper
 
+from .forms import WizardImportPaymentCSV
+from .import_payments_from_csv import process
+
 class InvoiceDetailInlineForm(forms.ModelForm):
     class Meta:
         model = InvoiceDetail
@@ -251,33 +254,41 @@ class PaymentAdmin(admin.ModelAdmin):
 
         info = self.model._meta.app_label, self.model._meta.model_name
 
-        print(urls)
-
         my_urls = [
             url(r'wizard_import_payment_csv/$', wrap(self.wizard_import_payment_csv), name='wizard_import_payment_csv'),
         ]
 
-        print(my_urls)
-
         return my_urls + urls
 
 
     def wizard_import_payment_csv(self, request):
         template = "admin/billing/payment/wizard_import_payment_csv.html"
 
-        from .forms import WizardImportPaymentCSV
-
         if request.method == 'POST':
             form = WizardImportPaymentCSV(request.POST, request.FILES)
             if form.is_valid():
-                form.analyze_CSV()
-                return HttpResponseRedirect('/success/url/')
+
+                # Analyze
+                new_payments = process(request.FILES["csv_file"])
+
+                # If the user didn't ask for commit yet
+                # display the result of the analyze (i.e. the matching)
+                if "commit" not in request.POST:
+                    return render(request, template, {
+                        'adminform': form,
+                        'opts': self.model._meta,
+                        'new_payments': new_payments
+                        })
+                else:
+                    import pdb; pdb.set_trace()
+                    # To be implemented
         else:
             form = WizardImportPaymentCSV()
 
         return render(request, template, {
             'adminform': form,
-            }, context_instance=RequestContext(request))
+            'opts': self.model._meta
+            })
 
 
 class MembershipFeeAdmin(admin.ModelAdmin):

+ 43 - 3
coin/billing/templates/admin/billing/payment/wizard_import_payment_csv.html

@@ -1,13 +1,53 @@
 {% extends "admin/change_form.html" %}
+{% load i18n %}
+
+{% block breadcrumbs %}
+<div class="breadcrumbs">
+    <a href="{% url 'admin:index' %}">{% trans 'Home' %}</a>
+    &rsaquo; <a href="{% url 'admin:app_list' app_label=opts.app_label %}">{{ opts.app_config.verbose_name }}</a>
+    &rsaquo; <a href="">Importer des paiements depuis un CSV</a>
+</div>
+{% endblock %}
 
 {% block content %}
-<h1>Import payments from a CSV</h1>
+<h1>Importer des paiements depuis un CSV</h1>
 <div id="content-main">
     <form method="POST" action="."  enctype="multipart/form-data">
         {% csrf_token %}
-        <fieldset class="module aligned wide" style="text-align:center;">
+        <fieldset style="text-align:center;">
             {{ adminform.as_p }}
-            <input type="submit" value="Analyze" class="default" style="float: none; margin:0 auto;"/>
+            {% if not new_payments %}
+                <input type="submit" value="Analser (simulation)" class="default" style="float: none; margin: 0 auto;"/>
+            {% else %}
+                <input type="checkbox" id="commit" name="commit" value="1" style="display:None" checked />
+                <input type="submit" value="Importer pour de vrai !" class="default" style="float: none; margin: 0 auto; background: #ffee77; color: black;"/>
+                <p style="color: red;">
+                Les paiements suivants seront importés. Passez en revue les membres matchés avant de cliquer sur 'Import' !
+                </p>
+                <p>
+                N.B. : Il faut resélectionner le fichier (désolé~).
+                </p>
+                <table style="margin: 0 auto;">
+                    <tr>
+                        <th>Date</th>
+                        <th>Montant</th>
+                        <th>Libellé</th>
+                        <th>Membre matché</th>
+                    </tr>
+                {% for payment in new_payments %}
+                    <tr>
+                        <td>{{ payment.date }}</td>
+                        <td>{{ payment.amount }}</td>
+                        <td><small>{{ payment.label }}</small></td>
+                        {% if payment.member_matched %}
+                            <td>{{ payment.member_matched }}</td>
+                        {% else %}
+                            <td><span style='color: orange; font-weight: bold;'>Non matché</span></td>
+                        {% endif %}
+                    </tr>
+                {% endfor %}
+                </table>
+            {% endif %}
         </fieldset>
     </form>
 </div>