from django.shortcuts import get_object_or_404
import jingo
import waffle

from django.contrib.auth.models import User
from challenges.models import Submission, Category
from projects.models import Project
from blogs.models import BlogEntry
from events.models import Event


def splash(request, project, slug, template_name='ignite/splash.html'):
    """Show an individual project challenge."""
    project = get_object_or_404(Project, slug=project)
    challenge = get_object_or_404(project.challenge_set, slug=slug)
    num_blogs = 3
    # have we announced the winners yet - switch template
    if waffle.switch_is_active('announce_winners'):
        template_name = 'ignite/homepage-winners.html'
        num_blogs = 5
    blogs = BlogEntry.objects.filter(
        page='splash'
    ).order_by("-updated",)[:num_blogs]
    # if the dev challenge is open we want to only show dev entries
    if request.development.is_open:
        entries = (Submission.objects.visible()
                                 .filter(phase__challenge=challenge)
                                 .filter(phase__name="Development")
                                 .order_by("?"))
        num_entries = len(entries)
        entries_from = 'apps'
        if num_entries < 5:
            entries = (Submission.objects.visible()
                                 .filter(phase__challenge=challenge)
                                 .filter(phase__name="Ideation")
                                 .order_by("?"))
            entries_from = 'ideas'
    else:
        entries = (Submission.objects.visible()
                                 .filter(phase__challenge=challenge)
                                 .filter(phase__name="Ideation")
                                 .order_by("?"))
        entries_from = 'ideas'

    event_list = Event.objects.get_featured()[:5]
    return jingo.render(request, template_name, {
        'challenge': challenge,
        'project': project,
        'phases': list(enumerate(challenge.phases.all(), start=1)),
        'entries': entries[:5],
        'categories': Category.objects.all(),
        'blogs': blogs,
        'event_list': event_list,
        'entries_from': entries_from,
    })


def about(request, project, slug, template_name='ignite/about.html'):
    if waffle.switch_is_active('announce_winners'):
        template_name = 'ignite/about-winners.html'
    return jingo.render(request, template_name)


def judges(request, project, slug, template_name='challenges/all_judges.html'):
    """ List all judges we have in the system """
    profiles = []
    for judge in User.objects.filter(groups__name='Judges'):
        profile = judge.get_profile()
        # we only want to show featured profiles
        if profile.featured == True:
            profiles.append(profile)


    return jingo.render(request, 'ignite/judges.html', {
        'profiles': profiles
    })


def terms(request, project, slug, template_name='static/terms_conditions.html'):
    return jingo.render(request, template_name, {})


def terms_development(request, project, slug, template_name='static/terms_conditions_development.html'):
    return jingo.render(request, template_name, {})


def fail(request, template_name='404.html'):
    return jingo.render(request, template_name, {}, status=404)


def app_fail(request, template_name='500.html'):
    return jingo.render(request, template_name, {}, status=500)


def action_unavailable_response(request, message=None,
                                template_name="action_unavailable.html"):
    """Generic page for unavailable actions"""
    context = {'message': message}
    return jingo.render(request, template_name, context, status=403)

[((14, 14, 14, 54), 'django.shortcuts.get_object_or_404', 'get_object_or_404', (), '', False, 'from django.shortcuts import get_object_or_404\n'), ((15, 16, 15, 67), 'django.shortcuts.get_object_or_404', 'get_object_or_404', (), '', False, 'from django.shortcuts import get_object_or_404\n'), ((18, 7, 18, 50), 'waffle.switch_is_active', 'waffle.switch_is_active', ({(18, 31, 18, 49): '"""announce_winners"""'}, {}), "('announce_winners')", False, 'import waffle\n'), ((59, 7, 59, 50), 'waffle.switch_is_active', 'waffle.switch_is_active', ({(59, 31, 59, 49): '"""announce_winners"""'}, {}), "('announce_winners')", False, 'import waffle\n'), ((61, 11, 61, 47), 'jingo.render', 'jingo.render', ({(61, 24, 61, 31): 'request', (61, 33, 61, 46): 'template_name'}, {}), '(request, template_name)', False, 'import jingo\n'), ((67, 17, 67, 59), 'django.contrib.auth.models.User.objects.filter', 'User.objects.filter', (), '', False, 'from django.contrib.auth.models import User\n'), ((74, 11, 76, 6), 'jingo.render', 'jingo.render', ({(74, 24, 74, 31): 'request', (74, 33, 74, 53): '"""ignite/judges.html"""', (74, 55, 76, 5): "{'profiles': profiles}"}, {}), "(request, 'ignite/judges.html', {'profiles': profiles})", False, 'import jingo\n'), ((80, 11, 80, 51), 'jingo.render', 'jingo.render', ({(80, 24, 80, 31): 'request', (80, 33, 80, 46): 'template_name', (80, 48, 80, 50): '{}'}, {}), '(request, template_name, {})', False, 'import jingo\n'), ((84, 11, 84, 51), 'jingo.render', 'jingo.render', ({(84, 24, 84, 31): 'request', (84, 33, 84, 46): 'template_name', (84, 48, 84, 50): '{}'}, {}), '(request, template_name, {})', False, 'import jingo\n'), ((88, 11, 88, 63), 'jingo.render', 'jingo.render', (), '', False, 'import jingo\n'), ((92, 11, 92, 63), 'jingo.render', 'jingo.render', (), '', False, 'import jingo\n'), ((99, 11, 99, 68), 'jingo.render', 'jingo.render', (), '', False, 'import jingo\n'), ((45, 17, 45, 45), 'events.models.Event.objects.get_featured', 'Event.objects.get_featured', ({}, {}), '()', False, 'from events.models import Event\n'), ((51, 22, 51, 44), 'challenges.models.Category.objects.all', 'Category.objects.all', ({}, {}), '()', False, 'from challenges.models import Submission, Category\n'), ((21, 12, 23, 5), 'blogs.models.BlogEntry.objects.filter', 'BlogEntry.objects.filter', (), '', False, 'from blogs.models import BlogEntry\n'), ((26, 19, 26, 47), 'challenges.models.Submission.objects.visible', 'Submission.objects.visible', ({}, {}), '()', False, 'from challenges.models import Submission, Category\n'), ((39, 19, 39, 47), 'challenges.models.Submission.objects.visible', 'Submission.objects.visible', ({}, {}), '()', False, 'from challenges.models import Submission, Category\n'), ((33, 23, 33, 51), 'challenges.models.Submission.objects.visible', 'Submission.objects.visible', ({}, {}), '()', False, 'from challenges.models import Submission, Category\n')]