<commit_before>from builtins import object
from datetime import timedelta

from bluebottle.exports.exporter import ExportModelResource
from bluebottle.impact.models import ImpactType
from bluebottle.initiatives.models import InitiativePlatformSettings
from bluebottle.segments.models import SegmentType


class ImpactMixin(object):

    def get_extra_fields(self):
        return super(ImpactMixin, self).get_extra_fields() + tuple([
            ("impact:{}".format(impact.slug), impact.name)
            for impact in ImpactType.objects.filter(active=True).all()
        ])


class SegmentMixin(object):
    segment_field = None

    def get_extra_fields(self):
        return super(SegmentMixin, self).get_extra_fields() + tuple([
            ("segment:{}".format(segment.slug), segment.name)
            for segment in SegmentType.objects.filter(is_active=True).all()
        ])


class DateRangeResource(ExportModelResource):
    range_field = 'created'
    select_related = None

    def get_queryset(self):
        qs = super(DateRangeResource, self).get_queryset()
        if self.select_related:
            qs = qs.select_related(*self.select_related)
        frm, to = self.kwargs.get('from_date'), self.kwargs.get('to_date')
        to = to + timedelta(days=1)
        return qs.filter(**{'%s__range' % self.range_field: (frm, to)})


class UserResource(SegmentMixin, DateRangeResource):
    range_field = 'date_joined'
    select_related = ('location', 'location__group')

    def get_queryset(self):
        return super(UserResource, self).get_queryset().exclude(email='<EMAIL>')


class InitiativeResource(DateRangeResource):
    select_related = (<commit_msg>Fix illegal characters in exports<commit_after>from builtins import object
from datetime import timedelta

from bluebottle.exports.exporter import ExportModelResource
from bluebottle.impact.models import ImpactType
from bluebottle.initiatives.models import InitiativePlatformSettings
from bluebottle.segments.models import SegmentType

from openpyxl.cell.cell import ILLEGAL_CHARACTERS_RE


class ImpactMixin(object):

    def get_extra_fields(self):
        return super(ImpactMixin, self).get_extra_fields() + tuple([
            ("impact:{}".format(impact.slug), impact.name)
            for impact in ImpactType.objects.filter(active=True).all()
        ])


class SegmentMixin(object):
    segment_field = None

    def get_extra_fields(self):
        return super(SegmentMixin, self).get_extra_fields() + tuple([
            ("segment:{}".format(segment.slug), segment.name)
            for segment in SegmentType.objects.filter(is_active=True).all()
        ])


class DateRangeResource(ExportModelResource):
    range_field = 'created'
    select_related = None

    def export_field(self, field, obj):
        result = super().export_field(field, obj)

        if type(result) == str:
            result = ILLEGAL_CHARACTERS_RE.sub('', result)

        return result

    def get_queryset(self):
        qs = super().get_queryset()
        if self.select_related:
            qs = qs.select_related(*self.select_related)
        frm, to = self.kwargs.get('from_date'), self.kwargs.get('to_date')
        to = to + timedelta(days=1)
        return qs.filter(**{'%s__range' % self.range_field: (frm, to)})


class UserResource(SegmentMixin, DateRangeResource):
    range_field = 'date_joined'
    select_related = ('location', 'location__group')

    def get_queryset(self):
        return super(UserResource, self).get_queryset().exclude(email='<EMAIL>')


class InitiativeResource(DateRangeResource):
    select_related = (
{'fraction_non_alphanumeric': 0.11006376490158026, 'fraction_numerical': 0.0005544774050457444, 'mean_word_length': 3.7598944591029024, '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': '5426022', 'n_tokens_mistral': 1098, 'n_tokens_neox': 1050, 'n_words': 216}