<commit_before>from __future__ import unicode_literals
import json

from django.core.urlresolvers import reverse
from django.http import Http404, HttpResponse, JsonResponse
from django.shortcuts import render, get_object_or_404, redirect
from django.template import loader, Context

from django.contrib.auth.models import User
from django.contrib import messages
from django.contrib.sites.models import Site

from account.decorators import login_required

from symposion.schedule.forms import SlotEditForm, ScheduleSectionForm
from symposion.schedule.models import Schedule, Day, Slot, Presentation, Session, SessionRole
from symposion.schedule.timetable import TimeTable

# FIXME: It is bad that I'm importing this and I feel bad:
from pinaxcon.proposals.models import ConferenceSpeaker, ConferenceSpeakerOrganizerRoles


def fetch_schedule(slug):
    qs = Schedule.objects.all()

    if slug is None:
        if qs.count() > 1:
            raise Http404()
        schedule = next(iter(qs), None)
        if schedule is None:
            raise Http404()
    else:
        schedule = get_object_or_404(qs, section__slug=slug)

    return schedule


def schedule_conference(request):

    sections = []

    if request.user.is_staff:
        section_days = Day.objects.filter(
                schedule__hidden=False).order_by('date')
    else:
        section_days = Day.objects.filter(schedule__hidden=False,
                schedule__published=True).order_by('date')

    for sd in section_days:
        days = [TimeTable(sd)]
        sections.append({
            "schedule": sd.schedule,
            "days": days,
            })

    ctx = {
        "sections": sections,
    }
    return render(request, "symposion/schedule/schedule_conference.html", ctx)


def schedule_detail(request, slug=None):

    schedule = fetch_schedule(slug)
    if not schedule.published and not request.user.is_staff:
        raise Http404()

    days_qs = Day.objects.filter(schedule=schedule)
    days = [TimeTable(day) for day in days_qs]

    ctx = {
        "schedule": schedule,
        "days": days,
    }
    return render(request, "symposion/schedule/schedule_detail.html", ctx)


def schedule_list(request, slug=None):
    schedule = fetch_schedule(slug)
    if not schedule.published and not request.user.is_staff:
        raise Http404()

    presentations = Presentation.objects.filter(section=schedule.section)
    presentations = presentations.exclude(cancelled=True).order_by("title")

    ctx = {
        "schedule": schedule,
        "presentations": presentations,
    }
    return render(request, "symposion/schedule/schedule_list.html", ctx)

def _speaker_data(speaker, extras=None):
    if extras is None:
        extras = {}
    data = {
        "speaker_id": speaker.id,
        "biography": speaker.biography,
        "biography_html": speaker.biography_html,
        "name": speaker.name,
    }
    try:
        data["photo"] = {
            "description": speaker.name,
            "url": speaker.photo.url
        }
    except ValueError:
        data["photo"] = {
            "description": speaker.name,
            "url": "https://static-cfp.pyohio.org/speaker_photo/default.png",
        }
    try:
        # Reaching into the app to get extra speaker data for now:
        full_speaker = ConferenceSpeaker.objects.get(speakerbase_ptr=speaker)
        data["twitter"] = full_speaker.twitter_username
    except:
        data["twitter"] = ''
    data.update(extras)
    return data

