botanic_apartments / catalogue / views.py

# -*- coding: utf-8 -*-
from annoying.decorators import render_to
from annoying.functions import get_object_or_None
from catalogue import forms
from catalogue import models
from catalogue import utils
from django.shortcuts import get_object_or_404
from django.shortcuts import redirect
from django.views.generic.list_detail import object_list
from django_utils.functions import compact_dict

#@render_to('catalogue/index.html')
def index(request):
    apartment_list = models.Apartment.objects.all()
    search_form = forms.SearchForm()
    find_by_id_form = forms.FindByIdForm()
    context = {
        'search_form': search_form,
        'find_by_id_form': find_by_id_form
    }
    return object_list(request,
                       queryset=apartment_list,
                       template_name='catalogue/index.html',
                       template_object_name='apartment',
                       allow_empty=True,
                       paginate_by=25,
                       extra_context=context
                       )

@render_to('catalogue/apartment.html')
def show(request, id=None, code=None):
    if id:
        apartment = get_object_or_404(models.Apartment, id=id)
    elif code:
        apartment = get_object_or_404(models.Apartment, code=code)
    search_form = forms.SearchForm()
    find_by_id_form = forms.FindByIdForm()
    reservation_form = forms.ReservationForm({'apartment': apartment.id}, empty_permitted=True)
    reservation_form.quiet = True
    return {
        'apartment': apartment,
        'search_form': search_form,
        'find_by_id_form': find_by_id_form,
        'reservation_form': reservation_form,
        }

def search(request):
    context = {}
    search_results = models.Apartment.objects.none()
    if request.method == 'POST':
        search_form = forms.SearchForm(request.POST)
        if search_form.is_valid():
            search_results = utils.search_apartment(compact_dict(search_form.cleaned_data), search_form)
    else:
        search_form = forms.SearchForm()
    find_by_id_form = forms.FindByIdForm()
    context.update({
                   'search_form': search_form,
                   'find_by_id_form': find_by_id_form
                   })
    return object_list(request,
                       queryset=search_results,
                       template_name='catalogue/search_results.json',
                       template_object_name='apartment',
                       allow_empty=True,
                       paginate_by=25,
                       extra_context=context,
                       mimetype='application/json'
                       )

#@render_to('catalogue/search.html')
def rooms(request, rooms):
    if rooms == u'vip':
        filter = {'level': 3}
    else:
        filter = {'rooms': rooms, 'level__lt': 3}
    search_results = utils.search_apartment(filter)
    search_form = forms.SearchForm(filter)
    find_by_id_form = forms.FindByIdForm()
    context = {
        'search_form': search_form,
        'find_by_id_form': find_by_id_form
    }
    return object_list(request,
                       queryset=search_results,
                       template_name='catalogue/search.html',
                       template_object_name='apartment',
                       allow_empty=True,
                       paginate_by=25,
                       extra_context=context
                       )

@render_to('catalogue/find_by_id.html')    
def find_by_id(request):
    if request.method == 'POST':
        find_by_id_form = forms.FindByIdForm(request.POST)
    else:
        find_by_id_form = forms.FindByIdForm()
    search_form = forms.SearchForm()
    find_by_id_form_valid = find_by_id_form.is_valid()
    if find_by_id_form_valid:
        id = find_by_id_form.cleaned_data.get('id', None)
        apartment = get_object_or_None(models.Apartment, code=id)
        if apartment is not None:
            return redirect(apartment)
    return {
        'find_by_id_form': find_by_id_form,
        'search_form': search_form,
        'find_by_id_form_valid': find_by_id_form_valid,
        'find_by_id_query': find_by_id_form.data.get('id', '')
        }

@render_to('catalogue/reserve.html')
def reserve(request):
    search_form = forms.SearchForm()
    find_by_id_form = forms.FindByIdForm()
    context = {
        'search_form': search_form,
        'find_by_id_form': find_by_id_form
    }
    return context

@render_to()
def validate_reservation_form(request):
    context = {}
    if request.method == 'POST':
        reservation_form = forms.ReservationForm(request.POST)
        context.update({'reservation_form_valid': reservation_form.is_valid()})
    else:
        reservation_form = forms.ReservationForm()
    context.update({'reservation_form': reservation_form})
    context.update({'TEMPLATE': 'partials/reservation_form.html'})
    return context
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.