Skip to content

Commit

Permalink
modified archive
Browse files Browse the repository at this point in the history
  • Loading branch information
varunpant committed Dec 27, 2012
1 parent e723cbd commit 0622a13
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 25 deletions.
2 changes: 2 additions & 0 deletions .settings/org.eclipse.ltk.core.refactoring.prefs
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
org.eclipse.ltk.core.refactoring.enable.project.refactoring.history=false
37 changes: 24 additions & 13 deletions controllers/main_controller.py
@@ -1,5 +1,5 @@
import web, calendar
from services import shared_helper, post_service,tag_service,page_service, settings_service,search_service
from services import shared_helper, post_service, tag_service, page_service, settings_service, search_service


###############################################################################
Expand Down Expand Up @@ -63,10 +63,10 @@ def GET(self, slug, page):
count = post_service.count_published_by_tag(tag.slug)
if count < 1:
return notfound("No post with this tag was found!")
offset = start_index(page,blog_settings.items_per_page)
offset = start_index(page, blog_settings.items_per_page)
posts = post_service.find_published_by_tag(tag.slug, offset, blog_settings.items_per_page)
title = "Topic: " + tag.title + "(" + str(count) +")"
page_count = total_page(count,blog_settings.items_per_page)
title = "Topic: " + tag.title + "(" + str(count) + ")"
page_count = total_page(count, blog_settings.items_per_page)
nextLink = previousLink = None
if page < page_count:
nextLink = "/archives/" + str(page + 1)
Expand All @@ -79,7 +79,10 @@ def GET(self, slug, page):
class Archives:
def GET(self):
posts = post_service.find_recent(blog_settings.items_per_page)
archives = post_service.get_archives()
_archives = post_service.get_archives()
archives = []
for archive in _archives:
archives.append({'full_month':calendar.month_name[archive.month],'month':archive.month, 'year':archive.year, 'posts_count':archive.posts_count})
return render.archive(posts, archives)

class ArchivePage:
Expand Down Expand Up @@ -210,12 +213,20 @@ class Search:
def GET(self):
q = web.input().q
page = safe_number(web.input(page="1").page)
result = search_service.search(q)
offset = start_index(page, blog_settings.items_per_page) + blog_settings.posts_in_home
count = safe_number(search_service.getCount(q)[0])
result = []
msg = None
page_count = total_page(50 - blog_settings.posts_in_home, blog_settings.items_per_page)
nextLink = previousLink = None
if page < page_count:
nextLink = "/search?q=%s&page=%s"%(q,str(page + 1))
if page > 1 :
previousLink = "/search?q=%s&page=%s"%(q,str(page - 1))
return render.search(q,result,nextLink,previousLink,msg)
nextLink = previousLink = None
if count > 0:
result = search_service.search(q, offset, blog_settings.items_per_page)
page_count = total_page(count - blog_settings.posts_in_home, blog_settings.items_per_page)
if page < page_count:
nextLink = "/search?q=%s&page=%s" % (q, str(page + 1))
if page > 1 :
previousLink = "/search?q=%s&page=%s" % (q, str(page - 1))
else:
msg = "No results were found for query \" " + q + " \""


return render.search(q, result, nextLink, previousLink, msg)
10 changes: 8 additions & 2 deletions services/search_service.py
@@ -1,8 +1,14 @@
from models import getSession

def search(q):
def search(q, offset, items_per_page):
session = getSession()
connection = session.connection()
resp = connection.execute("SELECT * FROM search where search MATCH '%s'"%q)
resp = connection.execute("SELECT * from(SELECT * FROM search where title LIKE '%" + str(q) + "%' UNION SELECT * FROM search where content LIKE '%" + str(q) + "%' ) ORDER BY title LIMIT " + str(items_per_page) + " OFFSET " + str(offset))
return resp.fetchall()

def getCount(q):
session = getSession()
connection = session.connection()
resp = connection.execute("SELECT count(*) from(SELECT * FROM search where title LIKE '%" + q + "%' UNION SELECT * FROM search where content LIKE '%" + q + "%' )")
return resp.fetchone()

20 changes: 15 additions & 5 deletions tests/search_service.py
Expand Up @@ -5,7 +5,7 @@
from models import createSchema, dropSchema, executeRaw


class TagServiceQueryingTestCase(unittest.TestCase):
class SearchServiceQueryingTestCase(unittest.TestCase):

def setUp(self):

Expand All @@ -24,14 +24,24 @@ def tearDown(self):


def test_search(self):
results = self.service.search('hello')
results = self.service.search('hello',0,5)
self.assertEqual(len(results), 3)
results = self.service.search('sup')
results = self.service.search('sup', 0, 5)
self.assertEqual(len(results), 1)
results = self.service.search('World')
results = self.service.search('World',0,5)
self.assertEqual(len(results), 4)
results = self.service.search('post')
results = self.service.search('post',0,5)
self.assertEqual(len(results), 4)

def test_search_count(self):
results = self.service.getCount('hello')
self.assertEqual(results[0], 3)
results = self.service.getCount('sup')
self.assertEqual(results[0], 1)
results = self.service.getCount('World')
self.assertEqual(results[0], 4)
results = self.service.getCount('post')
self.assertEqual(results[0], 4)

if __name__ == '__main__':
unittest.main()
Expand Down
5 changes: 2 additions & 3 deletions views/themes/light/archive.html
Expand Up @@ -16,9 +16,8 @@ <h4>Recent</h4>
<h4>Archive</h4>

<ul id="monthly">
$for post in archives:
$for archive in archives:
<li>
<a href="/archives/1/$:post.year/$:post.month" rel="directory">$:post.month $:post.year &nbsp;&nbsp;($:post.posts_count)</a>
<a href="/archives/1/$:archive['year']/$:archive['month']" rel="directory">$:archive['full_month'] $:archive['year'] &nbsp;&nbsp;($:archive['posts_count'])</a>
</li>

</ul>
10 changes: 8 additions & 2 deletions views/themes/light/search.html
@@ -1,4 +1,4 @@
$def with (query,searchResults,next,previous,msg)
$def with (query,searchResults,nextLink,previousLink,msg)
$var title = "Search results for query: " + query
<h4 style="margin-bottom:10px; ">Search Results for: $:query</h4>
$if msg:
Expand All @@ -22,4 +22,10 @@ <h4><a href="/posts/$:post.slug">$:post.slug</a></h4>
<a href="/posts/$:post.slug"><b>read on</b> &raquo;</a>
</div>

</article>
</article>
<div id="post-nextprev" class="clearfix">
$if previousLink:
<div id="post-next" > <a class="btn btn-inverse" href="$:previousLink" rel="prev">« More recent articles</a></div>
$if nextLink:
<div id="post-prev" ><a class="btn btn-inverse" href="$:nextLink" rel="next">Previous articles »</a></div>
</div>

0 comments on commit 0622a13

Please sign in to comment.