views.py 1.82 KB
# -*- coding: utf-8 -*-
from django.contrib.auth.decorators import permission_required
from django.core.urlresolvers import reverse
from common.decorators import render
from dictionary.models import LexicalClass, editable_vocabularies, \
    get_exclusion_classes, PatternType
from patterns.ajax_pattern_view import PatternQuery


@permission_required('dictionary.view_pattern')
@render()
def pattern_view(request):
    lexical_class_options = [
        (lc.symbol, lc.symbol) for lc in LexicalClass.objects.all()]
    editable_vocabs = editable_vocabularies(request.user)
    vocabs = {}
    for v in editable_vocabs:
        vocabs.update(dict((str(q.pk), v.id) for q in v.qualifiers.all()))
    js_vars = {
        'ajax_get_page': reverse('get_patterns'),
        'ajax_edit_form': reverse('pattern_edit_form'),
        'ajax_update_pattern': reverse('update_pattern'),
        'ajax_search_index': reverse('patterns_search_index'),
        'ajax_save_columns': reverse('patterns_save_columns'),
        'ajax_new_ending_row': reverse('new_ending_row'),
        'ajax_create_pattern': reverse('create_pattern'),
        'ajax_pattern_preview': reverse('pattern_preview'),
        'ajax_history_table': reverse('pattern_table'),
        'exclusion_classes': get_exclusion_classes(),
        'auto_search': request.user.usersettings.incremental_search,
        'lexical_class_options': lexical_class_options,
        'pattern_type_options': PatternType.options(),
        'vocabs': vocabs,
        'initial_columns': PatternQuery.default_columns,
    }
    session_variables = (
        'pattern-filter',
        'pattern-sort_rules',
        'pattern-columns',
    )
    for var in session_variables:
        if var in request.session:
            js_name = var.split('-', 1)[1]
            js_vars[js_name] = request.session[var]
    return {'js_vars': js_vars}