<commit_before># -*- coding: UTF-8 -*-
from django import forms
from django import http
from django import template
from django.conf import settings
from django.conf.urls.defaults import url, patterns
from django.contrib import admin
from django.core import mail
from django.core.urlresolvers import reverse
from django.shortcuts import render_to_response, redirect
from conference import admin as cadmin
from conference import models as cmodels
from p3 import models
from p3 import dataaccess

import csv
from cStringIO import StringIO

class TicketConferenceAdmin(cadmin.TicketAdmin):
    list_display = cadmin.TicketAdmin.list_display + ('_order', '_assigned', '_tagline',)
    list_filter = cadmin.TicketAdmin.list_filter + ('orderitem__order___complete',)

    class Media:
        js = ('p5/j/jquery-flot/jquery.flot.js',)

    def _order(self, o):
        return o.orderitem.order.code

    def _assigned(self, o):
        if o.p3_conference:
            return o.p3_conference.assigned_to
        else:
            return ''

    def _tagline(self, o):
        if o.p3_conference:
            return o.p3_conference.tagline
        else:
            return ''

    def changelist_view(self, request, extra_context=None):
        if not request.GET:
            q = request.GET.copy()
            q['fare__conference'] = settings.CONFERENCE_CONFERENCE
            q['fare__ticket_type__exact'] = 'conference'
            q['orderitem__order___complete__exact'] = 1
            request.GET = q
            request.META['QUERY_STRING'] = request.GET.urlencode()
        return super(TicketConferenceAdmin,self).changelist_view(request, extra_context=extra_context)

    def queryset(self, request):
        qs = super(TicketConferenceAdmin, self).queryset(request)
        qs = qs.select_related('orderitem__order', 'p3_conference', 'user', 'fare', )
        return qs

    def get_urls(self):
        urls = super(TicketConferenceAdmin, self).get_urls()
        my_urls = patterns('',
            url(r'^stats/data/$', self.admin_site.admin_view(self.stats_data), name='p3-ticket-stats-data'),
            #url(r'^stats/details/$', self.admin_site.admin_view(self.stats_details), name='p3-ticket-stats-details'),
            #url(r'^stats/details/csv$', self.admin_site.admin_view(self.stats_details_csv), name='p3-ticket-stats-details-csv'),
        )
        return my_urls + urls

    def stats_data(self, request):
        from conference.views import json_dumps
        from django.db.models import Q
        from collections import defaultdict
        from microblog.models import PostContent
        import datetime

        conferences = cmodels.Conference.objects\
            .order_by('conference_start')

        output = {}
        for c in conferences:
            tickets = cmodels.Ticket.objects\
                .filter(fare__conference=c)\
                .filter(Q(orderitem__order___complete=True) | Q(orderitem__order__method__in=('bank', 'admin')))\
                .select_related('fare', 'orderitem__order')
            data = {
                'conference': defaultdict(lambda: 0),
                'partner': defaultdict(lambda: 0),
                'event': defaultdict(lambda: 0),
                'other': defaultdict(lambda: 0),
            }
            for t in tickets:
                tt = t.fare.ticket_type
                date = t.orderitem.order.created.date()
                offset = date - c.conference_start
                data[tt][offset.days] += 1

            for k, v in data.items():
                data[k] = sorted(v.items())


            dlimit = datetime.date(c.conference_start.year, 1, 1)
            deadlines = cmodels.DeadlineContent.objects\
                .filter(language='en')\
                .filter(deadline__date__lte=c.conference_start, deadline__date__gte=dlimit)\
                .select_related('deadline')\
                .order_by('deadline__date')
            markers = [ ((d.deadline.date - c.conference_start).days, 'CAL: ' + (d.headline or d.body)) for d in deadlines ]

            posts = PostContent.objects\
                .filter(language='en')\
                .filter(post__date__lte=c.conference_start, post__date__gte=dlimit)\
                .filter(post__status='P')\
                .select_related('post')\
                .order_by('post__date')
            markers += [ ((d.post.date.date() - c.conference_start).days, 'BLOG: ' + d.headline) for d in posts ]

            output[c.code] = {
                'data': data,
                'markers': markers,
            }

        return http.HttpResponse(json_dumps(output), 'text/javascript')

    def stats_details_csv(self, request):
        code = request.GET['code']
        conference = request.GET['conference']
        stats = self.stats(conference, stat=code)
        if not stats:
            raise http.Http404()
        columns = (
            'attendee', 'attendee_email',
            'buyer', 'buyer_email',
            'order',
        )
        buff = StringIO()
        writer = csv.DictWriter(buff, columns)
        writer.writerow(dict(zip(columns, columns)))
        for ticket in stats[0]['details']:
            try:
                p3c = ticket.p3_conference
            except models.TicketConference.DoesNotExist:
                p3c = None
            row = {
                'attendee': ticket.name,
                'attendee_email': p3c.assigned_to if p3c else '',
                'buyer': ticket.orderitem.order.user.name(),
                'buyer_email': ticket.orderitem.order.user.user.email,
                'order': ticket.orderitem.order.code,
            }
            for k, v in row.items():
                try:
                    row[k] = v.encode('utf-8')
                except:
                    pass
            writer.writerow(row)
        return http.HttpResponse(buff.getvalue(), mimetype="text/csv")

