summaryrefslogtreecommitdiffstats
path: root/combo/apps/lingo/manager_views.py
blob: ed2afb417afea25f4a1200bf5a2e02ac84ae08da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# 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 .models import PaymentBackend, Regie, Transaction


REGIE_FIELDS = ['label', 'slug', 'description', 'payment_backend', 'is_default', 'webservice_url',
                'extra_fees_ws_url', 'payment_min_amount', 'text_on_success']


class RegieListView(ListView):
    model = Regie


class RegieCreateView(CreateView):
    model = Regie
    fields = REGIE_FIELDS
    success_url = reverse_lazy('lingo-manager-regie-list')

    def get_initial(self):
        if self.model.objects.all().count() == 0:
            return {'is_default': True}
        return {}


class RegieUpdateView(UpdateView):
    model = Regie
    fields = REGIE_FIELDS
    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