<commit_before>import math

from django.utils.html import urlize
from wagtail.wagtailcore.blocks import StructBlock
from wagtail.wagtailcore.blocks.field_block import TextBlock

from isi_mip.climatemodels.models import InputData, OutputData, ImpactModel
from isi_mip.contrib.blocks import IntegerBlock, RichTextBlock


class ImpactModelsBlock(StructBlock):
    description = RichTextBlock()
    rows_per_page = IntegerBlock(default=20, min_value=1, required=True)

    def get_context(self, value):
        context = super().get_context(value)

        ims = ImpactModel.objects.order_by('name')

        # Filter und Suchfelder
        context['tableid'] = 'selectortable'
        context['searchfield'] = {'value': ''}
        sector_options = [{'value': x} for x in ims.values_list('sector', flat=True).distinct().order_by('sector')]
        cdriver_options = [{'value': x} for x in
                           InputData.objects.values_list('name', flat=True).distinct().order_by('name')]
        context['selectors'] = [
            {'colnumber': '2', 'all_value': 'All Sectors', 'options': sector_options, 'name': 'sector'},
            {'colnumber': '3', 'all_value': 'All Climate Drivers', 'options': cdriver_options, 'name': 'driver'},
        ]

        # Tabelle
        context['id'] = 'selectortable'
        context['head'] = {
            'cols': [{'text': 'Model'}, {'text': 'Sector'}, {'text': 'Climate Driver'}, {'text': 'Contact'}]
        }
        numpages = math.ceil(ims.count() / value.get('rows_per_page'))
        context['pagination'] = {
            'rowsperpage': (value.get('rows_per_page')),
            'numberofpages': numpages,  # number of pages with current filters
            'pagenumbers': [{'number': i + 1, 'invisible': False} for i in range(numpages)],
            'activepage': 1,  # set to something between 1 and numberofpages
        }
        context['norowvisible'] = False  # true when no row is visible

        bodyrows = []
        for i, imodel in enumerate(ims):
            datasets = [str(x) for x in imodel.climate_data_sets.all()]
            # import ipdb; ipdb.set_trace()
            cpeople = ["{0.name}<br/><a href='mailto:{0.email}'>{0.email}</a>".format(x) for x in
                       imodel.contactperson_set.all()]
            values = [["<a href='details/{0.id}/'>{0.name}</a>".format(imodel)], [imodel.sector]]
            values += [datasets] + [["<br/>".join(cpeople)]]
            row = {
                'invisible': i >= value.get('rows_per_page'),
                'cols': [{'texts': x} for x in values]
            }
            bodyrows.append(row)

        context['body'] = {'rows': bodyrows}
        return context

    class Meta:
        icon = 'fa fa-database'
        template = 'blocks/impact_models_block.html'


class InputDataBlock(StructBlock):
    row_limit = IntegerBlock(default=10, min_value=1, max_value=30, label='Input Data Row Limit')

    def get_context(self, value):
        context = super().get_context(value)

        context['head'] = {'cols': [{'text': 'Data Set'}, {'text': 'Data Type'}, {'text': 'Description'}]}
        context['body'] = {
            'rowlimit': {'buttontext': 'See all <i class="fa fa-chevron-down"></i>',
                         'rownumber': value.get('row_limit')},
            'rows': []
        }

        inputdata = InputData.objects.all()
        for i, idata in enumerate(inputdata):
            link = "<a href='details/{0.id}'>{0.name}</a>".format(idata)
            context['body']['rows'] += [
                {'cols': [
                    {'texts': [link]},
                    {'texts': [idata.data_type]},
                    {'texts': [urlize(idata.description)]}],
                    'invisible': i >= value.get('row_limit')
                }
            ]

        if value.get('row_limit') < inputdata.count():
            context['showalllink'] = {'buttontext': 'See all <i class="fa fa-chevron-down"></i>'}
        return context

    class Meta:
        icon = 'fa fa-database'
        # template = 'blocks/input_data_block.html'
        template = 'widgets/table.html'

