filtering.py
11.9 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# -*- coding: utf-8 -*-
from django.contrib.auth.models import User
from accounts.models import get_anon_profile
from dictionary.forms import FilterForm
from dictionary.models import Frame_Char_Model, Frame_Opinion_Value, Lemma_Status, \
NKJP_Source, POS, Vocabulary, \
get_frame_char_and_its_value
def schemata_filter_options():
# pobieranie wartosci aspektu
aspect_model = Frame_Char_Model.objects.get(model_name=u'ASPEKT')
aspect_vals_objs = aspect_model.frame_char_values.order_by('-priority')
aspect_options = [{'name': '*', 'value': '*'}]
aspect_options.extend([{'name': val.value, 'value': val.value} for val in aspect_vals_objs])
# pobieranie wartosci zwrotnosci
reflex_model = Frame_Char_Model.objects.get(model_name=u'ZWROTNOŚĆ')
reflex_vals_objs = reflex_model.frame_char_values.order_by('-priority')
reflex_options = [{'name': '*', 'value': '*'}]
reflex_options.extend([{'name': val.value, 'value': val.value} for val in reflex_vals_objs])
# pobieranie wartosci negatywnosci
neg_model = Frame_Char_Model.objects.get(model_name=u'NEGATYWNOŚĆ')
neg_vals_objs = neg_model.frame_char_values.order_by('-priority')
neg_options = [{'name': '*', 'value': '*'}]
neg_options.extend([{'name': val.value, 'value': val.value} for val in neg_vals_objs])
# pobieranie wartosci predykatywnosci
pred_model = Frame_Char_Model.objects.get(model_name=u'PREDYKATYWNOŚĆ')
pred_vals_objs = pred_model.frame_char_values.order_by('-priority')
pred_options = [{'name': '*', 'value': '*'}]
pred_options.extend([{'name': val.value, 'value': val.value} for val in pred_vals_objs])
# pobieranie opinii o schemacie
opinion_options = [{'name': '*', 'value': '*'}]
opinion_options.extend([{'name': val.value, 'value': val.value} for val in Frame_Opinion_Value.objects.order_by('priority')])
schema_type_options = [{'name': '*', 'value': '*'},
{'name': 'normalny', 'value': 'normal'},
{'name': 'frazeologiczny', 'value': 'phraseologic'}]
return {'schema_type_options': schema_type_options,
'reflex_options': reflex_options,
'aspect_options': aspect_options,
'neg_options': neg_options,
'pred_options': pred_options,
'opinion_options': opinion_options}
def all_filter_rules_loaded(rules):
if set(default_filter_rules().keys()) != set(rules):
return False
return True
def default_filter_rules():
return {'lemma': '.*',
'pos': None,
'contains_phraseology': None,
'owner': None,
'phraseologist': None,
'semanticist': None,
'vocabulary': None,
'status': None,
'example_source': None,
'approver': None,
'reflex': None,
'negativity': None,
'predicativity': None,
'aspect': None,
'argument': '.*',
'position': '.*',
'frame_opinion' : None,
'sender': None,
'schema_type': None}
def prepare_filter_form(request):
if request.session.has_key('lemma_preview') and request.session['lemma_preview']:
if not request.session.has_key('filter_rules_lemma_preview'):
request.session['filter_rules_lemma_preview'] = default_filter_rules()
filter_rules = request.session['filter_rules_lemma_preview']
else:
if not request.session.has_key('filter_rules'):
request.session['filter_rules'] = default_filter_rules()
filter_rules = request.session['filter_rules']
users = User.objects.none()
phraseologists = User.objects.none()
semanticists = User.objects.none()
vocabularies = Vocabulary.objects.none()
senders = User.objects.none()
statuses = get_anon_profile().visible_statuses.all()
can_confirm_example = False
if request.user.is_authenticated():
users = User.objects.filter(lemmas__old=False).distinct().order_by('username')
phraseologists = User.objects.filter(phraseologist_lemmas__old=False).distinct().order_by('username')
semanticists = User.objects.filter(semanticist_lemmas__old=False).distinct().order_by('username')
vocabularies = request.user.visible_vocabularies.all()
senders = User.objects.order_by('groups__group_settings__priority')
statuses = Lemma_Status.objects.all()
if request.user.has_perm('dictionary.confirm_example') or request.user.is_superuser:
can_confirm_example = True
form = FilterForm(users=users,
phraseologists=phraseologists,
semanticists=semanticists,
vocabularies=vocabularies,
senders=senders,
statuses=statuses,
lemma=filter_rules['lemma'],
sel_pos=filter_rules['pos'],
contains_phraseology=filter_rules['contains_phraseology'],
sel_user=filter_rules['owner'],
sel_phraseologist=filter_rules['phraseologist'],
sel_semanticist=filter_rules['semanticist'],
sel_vocabulary=filter_rules['vocabulary'],
sel_status=filter_rules['status'],
sel_reflex=filter_rules['reflex'],
sel_negativity=filter_rules['negativity'],
sel_predicativity=filter_rules['predicativity'],
sel_aspect=filter_rules['aspect'],
sel_has_argument=filter_rules['argument'],
sel_has_position=filter_rules['position'],
sel_frame_opinion=filter_rules['frame_opinion'],
can_confirm_example = can_confirm_example,
sel_example_source=filter_rules['example_source'],
sel_approver=filter_rules['approver'],
sel_sender=filter_rules['sender'],
sel_schema_type=filter_rules['schema_type'])
return form
def save_lemma_filters_and_get_schemata_filter_setup(request, filter_dict):
if filter_dict['pos']:
pos_obj = POS.objects.get(id=filter_dict['pos'])
else:
pos_obj = None
if filter_dict['owner']:
owner_obj = User.objects.get(id=filter_dict['owner'])
else:
owner_obj = None
if filter_dict['phraseologist']:
phraseologist_obj = User.objects.get(id=filter_dict['phraseologist'])
else:
phraseologist_obj = None
if filter_dict['semanticist']:
semanticist_obj = User.objects.get(id=filter_dict['semanticist'])
else:
semanticist_obj = None
if filter_dict['vocabulary']:
vocabulary_obj = Vocabulary.objects.get(name=filter_dict['vocabulary'])
else:
vocabulary_obj = None
if filter_dict['status']:
status_obj = Lemma_Status.objects.get(id=filter_dict['status'])
else:
status_obj = None
if filter_dict['example_source']:
nkjp_source_obj = NKJP_Source.objects.get(id=filter_dict['example_source'])
else:
nkjp_source_obj = None
if filter_dict['approver']:
approver_obj = User.objects.get(id=filter_dict['approver'])
else:
approver_obj = None
if filter_dict['has_message_from']:
try:
sender_obj = User.objects.get(pk=filter_dict['has_message_from'])
except User.DoesNotExist:
sender_obj = None
else:
sender_obj = None
reflex_obj, reflex_val = get_frame_char_and_its_value(filter_dict['reflex'], '*')
negativity_obj, negativity_val = get_frame_char_and_its_value(filter_dict['negativity'], '*')
aspect_obj, aspect_val = get_frame_char_and_its_value(filter_dict['aspect'], '*')
pred_obj, pred_val = get_frame_char_and_its_value(filter_dict['predicativity'], '*')
if filter_dict['frame_opinion']:
frame_opinion_obj = Frame_Opinion_Value.objects.get(id=filter_dict['frame_opinion'])
opinion_val = frame_opinion_obj.value
else:
frame_opinion_obj = None
opinion_val = '*'
if 'schema_type' in filter_dict:
schema_type = filter_dict['schema_type']
else:
schema_type = None
if request.session.has_key('lemma_preview') and request.session['lemma_preview']:
request.session['filter_rules_lemma_preview'] = {'pos' : pos_obj,
'contains_phraseology': filter_dict['contains_phraseology'],
'owner' : owner_obj,
'phraseologist' : phraseologist_obj,
'semanticist' : semanticist_obj,
'vocabulary' : vocabulary_obj,
'status' : status_obj,
'example_source' : nkjp_source_obj,
'approver' : approver_obj,
'reflex' : reflex_obj,
'negativity' : negativity_obj,
'predicativity' : pred_obj,
'aspect' : aspect_obj,
'argument' : filter_dict['has_argument'],
'position' : filter_dict['has_position'],
'lemma' : filter_dict['lemma'],
'frame_opinion' : frame_opinion_obj,
'sender' : sender_obj,
'schema_type' : schema_type}
else:
request.session['filter_rules'] = {'pos' : pos_obj,
'contains_phraseology': filter_dict['contains_phraseology'],
'owner' : owner_obj,
'phraseologist' : phraseologist_obj,
'semanticist' : semanticist_obj,
'vocabulary' : vocabulary_obj,
'status' : status_obj,
'example_source' : nkjp_source_obj,
'approver' : approver_obj,
'reflex' : reflex_obj,
'negativity' : negativity_obj,
'predicativity' : pred_obj,
'aspect' : aspect_obj,
'argument' : filter_dict['has_argument'],
'position' : filter_dict['has_position'],
'lemma' : filter_dict['lemma'],
'frame_opinion' : frame_opinion_obj,
'sender' : sender_obj,
'schema_type' : schema_type}
return {'filter_frames': filter_dict['filter_frames'],
'schema_type' : schema_type,
'reflex' : reflex_val,
'negativity' : negativity_val,
'predicativity': pred_val,
'opinion' : opinion_val,
'aspect' : aspect_val,
'position' : filter_dict['has_position'],
'argument' : filter_dict['has_argument']}