ajax_lexeme_slickgrid.py 17.7 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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
# -*- coding: utf-8 -*-
from hashlib import md5
import locale

from django.db.models import Count, Q
from django.core.cache import cache
from django.utils.encoding import force_unicode

from dictionary.models import Lexeme, LexemeAttribute, PartOfSpeech, \
    LexemeList, LexemeIndex, reader_lexemes
from dictionary.ajax_slickgrid import SlickGridQuery
from common.decorators import ajax
from common.util import bisect_left, reverse, json_encode
from dictionary.util import check_query_params

locale.setlocale(locale.LC_ALL, 'pl_PL.UTF-8')

ASPECT = LexemeAttribute.objects.get(name=u'aspekt')

COLOR_SCHEMES = dict(PartOfSpeech.objects.values_list('symbol', 'color_scheme'))


class DBList(object):
    def __init__(self, lexeme_grid):
        self.lexeme_indices = lexeme_grid.lexeme_list.lexemeindex_set
        self.count = self.lexeme_indices.count()

    def __getitem__(self, item):
        return self.lexeme_indices.get(index=item).lexeme

    def __len__(self):
        return self.count


class LexemeQuery(SlickGridQuery):
    model = Lexeme
    sort_field = 'entry'
    default_columns = ('entry', 'abbr_pos', 'genders')
    column_data = {
        'id': lambda lexeme: lexeme.id,
        'scheme': lambda lexeme: COLOR_SCHEMES[lexeme.part_of_speech_id],
        'entry': lambda lexeme: lexeme.entry,
        'abbr_pos': lambda lexeme: lexeme.part_of_speech.abbr_name,
        'pos': lambda lexeme: lexeme.part_of_speech_id,
        'patterns': lambda lexeme: lexeme.lip_data()['patterns'],
        'genders': lambda lexeme:
            getattr(lexeme.attribute_value(ASPECT), 'value', '') or
            lexeme.lip_data()['genders'],
        'vocabs': lambda lexeme:
            '/'.join(lexeme.vocabularies.values_list('id', flat=True)),
        'owner': lambda lexeme: lexeme.owner_vocabulary_id,
        'status': lambda lexeme:
            force_unicode(dict(Lexeme.STATUS_CHOICES).get(lexeme.status)),
        'qualifiers': lambda lexeme:
            ', '.join(lexeme.qualifiers.values_list('label', flat=True)),
    }

    filter_field_translation = {
        'form': 'lexemeform__form',
        'lexeme_qualifier': 'qualifiers__id',
        'lip_qualifier': 'lexemeinflectionpattern__qualifiers__id',
        'qualifier': 'qualifiers_cache__id',
        'classification_value': 'classificationvalue__id',
        'pattern_name': 'lexemeinflectionpattern__pattern__name',
        'pattern_type': 'lexemeinflectionpattern__pattern__type_id',
        'gender': 'lexemeinflectionpattern__gender_id',
        'containing_vocabulary': 'vocabularies__id',
        'owner_vocabulary': 'owner_vocabulary_id',
        'pattern_count': 'pc',
        'gender_count': 'gc',
        'cr_type': 'refs_to__type_id',
        'borrowing_source': 'borrowing_source_id',
        'responsible': 'responsible_id',
        'abbr_pos': 'part_of_speech__abbr_name',
    }

    def __init__(self, *args, **kwargs):
        super(LexemeQuery, self).__init__(*args, **kwargs)
        self.visible_vocabs = self.query_params['visible_vocabs']
        self.reader = self.query_params['reader']
        self.lexeme_list = self.get_lexeme_list()

    def a_fronte(self):
        return self.sort_rules[0] == 'a_fronte'

    def sort_queryset(self, queryset):
        order_list = [self.sort_field, 'id']
        if not self.a_fronte():
            queryset = queryset.extra(select={'rev': "reverse(haslo)"})
            order_list[0] = 'rev'
        return queryset.extra(order_by=order_list)

    def apply_complex_filter_rule(self, queryset, rule):
        lookup = self.lookup_translation[rule['op']]
        negated = (lookup[0] == '-')
        lookup = lookup.lstrip('-')
        fields = [
            self.translate_filter_field(field) for field in rule['fields']]
        data = rule['data']
        q = reduce(
            lambda q1, q2: q1 | q2,
            (Q(**{(field + '__' + lookup): data}) for field in fields))
        if negated:
            queryset = queryset.exclude(q)
        else:
            queryset = queryset.filter(q).distinct()
        return queryset

    def apply_filter_rule(self, queryset, rule):
        lookup = self.lookup_translation[rule['op']]
        negated = (lookup[0] == '-')
        field, data = rule['field'], rule['data']
        new_rule = dict(rule)
        if field == 'pattern_count':
            queryset = queryset.annotate(
                pc=Count('lexemeinflectionpattern__pattern', distinct=True))
        elif field == 'gender_count':
            queryset = queryset.annotate(
                gc=Count('lexemeinflectionpattern__gender', distinct=True))
        elif not field.startswith('extra') and lookup in ['in', '-in']:
            if not data:
                new_rule['op'] = 'isnull'
                lookup = new_rule['op']
                new_rule['data'] = not negated
        elif field == 'pronunciation':
            new_rule['fields'] = [
                'pronunciation',
                'lexemeinflectionpattern__pronunciation'
            ]
            return self.apply_complex_filter_rule(queryset, new_rule)
        elif field.startswith('extra'):
            attr = LexemeAttribute.objects.get(id=int(field.split('-')[1]))
            new_rule['field'] = 'lexemeav__attribute_value_id'
            if attr.closed and not data:
                new_rule['op'] = 'in' if negated else '-in'
                lookup = new_rule['op']
                values = attr.values.all().values_list('id', flat=True)
                new_rule['data'] = tuple(values)
            elif not attr.closed:
                new_rule['field'] = 'value'
                # lekki abuse
                matching_values = super(LexemeQuery, self).apply_filter_rule(
                    attr.values, new_rule).values_list('id', flat=True)
                new_rule = {
                    'field': 'lexemeav__attribute_value_id',
                    'op': 'in',
                    'data': tuple(matching_values),
                }

        # optymalizacja warunków "różne od"
        def ident(x): return x

        def int_list(l):
            return [int(i) for i in l] if l else []

        indirect_fields = {
            'containing_vocabulary': (
                'slownik', 'leksemy_w_slownikach', 'l_id', ident),
            'lexeme_qualifier': (
                'qualifier_id', 'kwalifikatory_leksemow', 'lexeme_id',
                int_list),
            'qualifier': (
                'qualifier_id', 'dictionary_lexemeformqualifier', 'lexeme_id',
                int_list),
            'classification_value': (
                'classification_value_id', 'dictionary_lexemecv', 'lexeme_id',
                int_list),
            'gender': (
                'gender_id', 'odmieniasie', 'l_id', int_list),
            'cr_type': (
                'typods_id', 'odsylacze', 'l_id_od', int_list),
            'extra': (
                'attribute_value_id', 'dictionary_lexemeav', 'lexeme_id',
                int_list),
        }
        if lookup == '-in' and (
                field in indirect_fields or
                field.startswith('extra') and attr.closed):
            key = 'extra' if field.startswith('extra') else field
            field1, table, field2, f = indirect_fields[key]
            for value in f(new_rule['data']):
                queryset = queryset.extra(where=[
                    '''%%s NOT IN (SELECT %s FROM %s WHERE %s = leksemy.id)'''
                    % (field1, table, field2)], params=[value])
            return queryset
        indirect_fields2 = {
            'pattern_name': (
                'w_id', 'odmieniasie', 'wzory', 'w_id', 'id', 'l_id', ident),
            'pattern_type': (
                'typ', 'odmieniasie', 'wzory', 'w_id', 'id', 'l_id', int_list),
            'lip_qualifier': (
                'qualifier_id', 'odmieniasie', 'kwalifikatory_odmieniasiow',
                'id', 'lexemeinflectionpattern_id', 'l_id', int_list),
        }
        if field in indirect_fields2 and lookup == '-in':
            field1, table1, table2, join1, join2, field2, f = \
                indirect_fields2[field]
            for value in f(new_rule['data']):
                queryset = queryset.extra(where=[
                    '''%%s NOT IN (SELECT b.%s FROM %s a
                    INNER JOIN %s b ON (a.%s = b.%s) WHERE a.%s = leksemy.id)'''
                    % (field1, table1, table2, join1, join2, field2)
                ], params=[value])
            return queryset
        return super(LexemeQuery, self).apply_filter_rule(queryset, new_rule)

    def get_queryset(self):
        lexemes = super(LexemeQuery, self).get_queryset()
        if self.reader:
            return reader_lexemes(lexemes)
        return lexemes.distinct().filter(
            vocabularies__id__in=self.visible_vocabs)

    # nieużywane
    def filter_from(self, queryset, from_value, upward):
        if self.a_fronte():
            return super(LexemeQuery, self).filter_from(
                queryset, from_value, upward)
        else:
            if upward:
                comp = '>='
            else:
                comp = '<='
            return queryset.extra(
                where=["reverse(haslo) " + comp + " %s"],
                params=[reverse(from_value)])

    # indeks wiersza w danym sortowaniu, w którym
    # znajdzie się rekord o danym id
    def row_index(self, lexeme_id):
        if self.lexeme_list:
            try:
                li = self.lexeme_list.lexemeindex_set.get(lexeme_id=lexeme_id)
                return li.index
            except LexemeIndex.DoesNotExist:
                return None
        else:
            id_list = self.get_id_list()
            try:
                return id_list.index(lexeme_id)
            except ValueError:
                return None

    def search_index(self, mask):
        if self.lexeme_list:
            try:
                if not self.a_fronte():
                    mask = reverse(mask)
                li = self.lexeme_list.lexemeindex_set.filter(entry__gte=mask)[0]
                return li.index
            except IndexError:
                count = self.lexeme_list.lexemeindex_set.count()
                return count - 1 if count else 0
        else:
            id_list = self.get_id_list()
            count = len(id_list)
            index = bisect_left(id_list, mask, cmp=self.lexeme_cmp())
        if index == count > 0:
            index -= 1
        return index

    def cache_key(self):
        key = json_encode(self.query_params, ensure_ascii=True)
        return md5(key.encode()).hexdigest()

    def get_cached_lexemes(self, refresh=True):
        key = self.cache_key()
        cached = cache.get(key)
        if refresh:
            cache.set(key, cached)
        return cached

    def cache_lexemes(self, id_list):
        key = self.cache_key()
        cache.set(key, id_list)
        key_list = cache.get('key_list', [])
        if key not in key_list:
            key_list.append(key)
        cache.set('key_list', key_list)

    def get_id_list(self, force_reload=False, refresh=True):
        if self.lexeme_list:
            return self.lexeme_list.lexemeindex_set.values_list(
                'lexeme_id', flat=True)
        if not force_reload:
            id_list = self.get_cached_lexemes(refresh=refresh)
        else:
            id_list = None
        if id_list is None:
            lexemes = self.get_sorted_queryset()
            if 'rev' in lexemes.query.extra_select:
                id_list = list(
                    row[0] for row in lexemes.values_list('id', 'rev'))
            else:
                id_list = list(lexemes.values_list('id', flat=True))
            self.cache_lexemes(id_list)
        return id_list

    def get_lexeme_list(self):
        try:
            return LexemeList.objects.get(
                filter=json_encode(self.filter), a_fronte=self.a_fronte(),
                vocabularies=json_encode(self.visible_vocabs))
        except LexemeList.DoesNotExist:
            return None

    def reload_lexeme_list(self):
        LexemeIndex.objects.filter(lexeme_list=self.lexeme_list).delete()
        lexeme_ids = self.get_sorted_queryset().values_list('id', 'entry')
        lexeme_indices = (
            LexemeIndex(
                lexeme_list=self.lexeme_list, lexeme_id=lexeme_id, index=i,
                entry=entry if self.a_fronte() else reverse(entry))
            for i, (lexeme_id, entry) in enumerate(lexeme_ids))
        LexemeIndex.objects.bulk_create(lexeme_indices, batch_size=4000)

    def lexeme_cmp(self):
        def fun(lexeme_id, mask):
            entry = Lexeme.all_objects.get(id=lexeme_id).entry
            return self.strcoll(entry, mask)
        return fun

    def strcoll(self, s1, s2):
        if not self.a_fronte():
            s1 = reverse(s1)
            s2 = reverse(s2)
        return locale.strcoll(s1, s2)

    def export_list(self, columns, output_file):
        column_translation = {
            'pos': 'part_of_speech_id',
            'abbr_pos': 'part_of_speech__abbr_name',
            'patterns': 'pattern_list',
            'genders': 'gender_list',
            'vocabs': 'vocab_list',
            'owner': 'owner_vocabulary_id',
            'qualifiers': 'qualifier_list',
        }
        lexemes = Lexeme.objects.filter(id__in=self.get_id_list())
        if 'patterns' in columns:
            lexemes = lexemes.extra(select={
                'pattern_list':
                    "select coalesce(string_agg(distinct w.w_id, '/'), '') "
                    "from wzory w join odmieniasie o on w.id = o.w_id "
                    "where o.l_id = leksemy.id",
            })
        if 'genders' in columns:
            lexemes = lexemes.extra(select={
                'gender_list':
                    """select coalesce(
                        (select val.value
                          from dictionary_lexemeav lav
                          join dictionary_lexemeattributevalue val
                            on lav.attribute_value_id = val.id
                          join dictionary_lexemeattribute attr
                            on (val.attribute_id = attr.id
                                and attr.name = 'aspekt')
                          where (leksemy.id = lav.lexeme_id)),
                        (select string_agg(distinct g.symbol, '/')
                          from dictionary_gender g
                          join odmieniasie o on g.id = o.gender_id
                          where o.l_id = leksemy.id),
                        '')"""
            })
        if 'vocabs' in columns:
            lexemes = lexemes.extra(select={
                'vocab_list':
                    "select coalesce(string_agg(ls.slownik, '/'), '') "
                    "from leksemy_w_slownikach ls "
                    "where ls.l_id = leksemy.id",
            })
        if 'qualifiers' in columns:
            lexemes = lexemes.extra(select={
                'qualifier_list':
                    "select coalesce(string_agg(kwal.kwal, ', '), '') "
                    "from kwalifikatory_leksemow kl "
                    "join kwalifikatory kwal on kl.qualifier_id = kwal.id "
                    "where kl.lexeme_id = leksemy.id",
            })
        fields = [
            column_translation.get(column, column) for column in columns]
        for row in lexemes.values(*fields):
            print >> output_file, '\t'.join(
                unicode(row[field]) for field in fields)


