diff --git a/LICENSE.md b/LICENSE.md
new file mode 100644
index 0000000..8efa578
--- /dev/null
+++ b/LICENSE.md
@@ -0,0 +1,11 @@
+Copyright (c) 2014, Maximilien Cuony, Malik Bougacha
+
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+
+2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\ No newline at end of file
diff --git a/server/app/settings.py b/server/app/settings.py
index 6fa7c9f..54ca69b 100644
--- a/server/app/settings.py
+++ b/server/app/settings.py
@@ -118,6 +118,7 @@
'main',
'users',
+ 'export',
'configs',
'paiements',
)
diff --git a/server/app/urls.py b/server/app/urls.py
index 84105b2..298ef37 100644
--- a/server/app/urls.py
+++ b/server/app/urls.py
@@ -7,15 +7,6 @@
urlpatterns = patterns(
'',
- # Examples:
- # url(r'^$', 'polybanking.views.home', name='home'),
- # url(r'^polybanking/', include('polybanking.foo.urls')),
-
- # Uncomment the admin/doc line below to enable admin documentation:
- # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
-
- # Uncomment the next line to enable the admin:
- # url(r'^admin/', include(admin.site.urls)),
(r'^users/login$', 'tequila.login'),
(r'^users/logout$', 'django.contrib.auth.views.logout', {'next_page': '/'}),
@@ -23,6 +14,7 @@
url(r'', include('main.urls')),
url(r'^api/', include('api.urls')),
url(r'^users/', include('users.urls')),
+ url(r'^export/', include('export.urls')),
url(r'^configs/', include('configs.urls')),
url(r'^paiements/', include('paiements.urls')),
diff --git a/server/data/pip-reqs.txt b/server/data/pip-reqs.txt
index 697a2df..0a97a6b 100644
--- a/server/data/pip-reqs.txt
+++ b/server/data/pip-reqs.txt
@@ -3,3 +3,5 @@ django-bootstrap3
requests
celery
django-celery
+python-dateutil
+PicklingTools
diff --git a/server/export/__init__.py b/server/export/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/server/export/forms.py b/server/export/forms.py
new file mode 100644
index 0000000..867848c
--- /dev/null
+++ b/server/export/forms.py
@@ -0,0 +1,38 @@
+from django import forms
+from django.utils.translation import ugettext_lazy as _
+
+
+from configs.models import Config
+
+
+class ExportForm(forms.Form):
+
+ FILE_TYPE_CHOICES = (
+ ('json', _('JSON')),
+ ('xml', _('XML')),
+ ('csv', _('CSV (Without logs)')),
+ )
+
+ RANGE_CHOICES = [
+ ('thismonth', _('This month')),
+ ('previousmonth', _('The previous month')),
+ ('sincemonth', _('Since a month')),
+ ('thisyear', _('This year')),
+ ('sinceyear', _('Since a year')),
+ ]
+
+ all_config = forms.BooleanField(help_text=_('Export transactions from all configs'), required=False)
+ config = forms.ModelChoiceField(queryset=None)
+
+ file_type = forms.ChoiceField(choices=FILE_TYPE_CHOICES)
+
+ range = forms.ChoiceField(choices=RANGE_CHOICES)
+
+ def __init__(self, user, *args, **kwargs):
+ super(ExportForm, self).__init__(*args, **kwargs)
+
+ if not user.is_superuser:
+ del self.fields['all_config']
+ self.fields['config'].queryset = Config.objects.filter(allowed_users=user).order_by('name').all()
+ else:
+ self.fields['config'].queryset = Config.objects.order_by('name').all()
diff --git a/server/export/models.py b/server/export/models.py
new file mode 100644
index 0000000..71a8362
--- /dev/null
+++ b/server/export/models.py
@@ -0,0 +1,3 @@
+from django.db import models
+
+# Create your models here.
diff --git a/server/export/templates/export/home.html b/server/export/templates/export/home.html
new file mode 100644
index 0000000..51c587b
--- /dev/null
+++ b/server/export/templates/export/home.html
@@ -0,0 +1,47 @@
+{% extends "base.html" %}
+{% load i18n %}
+{% load bootstrap3 %}
+
+{% block title %}{{block.super}} :: {% trans "Export data" %}{% endblock %}
+
+{% block content %}
+
+
+
+
{% trans "Export of data" %}
+
+
+ - {% trans "Home" %}
+
+ - {% trans "Export form" %}
+
+
+
+
+
+
+
{% trans "Export parameters" %}
+
+
+
+
+
+
+
+
+
+{% endblock %}
\ No newline at end of file
diff --git a/server/export/tests.py b/server/export/tests.py
new file mode 100644
index 0000000..501deb7
--- /dev/null
+++ b/server/export/tests.py
@@ -0,0 +1,16 @@
+"""
+This file demonstrates writing tests using the unittest module. These will pass
+when you run "manage.py test".
+
+Replace this with more appropriate tests for your application.
+"""
+
+from django.test import TestCase
+
+
+class SimpleTest(TestCase):
+ def test_basic_addition(self):
+ """
+ Tests that 1 + 1 always equals 2.
+ """
+ self.assertEqual(1 + 1, 2)
diff --git a/server/export/urls.py b/server/export/urls.py
new file mode 100644
index 0000000..e5c3311
--- /dev/null
+++ b/server/export/urls.py
@@ -0,0 +1,12 @@
+# -*- coding: utf-8 -*-
+
+from django.conf.urls import patterns, url
+
+urlpatterns = patterns(
+ 'export.views',
+
+
+ url(r'^$', 'home'),
+
+
+)
diff --git a/server/export/views.py b/server/export/views.py
new file mode 100644
index 0000000..e40789f
--- /dev/null
+++ b/server/export/views.py
@@ -0,0 +1,134 @@
+# -*- coding: utf-8 -*-
+
+from django.shortcuts import get_object_or_404, render_to_response, redirect
+from django.template import RequestContext
+from django.core.context_processors import csrf
+from django.views.decorators.csrf import csrf_exempt
+from django.http import Http404, HttpResponse, HttpResponseForbidden, HttpResponseNotFound
+from django.utils.encoding import smart_str
+from django.conf import settings
+from django.contrib.admin.views.decorators import staff_member_required
+from django.contrib.auth.decorators import login_required, user_passes_test
+from django.http import HttpResponseRedirect
+from django.db import connections
+from django.core.paginator import InvalidPage, EmptyPage, Paginator
+from django.core.cache import cache
+from django.core.urlresolvers import reverse
+from django.contrib import messages
+from django.utils.translation import ugettext_lazy as _
+
+from django.utils.timezone import now
+import datetime
+from dateutil.relativedelta import relativedelta
+import json
+import csv
+from huTools.structured import dict2xml
+
+
+from export.forms import ExportForm
+
+from paiements.models import Transaction
+
+
+def home(request):
+ """Show the form to export data"""
+
+ if request.method == 'POST': # If the form has been submitted...
+ form = ExportForm(request.user, request.POST)
+
+ if form.is_valid(): # If the form is valid
+
+ config = form.cleaned_data['config']
+ all_config = form.cleaned_data['all_config']
+ range = form.cleaned_data['range']
+ file_type = form.cleaned_data['file_type']
+
+ if not request.user.is_superuser and all_config:
+ raise Http404
+
+ if not request.user.is_superuser and not request.user in config.allowed_users:
+ raise Http404
+
+ transactions = Transaction.objects.order_by('creation_date')
+
+ file_name = u'Export_'
+
+ if not all_config:
+ transactions = transactions.filter(config=config)
+
+ file_name += transactions.name + u'_'
+
+ else:
+
+ file_name += u'ALL_'
+
+
+ if range == 'thismonth':
+ start_date = now() + relativedelta(day=1, minute=0, hour=0, second=0, microsecond=0)
+ end_date = now() + relativedelta(day=1, months=+1, seconds=-1, minute=0, hour=0, second=0, microsecond=0)
+ elif range == 'previousmonth':
+ start_date = now() + relativedelta(day=1, months=-1, minute=0, hour=0, second=0, microsecond=0)
+ end_date = now() + relativedelta(day=1, seconds=-1, minute=0, hour=0, second=0, microsecond=0)
+ elif range == 'sincemonth':
+ start_date = now() + relativedelta(months=-1)
+ end_date = now()
+ elif range == 'thisyear':
+ start_date = now() + relativedelta(day=1, month=1, minute=0, hour=0, second=0, microsecond=0)
+ end_date = now() + relativedelta(day=1, month=1, years=+1, seconds=-1, minute=0, hour=0, second=0, microsecond=0)
+ elif range == 'sinceyear':
+ start_date = now() + relativedelta(years=-1)
+ end_date = now()
+
+ transactions = transactions.filter(creation_date__gte=start_date, creation_date__lt=end_date).all()
+
+ file_name += start_date.strftime('%Y-%m-%d_%H.%M.%S') + u' - ' + end_date.strftime('%Y-%m-%d_%H.%M.%S')
+
+
+ # Commong for json and xml
+ data = [dict(tr.dump_api().items() + {'logs': [log.dump_api() for log in tr.transactionlog_set.order_by('when').all()]}.items()) for tr in transactions]
+
+ if file_type == 'json':
+
+ response = HttpResponse(json.dumps(data), content_type='text/json')
+
+ response['Content-Disposition'] = 'attachment; filename="%s.json"' % (file_name, )
+
+ return response
+
+ elif file_type == 'xml':
+
+
+ response = HttpResponse(dict2xml({'export': data}, pretty=True), content_type='text/xml')
+
+ response['Content-Disposition'] = 'attachment; filename="%s.xml"' % (file_name, )
+
+ return response
+
+ elif file_type == 'csv':
+
+ response = HttpResponse(content_type='text/csv')
+ response['Content-Disposition'] = 'attachment; filename="%s.csv"' % (file_name, )
+
+ writer = csv.writer(response)
+
+ headers = ['reference', 'extra_data', 'amount', 'postfinance_id', 'postfinance_status', 'internal_status', 'ipn_needed', 'creation_date', 'last_userforwarded_date', 'last_user_back_from_postfinance_date', 'last_postfinance_ipn_date', 'last_ipn_date', 'postfinance_status_text', 'internal_status_text']
+
+ writer.writerow(headers)
+
+ for tr in transactions:
+ data = []
+ trdata = tr.dump_api()
+
+ for val in headers:
+ data.append(trdata[val])
+
+ writer.writerow(data)
+
+ return response
+
+
+
+ else:
+ form = ExportForm(request.user)
+
+ return render_to_response('export/home.html', {'form': form}, context_instance=RequestContext(request))
diff --git a/server/libs/utils.py b/server/libs/utils.py
index 300182a..e148af4 100644
--- a/server/libs/utils.py
+++ b/server/libs/utils.py
@@ -1,6 +1,5 @@
import hashlib
-
def compute_sign(secret, data):
"""Compute the signature for a dict"""
@@ -19,3 +18,4 @@ def escape_chars(s):
h.update(';')
return h.hexdigest()
+
diff --git a/server/paiements/templates/paiements/transactions/list.html b/server/paiements/templates/paiements/transactions/list.html
index 0490fa1..647e557 100644
--- a/server/paiements/templates/paiements/transactions/list.html
+++ b/server/paiements/templates/paiements/transactions/list.html
@@ -5,9 +5,9 @@
{% block content %}
-
+
-{% trans "Configs" %}
+{% trans "Transactions" %}
- {% trans "Home" %}
diff --git a/server/paiements/views.py b/server/paiements/views.py
index 010f2d6..ce1c558 100644
--- a/server/paiements/views.py
+++ b/server/paiements/views.py
@@ -242,6 +242,7 @@ def transactions_list(request):
if configPk != 'all' or not request.user.is_superuser:
try:
config = get_object_or_404(Config, pk=configPk)
+ configPk = int(configPk)
except:
config = None
diff --git a/server/templates/base.html b/server/templates/base.html
index 7f1187a..e32aab4 100644
--- a/server/templates/base.html
+++ b/server/templates/base.html
@@ -53,6 +53,7 @@
{% if user.is_superuser %}- {% trans "Users" %}
{% endif %}
- {% trans "Configs" %}
- {% trans "Transactions" %}
+ - {% trans "Export" %}