class OutputDataBlock(StructBlock):
    rows_per_page = IntegerBlock(default=20, min_value=1, required=True)

    def get_context(self, value):
        context = super().get_context(value)
        context['title'] = 'Overview'

        context['head'] = {'cols': [{'text': 'Sector'}, {'text': 'Model'}, {'text': 'Scenario'},
                                    {'text': 'Climate Driver'}, {'text': 'Date'}]}
        context['body'] = {
            'rows': [],
        }

        outputdata = OutputData.objects.order_by('sector','model')
        for i, odat in enumerate(outputdata):
            drivers = [x.name for x in odat.drivers.all()]
            scenarios = ', '.join(x.name for x in odat.scenarios.all())
            context['body']['rows'] += [
                {
                    'invisible': i >= value.get('rows_per_page'),
                    'cols': [
                    {'texts': [odat.sector]},
                    {'texts': [odat.model.name]},<commit_msg>pagination for input data<commit_after>import math

from django.utils.html import urlize
from wagtail.wagtailcore.blocks import StructBlock
from wagtail.wagtailcore.blocks.field_block import TextBlock

from isi_mip.climatemodels.models import InputData, OutputData, ImpactModel
from isi_mip.contrib.blocks import IntegerBlock, RichTextBlock


class ImpactModelsBlock(StructBlock):
    description = RichTextBlock()
    rows_per_page = IntegerBlock(default=20, min_value=1, required=True)

    def get_context(self, value):
        context = super().get_context(value)

        ims = ImpactModel.objects.order_by('name')

        # Filter und Suchfelder
        context['tableid'] = 'selectortable'
        context['searchfield'] = {'value': ''}
        sector_options = [{'value': x} for x in ims.values_list('sector', flat=True).distinct().order_by('sector')]
        cdriver_options = [{'value': x} for x in
                           InputData.objects.values_list('name', flat=True).distinct().order_by('name')]
        context['selectors'] = [
            {'colnumber': '2', 'all_value': 'All Sectors', 'options': sector_options, 'name': 'sector'},
            {'colnumber': '3', 'all_value': 'All Climate Drivers', 'options': cdriver_options, 'name': 'driver'},
        ]

        # Tabelle
        context['id'] = 'selectortable'
        context['head'] = {
            'cols': [{'text': 'Model'}, {'text': 'Sector'}, {'text': 'Climate Driver'}, {'text': 'Contact'}]
        }
        numpages = math.ceil(ims.count() / value.get('rows_per_page'))
        context['pagination'] = {
            'rowsperpage': (value.get('rows_per_page')),
            'numberofpages': numpages,  # number of pages with current filters
            'pagenumbers': [{'number': i + 1, 'invisible': False} for i in range(numpages)],
            'activepage': 1,  # set to something between 1 and numberofpages
        }
        context['norowvisible'] = False  # true when no row is visible

        context['body'] = {'rows': []}
        for i, imodel in enumerate(ims):
            datasets = [str(x) for x in imodel.climate_data_sets.all()]
            cpeople = ["{0.name}<br/><a href='mailto:{0.email}'>{0.email}</a>".format(x) for x in
                       imodel.contactperson_set.all()]
            values = [["<a href='details/{0.id}/'>{0.name}</a>".format(imodel)], [imodel.sector]]
            values += [datasets] + [["<br/>".join(cpeople)]]
            row = {
                'invisible': i >= value.get('rows_per_page'),
                'cols': [{'texts': x} for x in values]
            }
            context['body']['rows'] += [row]

        return context

    class Meta:
        icon = 'fa fa-database'
        template = 'blocks/impact_models_block.html'


class InputDataBlock(StructBlock):
    row_limit = IntegerBlock(default=10, min_value=1, max_value=30, label='Input Data Row Limit')

    def get_context(self, value):
        context = super().get_context(value)

        context['head'] = {'cols': [{'text': 'Data Set'}, {'text': 'Data Type'}, {'text': 'Description'}]}
        context['body'] = {'rows': []}

        inputdata = InputData.objects.all()
        numpages = math.ceil(inputdata.count() / value.get('row_limit'))

        for i, idata in enumerate(inputdata):
            link = "<a href='details/{0.id}'>{0.name}</a>".format(idata)
            context['body']['rows'] += [
                {'cols': [
                    {'texts': [link]},
                    {'texts': [idata.data_type]},
                    {'texts': [urlize(idata.description)]}],
                    'invisible': i >= value.get('row_limit')
                }
            ]
        context['pagination'] = {
            'rowsperpage': (value.get('rows_per_page')),
            'numberofpages': numpages,  # number of pages with current filters
            'pagenumbers': [{'number': i + 1, 'invisible': False} for i in range(numpages)],
            'activepage': 1,  # set to something between 1 and numberofpages
        }
        # if value.get('row_limit') < inputdata.count():
        #     context['showalllink'] = {'buttontext': 'See all <i class="fa fa-chevron-down"></i>'}
        print(context)
        return context

    class Meta:
        icon = 'fa fa-database'
        # template = 'blocks/input_data_block.html'
        template = 'widgets/table.html'

class OutputDataBlock(StructBlock):
    rows_per_page = IntegerBlock(default=20, min_value=1, required=True)

    def get_context(self, value):
        context = super().get_context(value)
        context['title'] = 'Overview'

        context['head'] = {'cols': [{'text': 'Sector'}, {'text': 'Model'}, {'text': 'Scenario'},
                                    {'text': 'Climate Driver'}, {'text': 'Date'}]}
        context['body'] = {
            'rows': [],
        }

        outputdata = OutputData.objects.order_by('sector','model')
        for i, odat in enumerate(outputdata):
            drivers = [x.name for x in odat.drivers.all()]
            scenarios = ', '.join(x.name for x in odat.scenarios.all())
            context['body']['rows'] += [
                {
                    'invisible': i >= value.get('rows_per_page'),
                    'cols': [
                    {'texts': [odat.sector]},
                    {'texts': [odat.model.name]},
{'fraction_non_alphanumeric': 0.16503737780333524, 'fraction_numerical': 0.004696185547249377, 'mean_word_length': 2.3596265292981324, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1760820', 'n_tokens_mistral': 3197, 'n_tokens_neox': 3080, 'n_words': 673}