views.py 2.38 KB
from collections import defaultdict

from django.shortcuts import render

from django.utils.translation import gettext as _

from phrase_expansions.models import PhraseExpansionType, PhraseExpansion

def EXPANSION_OPINION():
    return  {
        'col' : _('potoczne'),
        'dat' : _('archaiczne'),
        'unc' : _('wątpliwe'),
        'cer' : _('pewne'),
    }
    
def phrase_expansions(request):
    expansions = defaultdict(list)
    expansions = [
        {
            'phrase_type'     : phrase_type,
            'title'           : '{} – {}'.format(phrase_type, title),
            'max_positions'   : max(exp_type.max_positions for exp_type in PhraseExpansionType.objects.filter(phrase_type=phrase_type)),
            'phrase_subtypes' : [
                {
                    'phrase_subtype' : exp_type.phrase_subtype,
                    'title'          : '{}({})'.format(phrase_type, exp_type.phrase_subtype),
                    'max_positions'  : exp_type.max_positions,
                    'expansions'     : [
                        {
                            'opinion_str'   : EXPANSION_OPINION()[exp.opinion.key],
                            'opinion_sym'   : exp.opinion.key,
                            'num_positions' : len(exp.positions.all()),
                            'positions'     : [
                                {
                                    'phrases' : [
                                        {
                                            'str'  : phr.text_rep,
                                            'desc' : phr.descriptions.get(lang=request.LANGUAGE_CODE).description,
                                        } for phr in pos.phrases.all()
                                    ]
                                } for pos in exp.positions.all()
                            ],
                        } for exp in exp_type.expansions.all()
                    ],
                } for exp_type in PhraseExpansionType.objects.filter(phrase_type=phrase_type)
            ],
        } for phrase_type, title in (
            ('advp', _('frazy przysłówkowe')),
            ('xp', _('frazy okolicznikowe')),
            ('comprepnp', _('frazy przyimkowe z przyimkiem złożonym')),
            ('distrp', _('fraza dystrybutywna')),
            ('possp', _('fraza posesywna')),
        )
    ]
    return render(request, 'phrase_expansions.html', { 'expansions' : expansions })