<commit_before># -*- coding: utf-8 -*-
""" Print out a JSON of accepted talks with the abstracts

"""
from   django.core.management.base import BaseCommand, CommandError
from   django.core  import urlresolvers
from   conference   import models
from   conference   import utils

from   p3           import models as p3_models
from   assopy       import models as assopy_models

from   collections  import defaultdict, OrderedDict
from   optparse     import make_option
import operator
import simplejson   as json
import traceback

### Globals

TYPE_NAMES = (
    ('keynote', 'Keynotes'),
    ('s', 'Talks'),
    ('t', 'Trainings'),
    ('p', 'Poster sessions'),
    ('h', 'Help desks'),
    ('europython', 'EuroPython sessions'),
    ('i', 'Other sessions'),
    )

def _check_talk_types(type_names):
    d = dict(type_names)
    for code, entry in models.TALK_TYPE:
        assert code in d, 'Talk type code %r is missing' % code
_check_talk_types(TYPE_NAMES)

### Helpers
def speaker_listing(talk):
    return u', '.join(
        u'{} {}'.format(speaker.user.first_name, speaker.user.last_name) for speaker in talk.get_all_speakers())


def speaker_emails(talk):
    return u', '.join(
        u'{}'.format(speaker.user.email) for speaker in talk.get_all_speakers())

def get_orders_from(user):
    return assopy_models.Order.objects.filter(_complete=True, user=user.id)

def get_tickets_assigned_to(user):
    return p3_models.TicketConference.objects.filter(assigned_to=user.email)

def is_ticket_assigned_to_someone_else(ticket, user):
    tickets = p3_models.TicketConference.objects.filter(ticket_id=ticket.id)

    #if user.email == u'<EMAIL>':
    #    from IPython.core.debugger import Tracer
    #    Tracer()()
    #print(user.email)

    if not tickets:
        return False
        #from IPython.core.debugger import Tracer
        #Tracer()()
        #raise RuntimeError('Could not find any ticket with ticket_id {}.'.format(ticket))

    if len(tickets) > 1:
        raise RuntimeError('You got more than one ticket from a ticket_id.'
                           'Tickets obtained: {}.'.format(tickets))

    tkt = tickets[0]
    if tkt.ticket.user_id != user.id:
        return True

    #if not hasattr(tkt, 'assigned_to'):
    #    return False

    if not tkt.assigned_to:
        return False

    if tkt.assigned_to == user.email:
        return False
    else:
        return True


def has_ticket(user):
    tickets = get_tickets_assigned_to(user)
    if tickets:
        return True

    user_tickets = list(user.ticket_set.all())
    orders = get_orders_from(user)
    if orders:
        order_tkts = [ordi.ticket for order in orders for ordi in order.orderitem_set.all() if ordi.ticket is not None]
        user_tickets.extend(order_tkts)

    for tkt in user_tickets:
        if not hasattr(tkt, 'fare'):
            from IPython.core.debugger import Tracer
            Tracer()()

        if tkt.fare.code.startswith('T'):
            if not is_ticket_assigned_to_someone_else(tkt, user):
                return True

    return False


def have_tickets(talk):
    usrs = talk.get_all_speakers()
    have_tkt = []
    for user in usrs:
        #from IPython.core.debugger import Tracer
        #Tracer()()
        try:
            have_tkt.append(has_ticket(user.user))
        except:
            print(traceback.format_exc())
            raise

    return have_tkt


def talk_title(talk):

    # Remove whitespace
    title = talk.title.strip()

    # Remove double spaces
    title = title.replace("  ", " ")

    # Remove quotes
    if title[0] == '"' and title[-1] == '"':
        title = title[1:-1]

    return title


def talk_schedule(talk):
    event = talk.get_event()<commit_msg>Cleanup code<commit_after># -*- coding: utf-8 -*-
""" Print out a JSON of accepted talks with the abstracts

"""
from   django.core.management.base import BaseCommand, CommandError
from   django.core  import urlresolvers
from   conference   import models
from   conference   import utils

from   p3           import models as p3_models
from   assopy       import models as assopy_models

from   collections  import defaultdict, OrderedDict
from   optparse     import make_option
import operator
import simplejson   as json
import traceback

### Globals

TYPE_NAMES = (
    ('keynote', 'Keynotes'),
    ('s', 'Talks'),
    ('t', 'Trainings'),
    ('p', 'Poster sessions'),
    ('h', 'Help desks'),
    ('europython', 'EuroPython sessions'),
    ('i', 'Other sessions'),
    )

def _check_talk_types(type_names):
    d = dict(type_names)
    for code, entry in models.TALK_TYPE:
        assert code in d, 'Talk type code %r is missing' % code
_check_talk_types(TYPE_NAMES)

### Helpers
def speaker_listing(talk):
    return u', '.join(
        u'{} {}'.format(speaker.user.first_name, speaker.user.last_name) for speaker in talk.get_all_speakers())


def speaker_emails(talk):
    return u', '.join(
        u'{}'.format(speaker.user.email) for speaker in talk.get_all_speakers())

def get_orders_from(user):
    return assopy_models.Order.objects.filter(_complete=True, user=user.id)

def get_tickets_assigned_to(user):
    return p3_models.TicketConference.objects.filter(assigned_to=user.email)

def is_ticket_assigned_to_someone_else(ticket, user):
    tickets = p3_models.TicketConference.objects.filter(ticket_id=ticket.id)

    if not tickets:
        return False
        #from IPython.core.debugger import Tracer
        #Tracer()()
        #raise RuntimeError('Could not find any ticket with ticket_id {}.'.format(ticket))

    if len(tickets) > 1:
        raise RuntimeError('You got more than one ticket from a ticket_id.'
                           'Tickets obtained: {}.'.format(tickets))

    tkt = tickets[0]
    if tkt.ticket.user_id != user.id:
        return True

    if not tkt.assigned_to:
        return False

    if tkt.assigned_to == user.email:
        return False
    else:
        return True


def has_ticket(user):
    tickets = get_tickets_assigned_to(user)
    if tickets:
        return True

    user_tickets = list(user.ticket_set.all())
    orders = get_orders_from(user)
    if orders:
        order_tkts = [ordi.ticket for order in orders for ordi in order.orderitem_set.all() if ordi.ticket is not None]
        user_tickets.extend(order_tkts)

    for tkt in user_tickets:
        if tkt.fare.code.startswith('T'):
            if not is_ticket_assigned_to_someone_else(tkt, user):
                return True

    return False


def have_tickets(talk):
    usrs = talk.get_all_speakers()
    have_tkt = []
    for user in usrs:
        try:
            have_tkt.append(has_ticket(user.user))
        except:
            print(traceback.format_exc())
            raise

    return have_tkt


def talk_title(talk):

    # Remove whitespace
    title = talk.title.strip()

    # Remove double spaces
    title = title.replace("  ", " ")

    # Remove quotes
    if title[0] == '"' and title[-1] == '"':
        title = title[1:-1]

    return title


def talk_schedule(talk):
    event = talk.get_event()
{'fraction_non_alphanumeric': 0.10780563819237853, 'fraction_numerical': 0.003116588751947868, 'mean_word_length': 2.8018309100700054, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, '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': '5520619', 'n_tokens_mistral': 2326, 'n_tokens_neox': 2250, 'n_words': 583}