Source

cmsplugin-googlesearch / cmsplugin_googlesearch / models.py

from django.db import models
from cms.models import CMSPlugin
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from django.contrib.sites.models import Site
from django.contrib.sites.managers import CurrentSiteManager


class GoogleSearchSettings(models.Model):
    """ Per site settings for Google Search """
    site = models.OneToOneField(Site, primary_key=True)
    api_version = models.CharField(_('API version'), max_length=5, blank=False, help_text=_('Google Search API version'))
    uri = models.URLField(_('URI'), verify_exists=True, max_length=255, blank=False, help_text=_('Google Search API URI'))
    user_agent = models.CharField(_('User-Agent header'), max_length=255, blank=True, help_text=_('Not required for Google API; eg: "User-Agent: Softphone Beta1.5"'))
    referrer = models.URLField(_('Referrer header'), verify_exists=True, max_length=255, blank=True, help_text=_('"Referer"[sic]; Leave blank for current site\'s domain; eg: "http://www.w3.org/hypertext/DataSources/Overview.html"'))
    search_engine_id = models.CharField(_('CSE id'), max_length=255, blank=True, help_text=_('Google Custom Search Engine (CSE) id'))
    result_size = models.CharField(_('Result set size'), max_length=50, blank=True, help_text=_('Size of Google API result set; leave blank for default'))
    objects = models.Manager()
    on_site = CurrentSiteManager()

    def __unicode__(self):
        return self.__doc__

    class Meta:
        verbose_name_plural = _('settings')


#get custom templates from settings or use default
CMSPLUGIN_GOOGLESEARCH_TEMPLATES_CHOICES = getattr(settings,"CMSPLUGIN_GOOGLESEARCH_TEMPLATES", (('default', 'googlesearch_plugin.html'),) )

#get custom css from settings or use default
CMSPLUGIN_GOOGLESEARCH_CSS_CHOICES = getattr(settings,"CMSPLUGIN_GOOGLESEARCH_CSS", (('0', ''),('1', 'left'),('2', 'right'),('3', 'center'),) )

class GoogleSearchPlugin(CMSPlugin):
    query = models.CharField(_('Query string'), max_length=255, blank=True, help_text=_('Custom query'))
    css = models.CharField(_('CSS class'), max_length=1, choices=CMSPLUGIN_GOOGLESEARCH_CSS_CHOICES, default='0', help_text=_('Additional CSS class to apply'))
    template = models.CharField(_('Plugin template'), max_length=255, choices=CMSPLUGIN_GOOGLESEARCH_TEMPLATES_CHOICES, default=CMSPLUGIN_GOOGLESEARCH_TEMPLATES_CHOICES[0][0], help_text=_('Template used to render plugin'))