<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals

import json
from datetime import datetime
import datetime as dt
from uuid import UUID

from django.contrib.auth import authenticate, login
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from django.db.models import Q
from django.http import Http404, HttpResponse
from django.shortcuts import render, redirect
from django import forms

# Create your views here.
from django_filters.views import FilterView
from django_ical.views import ICalFeed
from django_tables2 import RequestConfig, LazyPaginator, SingleTableMixin

from api.models import Agency, Launch, Astronaut, Launcher, SpaceStation, SpacecraftConfiguration, LauncherConfig, \
    Events
from bot.models import NewsItem
from web.filters import LauncherConfigListFilter
from web.tables import LaunchVehicleTable, LauncherConfigTable


def get_youtube_url(launch):
    for url in launch.vid_urls.all():
        if 'youtube' in url.vid_url:
            return url.vid_url


def index(request):
    news = NewsItem.objects.all().order_by('-created_at')[:6]
    last_six_hours = datetime.now() - dt.timedelta(hours=6)
    event = Events.objects.all().filter(date__gte=last_six_hours).order_by('date').first()
    events = Events.objects.all().filter(date__gte=last_six_hours).order_by('date')[1:4]
    previous_launches = Launch.objects.filter(net__lte=datetime.utcnow()).order_by('-net')[:10]
    _launches = Launch.objects.filter(net__gte=datetime.utcnow()).filter(Q(status__id=1) | Q(status__id=2)).order_by(
        'net')[:3]

    in_flight_launch = Launch.objects.filter(status__id=6).order_by('-net').first()
    recently_launched = Launch.objects.filter(net__gte=datetime.utcnow() - dt.timedelta(hours=2),
                                              net__lte=datetime.utcnow()).order_by('-net').first()
    _next_launch = Launch.objects.filter(net__gte=datetime.utcnow()).order_by('net').first()

    if in_flight_launch:
        launch = in_flight_launch
        _launches = _launches[:2]
    elif recently_launched:
        launch = recently_launched
        _launches = _launches[1:3]
    else:
        launch = _next_launch
        _launches = _launches[1:3]

    if launch.image_url:
        launch_image = launch.image_url.url
    elif launch.rocket.configuration.image_url:
        launch_image = launch.rocket.configuration.image_url.url
    elif launch.infographic_url:
        launch_image = launch.infographic_url.url
    else:
        launch_image = None

    first_launch = _launches[0]
    if first_launch.image_url:
        first_launch_image = first_launch.image_url.url
    elif first_launch.rocket.configuration.image_url:
        first_launch_image = first_launch.rocket.configuration.image_url.url
    elif first_launch.infographic_url:
        first_launch_image = first_launch.infographic_url.url
    else:
        first_launch_image = None
    second_launch = _launches[1]

    if second_launch.image_url:
        second_launch_image = second_launch.image_url.url
    elif second_launch.rocket.configuration.image_url:
        second_launch_image = second_launch.rocket.configuration.image_url.url
    elif second_launch.infographic_url:
        second_launch_image = second_launch.infographic_url.url
    else:
        second_launch_image = None

    return render(request, 'web/index.html', {'launch': launch,
                                              'launch_image': launch_image,
                                              'first_launch': first_launch,
                                              'first_launch_image': first_launch_image,
                                              'second_launch': second_launch,
                                              'second_launch_image': second_launch_image,
                                              'youtube_url': get_youtube_url(_next_launch),
                                              'news': news,
                                              'previous_launches': previous_launches,
                                              'event': event,
                                              'events': events})


def app(request):
    in_flight_launch = Launch.objects.filter(status__id=6).order_by('-net').first()
    if in_flight_launch:
        return render(request, 'web/app.html', {'launch': in_flight_launch,
                                                'youtube_url': get_youtube_url(in_flight_launch)})

    recently_launched = Launch.objects.filter(net__gte=datetime.utcnow() - dt.timedelta(hours=2),
                                              net__lte=datetime.utcnow()).order_by('-net').first()
    if recently_launched:
        return render(request, 'web/app.html', {'launch': recently_launched,
                                                'youtube_url': get_youtube_url(recently_launched)})
    else:
        _next_launch = Launch.objects.filter(net__gte=datetime.utcnow()).order_by('net').first()
        return render(request, 'web/app.html', {'launch': _next_launch,
                                                'youtube_url': get_youtube_url(_next_launch)})


# Create your views here.
def next_launch(request):
    in_flight_launch = Launch.objects.filter(status__id=6).order_by('-net').first()
    if in_flight_launch:
        return redirect('launch_by_slug', slug=in_flight_launch.slug)
    recently_launched = Launch.objects.filter(net__gte=datetime.utcnow() - dt.timedelta(hours=6),
                                              net__lte=datetime.utcnow()).order_by('-net').first()
    if recently_launched:
        return redirect('launch_by_slug', slug=recently_launched.slug)
    else:
        _next_launch = Launch.objects.filter(net__gte=datetime.utcnow()).order_by('net').first()
        return redirect('launch_by_slug', slug=_next_launch.slug)


# Create your views here.
def launch_by_slug(request, slug):
    try:
        val = UUID(slug, version=4)
        try:
            launch = Launch.objects.get(id=slug)
            return redirect('launch_by_slug', slug=launch.slug)
        except ObjectDoesNotExist:
            raise Http404
    except ValueError:
        # If it's a value error, then the string
        # is not a valid hex code for a UUID.
        try:
            return create_launch_view(request, Launch.objects.get(slug=slug))
        except ObjectDoesNotExist:
            raise Http404
<commit_msg>Avoid redirect.<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals

import json
from datetime import datetime
import datetime as dt
from uuid import UUID

from django.contrib.auth import authenticate, login
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from django.db.models import Q
from django.http import Http404, HttpResponse
from django.shortcuts import render, redirect
from django import forms

