Skip to content

Commit

Permalink
Licence, export
Browse files Browse the repository at this point in the history
  • Loading branch information
the-glu committed Jan 15, 2014
1 parent b234e8d commit 98f1aff
Show file tree
Hide file tree
Showing 15 changed files with 271 additions and 17 deletions.
11 changes: 11 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions server/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@

'main',
'users',
'export',
'configs',
'paiements',
)
Expand Down
10 changes: 1 addition & 9 deletions server/app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,14 @@

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': '/'}),

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')),

Expand Down
2 changes: 2 additions & 0 deletions server/data/pip-reqs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ django-bootstrap3
requests
celery
django-celery
python-dateutil
PicklingTools
Empty file added server/export/__init__.py
Empty file.
38 changes: 38 additions & 0 deletions server/export/forms.py
Original file line number Diff line number Diff line change
@@ -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()
3 changes: 3 additions & 0 deletions server/export/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
47 changes: 47 additions & 0 deletions server/export/templates/export/home.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{% extends "base.html" %}
{% load i18n %}
{% load bootstrap3 %}

{% block title %}{{block.super}} :: {% trans "Export data" %}{% endblock %}

{% block content %}

<script type="text/javascript">$('#nav-export').addClass('active');</script>

<h2>{% trans "Export of data" %}</h2>

<ol class="breadcrumb">
<li><a href="{% url 'main.views.home' %}"><i class="fa fa-home"></i> {% trans "Home" %}</a></li>

<li class="active"><i class="fa fa-truck"></i> {% trans "Export form" %}</li>

</ol>

<div class="row-fluid">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">{% trans "Export parameters" %}</h3>
</div>
<div class="panel-body">

<form action="" method="POST">
{% csrf_token %}

{% bootstrap_form form %}

<div class="row-fluid box-section" style="text-align: right;">
<input type="submit" class="btn btn-primary" value="{% trans "Save" %}">
</div>

</form>
</div>
</div>

</div>

<script type="text/javascript">
$('#id_config, #id_file_type, #id_range').select2();
</script>


{% endblock %}
16 changes: 16 additions & 0 deletions server/export/tests.py
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 12 additions & 0 deletions server/export/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# -*- coding: utf-8 -*-

from django.conf.urls import patterns, url

urlpatterns = patterns(
'export.views',


url(r'^$', 'home'),


)
134 changes: 134 additions & 0 deletions server/export/views.py
Original file line number Diff line number Diff line change
@@ -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))
2 changes: 1 addition & 1 deletion server/libs/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import hashlib


def compute_sign(secret, data):
"""Compute the signature for a dict"""

Expand All @@ -19,3 +18,4 @@ def escape_chars(s):
h.update(';')

return h.hexdigest()

4 changes: 2 additions & 2 deletions server/paiements/templates/paiements/transactions/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

{% block content %}

<script type="text/javascript">$('#nav-transations').addClass('active');</script>
<script type="text/javascript">$('#nav-transactions').addClass('active');</script>

<h2>{% trans "Configs" %}</h2>
<h2>{% trans "Transactions" %}</h2>

<ol class="breadcrumb">
<li><a href="{% url 'main.views.home' %}"><i class="fa fa-home"></i> {% trans "Home" %}</a></li>
Expand Down
1 change: 1 addition & 0 deletions server/paiements/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 2 additions & 5 deletions server/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
{% if user.is_superuser %}<li id="nav-users"><a href="{% url 'users.views.list' %}"><i class="fa fa-users"></i> {% trans "Users" %}</a></li>{% endif %}
<li id="nav-configs"><a href="{% url 'configs.views.list' %}"><i class="fa fa-cogs"></i> {% trans "Configs" %}</a></li>
<li id="nav-transactions"><a href="{% url 'paiements.views.transactions_list' %}"><i class="fa fa-money"></i> {% trans "Transactions" %}</a></li>
<li id="nav-export"><a href="{% url 'export.views.home' %}"><i class="fa fa-truck"></i> {% trans "Export" %}</a></li>
</ul>

<ul class="nav navbar-nav navbar-right navbar-user">
Expand All @@ -76,13 +77,9 @@

</div><!-- /#page-wrapper -->

<div class="copy">&copy; Maximilien Cuony - 2014 - <a href="http://agepoly.epfl.ch">AGEPoly</a>/<a href="http://polylan.ch">PolyLAN</a></div>
<div class="copy">&copy; Maximilien Cuony &amp; Malik Bougacha - 2014 - <a href="http://opensource.org/licenses/BSD-2-Clause" target="_blank">BSD</a> - <a href="http://agepoly.epfl.ch">AGEPoly</a>/<a href="http://polylan.ch">PolyLAN</a></div>

</div><!-- /#wrapper -->





</body>
</html>

0 comments on commit 98f1aff

Please sign in to comment.