<commit_before>from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
from .models import CustomUser
from management.admin import MembershipInline
from django.utils.translation import ugettext as _
from django.http import HttpResponse
from PIL import Image, ImageDraw, ImageFont

admin.site.unregister(User)

@admin.register(CustomUser)
class CustomUserAdmin(admin.ModelAdmin):
    model = CustomUser
    can_delete = False
    inlines = [MembershipInline,]
    exclude = ('position',)
    actions = ['print_cards',]
    fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('user', 'id_photo', 'nickname', 'birthdate', 'gender')}
        ),
        (_('Competition'), {
            'classes': ('wide','collapse'),
            'fields': ('competition_license', 'competition_expiration')}
        ),
        (_('Miscellaneous'), {
            'classes': ('wide','collapse'),
            'fields': ('phone', 'size')}
        ),
        (_('Privacy Scopes'), {
            'classes': ('wide', 'collapse'),
            'fields': ('mail_scope', 'phone_scope', 'global_scope',
                'diffusion_authorisation')}
        )
    )


    #TODO: Collapse inlines

    #TODO: Print multiple cards one below each other.
    def print_cards(self, request, queryset):
        for customuser in queryset:
            if not customuser.is_member():
                continue
            im = Image.open('static/member_card.png')
            width = 900
            height = 570
            header_height = 120.0
            interspace = 27.0
            big_width = 210.0
            diff_big_small_width = 18.0
            info_height = 69
            color1 = (52,152,219)
            info = [str(customuser.last_membership().expiration_date),
                    str(customuser.user.id), customuser.user.first_name,
                    customuser.user.last_name]
            id_photo = Image.open(customuser.id_photo.path)
            font = ImageFont.truetype('DroidSansMono.ttf', 36)
            id_ratio = 45/35

            #Crop the photo to match an id photo ratio.
            if id_photo.height / id_photo.width > id_ratio:
                new_height = id_photo.width * id_ratio
                diff_height = (id_photo.height - new_height)
                id_photo = id_photo.crop((0, int(diff_height/2), id_photo.width,
                    int(id_photo.height - diff_height/2)))
            elif id_photo.height / id_photo.width < id_ratio:
                new_width = id_photo.height / id_ratio
                diff_width = (id_photo.width - new_width)
                id_photo = id_photo.crop((int(diff_width/2), 0,
                    int(id_photo.width - diff_width/2), id_photo.height))

            height_id = int(height - header_height - 75)
            width_id = int(height_id / id_ratio)
            id_photo = id_photo.resize((width_id, height_id))

            draw = ImageDraw.Draw(im)

            space = interspace + header_height
            for value in info:
                #Adapt font size to available space.
                font_size = 45
                font = ImageFont.truetype('DroidSansMono.ttf', font_size)
                (value_width, value_height) = draw.textsize(value, font=font)
                while value_width > width - (big_width + width_id):
                    if font_size == 1:
                        break
                    font_size -= 1
                    font = ImageFont.truetype('DroidSansMono.ttf', font_size)
                    (value_width, value_height) = draw.textsize(value, font=font)

                draw.text((2 + big_width, space + \
                            (info_height - value_height ) / 2), value,
                            fill=color1, font=font)
                space = space + interspace + info_height
                big_width = big_width - diff_big_small_width - \
                    (diff_big_small_width) * (interspace / info_height)


            im.paste(id_photo, (width - id_photo.width - 2, int(header_height + 5)))

            del draw
            response = HttpResponse(content_type='image/png')
            im.save(response, 'PNG')
            return response


    print_cards.short_description = _('Print member cards for the selected users.')

class CustomUserInline(admin.StackedInline):
    model = CustomUser
    can_delete = False
    exclude = ('position',)

@admin.register(User)
class UserAdmin(UserAdmin):
    inlines = (CustomUserInline,)
    can_delete = False<commit_msg>Remove group and user of contrib packages from admin.<commit_after>from django.contrib import admin