# Create your views here.
from django_filters.views import FilterView
from django_ical.views import ICalFeed
from django_tables2 import RequestConfig, LazyPaginator, SingleTableMixin

from api.models import Agency, Launch, Astronaut, Launcher, SpaceStation, SpacecraftConfiguration, LauncherConfig, \
    Events
from bot.models import NewsItem
from web.filters import LauncherConfigListFilter
from web.tables import LaunchVehicleTable, LauncherConfigTable


def get_youtube_url(launch):
    for url in launch.vid_urls.all():
        if 'youtube' in url.vid_url:
            return url.vid_url


def index(request):
    news = NewsItem.objects.all().order_by('-created_at')[:6]
    last_six_hours = datetime.now() - dt.timedelta(hours=6)
    event = Events.objects.all().filter(date__gte=last_six_hours).order_by('date').first()
    events = Events.objects.all().filter(date__gte=last_six_hours).order_by('date')[1:4]
    previous_launches = Launch.objects.filter(net__lte=datetime.utcnow()).order_by('-net')[:10]
    _launches = Launch.objects.filter(net__gte=datetime.utcnow()).filter(Q(status__id=1) | Q(status__id=2)).order_by(
        'net')[:3]

    in_flight_launch = Launch.objects.filter(status__id=6).order_by('-net').first()
    recently_launched = Launch.objects.filter(net__gte=datetime.utcnow() - dt.timedelta(hours=2),
                                              net__lte=datetime.utcnow()).order_by('-net').first()
    _next_launch = Launch.objects.filter(net__gte=datetime.utcnow()).order_by('net').first()

    if in_flight_launch:
        launch = in_flight_launch
        _launches = _launches[:2]
    elif recently_launched:
        launch = recently_launched
        _launches = _launches[1:3]
    else:
        launch = _next_launch
        _launches = _launches[1:3]

    if launch.image_url:
        launch_image = launch.image_url.url
    elif launch.rocket.configuration.image_url:
        launch_image = launch.rocket.configuration.image_url.url
    elif launch.infographic_url:
        launch_image = launch.infographic_url.url
    else:
        launch_image = None

    first_launch = _launches[0]
    if first_launch.image_url:
        first_launch_image = first_launch.image_url.url
    elif first_launch.rocket.configuration.image_url:
        first_launch_image = first_launch.rocket.configuration.image_url.url
    elif first_launch.infographic_url:
        first_launch_image = first_launch.infographic_url.url
    else:
        first_launch_image = None
    second_launch = _launches[1]

    if second_launch.image_url:
        second_launch_image = second_launch.image_url.url
    elif second_launch.rocket.configuration.image_url:
        second_launch_image = second_launch.rocket.configuration.image_url.url
    elif second_launch.infographic_url:
        second_launch_image = second_launch.infographic_url.url
    else:
        second_launch_image = None

    return render(request, 'web/index.html', {'launch': launch,
                                              'launch_image': launch_image,
                                              'first_launch': first_launch,
                                              'first_launch_image': first_launch_image,
                                              'second_launch': second_launch,
                                              'second_launch_image': second_launch_image,
                                              'youtube_url': get_youtube_url(_next_launch),
                                              'news': news,
                                              'previous_launches': previous_launches,
                                              'event': event,
                                              'events': events})


def app(request):
    in_flight_launch = Launch.objects.filter(status__id=6).order_by('-net').first()
    if in_flight_launch:
        return render(request, 'web/app.html', {'launch': in_flight_launch,
                                                'youtube_url': get_youtube_url(in_flight_launch)})

    recently_launched = Launch.objects.filter(net__gte=datetime.utcnow() - dt.timedelta(hours=2),
                                              net__lte=datetime.utcnow()).order_by('-net').first()
    if recently_launched:
        return render(request, 'web/app.html', {'launch': recently_launched,
                                                'youtube_url': get_youtube_url(recently_launched)})
    else:
        _next_launch = Launch.objects.filter(net__gte=datetime.utcnow()).order_by('net').first()
        return render(request, 'web/app.html', {'launch': _next_launch,
                                                'youtube_url': get_youtube_url(_next_launch)})


# Create your views here.
def next_launch(request):
    in_flight_launch = Launch.objects.filter(status__id=6).order_by('-net').first()
    if in_flight_launch:
        return redirect('launch_by_slug', slug=in_flight_launch.slug)
    recently_launched = Launch.objects.filter(net__gte=datetime.utcnow() - dt.timedelta(hours=6),
                                              net__lte=datetime.utcnow()).order_by('-net').first()
    if recently_launched:
        return redirect('launch_by_slug', slug=recently_launched.slug)
    else:
        _next_launch = Launch.objects.filter(net__gte=datetime.utcnow()).order_by('net').first()
        return redirect('launch_by_slug', slug=_next_launch.slug)


# Create your views here.
def launch_by_slug(request, slug):
    try:
        val = UUID(slug, version=4)
        try:
            launch = Launch.objects.get(id=slug)
            if str(launch.id) == launch.slug:
                return create_launch_view(request, Launch.objects.get(slug=slug))
            return redirect('launch_by_slug', slug=launch.slug)
        except ObjectDoesNotExist:
            raise Http404
    except ValueError:
        # If it's a value error, then the string
        # is not a valid hex code for a UUID.
        try:
            return create_launch_view(request, Launch.objects.get(slug=slug))
        except ObjectDoesNotExist:
            raise Http404

{'fraction_non_alphanumeric': 0.09546001202645821, 'fraction_numerical': 0.005111244738424534, 'mean_word_length': 2.5612955032119915, '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': 64, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5234332', 'n_tokens_mistral': 3920, 'n_tokens_neox': 3728, 'n_words': 695}