<commit_before>from pprint import pprint

from django.db.models import Count, Sum
from django.shortcuts import render
from django.urls import reverse
from django.views import View

import lotes.models
from cd.queries.visao import get_solic_dict


class VisaoRua(View):

    def __init__(self):
        self.template_name = 'cd/visao_rua.html'
        self.title_name = 'Visão geral de rua do CD'

    def mount_context(self, rua):
        context = {'rua': rua}

        solic_dict = get_solic_dict(rua)

        locais_recs = lotes.models.Lote.objects.filter(
            local__startswith=rua
        ).exclude(
            local__isnull=True
        ).exclude(
            local__exact=''
        ).values('local').annotate(
            qlotes=Count('lote'),
            qtdsum=Sum('qtd')
        ).order_by('local')
        if len(locais_recs) == 0:
            return context

        data = list(locais_recs.values(
            'local', 'qlotes', 'qtdsum'))

        for row in data:
            if row['local'] in solic_dict:
                row['solicitacoes'] = ', '.join(solic_dict[row['local']])
            else:
                row['solicitacoes'] = '-'
            row['local|TARGET'] = '_BLANK'
            row['local|LINK'] = reverse(
                'cd:estoque_filtro', args=['E', row['local']])

        headers = ['Endereço', 'Solicitações', 'Lotes (caixas)', 'Qtd. itens']
        fields = ['local', 'solicitacoes', 'qlotes', 'qtdsum']

        total = data[0].copy()
        total['solicitacoes'] = ''
        total['local'] = 'Total:'
        total['|STYLE'] = 'font-weight: bold;'
        quant_fileds = ['qlotes', 'qtdsum']
        for field in quant_fileds:
            total[field] = 0
        for row in data:
            for field in quant_fileds:
                total[field] += row[field]
        data.append(total)

        context.update({
            'headers': headers,
            'fields': fields,
            'data': data,
            'style': {3: 'text-align: right;',
                      4: 'text-align: right;'},
        })

        return context

    def get(self, request, *args, **kwargs):
        rua = kwargs['rua'].upper()
        context = {'titulo': self.title_name}
        data = self.mount_context(rua)<commit_msg>view VisaoRua total agora OK<commit_after>from pprint import pprint

from django.db.models import Count, Sum
from django.shortcuts import render
from django.urls import reverse
from django.views import View

import lotes.models
from cd.queries.visao import get_solic_dict


class VisaoRua(View):

    def __init__(self):
        self.template_name = 'cd/visao_rua.html'
        self.title_name = 'Visão geral de rua do CD'

    def mount_context(self, rua):
        context = {'rua': rua}

        solic_dict = get_solic_dict(rua)

        locais_recs = lotes.models.Lote.objects.filter(
            local__startswith=rua
        ).exclude(
            local__isnull=True
        ).exclude(
            local__exact=''
        ).values('local').annotate(
            qlotes=Count('lote'),
            qtdsum=Sum('qtd')
        ).order_by('local')
        if len(locais_recs) == 0:
            return context

        data = list(locais_recs.values(
            'local', 'qlotes', 'qtdsum'))

        total = data[0].copy()
        for row in data:
            if row['local'] in solic_dict:
                row['solicitacoes'] = ', '.join(solic_dict[row['local']])
            else:
                row['solicitacoes'] = '-'
            row['local|TARGET'] = '_BLANK'
            row['local|LINK'] = reverse(
                'cd:estoque_filtro', args=['E', row['local']])

        headers = ['Endereço', 'Solicitações', 'Lotes (caixas)', 'Qtd. itens']
        fields = ['local', 'solicitacoes', 'qlotes', 'qtdsum']

        total['local'] = 'Total:'
        total['solicitacoes'] = ''
        total['|STYLE'] = 'font-weight: bold;'
        quant_fileds = ['qlotes', 'qtdsum']
        for field in quant_fileds:
            total[field] = 0
        for row in data:
            for field in quant_fileds:
                total[field] += row[field]
        data.append(total)

        context.update({
            'headers': headers,
            'fields': fields,
            'data': data,
            'style': {3: 'text-align: right;',
                      4: 'text-align: right;'},
        })

        return context

    def get(self, request, *args, **kwargs):
        rua = kwargs['rua'].upper()
        context = {'titulo': self.title_name}
        data = self.mount_context(rua)
{'fraction_non_alphanumeric': 0.13793103448275862, 'fraction_numerical': 0.0021963540522732263, 'mean_word_length': 1.998025016458196, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4521670', 'n_tokens_mistral': 1465, 'n_tokens_neox': 1417, 'n_words': 315}