<commit_before># -*- coding: UTF-8 -*-
from django import forms
from django import http
from django import template
from django.conf import settings as dsettings
from django.conf.urls.defaults import url, patterns
from django.contrib import admin
from django.core import urlresolvers
from django.db import transaction
from django.db.models import Q
from django.shortcuts import get_object_or_404, redirect, render_to_response
from django.utils.safestring import mark_safe
from assopy import models, settings
if settings.GENRO_BACKEND:
    from assopy.clients import genro
from collections import defaultdict
from datetime import datetime

class CountryAdmin(admin.ModelAdmin):
    list_display = ('printable_name', 'vat_company', 'vat_company_verify', 'vat_person')
    list_editable = ('vat_company', 'vat_company_verify', 'vat_person')
    search_fields = ('name', 'printable_name', 'iso', 'numcode')

admin.site.register(models.Country, CountryAdmin)

class ReadOnlyWidget(forms.widgets.HiddenInput):

    def __init__(self, display=None, *args, **kwargs):
        self.display = display
        super(ReadOnlyWidget, self).__init__(*args, **kwargs)

    def render(self, name, value, attrs=None):
        output = []
        output.append(u'<span>%s</span>' % (self.display or value))
        output.append(super(ReadOnlyWidget, self).render(name, value, attrs))
        return mark_safe(u''.join(output))

class OrderItemAdminForm(forms.ModelForm):
    class Meta:
        model = models.OrderItem

    def __init__(self, *args, **kwargs):
        super(OrderItemAdminForm, self).__init__(*args, **kwargs)
        from conference.models import Ticket
        self.fields['ticket'].queryset = Ticket.objects.all().select_related('fare')
        instance = kwargs.get('instance',None)
        if instance and instance.order.invoices.exclude(payment_date=None).exists():
            # se ho emesso un invoice impedisco di variare alcuni campi degli gli order items
            for f in ('ticket', 'price', 'vat', 'code'):
                self.fields[f].widget = ReadOnlyWidget(display = getattr(self.instance, f))

class OrderItemInlineAdmin(admin.TabularInline):
    model = models.OrderItem
    form = OrderItemAdminForm

    def get_formset(self, request, obj=None, **kwargs):
        # se ho emesso un invoice impedisco di variare gli order items
        if obj and obj.invoices.exclude(payment_date=None).exists():
            self.can_delete = False
            self.max_num = obj.invoices.exclude(payment_date=None).count()
        else:
            self.can_delete = True
            self.max_num = None
        return super(OrderItemInlineAdmin, self).get_formset(request, obj, **kwargs)

class OrderAdminForm(forms.ModelForm):
    method = forms.ChoiceField(choices=(
        ('admin', 'Admin'),
        ('paypal', 'PayPal'),
        ('cc', 'Credit Card'),
        ('bank', 'Bank'),
    ))
    class Meta:
        model = models.Order
        exclude = ('method',)

    def __init__(self, *args, **kwargs):
        super(OrderAdminForm, self).__init__(*args, **kwargs)
        self.fields['user'].queryset = models.User.objects.all().select_related('user')
        if self.initial:
            self.fields['method'].initial = self.instance.method

    def clean_assopy_id(self):
        aid = self.cleaned_data.get('assopy_id')
        if aid == '':
            aid = None
        return aid

    def save(self, *args, **kwargs):
        self.instance.method = self.cleaned_data['method']
        return super(OrderAdminForm, self).save(*args, **kwargs)

class OrderAdmin(admin.ModelAdmin):
    list_display = (
        'code', '_user', '_email',
        '_created', 'method',
        '_items', '_complete', '_invoice',
        '_total_nodiscount', '_discount', '_total_payed',
    )
    list_select_related = True
    list_filter = ('method', '_complete',)
    list_per_page = 20
    search_fields = (
        'code', 'card_name',
        'user__user__first_name', 'user__user__last_name', 'user__user__email',
        'billing_notes',
    )
    date_hierarchy = 'created'
    actions = ('do_edit_invoices',)

    form = OrderAdminForm

    inlines = (
        OrderItemInlineAdmin,
    )

    def has_delete_permission(self, request, obj=None):
        # se ho emesso un invoice impedisco di cancellare l'ordine<commit_msg>alcuni raw fields per l'order in modo da caricare la pagina di admin piu' velocemente<commit_after># -*- coding: UTF-8 -*-
