Demo entry 5980142

views

   

Submitted by anonymous on Sep 12, 2016 at 11:10
Language: Python. Code size: 6.5 kB.

from django.shortcuts import render, render_to_response
from models import *
from django.http import HttpResponse, HttpResponseRedirect
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.core.urlresolvers import reverse
from django.views.decorators.csrf import csrf_exempt
import json as m_json

# Create your views here.

def nearbyloc(request):
    near_list = NearBy.objects.all()
    category_list = Category.objects.all()
    print category_list
    print near_list, len(near_list)
    paginator = Paginator(near_list, 6)
    page = request.GET.get('page')
    try:
        near = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        near = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        near = paginator.page(paginator.num_pages)
    print near    
    return render_to_response('near-by-attractions.html', {"nearby": near, "category":category_list,'paginate_by':5})

def group(request,group_name,slug):
    """
    To sort and the display Near-by-Interests according to
    location and category selected by user
    'group_name' is passed with url eg: /ker/ for Kerala, /gur/ for Guruvayoor ...
    """
    print "Group Selected"
    cat_id = group_name[3:]
    cat_name = Category.objects.get(id=cat_id)
    print "Category name ",cat_name, " Category ID ",cat_id
    category_list = Category.objects.all()
    if group_name:
        if group_name[:3] == 'ker':
            fil_list = NearBy.objects.filter(category=cat_id).filter(location='Kerala')
            print len([(i.location, i.category) for i in fil_list])
        if group_name[:3] == 'gur':
            fil_list = NearBy.objects.filter(category=cat_id).filter(location='Guruvayoor')
            print len([(i.location, i.category) for i in fil_list])
        if group_name[:3] == 'thr':
            fil_list = NearBy.objects.filter(category=cat_id).filter(location='Thrissur')
            print len([(i.location, i.category) for i in fil_list])                      

        paginator = Paginator(fil_list, 6)
        page = request.GET.get('page')
        try:
            near = paginator.page(page)
        except PageNotAnInteger:
            # If page is not an integer, deliver first page.
            near = paginator.page(1)
        except EmptyPage:
            # If page is out of range (e.g. 9999), deliver last page of results.
            near = paginator.page(paginator.num_pages)
        return render_to_response('near-by-attractions.html', {"nearby": near, "category":category_list})
    else:
        return HttpResponseRedirect(reverse('nearby_url'))

def tags(request,tag_name):
    """
    To request 
    """
    print request, tag_name
    category_list = Category.objects.all()
    Tags = ['Temple','Tours','Tourism','Garage','ATM','Laboratory','School','Hospitals','Shopping','Entertain','Bank','Hotels','Police','Gas']
    if tag_name in Tags:
        index_of = Tags.index(str(tag_name))
        tag_name = Tags[index_of]
        print tag_name
        tag_list = NearBy.objects.filter(tags=tag_name)
        print tag_list, [i.tags for i in tag_list]
        
        paginator = Paginator(tag_list, 6)
        page = request.GET.get('page')
        try:
            near = paginator.page(page)
        except PageNotAnInteger:
            # If page is not an integer, deliver first page.
            near = paginator.page(1)
        except EmptyPage:
            # If page is out of range (e.g. 9999), deliver last page of results.
            near = paginator.page(paginator.num_pages)
        return render_to_response('near-by-attractions.html', {"nearby": near, "category":category_list})

    else:
        return HttpResponseRedirect(reverse('nearby_url'))



@csrf_exempt
def direction(request):
    print "Gone to direction"
    data = {}
    if request.method == "POST":
        object_id = request.POST.get('id')
        print object_id
        obj = NearBy.objects.get(id=object_id)
        data['name'] = obj.name
        data['category'] = obj.category.name
        data['src'] = obj.direction
        print data
        return HttpResponse(m_json.dumps(data), content_type="application/json")

@csrf_exempt
def search_result_ajax(request):
    if request.is_ajax():
        print "Request ajax"
        q = request.GET.get('term', '')
        print q
        name_select = NearBy.objects.filter(name__icontains = q )[:8]
        print [name.name for name in name_select] 
        # print [category.name for category in category_select]
        results = []
        demo = []
        for element in name_select:
            info_json = {}
            info_json['id'] = element.id
            info_json['label'] = element.name
            info_json['value'] = element.name
            if not info_json['value'] in demo:
                print info_json['value']
                demo.append(info_json)     
            results.append(info_json)
            print "DEMO List :",demo," END of DEMO"
        data = m_json.dumps(demo)
        print data
    elif not request.is_ajax():
        if not request.method == 'POST':
            print "Request not POST ..."
            if 'search-result-post' in request.session:
                request.POST = request.session['search-result-post']
                request.method = 'POST'

        if request.method == "POST":
            print "Request is POST ..."
            request.session['search-result-post'] = request.POST
            category_list = Category.objects.all()
            searched_for = request.POST.get("sear")
            print searched_for
            data_result = NearBy.objects.filter(name=searched_for)
            print data_result, type(data_result)
            paginator = Paginator(data_result, 6)
            page = request.GET.get('page')
            try:
                near_search = paginator.page(page)
            except PageNotAnInteger:
                # If page is not an integer, deliver first page.
                near_search = paginator.page(1)
            except EmptyPage:
                # If page is out of range (e.g. 9999), deliver last page of results.
                near_search = paginator.page(paginator.num_pages)        
            return render_to_response('near-by-attractions.html', {"search_result": near_search, "category":category_list})
    else:
        data = 'fail'
    mimetype = 'application/json'
    return HttpResponse(data, mimetype)    

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).