1
0
mirror of https://github.com/djohnlewis/stackdump synced 2024-12-04 23:17:37 +00:00

Implemented random questions on home pages. Also consolidated the index pages into a single template.

This commit is contained in:
Samuel Lai 2012-02-11 22:32:23 +11:00
parent 6c58938d44
commit 06e210d37a
7 changed files with 52 additions and 58 deletions

View File

@ -565,8 +565,10 @@
<dynamicField name="ignored_*" type="ignored" multiValued="true"/>
<dynamicField name="attr_*" type="text_general" indexed="true" stored="true" multiValued="true"/>
<dynamicField name="random_*" type="random" />
-->
<!-- this random field allows retrieval of random records by sorting by a
field named random_* where the * is a random number/string. -->
<dynamicField name="random_*" type="random" indexed="true" stored="false" />
<!-- uncomment the following to ignore any fields that don't already match an existing
field name or dynamic field, rather than reporting them as an error.

View File

@ -192,6 +192,10 @@ pre code {
float: right;
}
.post-tags {
margin-bottom: 10px;
}
.question h1 {
border-bottom: 1px solid #CCCCCC;
padding-bottom: 7px;

View File

@ -4,6 +4,7 @@ import threading
import functools
import re
import math
import random
try:
# For Python < 2.6 or people using a newer version of simplejson
@ -192,27 +193,33 @@ def serve_static(filename):
@get('/')
@uses_templates
@uses_solr
@uses_db
def index():
context = { }
context['site_root_path'] = ''
context['sites'] = Site.select()
context['random_questions'] = get_random_questions()
return render_template('index.html', context)
@get('/:site_key#[\w\.]+#')
@get('/:site_key#[\w\.]+#/')
@uses_templates
@uses_solr
@uses_db
def site_index(site_key):
context = { }
context['site_root_path'] = '%s/' % site_key
context['sites'] = Site.select()
try:
context['site'] = Site.selectBy(key=site_key).getOne()
except SQLObjectNotFound:
raise HTTPError(code=404, output='No site exists with the key %s.' % site_key)
return render_template('site_index.html', context)
context['random_questions'] = get_random_questions(site_key=site_key)
return render_template('index.html', context)
@get('/search')
@uses_templates
@ -220,7 +227,6 @@ def site_index(site_key):
@uses_db
def search():
context = { }
context['site_root_path'] = ''
context['sites'] = Site.select()
search_context = perform_search()
@ -237,7 +243,6 @@ def search():
@uses_db
def site_search(site_key):
context = { }
context['site_root_path'] = '%s/' % site_key
# the template uses this to allow searching on other sites
context['sites'] = Site.select()
@ -261,7 +266,6 @@ def site_search(site_key):
@uses_db
def view_question(site_key, question_id):
context = { }
context['site_root_path'] = '%s/' % site_key
try:
context['site'] = Site.selectBy(key=site_key).getOne()
@ -533,6 +537,19 @@ def perform_search(site_key=None):
return context
def get_random_questions(site_key=None, count=3):
random_field_name = 'random_%d %s' % (random.randint(1000, 9999), random.choice(['asc', 'desc']))
query = '*:*'
if site_key:
query = ' siteKey:%s' % site_key
results = solr_conn().search(query, rows=count, sort=random_field_name)
decode_json_fields(results)
retrieve_users(results, question_only=True, ignore_comments=True)
retrieve_sites(results)
return results
# END VIEW HELPERS
# INITIALISATION

View File

@ -17,13 +17,13 @@
<a href="{{ SETTINGS.APP_URL_ROOT }}">Stackdump</a>
{% if site %}
<span class="topbar-divider">//</span>
<a href="{{ SETTINGS.APP_URL_ROOT }}{{ site_root_path }}">{{ site.name }}</a>
<a href="{{ SETTINGS.APP_URL_ROOT }}{{ site.key }}">{{ site.name }}</a>
from {{ site.dump_date }}
{% endif %}
</span>
<form method="get" action="{{ SETTINGS.APP_URL_ROOT }}{{ site_root_path }}search" class="pull-right">
<form method="get" action="{{ SETTINGS.APP_URL_ROOT }}{% if site %}{{ site.key }}/{% endif %}search" class="pull-right">
<input type="text" placeholder="Search {% if site %}{{ site.name }}{% endif %}" />
</form>
</div>

View File

@ -44,6 +44,7 @@
</li>
{% endfor %}
</ul>
{% if total_pages and total_pages > 1 %}
<div class="pagination">
<ul>
{% if current_page > 1 %}
@ -63,4 +64,5 @@
{% endif %}
</ul>
</div>
{% endif %}
{% endif %}

View File

@ -1,16 +1,30 @@
{% extends 'base.html' %}
{% block title %}Stackdump Home{% endblock %}
{% block title %}
{% if site %}
Stackdump // {{ site.name }} Home
{% else %}
Stackdump Home
{% endif %}
{% endblock %}
{% block body %}
<div class="row">
<div class="span12">
<div class="search-container">
{% if site %}
<img class="site-logo" src="{{ SETTINGS.APP_URL_ROOT }}media/logos/{{ site.key }}.png" alt="{{ site.name }} logo" />
<h1 class="site-title">
{{ site.name }}
<small class="tagline">{{ site.desc }}</small>
</h1>
{% else %}
<h1>
Welcome to Stackdump,
<small class="tagline">the offline browser for StackExchange sites.</small>
</h1>
<form id="search" method="get" action="{{ SETTINGS.APP_URL_ROOT }}search">
{% endif %}
<form id="search" method="get" action="{{ SETTINGS.APP_URL_ROOT }}{% if site %}{{ site.key }}/{% endif %}search">
<input type="text" class="xlarge" name="q" />
<input type="submit" class="btn primary" value="Search" />
</form>
@ -18,17 +32,8 @@
<div class="random-questions-container">
<h3>Random questions</h3>
<ul class="random-questions">
<li>
Question 1
</li>
<li>
Question 2
</li>
<li>
Question 3
</li>
</ul>
{% set results = random_questions %}
{% include 'includes/results.html.inc' %}
</div>
</div>
<div class="span4">

View File

@ -1,36 +0,0 @@
{% extends 'base.html' %}
{% block title %}Stackdump // {{ site.name }} Home{% endblock %}
{% block body %}
<div class="row">
<div class="span16">
<div class="search-container">
<img class="site-logo" src="{{ SETTINGS.APP_URL_ROOT }}media/logos/{{ site.key }}.png" alt="{{ site.name }} logo" />
<h1 class="site-title">
{{ site.name }}
<small class="tagline">{{ site.desc }}</small>
</h1>
<form id="search" method="get" action="{{ SETTINGS.APP_URL_ROOT }}{{ site_root_path }}search">
<input type="text" class="xlarge" name="q" />
<input type="submit" class="btn primary" value="Search" />
</form>
</div>
<div class="random-questions-container">
<h3>Random questions</h3>
<ul class="random-questions">
<li>
Question 1
</li>
<li>
Question 2
</li>
<li>
Question 3
</li>
</ul>
</div>
</div>
</div>
{% endblock %}