from django import forms
from django import http
from django import template
from django.conf import settings as dsettings
from django.conf.urls.defaults import url, patterns
from django.contrib import admin
from django.core import urlresolvers
from django.db import transaction
from django.db.models import Q
from django.shortcuts import get_object_or_404, redirect, render_to_response
from django.utils.safestring import mark_safe
from assopy import models, settings
if settings.GENRO_BACKEND:
    from assopy.clients import genro
from collections import defaultdict
from datetime import datetime

class CountryAdmin(admin.ModelAdmin):
    list_display = ('printable_name', 'vat_company', 'vat_company_verify', 'vat_person')
    list_editable = ('vat_company', 'vat_company_verify', 'vat_person')
    search_fields = ('name', 'printable_name', 'iso', 'numcode')

admin.site.register(models.Country, CountryAdmin)

class ReadOnlyWidget(forms.widgets.HiddenInput):

    def __init__(self, display=None, *args, **kwargs):
        self.display = display
        super(ReadOnlyWidget, self).__init__(*args, **kwargs)

    def render(self, name, value, attrs=None):
        output = []
        output.append(u'<span>%s</span>' % (self.display or value))
        output.append(super(ReadOnlyWidget, self).render(name, value, attrs))
        return mark_safe(u''.join(output))

class OrderItemAdminForm(forms.ModelForm):
    class Meta:
        model = models.OrderItem

    def __init__(self, *args, **kwargs):
        super(OrderItemAdminForm, self).__init__(*args, **kwargs)
        from conference.models import Ticket
        self.fields['ticket'].queryset = Ticket.objects.all().select_related('fare')
        instance = kwargs.get('instance',None)
        if instance and instance.order.invoices.exclude(payment_date=None).exists():
            # se ho emesso un invoice impedisco di variare alcuni campi degli gli order items
            for f in ('ticket', 'price', 'vat', 'code'):
                self.fields[f].widget = ReadOnlyWidget(display = getattr(self.instance, f))

class OrderItemInlineAdmin(admin.TabularInline):
    model = models.OrderItem
    form = OrderItemAdminForm
    raw_id_fields = ('ticket',)

    def get_formset(self, request, obj=None, **kwargs):
        # se ho emesso un invoice impedisco di variare gli order items
        if obj and obj.invoices.exclude(payment_date=None).exists():
            self.can_delete = False
            self.max_num = obj.invoices.exclude(payment_date=None).count()
        else:
            self.can_delete = True
            self.max_num = None
        return super(OrderItemInlineAdmin, self).get_formset(request, obj, **kwargs)

class OrderAdminForm(forms.ModelForm):
    method = forms.ChoiceField(choices=(
        ('admin', 'Admin'),
        ('paypal', 'PayPal'),
        ('cc', 'Credit Card'),
        ('bank', 'Bank'),
    ))
    class Meta:
        model = models.Order
        exclude = ('method',)

    def __init__(self, *args, **kwargs):
        super(OrderAdminForm, self).__init__(*args, **kwargs)
        self.fields['user'].queryset = models.User.objects.all().select_related('user')
        if self.initial:
            self.fields['method'].initial = self.instance.method

    def clean_assopy_id(self):
        aid = self.cleaned_data.get('assopy_id')
        if aid == '':
            aid = None
        return aid

    def save(self, *args, **kwargs):
        self.instance.method = self.cleaned_data['method']
        return super(OrderAdminForm, self).save(*args, **kwargs)

class OrderAdmin(admin.ModelAdmin):
    list_display = (
        'code', '_user', '_email',
        '_created', 'method',
        '_items', '_complete', '_invoice',
        '_total_nodiscount', '_discount', '_total_payed',
    )
    list_select_related = True
    list_filter = ('method', '_complete',)
    raw_id_fields = ('user',)
    search_fields = (
        'code', 'card_name',
        'user__user__first_name', 'user__user__last_name', 'user__user__email',
        'billing_notes',
    )
    date_hierarchy = 'created'
    actions = ('do_edit_invoices',)

    form = OrderAdminForm

    inlines = (
        OrderItemInlineAdmin,
    )

    def has_delete_permission(self, request, obj=None):
        # se ho emesso un invoice impedisco di cancellare l'ordine
{'fraction_non_alphanumeric': 0.1205463858850313, 'fraction_numerical': 0.0011383039271485487, 'mean_word_length': 3.336623889437315, 'pattern_counts': {'":': 0, '<': 7, '<?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': '2432710', 'n_tokens_mistral': 2614, 'n_tokens_neox': 2572, 'n_words': 631}