<commit_before>from django.shortcuts import render
from django import forms
from django.http import HttpResponse
from cat.models import MuseumObject
import django_filters
import django_tables2 as tables
from django_tables2 import RequestConfig
from django_tables2.utils import AttributeDict
from django_tables2.utils import A


class ItemTable(tables.Table):
    photo = tables.TemplateColumn('<img src="{% firstof '
    'record.artefactrepresentation_set.all.0.image.url_64x64 '
    '"http://placehold.it/48" %}" width="64px" height="64px" '
    'alt="">')
    registration_number = tables.LinkColumn(
            'artefact_view', args=[A('registration_number')])

    class Meta:
        model = MuseumObject
        sequence = ("photo", "registration_number", "...")


class ItemFilterSet(django_filters.FilterSet):
#    collections = django_filters.ModelChoiceFilter(name='Collection',
#            extra = lambda f: {'queryset':
#             f.rel.to._default_manager.complex_filter(f.rel.limit_choices_to),
#              'to_field_name': f.rel.field_name})

    class Meta:
        model = MuseumObject
        fields = ['registration_number', 'functional_category', 'category',
                'artefact_type', 'loan_status', 'access_status',
                'record_status', 'raw_material', 'description',
                'storage_section', 'storage_unit', 'storage_bay',
                'storage_shelf_box_drawer', 'acquisition_date',
                'acquisition_method', 'cultural_bloc', 'donor', 'collector',
                'collections__title'
                ]


class ColumnForm(forms.Form):
    """For selecting which columns are included in search results"""
    columns = forms.MultipleChoiceField(
            widget=forms.SelectMultiple(attrs={'size': '15'}))
    #widget=forms.CheckboxSelectMultiple)

    def __init__(self, model, *args, **kwargs):
        self.model = model
        super(ColumnForm, self).__init__(*args, **kwargs)
        self.fields['columns'].choices = [(f.name, f.verbose_name.title())
            for f in self.model._meta.fields]

    def get_excluded_names(self):
        """Return list of field names to exclude"""
        desired_fields = self.get_desired_field_names()
        all_fields = [f for f in self.model._meta.fields]

        return [field.name
                for field in all_fields if field.name not in desired_fields]

    def get_desired_fields(self):
        """Return list of fields (not just field name)"""
        desired_fields = self.get_desired_field_names()
        all_fields = [f for f in MuseumObject._meta.fields]

        return [field for field in all_fields if field.name in
                desired_fields]

    def get_desired_field_names(self):
        desired_fields = []
        if self.is_valid():
            desired_fields = self.cleaned_data['columns']<commit_msg>Allow searching for multiple registration numbers.<commit_after>from django.shortcuts import render
from django import forms
from django.http import HttpResponse
from cat.models import MuseumObject
import django_filters
import django_tables2 as tables
from django_tables2 import RequestConfig
from django_tables2.utils import AttributeDict
from django_tables2.utils import A


class ItemTable(tables.Table):
    photo = tables.TemplateColumn('<img src="{% firstof '
    'record.artefactrepresentation_set.all.0.image.url_64x64 '
    '"http://placehold.it/48" %}" width="64px" height="64px" '
    'alt="">')
    registration_number = tables.LinkColumn(
            'artefact_view', args=[A('registration_number')])

    class Meta:
        model = MuseumObject
        sequence = ("photo", "registration_number", "...")


def registration_number_filter(queryset, values):
    if values:
        values = [int(v) for v in values.split(' ')]
        return queryset.filter(registration_number__in=values)
    else:
        return queryset


class ItemFilterSet(django_filters.FilterSet):
#    collections = django_filters.ModelChoiceFilter(name='Collection',
#            extra = lambda f: {'queryset':
#             f.rel.to._default_manager.complex_filter(f.rel.limit_choices_to),
#              'to_field_name': f.rel.field_name})
    registration_number = django_filters.CharFilter(
            action=registration_number_filter)

    class Meta:
        model = MuseumObject
        fields = ['registration_number', 'functional_category', 'category',
                'artefact_type', 'loan_status', 'access_status',
                'record_status', 'raw_material', 'description',
                'storage_section', 'storage_unit', 'storage_bay',
                'storage_shelf_box_drawer', 'acquisition_date',
                'acquisition_method', 'cultural_bloc', 'donor', 'collector',
                'collections__title'
                ]


class ColumnForm(forms.Form):
    """For selecting which columns are included in search results"""
    columns = forms.MultipleChoiceField(
            widget=forms.SelectMultiple(attrs={'size': '15'}))
    #widget=forms.CheckboxSelectMultiple)

    def __init__(self, model, *args, **kwargs):
        self.model = model
        super(ColumnForm, self).__init__(*args, **kwargs)
        self.fields['columns'].choices = [(f.name, f.verbose_name.title())
            for f in model._meta.fields]

    def get_excluded_names(self):
        """Return list of field names to exclude"""
        desired_fields = self.get_desired_field_names()
        all_fields = [f for f in self.model._meta.fields]

        return [field.name
                for field in all_fields if field.name not in desired_fields]

    def get_desired_fields(self):
        """Return list of fields (not just field name)"""
        desired_fields = self.get_desired_field_names()
        all_fields = [f for f in MuseumObject._meta.fields]

        return [field for field in all_fields if field.name in
                desired_fields]

    def get_desired_field_names(self):
        desired_fields = []
        if self.is_valid():
            desired_fields = self.cleaned_data['columns']
{'fraction_non_alphanumeric': 0.10806797558158719, 'fraction_numerical': 0.005609635373700709, 'mean_word_length': 3.054849498327759, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1829537', 'n_tokens_mistral': 1742, 'n_tokens_neox': 1680, 'n_words': 397}