from django.contrib.auth.models import (User, Group)
from django.contrib.auth.admin import UserAdmin
from .models import CustomUser
from management.admin import MembershipInline
from django.utils.translation import ugettext as _
from django.http import HttpResponse
from PIL import Image, ImageDraw, ImageFont

admin.site.unregister(User)
admin.site.unregister(Group)

@admin.register(CustomUser)
class CustomUserAdmin(admin.ModelAdmin):
    model = CustomUser
    can_delete = False
    inlines = [MembershipInline,]
    exclude = ('position',)
    actions = ['print_cards',]
    fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('user', 'id_photo', 'nickname', 'birthdate', 'gender')}
        ),
        (_('Competition'), {
            'classes': ('wide','collapse'),
            'fields': ('competition_license', 'competition_expiration')}
        ),
        (_('Miscellaneous'), {
            'classes': ('wide','collapse'),
            'fields': ('phone', 'size')}
        ),
        (_('Privacy Scopes'), {
            'classes': ('wide', 'collapse'),
            'fields': ('mail_scope', 'phone_scope', 'global_scope',
                'diffusion_authorisation')}
        )
    )


    #TODO: Collapse inlines

    #TODO: Print multiple cards one below each other.
    def print_cards(self, request, queryset):
        for customuser in queryset:
            if not customuser.is_member():
                continue
            im = Image.open('static/member_card.png')
            width = 900
            height = 570
            header_height = 120.0
            interspace = 27.0
            big_width = 210.0
            diff_big_small_width = 18.0
            info_height = 69
            color1 = (52,152,219)
            info = [str(customuser.last_membership().expiration_date),
                    str(customuser.user.id), customuser.user.first_name,
                    customuser.user.last_name]
            id_photo = Image.open(customuser.id_photo.path)
            font = ImageFont.truetype('DroidSansMono.ttf', 36)
            id_ratio = 45/35

            #Crop the photo to match an id photo ratio.
            if id_photo.height / id_photo.width > id_ratio:
                new_height = id_photo.width * id_ratio
                diff_height = (id_photo.height - new_height)
                id_photo = id_photo.crop((0, int(diff_height/2), id_photo.width,
                    int(id_photo.height - diff_height/2)))
            elif id_photo.height / id_photo.width < id_ratio:
                new_width = id_photo.height / id_ratio
                diff_width = (id_photo.width - new_width)
                id_photo = id_photo.crop((int(diff_width/2), 0,
                    int(id_photo.width - diff_width/2), id_photo.height))

            height_id = int(height - header_height - 75)
            width_id = int(height_id / id_ratio)
            id_photo = id_photo.resize((width_id, height_id))

            draw = ImageDraw.Draw(im)

            space = interspace + header_height
            for value in info:
                #Adapt font size to available space.
                font_size = 45
                font = ImageFont.truetype('DroidSansMono.ttf', font_size)
                (value_width, value_height) = draw.textsize(value, font=font)
                while value_width > width - (big_width + width_id):
                    if font_size == 1:
                        break
                    font_size -= 1
                    font = ImageFont.truetype('DroidSansMono.ttf', font_size)
                    (value_width, value_height) = draw.textsize(value, font=font)

                draw.text((2 + big_width, space + \
                            (info_height - value_height ) / 2), value,
                            fill=color1, font=font)
                space = space + interspace + info_height
                big_width = big_width - diff_big_small_width - \
                    (diff_big_small_width) * (interspace / info_height)


            im.paste(id_photo, (width - id_photo.width - 2, int(header_height + 5)))

            del draw
            response = HttpResponse(content_type='image/png')
            im.save(response, 'PNG')
            return response


    print_cards.short_description = _('Print member cards for the selected users.')

class CustomUserInline(admin.StackedInline):
    model = CustomUser
    can_delete = False
    exclude = ('position',)
{'fraction_non_alphanumeric': 0.10459518599562363, 'fraction_numerical': 0.011816192560175055, 'mean_word_length': 1.9157894736842105, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '3046341', 'n_tokens_mistral': 2694, 'n_tokens_neox': 2541, 'n_words': 568}