# 博客相关的表单定义
import logging

from django import forms
from haystack.forms import SearchForm  # Haystack搜索表单基类

logger = logging.getLogger(__name__)


class BlogSearchForm(SearchForm):
    """博客搜索表单"""
    querydata = forms.CharField(required=True)  # 搜索关键词字段

    def search(self):
        """执行搜索操作"""
        datas = super(BlogSearchForm, self).search()
        if not self.is_valid():
            return self.no_query_found()

        # 记录搜索关键词到日志
        if self.cleaned_data['querydata']:
            logger.info(self.cleaned_data['querydata'])
        return datas
