Source

cmsplugin-googlesearch / cmsplugin_googlesearch / views.py

from django.shortcuts import render_to_response
from django.template import RequestContext
from cmsplugin_googlesearch.models import GoogleSearchSettings
from cmsplugin_googlesearch.forms import GoogleSearchForm
import django_pipes
from django.contrib.sites.models import Site


class GoogleSearch(django_pipes.Pipe):
    """ Formats and executes Google API searches """

    #get settings for cmsplugin_googlesearch
    g = GoogleSearchSettings.on_site.all().select_related()[0]

#    import ipdb; ipdb.set_trace()
    uri = g.uri

    #if referrer is not in settings, get it from current site
    if not g.referrer:
        g.referrer = 'http://%s' % Site.objects.get_current().domain

    headers = {}
    if g.user_agent:
        headers.update({'User-Agent' : g.user_agent})
    if g.referrer:
        headers.update({'User-Referer' : g.referrer})

#    import ipdb; ipdb.set_trace()

#    @staticmethod
    @classmethod
    def fetch(self, query):
#        import ipdb; ipdb.set_trace()

        g = self.g

        GET_args = {
            'q': query,                                     #query string, eg: 'q=Paris%20Hilton'
        }

        GET_args.update({'v': g.api_version })              #google api version is required

        if g.search_engine_id:
            GET_args.update({'cx': g.search_engine_id })
        if g.result_size:
            GET_args.update({'rsz': g.result_size })

        resp = GoogleSearch.objects.get(GET_args)           #execute the search

        if resp and hasattr(resp, "responseData") and hasattr(resp.responseData, "results"):
            return resp.responseData.results


def search(request, form_class=GoogleSearchForm, template_name='plugins/cmsplugin_googlesearch/googlesearch.html'):
    """ Renders a search form, validates its input, returns results """

    #initial values
    query = ''
    results = ''
    form = form_class()

    #if the form has been sent, get results; otherwise send the empty form
    if request.method == 'POST':
        form = form_class(request.POST)
#        import pdb; pdb.set_trace()
        if form.is_valid():
            cd = form.cleaned_data
            query = request.GET.get('q', cd['query'])
            results = GoogleSearch.fetch(query)

#    import pdb; pdb.set_trace()

    #django-cms requires RequestContext
    return render_to_response(template_name, {'form': form, 'results': results, 'q': query, }, context_instance=RequestContext(request))
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.