# Zapytanie o indeks wiersza o pewnym id przy danym sortowaniu
@ajax(login_required=False, method='get')
def row_index(request, id, query_params):
    check_query_params(request, query_params)
    query = LexemeQuery(query_params)
    return {'index': query.row_index(id)}


@ajax(login_required=False, method='get')
def search_index(request, query_params, search=''):
    check_query_params(request, query_params)
    query = LexemeQuery(query_params)
    return {'index': query.search_index(search)}


@ajax(login_required=False, method='get')
def get_lexemes(request, from_page, to_page, rows, query_params, columns,
                force_reload=False):
    check_query_params(request, query_params)
    session_prefix = 'reader_' if query_params['reader'] else ''
    request.session[session_prefix + 'sort_rules'] = query_params['sort_rules']
    request.session[session_prefix + 'filter'] = query_params['filter']
    request.session[session_prefix + 'columns'] = columns
    query = LexemeQuery(query_params, columns=columns)
    start, response_rowcount = query.count_pages(from_page, to_page, rows)
    if query.lexeme_list:
        count = query.lexeme_list.lexemeindex_set.count()
        sublist = list(query.lexeme_list.lexemeindex_set.filter(
            index__gte=start, index__lt=start + response_rowcount)
           .values_list('lexeme_id', flat=True))
    else:
        id_list = query.get_id_list(force_reload)
        count = len(id_list)
        sublist = id_list[start:start + response_rowcount]
    # brzydka łata
    lexemes_qs = prefetch(Lexeme.all_objects.filter(id__in=sublist))
    lexemes_dict = dict((l.id, l) for l in lexemes_qs)
    lexemes = [lexemes_dict[lexeme_id] for lexeme_id in sublist]
    return {
        'rows': query.prepare_rows(lexemes),
        'count': count,
        'page': from_page,
    }


@ajax(login_required=False, method='get')
def search_by_form(request, query_params, columns, exponent):
    check_query_params(request, query_params)
    query = LexemeQuery(query_params, columns)
    lexemes = query.get_sorted_queryset().filter(lexemeform__form=exponent)
    rows = [row for row in query.prepare_rows(lexemes)]
    for row in rows:
        row_idx = query.row_index(row['id'])
        if row_idx is not None:
            row['row'] = row_idx
    return {
        'rows': rows,
    }


def prefetch(queryset):
    return queryset.select_related(
        'owner_vocabulary', 'part_of_speech').prefetch_related(
        'lexemeinflectionpattern_set__pattern',
        'lexemeinflectionpattern_set__gender', 'vocabularies')