admin.site.unregister(cmodels.Ticket)
admin.site.register(cmodels.Ticket, TicketConferenceAdmin)
<commit_msg>codice vecchio, la funzionalità è stata completamwente inglobata in conference<commit_after># -*- coding: UTF-8 -*-
from django import forms
from django import http
from django import template
from django.conf import settings
from django.conf.urls.defaults import url, patterns
from django.contrib import admin
from django.core import mail
from django.core.urlresolvers import reverse
from django.shortcuts import render_to_response, redirect
from conference import admin as cadmin
from conference import models as cmodels
from p3 import models
from p3 import dataaccess

class TicketConferenceAdmin(cadmin.TicketAdmin):
    list_display = cadmin.TicketAdmin.list_display + ('_order', '_assigned', '_tagline',)
    list_filter = cadmin.TicketAdmin.list_filter + ('orderitem__order___complete',)

    class Media:
        js = ('p5/j/jquery-flot/jquery.flot.js',)

    def _order(self, o):
        return o.orderitem.order.code

    def _assigned(self, o):
        if o.p3_conference:
            return o.p3_conference.assigned_to
        else:
            return ''

    def _tagline(self, o):
        if o.p3_conference:
            return o.p3_conference.tagline
        else:
            return ''

    def changelist_view(self, request, extra_context=None):
        if not request.GET:
            q = request.GET.copy()
            q['fare__conference'] = settings.CONFERENCE_CONFERENCE
            q['fare__ticket_type__exact'] = 'conference'
            q['orderitem__order___complete__exact'] = 1
            request.GET = q
            request.META['QUERY_STRING'] = request.GET.urlencode()
        return super(TicketConferenceAdmin,self).changelist_view(request, extra_context=extra_context)

    def queryset(self, request):
        qs = super(TicketConferenceAdmin, self).queryset(request)
        qs = qs.select_related('orderitem__order', 'p3_conference', 'user', 'fare', )
        return qs

    def get_urls(self):
        urls = super(TicketConferenceAdmin, self).get_urls()
        my_urls = patterns('',
            url(r'^stats/data/$', self.admin_site.admin_view(self.stats_data), name='p3-ticket-stats-data'),
        )
        return my_urls + urls

    def stats_data(self, request):
        from conference.views import json_dumps
        from django.db.models import Q
        from collections import defaultdict
        from microblog.models import PostContent
        import datetime

        conferences = cmodels.Conference.objects\
            .order_by('conference_start')

        output = {}
        for c in conferences:
            tickets = cmodels.Ticket.objects\
                .filter(fare__conference=c)\
                .filter(Q(orderitem__order___complete=True) | Q(orderitem__order__method__in=('bank', 'admin')))\
                .select_related('fare', 'orderitem__order')
            data = {
                'conference': defaultdict(lambda: 0),
                'partner': defaultdict(lambda: 0),
                'event': defaultdict(lambda: 0),
                'other': defaultdict(lambda: 0),
            }
            for t in tickets:
                tt = t.fare.ticket_type
                date = t.orderitem.order.created.date()
                offset = date - c.conference_start
                data[tt][offset.days] += 1

            for k, v in data.items():
                data[k] = sorted(v.items())


            dlimit = datetime.date(c.conference_start.year, 1, 1)
            deadlines = cmodels.DeadlineContent.objects\
                .filter(language='en')\
                .filter(deadline__date__lte=c.conference_start, deadline__date__gte=dlimit)\
                .select_related('deadline')\
                .order_by('deadline__date')
            markers = [ ((d.deadline.date - c.conference_start).days, 'CAL: ' + (d.headline or d.body)) for d in deadlines ]

            posts = PostContent.objects\
                .filter(language='en')\
                .filter(post__date__lte=c.conference_start, post__date__gte=dlimit)\
                .filter(post__status='P')\
                .select_related('post')\
                .order_by('post__date')
            markers += [ ((d.post.date.date() - c.conference_start).days, 'BLOG: ' + d.headline) for d in posts ]

            output[c.code] = {
                'data': data,
                'markers': markers,
            }

        return http.HttpResponse(json_dumps(output), 'text/javascript')

admin.site.unregister(cmodels.Ticket)
admin.site.register(cmodels.Ticket, TicketConferenceAdmin)

{'fraction_non_alphanumeric': 0.10985567793391568, 'fraction_numerical': 0.004557538928978352, 'mean_word_length': 2.206392694063927, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6499202', 'n_tokens_mistral': 3153, 'n_tokens_neox': 3013, 'n_words': 620}