filtering.py 11.9 KB
# -*- 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']}