search module, wip

This commit is contained in:
scragg 2014-11-10 10:54:46 -06:00
parent c2404760b8
commit 312c61ef61
11 changed files with 59 additions and 5 deletions

2
Vagrantfile vendored
View file

@ -9,7 +9,7 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
vb.cpus = 2 vb.cpus = 2
end end
config.vm.provision "shell", path: "install.sh", privileged: "false" config.vm.provision "shell", path: "install.sh", privileged: false
end end
Vagrant::Config.run do |config| Vagrant::Config.run do |config|

View file

@ -11,6 +11,7 @@ import httplib
import traceback import traceback
import click import click
from flask import Flask, request, render_template, url_for, redirect, g from flask import Flask, request, render_template, url_for, redirect, g
from flask.ext.elastic import Elastic
from flask.ext.cache import Cache from flask.ext.cache import Cache
from flask.ext.login import LoginManager, current_user from flask.ext.login import LoginManager, current_user
from flask.ext.sqlalchemy import SQLAlchemy, declarative_base, Model, _QueryProperty from flask.ext.sqlalchemy import SQLAlchemy, declarative_base, Model, _QueryProperty
@ -160,6 +161,7 @@ def create_app(config=None):
db.init_app(app) db.init_app(app)
cache.init_app(app) cache.init_app(app)
assets.init_app(app) assets.init_app(app)
elastic.init_app(app)
for status_code in httplib.responses: for status_code in httplib.responses:
if status_code >= 400: if status_code >= 400:
@ -197,6 +199,7 @@ login_manager = LoginManager()
db = SQLAlchemy() db = SQLAlchemy()
cache = Cache() cache = Cache()
assets = Assets() assets = Assets()
elastic = Elastic()
assets.register('main.js', assets.register('main.js',
'vendor/jquery/dist/jquery.js', 'vendor/jquery/dist/jquery.js',

View file

@ -95,6 +95,8 @@ CACHE_REDIS_DB = '0'
#CACHE_TYPE = 'memcached' #CACHE_TYPE = 'memcached'
CACHE_MEMCACHED_SERVERS = ['127.0.0.1:11211'] CACHE_MEMCACHED_SERVERS = ['127.0.0.1:11211']
ELASTICSEARCH_URL = 'http://127.0.0.1:9200'
# Get ReCaptcha Keys for your domain here: # Get ReCaptcha Keys for your domain here:
# https://www.google.com/recaptcha/admin#whyrecaptcha # https://www.google.com/recaptcha/admin#whyrecaptcha
RECAPTCHA_ENABLE = False RECAPTCHA_ENABLE = False
@ -146,4 +148,4 @@ if ENV != "DEV":
ASSETS_DEBUG = False ASSETS_DEBUG = False
SQLALCHEMY_ECHO = False SQLALCHEMY_ECHO = False
MODULES = ['wiki', 'auth'] MODULES = ['wiki', 'auth', 'search']

View file

@ -7,12 +7,15 @@ def hook_func(name, fn):
@wraps(fn) @wraps(fn)
def wrapper(self, *args, **kwargs): def wrapper(self, *args, **kwargs):
for hook, a, kw in self.__class__._pre_hooks.get(name) or []: for hook, a, kw in self.__class__._pre_hooks.get(name) or []:
hook(*a, **kw) hook(*args, **kwargs)
rv = fn(self, *args, **kwargs) rv = fn(self, *args, **kwargs)
# Attach return value for post hooks
kwargs.update(dict(rv=rv))
for hook, a, kw in self.__class__._post_hooks.get(name) or []: for hook, a, kw in self.__class__._post_hooks.get(name) or []:
hook(*a, **kw) hook(*args, **kwargs)
return rv return rv
return wrapper return wrapper

View file

View file

@ -0,0 +1,15 @@
from realms.modules.wiki.models import Wiki
from realms.modules.search.models import Search
@Wiki.after('write_page')
def wiki_write_page(name, content, **kwargs):
body = dict(name=name,
content=content)
body.update(kwargs)
return Search.index('wiki', 'page', body=body)
@Wiki.after('rename_page')
def wiki_rename_page(*args, **kwargs):
pass

View file

@ -0,0 +1,17 @@
from realms import elastic
from realms.lib.model import HookMixin
class Search(HookMixin):
@classmethod
def index(cls, index, doc_type, id_=None, body=None):
return elastic.index(index=index, doc_type=doc_type, id=id_, body=body)
@classmethod
def wiki(cls, query):
return elastic.search(index='wiki', body={"query": {"match_all": {}}})
@classmethod
def users(cls, query):
pass

View file

@ -0,0 +1,10 @@
from flask import abort, g, render_template, request, redirect, Blueprint, flash, url_for, current_app
from .models import Search
blueprint = Blueprint('search', __name__)
@blueprint.route('/_search')
def search():
results = Search.wiki(request.args.get('q'))
print results
return render_template('search/search.html', results=results)

View file

@ -124,7 +124,6 @@ class Wiki(HookMixin):
return ret return ret
def rename_page(self, old_name, new_name, username=None, email=None, message=None): def rename_page(self, old_name, new_name, username=None, email=None, message=None):
"""Rename page. """Rename page.

View file

@ -0,0 +1,4 @@
{% extends 'layout.html' %}
{% block body %}
{% endblock %}

View file

@ -26,6 +26,7 @@ setup(name='realms-wiki',
'Flask==0.10.1', 'Flask==0.10.1',
'Flask-Assets==0.10', 'Flask-Assets==0.10',
'Flask-Cache==0.13.1', 'Flask-Cache==0.13.1',
'Flask-Elastic==0.2',
'Flask-Login==0.2.11', 'Flask-Login==0.2.11',
'Flask-SQLAlchemy==2.0', 'Flask-SQLAlchemy==2.0',
'Flask-WTF==0.10.2', 'Flask-WTF==0.10.2',