<commit_before>import csv
import datetime

from daterange_filter.filter import DateRangeFilter
from django.contrib import admin
from django.contrib.auth.models import User
from django.http import HttpResponse
from django.utils.timezone import localtime
from django.conf import settings
from django.contrib.staticfiles.templatetags.staticfiles import static
from django.db.models import Q
from django.shortcuts import redirect
from django.utils.translation import ugettext_lazy as _

from gem.models import GemUserProfile, GemCommentReport
from gem.tasks import send_export_email_gem

from import_export.fields import Field
from import_export.widgets import DateTimeWidget, DateWidget, ForeignKeyWidget
from import_export.resources import ModelResource, Diff
from import_export.results import RowResult

from molo.commenting.admin import MoloCommentAdmin
from molo.commenting.models import MoloComment
from molo.profiles.admin import FrontendUsersModelAdmin, FrontendUsersDateRangeFilter
from molo.profiles.admin import ProfileUserAdmin
from molo.profiles.admin_import_export import FrontendUsersResource
from molo.profiles.admin_views import FrontendUsersAdminView
from molo.profiles.models import UserProfile
from molo.surveys.admin import SegmentUserGroupAdmin
from molo.surveys.models import SegmentUserGroup

from wagtail.contrib.modeladmin.views import IndexView
from wagtail.contrib.modeladmin.helpers import PermissionHelper
from wagtail.wagtailadmin import messages
from wagtail.wagtailcore.models import Site

from .admin_forms import FrontEndAgeToDateOfBirthFilter, UserListForm


def download_as_csv_gem(self, request, queryset):
    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment;filename=export.csv'
    writer = csv.writer(response)
    user_model_fields = ('id', 'username', 'is_active', 'last_login')
    profile_fields = ('date_of_birth',)
    gem_profile_fields = ('gender',)
    field_names = user_model_fields + profile_fields + gem_profile_fields
    writer.writerow(field_names)
    for obj in queryset:
        if hasattr(obj, 'gem_profile'):
            if obj.profile.alias:
                obj.profile.alias = obj.profile.alias.encode('utf-8')
            obj.username = obj.username.encode('utf-8')
            obj.date_joined = obj.date_joined.strftime("%Y-%m-%d %H:%M")
            writer.writerow(
                [getattr(obj, field) for field in user_model_fields] +
                [getattr(obj.profile, field) for field in profile_fields] +
                [getattr(
                    obj.gem_profile, field) for field in gem_profile_fields])
    return response
download_as_csv_gem.short_description = "Download selected as csv gem"


class GemUserProfileInlineModelAdmin(admin.StackedInline):
    model = GemUserProfile
    can_delete = False


class UserProfileInlineModelAdmin(admin.StackedInline):
    model = UserProfile
    can_delete = False


class GemCommentReportModelAdmin(admin.StackedInline):
    model = GemCommentReport
    can_delete = True
    max_num = 0
    actions = None
    readonly_fields = ["user", "reported_reason", ]


class GemFrontendUsersResource(FrontendUsersResource):
        gender = Field()

        class Meta:
            export_order = FrontendUsersResource.Meta.export_order + (
                'gender',)

        def dehydrate_gender(self, user):
            if hasattr(user, 'gem_profile'):
                return user.gem_profile.get_gender_display() if hasattr(
                    user, 'gem_profile') else ''
            return None


class TzDateTimeWidget(DateTimeWidget):

    def render(self, value, obj):
        if settings.USE_TZ:
            value = localtime(value)
        return super(TzDateTimeWidget, self).render(value, obj)<commit_msg>Update the fields which get exported in the email<commit_after>import csv
import datetime

from daterange_filter.filter import DateRangeFilter
from django.contrib import admin
from django.contrib.auth.models import User
from django.http import HttpResponse
from django.utils.timezone import localtime
from django.conf import settings
from django.contrib.staticfiles.templatetags.staticfiles import static
from django.db.models import Q
from django.shortcuts import redirect
from django.utils.translation import ugettext_lazy as _

from gem.models import GemUserProfile, GemCommentReport
from gem.tasks import send_export_email_gem

from import_export.fields import Field
from import_export.widgets import DateTimeWidget, DateWidget, ForeignKeyWidget
from import_export.resources import ModelResource, Diff
from import_export.results import RowResult

from molo.commenting.admin import MoloCommentAdmin
from molo.commenting.models import MoloComment
from molo.profiles.admin import FrontendUsersModelAdmin, FrontendUsersDateRangeFilter
from molo.profiles.admin import ProfileUserAdmin
from molo.profiles.admin_import_export import FrontendUsersResource
from molo.profiles.admin_views import FrontendUsersAdminView
from molo.profiles.models import UserProfile
from molo.surveys.admin import SegmentUserGroupAdmin
from molo.surveys.models import SegmentUserGroup

from wagtail.contrib.modeladmin.views import IndexView
from wagtail.contrib.modeladmin.helpers import PermissionHelper
from wagtail.wagtailadmin import messages
from wagtail.wagtailcore.models import Site

from .admin_forms import FrontEndAgeToDateOfBirthFilter, UserListForm


def download_as_csv_gem(self, request, queryset):
    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment;filename=export.csv'
    writer = csv.writer(response)
    user_model_fields = ('id', 'username', 'is_active', 'last_login')
    profile_fields = ('date_of_birth',)
    gem_profile_fields = ('gender',)
    field_names = user_model_fields + profile_fields + gem_profile_fields
    writer.writerow(field_names)
    for obj in queryset:
        if hasattr(obj, 'gem_profile'):
            if obj.profile.alias:
                obj.profile.alias = obj.profile.alias.encode('utf-8')
            obj.username = obj.username.encode('utf-8')
            obj.date_joined = obj.date_joined.strftime("%Y-%m-%d %H:%M")
            writer.writerow(
                [getattr(obj, field) for field in user_model_fields] +
                [getattr(obj.profile, field) for field in profile_fields] +
                [getattr(
                    obj.gem_profile, field) for field in gem_profile_fields])
    return response
download_as_csv_gem.short_description = "Download selected as csv gem"


class GemUserProfileInlineModelAdmin(admin.StackedInline):
    model = GemUserProfile
    can_delete = False


class UserProfileInlineModelAdmin(admin.StackedInline):
    model = UserProfile
    can_delete = False


class GemCommentReportModelAdmin(admin.StackedInline):
    model = GemCommentReport
    can_delete = True
    max_num = 0
    actions = None
    readonly_fields = ["user", "reported_reason", ]


class GemFrontendUsersResource(FrontendUsersResource):
        gender = Field()

        class Meta:
            fields = ('id', 'username', 'date_of_birth',
                      'is_active', 'last_login', 'gender')

            export_order = ('id', 'username', 'date_of_birth',
                            'is_active', 'last_login', 'gender')

        def dehydrate_gender(self, user):
            if hasattr(user, 'gem_profile'):
                return user.gem_profile.get_gender_display() if hasattr(
                    user, 'gem_profile') else ''
            return None


class TzDateTimeWidget(DateTimeWidget):

    def render(self, value, obj):
        if settings.USE_TZ:
            value = localtime(value)
        return super(TzDateTimeWidget, self).render(value, obj)
{'fraction_non_alphanumeric': 0.0757126273700503, 'fraction_numerical': 0.000773893976525216, 'mean_word_length': 3.889029003783102, '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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1816234', 'n_tokens_mistral': 2184, 'n_tokens_neox': 2130, 'n_words': 542}