pagination_types.py
3.52 KB
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
# -*- coding: utf-8 -*-
from datetime import datetime
from django.contrib.auth.models import User
from django.forms import Form, ChoiceField, Select, ModelChoiceField, \
DateField, DateInput
from dictionary.models import History, Lexeme, filter_visible, Pattern
types = {}
def lexeme_history_list(params, user):
objects = History.objects.exclude(lexeme=None).distinct().values(
'lexeme', 'user', 'transaction_began')
lexemes = filter_visible(Lexeme.all_objects, user).exclude(entry='')
for field, lookup, value in params['filters']:
if field == 'user':
if lookup == 'eq':
objects = objects.filter(user__pk=value)
elif lookup == 'ne':
objects = objects.exclude(user__username=value)
if field == 'time':
try:
date = datetime.strptime(value, '%d.%m.%Y')
if lookup == 'from':
objects = objects.filter(transaction_began__gte=date)
elif lookup == 'to':
objects = objects.filter(transaction_began__lte=date)
except ValueError:
pass
objects = objects.filter(lexeme__in=lexemes)
objects = objects.order_by(params['order_by'])
return objects
def pattern_history_list(params, user):
objects = History.objects.exclude(pattern=None).distinct().values(
'pattern', 'user', 'transaction_began')
patterns = Pattern.all_objects.exclude(name__startswith=u'nowy wzór')
for field, lookup, value in params['filters']:
if field == 'user':
if lookup == 'eq':
objects = objects.filter(user__pk=value)
elif lookup == 'ne':
objects = objects.exclude(user__username=value)
if field == 'time':
try:
date = datetime.strptime(value, '%d.%m.%Y')
if lookup == 'from':
objects = objects.filter(transaction_began__gte=date)
elif lookup == 'to':
objects = objects.filter(transaction_began__lte=date)
except ValueError:
pass
objects = objects.filter(pattern__in=patterns)
objects = objects.order_by(params['order_by'])
return objects
class HistoryFilterFieldForm(Form):
field = ChoiceField(
choices=[], label=u'', widget=Select(attrs={'class': 'field-choice'}))
def __init__(self, **kwargs):
super(HistoryFilterFieldForm, self).__init__(**kwargs)
filters = HISTORY_FILTERS
choices = [(key, value[0]) for key, value in filters.iteritems()]
self.fields['field'].choices = choices
class UserFilterForm(Form):
user = ModelChoiceField(
queryset=User.objects.order_by('username'), label=u'',
widget=Select(attrs={'class': 'value'}))
class TimeFilterForm(Form):
time = DateField(input_formats=['%d.%m.%Y'], label=u'',
widget=DateInput(attrs={'class': 'datepicker value'}))
HISTORY_FILTERS = {
'user': (
u'Użytkownik',
[('eq', u'równy')],
UserFilterForm,
),
'time': (
u'Data',
[('from', u'od'), ('to', u'do')],
TimeFilterForm,
),
}
def dummy_perm(user, object, action):
return True
types['lexeme_history'] = (
lexeme_history_list,
'lexeme_history_item.html',
dummy_perm,
{},
HISTORY_FILTERS,
HistoryFilterFieldForm,
)
types['pattern_history'] = (
pattern_history_list,
'pattern_history_item.html',
dummy_perm,
{},
HISTORY_FILTERS,
HistoryFilterFieldForm,
)