From 312c61ef61e937f11994f54184994c61b08211b0 Mon Sep 17 00:00:00 2001 From: scragg Date: Mon, 10 Nov 2014 10:54:46 -0600 Subject: [PATCH] search module, wip --- Vagrantfile | 2 +- realms/__init__.py | 3 +++ realms/config/__init__.py | 4 +++- realms/lib/hook.py | 7 +++++-- realms/modules/search/__init__.py | 0 realms/modules/search/hooks.py | 15 +++++++++++++++ realms/modules/search/models.py | 17 +++++++++++++++++ realms/modules/search/views.py | 10 ++++++++++ realms/modules/wiki/models.py | 1 - realms/templates/search/search.html | 4 ++++ setup.py | 1 + 11 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 realms/modules/search/__init__.py create mode 100644 realms/modules/search/hooks.py create mode 100644 realms/modules/search/models.py create mode 100644 realms/modules/search/views.py create mode 100644 realms/templates/search/search.html diff --git a/Vagrantfile b/Vagrantfile index 56acd78..363069f 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -9,7 +9,7 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| vb.cpus = 2 end - config.vm.provision "shell", path: "install.sh", privileged: "false" + config.vm.provision "shell", path: "install.sh", privileged: false end Vagrant::Config.run do |config| diff --git a/realms/__init__.py b/realms/__init__.py index 08de16f..998319f 100644 --- a/realms/__init__.py +++ b/realms/__init__.py @@ -11,6 +11,7 @@ import httplib import traceback import click 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.login import LoginManager, current_user from flask.ext.sqlalchemy import SQLAlchemy, declarative_base, Model, _QueryProperty @@ -160,6 +161,7 @@ def create_app(config=None): db.init_app(app) cache.init_app(app) assets.init_app(app) + elastic.init_app(app) for status_code in httplib.responses: if status_code >= 400: @@ -197,6 +199,7 @@ login_manager = LoginManager() db = SQLAlchemy() cache = Cache() assets = Assets() +elastic = Elastic() assets.register('main.js', 'vendor/jquery/dist/jquery.js', diff --git a/realms/config/__init__.py b/realms/config/__init__.py index 8f8eedb..a961de3 100644 --- a/realms/config/__init__.py +++ b/realms/config/__init__.py @@ -95,6 +95,8 @@ CACHE_REDIS_DB = '0' #CACHE_TYPE = 'memcached' CACHE_MEMCACHED_SERVERS = ['127.0.0.1:11211'] +ELASTICSEARCH_URL = 'http://127.0.0.1:9200' + # Get ReCaptcha Keys for your domain here: # https://www.google.com/recaptcha/admin#whyrecaptcha RECAPTCHA_ENABLE = False @@ -146,4 +148,4 @@ if ENV != "DEV": ASSETS_DEBUG = False SQLALCHEMY_ECHO = False -MODULES = ['wiki', 'auth'] +MODULES = ['wiki', 'auth', 'search'] diff --git a/realms/lib/hook.py b/realms/lib/hook.py index e772a1f..bf6a00e 100644 --- a/realms/lib/hook.py +++ b/realms/lib/hook.py @@ -7,12 +7,15 @@ def hook_func(name, fn): @wraps(fn) def wrapper(self, *args, **kwargs): for hook, a, kw in self.__class__._pre_hooks.get(name) or []: - hook(*a, **kw) + hook(*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 []: - hook(*a, **kw) + hook(*args, **kwargs) return rv return wrapper diff --git a/realms/modules/search/__init__.py b/realms/modules/search/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/realms/modules/search/hooks.py b/realms/modules/search/hooks.py new file mode 100644 index 0000000..d8a7467 --- /dev/null +++ b/realms/modules/search/hooks.py @@ -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 \ No newline at end of file diff --git a/realms/modules/search/models.py b/realms/modules/search/models.py new file mode 100644 index 0000000..e0d92c1 --- /dev/null +++ b/realms/modules/search/models.py @@ -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 \ No newline at end of file diff --git a/realms/modules/search/views.py b/realms/modules/search/views.py new file mode 100644 index 0000000..049d849 --- /dev/null +++ b/realms/modules/search/views.py @@ -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) \ No newline at end of file diff --git a/realms/modules/wiki/models.py b/realms/modules/wiki/models.py index a513755..e5b4ce9 100644 --- a/realms/modules/wiki/models.py +++ b/realms/modules/wiki/models.py @@ -124,7 +124,6 @@ class Wiki(HookMixin): return ret - def rename_page(self, old_name, new_name, username=None, email=None, message=None): """Rename page. diff --git a/realms/templates/search/search.html b/realms/templates/search/search.html new file mode 100644 index 0000000..38095da --- /dev/null +++ b/realms/templates/search/search.html @@ -0,0 +1,4 @@ +{% extends 'layout.html' %} +{% block body %} + +{% endblock %} diff --git a/setup.py b/setup.py index ad50832..8677160 100644 --- a/setup.py +++ b/setup.py @@ -26,6 +26,7 @@ setup(name='realms-wiki', 'Flask==0.10.1', 'Flask-Assets==0.10', 'Flask-Cache==0.13.1', + 'Flask-Elastic==0.2', 'Flask-Login==0.2.11', 'Flask-SQLAlchemy==2.0', 'Flask-WTF==0.10.2',