def _presentation_data(presentation):
    from pprint import pprint
    speakers_data = [_speaker_data(presentation.speaker)]
    additional_speakers = [_speaker_data(s) for s in presentation.proposal.additional_speakers.all()]
    speakers_data.extend(additional_speakers)
    data = {
        "presentation_id": presentation.id,
        "title": presentation.title,
        "abstract": presentation.abstract,
        "abstract_html": presentation.abstract_html,
        "description": presentation.description,<commit_msg>use gravatar if no profile photo<commit_after>from __future__ import unicode_literals
import hashlib
import json

from django.core.urlresolvers import reverse
from django.http import Http404, HttpResponse, JsonResponse
from django.shortcuts import render, get_object_or_404, redirect
from django.template import loader, Context

from django.contrib.auth.models import User
from django.contrib import messages
from django.contrib.sites.models import Site

from account.decorators import login_required

from symposion.schedule.forms import SlotEditForm, ScheduleSectionForm
from symposion.schedule.models import Schedule, Day, Slot, Presentation, Session, SessionRole
from symposion.schedule.timetable import TimeTable

# FIXME: It is bad that I'm importing this and I feel bad:
from pinaxcon.proposals.models import ConferenceSpeaker, ConferenceSpeakerOrganizerRoles


def fetch_schedule(slug):
    qs = Schedule.objects.all()

    if slug is None:
        if qs.count() > 1:
            raise Http404()
        schedule = next(iter(qs), None)
        if schedule is None:
            raise Http404()
    else:
        schedule = get_object_or_404(qs, section__slug=slug)

    return schedule


def schedule_conference(request):

    sections = []

    if request.user.is_staff:
        section_days = Day.objects.filter(
                schedule__hidden=False).order_by('date')
    else:
        section_days = Day.objects.filter(schedule__hidden=False,
                schedule__published=True).order_by('date')

    for sd in section_days:
        days = [TimeTable(sd)]
        sections.append({
            "schedule": sd.schedule,
            "days": days,
            })

    ctx = {
        "sections": sections,
    }
    return render(request, "symposion/schedule/schedule_conference.html", ctx)


def schedule_detail(request, slug=None):

    schedule = fetch_schedule(slug)
    if not schedule.published and not request.user.is_staff:
        raise Http404()

    days_qs = Day.objects.filter(schedule=schedule)
    days = [TimeTable(day) for day in days_qs]

    ctx = {
        "schedule": schedule,
        "days": days,
    }
    return render(request, "symposion/schedule/schedule_detail.html", ctx)


def schedule_list(request, slug=None):
    schedule = fetch_schedule(slug)
    if not schedule.published and not request.user.is_staff:
        raise Http404()

    presentations = Presentation.objects.filter(section=schedule.section)
    presentations = presentations.exclude(cancelled=True).order_by("title")

    ctx = {
        "schedule": schedule,
        "presentations": presentations,
    }
    return render(request, "symposion/schedule/schedule_list.html", ctx)

def _speaker_data(speaker, extras=None):
    if extras is None:
        extras = {}
    data = {
        "speaker_id": speaker.id,
        "biography": speaker.biography,
        "biography_html": speaker.biography_html,
        "name": speaker.name,
    }
    try:
        data["photo"] = {
            "description": speaker.name,
            "url": speaker.photo.url
        }
    except ValueError:
        speaker_hash = hashlib.md5(speaker.email).hexdigest()
        gravatar_url = "https://www.gravatar.com/avatar/{}?s=250&r=g&d=http%3A%2F%2Fstatic-cfp.pyohio.org%2Fspeaker_photo%2Fdefault.png".format(speaker_hash)
        data["photo"] = {
            "description": speaker.name,
            "url": gravatar_url,
        }
    try:
        # Reaching into the app to get extra speaker data for now:
        full_speaker = ConferenceSpeaker.objects.get(speakerbase_ptr=speaker)
        data["twitter"] = full_speaker.twitter_username
    except:
        data["twitter"] = ''
    data.update(extras)
    return data

def _presentation_data(presentation):
    from pprint import pprint
    speakers_data = [_speaker_data(presentation.speaker)]
    additional_speakers = [_speaker_data(s) for s in presentation.proposal.additional_speakers.all()]
    speakers_data.extend(additional_speakers)
    data = {
        "presentation_id": presentation.id,
        "title": presentation.title,
        "abstract": presentation.abstract,
        "abstract_html": presentation.abstract_html,
        "description": presentation.description,
{'fraction_non_alphanumeric': 0.09780632650587807, 'fraction_numerical': 0.0064234638225669615, 'mean_word_length': 3.1095617529880477, 'pattern_counts': {'":': 40, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, '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': '940804', 'n_tokens_mistral': 2439, 'n_tokens_neox': 2346, 'n_words': 548}