<commit_before>from django.views.generic import DetailView, TemplateView

from wafer.schedule.models import Venue, Slot
from wafer.schedule.admin import check_schedule


class ScheduleRow(object):
    """This is a helpful containter for the schedule view to keep sanity"""
    def __init__(self, slot, venue_list):
        self.slot = slot
        self.venue_list = venue_list
        self.items = {}

    def get_sorted_items(self):
        sorted_items = []
        for venue in self.venue_list:
            if venue in self.items:
                sorted_items.append(self.items[venue])
        return sorted_items

    def __repr__(self):
        """Debugging aid"""
        return '%s - %s' % (self.slot, self.get_sorted_items())


class VenueView(DetailView):
    template_name = 'wafer.schedule/venue.html'
    model = Venue


class ScheduleView(TemplateView):
    template_name = 'wafer.schedule/full_schedule.html'

    def get_context_data(self, **kwargs):
        context = super(ScheduleView, self).get_context_data(**kwargs)
        # Check if the schedule is valid
        if not check_schedule():
            return context
        venue_list = list(Venue.objects.all())
        context['venue_list'] = venue_list
        # We create a list of slots and schedule items
        schedule_days = {}
        seen_items = {}
        for slot in Slot.objects.all():
            day = slot.get_day()
            schedule_days.setdefault(day, [])
            row = ScheduleRow(slot, venue_list)
            schedule_days[day].append(row)
            skip = []
            for item in slot.scheduleitem_set.all():
                if item in seen_items:
                    # Inc rowspan
                    seen_items[item]['rowspan'] += 1
                    # Note that we need to skip this during colspan checks
                    skip.append(item.venue)
                    continue
                scheditem = {'item': item, 'rowspan': 1, 'colspan': 1}
                row.items[item.venue] = scheditem
                seen_items[item] = scheditem
            cur_item = None
            colspan = 1
            # Fixup colspans
            for venue in venue_list:
                # This may create holes in the table - the administrator
                # needs to sort that out by ordering the venues correctly
                if day not in venue.days.all():
                    scheditem = {'item': 'unavailable',
                                 'rowspan': 1, 'colspan': 1}
                    row.items[venue] = scheditem
                    # Make holes more obvious
                    cur_item = scheditem
                    colspan = 1
                elif venue in skip:
                    # Nothing to see here
                    continue
                elif venue not in row.items:
                    if cur_item:
                        cur_item['colspan'] += 1
                    else:
                        colspan += 1
                else:
                    cur_item = row.items[venue]
                    cur_item['colspan'] = colspan
                    colspan = 1
        # We turn the dict into a list here so we have a specified
        # ordering by date
        context['table_days'] = sorted(schedule_days.items(),
                                       key=lambda x: x[0].date)

        return context<commit_msg>Split heavy schedule lifting into a function<commit_after>from django.views.generic import DetailView, TemplateView

from wafer.schedule.models import Venue, Slot
from wafer.schedule.admin import check_schedule


class ScheduleRow(object):
    """This is a helpful containter for the schedule view to keep sanity"""
    def __init__(self, slot, venue_list):
        self.slot = slot
        self.venue_list = venue_list
        self.items = {}

    def get_sorted_items(self):
        sorted_items = []
        for venue in self.venue_list:
            if venue in self.items:
                sorted_items.append(self.items[venue])
        return sorted_items

    def __repr__(self):
        """Debugging aid"""
        return '%s - %s' % (self.slot, self.get_sorted_items())


class VenueView(DetailView):
    template_name = 'wafer.schedule/venue.html'
    model = Venue


def generate_schedule_dict(venue_list):
    """Helper function which creates a dictionary of the schedule"""
    # We create a list of slots and schedule items
    schedule_days = {}
    seen_items = {}
    for slot in Slot.objects.all():
        day = slot.get_day()
        schedule_days.setdefault(day, [])
        row = ScheduleRow(slot, venue_list)
        schedule_days[day].append(row)
        skip = []
        for item in slot.scheduleitem_set.all():
            if item in seen_items:
                # Inc rowspan
                seen_items[item]['rowspan'] += 1
                # Note that we need to skip this during colspan checks
                skip.append(item.venue)
                continue
            scheditem = {'item': item, 'rowspan': 1, 'colspan': 1}
            row.items[item.venue] = scheditem
            seen_items[item] = scheditem
        cur_item = None
        colspan = 1
        # Fixup colspans
        for venue in venue_list:
            # This may create holes in the table - the administrator
            # needs to sort that out by ordering the venues correctly
            if day not in venue.days.all():
                scheditem = {'item': 'unavailable',
                             'rowspan': 1, 'colspan': 1}
                row.items[venue] = scheditem
                # Make holes more obvious
                cur_item = scheditem
                colspan = 1
            elif venue in skip:
                # Nothing to see here
                continue
            elif venue not in row.items:
                if cur_item:
                    cur_item['colspan'] += 1
                else:
                    colspan += 1
            else:
                cur_item = row.items[venue]
                cur_item['colspan'] = colspan
                colspan = 1
    return schedule_days


class ScheduleView(TemplateView):
    template_name = 'wafer.schedule/full_schedule.html'

    def get_context_data(self, **kwargs):
        context = super(ScheduleView, self).get_context_data(**kwargs)
        # Check if the schedule is valid
        if not check_schedule():
            return context
        venue_list = list(Venue.objects.all())
        context['venue_list'] = venue_list
        schedule_days = generate_schedule_dict(venue_list)
        # We turn the dict into a list here so we have a specified
        # ordering by date
        context['table_days'] = sorted(schedule_days.items(),
                                       key=lambda x: x[0].date)

        return context
{'fraction_non_alphanumeric': 0.08298877731836976, 'fraction_numerical': 0.0032486709982279976, 'mean_word_length': 1.6696886085928262, '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': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7356616', 'n_tokens_mistral': 1810, 'n_tokens_neox': 1710, 'n_words': 522}