Basic search works for now
This commit is contained in:
parent
0b61fa7ff2
commit
2f1f38ca5c
|
@ -58,9 +58,12 @@ class Archive(Index):
|
|||
from dynastie.models import Post, Blog
|
||||
|
||||
self.hooks = {'posts' : self.createPosts,
|
||||
'navigation' : self.createNavigation,
|
||||
'archive' : self.createArchive,
|
||||
'tags' : self.createTags}
|
||||
'navigation' : self.createNavigation,
|
||||
'archive' : self.createArchive,
|
||||
'tags' : self.createTags,
|
||||
'replace' : self.createReplace}
|
||||
|
||||
self.blog = blog
|
||||
|
||||
if not os.path.exists(src + '/_archive.html'):
|
||||
self.addError('No _archive.html found, exiting')
|
||||
|
|
|
@ -25,9 +25,12 @@ class Category(Index):
|
|||
from dynastie.models import Post, Blog, Category
|
||||
|
||||
self.hooks = {'posts' : self.createPosts,
|
||||
'navigation' : self.createNavigation,
|
||||
'category' : self.createCategory,
|
||||
'tags' : self.createTags}
|
||||
'navigation' : self.createNavigation,
|
||||
'category' : self.createCategory,
|
||||
'tags' : self.createTags,
|
||||
'replace' : self.createReplace}
|
||||
|
||||
self.blog = blog
|
||||
|
||||
if not os.path.exists(src + '/_category.html'):
|
||||
self.addError('No _category.html found, exiting')
|
||||
|
|
|
@ -21,10 +21,10 @@ class StrictUTF8Writer(codecs.StreamWriter):
|
|||
object = object.replace('>', '>')
|
||||
object = object.replace('"', '"')
|
||||
object = object.replace(''', "'")
|
||||
object = object.replace('&', "&")
|
||||
object = object.replace('&', '&')
|
||||
|
||||
if not type(object) == unicode:
|
||||
self.value = self.value + unicode(object, "utf-8")
|
||||
self.value = self.value + unicode(object, 'utf-8')
|
||||
else:
|
||||
self.value = self.value + object
|
||||
return self.value
|
||||
|
|
|
@ -14,6 +14,25 @@ class Index(DynastieGenerator):
|
|||
posts_per_page = 0
|
||||
filename = 'index'
|
||||
dirname = ''
|
||||
blog = None
|
||||
|
||||
def createReplace(self, posts, dom, root, replace_elem):
|
||||
if not replace_elem.hasAttribute('div_name'):
|
||||
self.addError('No attribute div_name for a replace tag')
|
||||
return
|
||||
|
||||
div_element = replace_elem.cloneNode(True)
|
||||
div_element.tagName = replace_elem.getAttribute('div_name')
|
||||
div_element.removeAttribute('div_name')
|
||||
for key,value in replace_elem.attributes.items():
|
||||
if key == 'div_name': continue
|
||||
|
||||
value = value.replace('dyn:blog_id', str(self.blog.id))
|
||||
|
||||
div_element.setAttribute(key, value)
|
||||
|
||||
root.replaceChild(div_element, replace_elem)
|
||||
return div_element
|
||||
|
||||
def createNavigation(self, posts, dom, root, node):
|
||||
if self.nb_pages == 0 or self.nb_pages == 1:
|
||||
|
@ -262,7 +281,10 @@ class Index(DynastieGenerator):
|
|||
self.hooks = {'posts' : self.createPosts,
|
||||
'navigation' : self.createNavigation,
|
||||
'recents' : self.createRecents,
|
||||
'tags' : self.createTags}
|
||||
'tags' : self.createTags,
|
||||
'replace' : self.createReplace}
|
||||
|
||||
self.blog = blog
|
||||
|
||||
if not os.path.exists(src + '/_index.html'):
|
||||
self.addError('No _index.html found, exiting')
|
||||
|
|
|
@ -31,6 +31,8 @@ class Post(Index):
|
|||
else:
|
||||
value = value.replace('dyn:comment_id', str(self.cur_comment.id))
|
||||
|
||||
value = value.replace('dyn:blog_id', str(self.blog.id))
|
||||
|
||||
div_element.setAttribute(key, value)
|
||||
|
||||
root.replaceChild(div_element, replace_elem)
|
||||
|
@ -177,6 +179,8 @@ class Post(Index):
|
|||
def generate(self, blog, src, output):
|
||||
from dynastie.models import Post, Blog
|
||||
|
||||
self.blog = blog
|
||||
|
||||
posts = Post.objects.all()
|
||||
|
||||
return self._generate(blog, src, output, posts)
|
||||
|
|
57
generators/search.py
Normal file
57
generators/search.py
Normal file
|
@ -0,0 +1,57 @@
|
|||
import os
|
||||
from datetime import datetime
|
||||
from xml.dom.minidom import parse, parseString
|
||||
from dynastie.generators.generator import DynastieGenerator, StrictUTF8Writer
|
||||
from dynastie.generators.index import Index
|
||||
from django.db import models
|
||||
|
||||
class Search(Index):
|
||||
|
||||
def createNoResultFound(self, posts, dom, root, node):
|
||||
posts_elem = self.createElement(dom, 'posts', '<b>No result found</b>')
|
||||
root.replaceChild(posts_elem, node)
|
||||
return posts_elem
|
||||
|
||||
def generate(self, blog, src, output, post_list):
|
||||
from dynastie.models import Post, Blog
|
||||
|
||||
self.blog = blog
|
||||
|
||||
self.hooks = {'posts' : self.createPosts,
|
||||
'replace' : self.createReplace}
|
||||
|
||||
if not os.path.exists(src + '/_search.html'):
|
||||
self.addError('No _search.html found, exiting')
|
||||
return self.report
|
||||
|
||||
try:
|
||||
dom = parse(src + '/_search.html')
|
||||
except xml.dom.DOMException as e:
|
||||
self.addError('Error parsing _search.html : ' + e)
|
||||
return self.report
|
||||
|
||||
post_nodes = dom.getElementsByTagNameNS(self.URI, "posts")
|
||||
if not post_nodes is None:
|
||||
if post_nodes[0].hasAttribute("limit"):
|
||||
self.posts_per_page = int(post_nodes[0].getAttribute("limit"))
|
||||
else:
|
||||
self.posts_per_page = 25
|
||||
else:
|
||||
self.addError('No tag dyn:posts found')
|
||||
|
||||
if len(post_list) == 0:
|
||||
self.hooks['posts'] = self.createNoResultFound
|
||||
|
||||
posts = []
|
||||
for post_id in post_list:
|
||||
post = Post.objects.get(pk=post_id)
|
||||
if not post is None:
|
||||
posts.append(post)
|
||||
|
||||
nodes = dom.getElementsByTagName("*")
|
||||
nodes[0] = self.parse(src, self.hooks, posts, dom, nodes[0])
|
||||
|
||||
writer = StrictUTF8Writer()
|
||||
nodes[0].writexml(writer)
|
||||
return writer.getvalue().encode('utf-8')
|
||||
|
|
@ -25,9 +25,12 @@ class Tag(Index):
|
|||
from dynastie.models import Post, Blog, Tag
|
||||
|
||||
self.hooks = {'posts' : self.createPosts,
|
||||
'navigation' : self.createNavigation,
|
||||
'tag' : self.createTag,
|
||||
'tags' : self.createTags}
|
||||
'navigation' : self.createNavigation,
|
||||
'tag' : self.createTag,
|
||||
'tags' : self.createTags,
|
||||
'replace' : self.createReplace}
|
||||
|
||||
self.blog = blog
|
||||
|
||||
if not os.path.exists(src + '/_tag.html'):
|
||||
self.addError('No _tag.html found, exiting')
|
||||
|
|
15
search.py
15
search.py
|
@ -50,6 +50,7 @@ class Search:
|
|||
filename = blog.src_path + '/_search.db'
|
||||
|
||||
if not os.path.exists(filename):
|
||||
print 'No search index !'
|
||||
return None
|
||||
|
||||
f = open(filename, 'rb')
|
||||
|
@ -169,16 +170,16 @@ class Search:
|
|||
def search(self, blog, string):
|
||||
hashtable = self._loadDatabase(blog)
|
||||
|
||||
string = self._prepare_string(string)
|
||||
string = self._prepare_string(string.encode('utf-8'))
|
||||
|
||||
wordlist = re.findall(self.wordreg, string)
|
||||
|
||||
res = {}
|
||||
for word in wordlist:
|
||||
if len(word) < 4:
|
||||
if len(word) < Search.MINIMUM_LETTERS:
|
||||
continue
|
||||
word = word.lower()
|
||||
while not word in hashtable and len(word) > 3:
|
||||
while not word in hashtable and len(word) > Search.MINIMUM_LETTERS:
|
||||
word = word[:-1]
|
||||
if word not in hashtable:
|
||||
continue
|
||||
|
@ -186,7 +187,11 @@ class Search:
|
|||
if not post in res:
|
||||
res[post] = 0
|
||||
res[post] = res[post] + 1
|
||||
|
||||
sorted_res = sorted(res.iteritems(), key=operator.itemgetter(1))
|
||||
|
||||
return sorted_res.reverse()
|
||||
sorted_res.reverse()
|
||||
|
||||
res = []
|
||||
for i in range(len(sorted_res)):
|
||||
res .append(sorted_res[i][0])
|
||||
return res
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
<link href="/rss.xml" rel="alternate" type="application/rss+xml" title="RSS 2.0" />
|
||||
<link href="/atom.xml" rel="alternate" type="application/atom+xml" title="Atom 1.0" />
|
||||
<link href="/css/blog.css" rel="stylesheet" type="text/css"/>
|
||||
<script type="text/javascript" src="/js/blog.js"> </script>
|
||||
</head>
|
||||
<body>
|
||||
<img id="logo" src="/images/tux_final.png"/>
|
||||
|
@ -23,17 +24,21 @@
|
|||
<dyn:block name="content"/>
|
||||
</div>
|
||||
<div class="menu">
|
||||
<!-- <div class="menu_content"> -->
|
||||
<!-- <div class="menu_content_header">Search</div> -->
|
||||
<!-- <div class="menu_content_content">La recherche</div> -->
|
||||
<!-- </div> -->
|
||||
<div class="menu_content">
|
||||
<div class="menu_content_header">Recherche</div>
|
||||
<div id="menu_main">
|
||||
<dyn:replace div_name="form" id="search_form" method="POST" action="/search/dyn:blog_id">
|
||||
<input type="text" name="text" onkeypress="handleKeyPress(event,this.form)"/>
|
||||
</dyn:replace>
|
||||
</div>
|
||||
</div>
|
||||
<div class="menu_content">
|
||||
<div class="menu_content_header">Menu principal</div>
|
||||
<div id="menu_main">
|
||||
<div class="menu_content_content"><a href="/">Première page</a></div>
|
||||
<div class="menu_content_content"><a href="/about.html">À propos</a></div>
|
||||
<div class="menu_content_content"><a href="http://indefero.soutade.fr">Projets personnels</a></div>
|
||||
</div>
|
||||
<div class="menu_content_content"><a href="/">Première page</a></div>
|
||||
<div class="menu_content_content"><a href="/about.html">À propos</a></div>
|
||||
<div class="menu_content_content"><a href="http://indefero.soutade.fr">Projets personnels</a></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="menu_content">
|
||||
<div class="menu_content_header">Catégories</div>
|
||||
|
|
|
@ -26,10 +26,14 @@
|
|||
<dyn:block name="content"/>
|
||||
</div>
|
||||
<div class="menu">
|
||||
<!-- <div class="menu_content"> -->
|
||||
<!-- <div class="menu_content_header">Search</div> -->
|
||||
<!-- <div class="menu_content_content">La recherche</div> -->
|
||||
<!-- </div> -->
|
||||
<div class="menu_content">
|
||||
<div class="menu_content_header">Recherche</div>
|
||||
<div id="menu_main">
|
||||
<dyn:replace div_name="form" id="search_form" method="POST" action="/search/dyn:blog_id">
|
||||
<input type="text" name="text" onkeypress="handleKeyPress(event,this.form)"/>
|
||||
</dyn:replace>
|
||||
</div>
|
||||
</div>
|
||||
<div class="menu_content">
|
||||
<div class="menu_content_header">Menu principal</div>
|
||||
<div id="menu_main">
|
||||
|
|
|
@ -443,3 +443,9 @@ ul li
|
|||
.color_emacs_vg { color: #B8860B } /* Name.Variable.Global */
|
||||
.color_emacs_vi { color: #B8860B } /* Name.Variable.Instance */
|
||||
.color_emacs_il { color: #666666 } /* Literal.Number.Integer.Long */
|
||||
|
||||
#search_form
|
||||
{
|
||||
padding:0;
|
||||
margin:0;
|
||||
}
|
||||
|
|
|
@ -91,3 +91,11 @@ function validateComment(id)
|
|||
|
||||
return true;
|
||||
}
|
||||
|
||||
function handleKeyPress(e){
|
||||
var key=e.keyCode || e.which;
|
||||
if (key==13){
|
||||
f = document.getElementById("search_form");
|
||||
f.submit();
|
||||
}
|
||||
}
|
BIN
static/js/tinymce/jscripts/tiny_mce/plugins/dynastiecolor/img/dynastiecolor.png
vendored
Normal file
BIN
static/js/tinymce/jscripts/tiny_mce/plugins/dynastiecolor/img/dynastiecolor.png
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 483 B |
3
templates/search.html
Normal file
3
templates/search.html
Normal file
|
@ -0,0 +1,3 @@
|
|||
{% autoescape off %}
|
||||
{{ result }}
|
||||
{% endautoescape %}
|
1
urls.py
1
urls.py
|
@ -35,6 +35,7 @@ urlpatterns = patterns('',
|
|||
url(r'^tag/edit/(\d+)$', 'dynastie.views.edit_tag', name='edit_tag'),
|
||||
url(r'^tag/delete/(\d+)$', 'dynastie.views.delete_tag', name='delete_tag'),
|
||||
url(r'^search/generate/(\d+)$', 'dynastie.views.generate_search',name='generate_search'),
|
||||
url(r'^search/(\d+)$', 'dynastie.views.search', name='search'),
|
||||
# url(r'^dynastie/', include('dynastie.foo.urls')),
|
||||
|
||||
# Uncomment the admin/doc line below to enable admin documentation:
|
||||
|
|
32
views.py
32
views.py
|
@ -526,6 +526,38 @@ def generate_search(request, blog_id):
|
|||
|
||||
return _generate(request, blog_id, report)
|
||||
|
||||
@csrf_exempt
|
||||
def search(request, blog_id):
|
||||
from dynastie.generators import search
|
||||
|
||||
ref = request.META['HTTP_REFERER']
|
||||
|
||||
b = Blog.objects.filter(pk=blog_id)
|
||||
|
||||
if len(b) == 0:
|
||||
return HttpResponseRedirect(ref)
|
||||
|
||||
b = b[0]
|
||||
|
||||
if 'text' in request.POST:
|
||||
text = request.POST['text']
|
||||
else:
|
||||
return HttpResponseRedirect(ref)
|
||||
|
||||
s = Search()
|
||||
post_list = s.search(b, text)
|
||||
|
||||
if post_list is None: post_list = []
|
||||
|
||||
s = search.Search()
|
||||
b.create_paths()
|
||||
|
||||
res = s.generate(b, b.src_path, b.output_path, post_list)
|
||||
|
||||
c = {'result' : res}
|
||||
|
||||
return render(request, 'templates/search.html', c)
|
||||
|
||||
@login_required
|
||||
def preview(request, blog_id):
|
||||
from dynastie.generators import post
|
||||
|
|
Loading…
Reference in New Issue
Block a user