1. Frank Becker
  2. MyHost

Source

MyHost / src / djweb / views.py

# -*- coding: utf-8 -*-

"""views"""
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.template.context import RequestContext
from django.shortcuts import render_to_response, get_object_or_404
from django.utils import timezone
from hosts.models import Host

__author__ = 'a8'

from django.views.generic import TemplateView
from hosts.forms import SearchForm

class StartView(TemplateView):
    template_name = "start.html"

    def get_context_data(self, **kwargs):
        context = super(StartView, self).get_context_data(**kwargs)
        context['now'] = timezone.now()
        context['hosts'] = [e[0] for e in Host.objects.all().values_list('name')]
        return context

def home(request):
    """look up the main search form

        for the moment only hosts are looked up. More to come
    """
    host_found = True   # flag if host was found
    form = SearchForm(request.POST or None)
    if form.is_valid():
        name = form.cleaned_data['search_string']
        try:
            host = Host.objects.get(name=name)
        except Host.DoesNotExist:
            host_found = False

        if host_found:
            return HttpResponseRedirect(
                reverse('host-detail', kwargs={'slug': name}))

    hosts = [e[0] for e in Host.objects.all().values_list('name')]

    return render_to_response(
        'start.html',
        locals(),
        context_instance=RequestContext(request),
        )