123 lines
4.0 KiB
Python
123 lines
4.0 KiB
Python
# lingo - basket and payment system
|
|
# Copyright (C) 2015 Entr'ouvert
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify it
|
|
# under the terms of the GNU Affero General Public License as published
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU Affero General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
import csv
|
|
import datetime
|
|
from dateutil import parser as date_parser
|
|
|
|
from django.core.urlresolvers import reverse_lazy
|
|
from django.db.models import Q
|
|
from django.utils import six
|
|
from django.utils.timezone import make_aware
|
|
from django.views.generic import CreateView, UpdateView, ListView, DeleteView
|
|
from django.http import HttpResponse
|
|
|
|
import eopayment
|
|
|
|
from .forms import RegieForm
|
|
from .models import PaymentBackend, Regie, Transaction
|
|
|
|
|
|
class RegieListView(ListView):
|
|
model = Regie
|
|
|
|
|
|
class RegieCreateView(CreateView):
|
|
model = Regie
|
|
fields = ['label', 'slug', 'description', 'payment_backend']
|
|
success_url = reverse_lazy('lingo-manager-regie-list')
|
|
|
|
|
|
class RegieUpdateView(UpdateView):
|
|
model = Regie
|
|
form_class = RegieForm
|
|
success_url = reverse_lazy('lingo-manager-regie-list')
|
|
|
|
|
|
class RegieDeleteView(DeleteView):
|
|
model = Regie
|
|
success_url = reverse_lazy('lingo-manager-regie-list')
|
|
|
|
|
|
class PaymentBackendListView(ListView):
|
|
model = PaymentBackend
|
|
|
|
|
|
class PaymentBackendCreateView(CreateView):
|
|
model = PaymentBackend
|
|
fields = '__all__'
|
|
success_url = reverse_lazy('lingo-manager-paymentbackend-list')
|
|
|
|
|
|
class PaymentBackendUpdateView(UpdateView):
|
|
model = PaymentBackend
|
|
fields = '__all__'
|
|
success_url = reverse_lazy('lingo-manager-paymentbackend-list')
|
|
|
|
|
|
class PaymentBackendDeleteView(DeleteView):
|
|
model = PaymentBackend
|
|
success_url = reverse_lazy('lingo-manager-paymentbackend-list')
|
|
|
|
|
|
class TransactionListView(ListView):
|
|
model = Transaction
|
|
paginate_by = 10
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super(TransactionListView, self).get_context_data(**kwargs)
|
|
context['query'] = self.request.GET.get('q') or ''
|
|
return context
|
|
|
|
def get_queryset(self):
|
|
qs = Transaction.objects.filter(
|
|
status__in=(eopayment.PAID, eopayment.ACCEPTED)).order_by('-start_date')
|
|
query = self.request.GET.get('q')
|
|
if query:
|
|
try:
|
|
date = date_parser.parse(query, dayfirst=True)
|
|
except:
|
|
qs = qs.filter(
|
|
Q(order_id=query) |
|
|
Q(bank_transaction_id=query) |
|
|
Q(items__subject__icontains=query)
|
|
)
|
|
else:
|
|
date = make_aware(date)
|
|
qs = qs.filter(start_date__gte=date, start_date__lt=date + datetime.timedelta(days=1))
|
|
return qs
|
|
|
|
|
|
def download_transactions_csv(request):
|
|
response = HttpResponse(content_type='text/csv')
|
|
response['Content-Disposition'] = 'attachment; filename="transactions.csv"'
|
|
writer = csv.writer(response)
|
|
transactions = Transaction.objects.filter(
|
|
status__in=(eopayment.PAID, eopayment.ACCEPTED)).order_by('-start_date')
|
|
for transaction in transactions:
|
|
row = [transaction.order_id,
|
|
transaction.bank_transaction_id,
|
|
transaction.start_date.strftime('%Y-%m-%d %H:%M:%S'),
|
|
transaction.get_user_name(),
|
|
str(transaction.amount)]
|
|
for item in transaction.items.all():
|
|
row.extend([item.subject, str(item.amount)])
|
|
if six.PY3:
|
|
writer.writerow([x for x in row])
|
|
else:
|
|
writer.writerow([unicode(x).encode('utf-8') for x in row])
|
|
return response
|