<commit_before>from datetime import datetime

import icalendar

from django.http import HttpResponse, Http404
from django.shortcuts import render, redirect
from django.utils import timezone
from django_date_extensions.fields import ApproximateDate

from .models import *


def index(request):

    stories = Story.objects.all().order_by('-created')[:4]

    return render(request, 'index.html', {
        'future_events': Event.objects.future(),
        'past_events': Event.objects.past(),
        'stories': stories,
    })


def events(request):

    return render(request, 'events.html', {
        'future_events': Event.objects.future(),
        'past_events': Event.objects.past(),
    })


def resources(request):
    return render(request, 'resources.html', {})


def organize(request):
    return render(request, 'organize.html', {})


def stories(request):

    return render(request, 'stories.html', {
        'stories': Story.objects.all().order_by('-created'),
    })


def event(request, city):

    now = timezone.now()
    now_approx = ApproximateDate(year=now.year, month=now.month, day=now.day)
    try:
        page = EventPage.objects.get(url=city)
        if not (request.user.is_authenticated()
                or request.GET.has_key('preview')):
            if not page.is_live:
                past = page.event.date <= now_approx
                return render(request, "event_not_live.html",
                              {'city': city, 'past': past})
    except EventPage.DoesNotExist:
        raise Http404

    menu = EventPageMenu.objects.filter(page=page)
    content = EventPageContent.objects.filter(page=page, is_public=True)

    return render(request, "event.html", {
        'page': page,
        'menu': menu,
        'content': content,
    })


def events_ical(request):
    events = Event.objects.public().order_by('-date')
    calendar = icalendar.Calendar()
    calendar['summary'] = "List of Django Girls events around the world"
    for event in events:
        ical_event = event.as_ical()
        if ical_event is None:<commit_msg>Refactored event view to use get_object_or_404.<commit_after>from datetime import datetime

import icalendar

from django.http import HttpResponse, Http404
from django.shortcuts import get_object_or_404, render, redirect
from django.utils import timezone
from django_date_extensions.fields import ApproximateDate

from .models import *


def index(request):

    stories = Story.objects.all().order_by('-created')[:4]

    return render(request, 'index.html', {
        'future_events': Event.objects.future(),
        'past_events': Event.objects.past(),
        'stories': stories,
    })


def events(request):

    return render(request, 'events.html', {
        'future_events': Event.objects.future(),
        'past_events': Event.objects.past(),
    })


def resources(request):
    return render(request, 'resources.html', {})


def organize(request):
    return render(request, 'organize.html', {})


def stories(request):

    return render(request, 'stories.html', {
        'stories': Story.objects.all().order_by('-created'),
    })


def event(request, city):

    now = timezone.now()
    now_approx = ApproximateDate(year=now.year, month=now.month, day=now.day)
    page = get_object_or_404(EventPage, url=city)

    can_show = request.user.is_authenticated() or 'preview' in request.GET
    if not page.is_live and not can_show:
        return render(
            request,
            'event_not_live.html',
            {'city': city, 'past': page.event.date <= now_approx}
        )

    menu = EventPageMenu.objects.filter(page=page)
    content = EventPageContent.objects.filter(page=page, is_public=True)

    return render(request, "event.html", {
        'page': page,
        'menu': menu,
        'content': content,
    })


def events_ical(request):
    events = Event.objects.public().order_by('-date')
    calendar = icalendar.Calendar()
    calendar['summary'] = "List of Django Girls events around the world"
    for event in events:
        ical_event = event.as_ical()
        if ical_event is None:
{'fraction_non_alphanumeric': 0.12703820880992941, 'fraction_numerical': 0.004867364322219518, 'mean_word_length': 3.1389728096676737, 'pattern_counts': {'":': 0, '<': 5, '<?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': '2915931', 'n_tokens_mistral': 1263, 'n_tokens_neox': 1204, 'n_words': 282}