refactored search engine, added new options
This commit is contained in:
parent
518ea259cd
commit
e92e5686c3
|
@ -9,7 +9,7 @@
|
||||||
<input type="text" name="title" placeholder="Search">
|
<input type="text" name="title" placeholder="Search">
|
||||||
<input type="submit">
|
<input type="submit">
|
||||||
</form>
|
</form>
|
||||||
<a href="{{ url('quest:index') }}">Advanced</a><br>
|
<a href="{{ url('quest:index') }}">Quests</a><br>
|
||||||
<br>
|
<br>
|
||||||
<a href="./quest/1">Unga Bunga Quest</a><br />
|
<a href="./quest/1">Unga Bunga Quest</a><br />
|
||||||
{% if request.user.is_authenticated %}
|
{% if request.user.is_authenticated %}
|
||||||
|
|
|
@ -431,10 +431,11 @@ def new_page(socket, data):
|
||||||
else:
|
else:
|
||||||
page_num = 'a'
|
page_num = 'a'
|
||||||
else:
|
else:
|
||||||
page_num = Page.objects.filter(
|
last_page = Page.objects.filter(
|
||||||
quest=quest,
|
quest=quest,
|
||||||
appendix=False
|
appendix=False
|
||||||
).order_by('page_num').last().page_num + 1
|
).order_by('page_num').last()
|
||||||
|
page_num = int(last_page.page_num) + 1
|
||||||
p = Page(
|
p = Page(
|
||||||
quest=quest,
|
quest=quest,
|
||||||
page_num=page_num,
|
page_num=page_num,
|
||||||
|
|
|
@ -5,20 +5,43 @@
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<form method="get" action="{{ url('quest:index') }}">
|
<form method="get" action="{{ url('quest:index') }}">
|
||||||
Author: <input type="text" name="author"><br>
|
Search terms: <input type="text" name="search_terms">
|
||||||
Title: <input type="text" name="title"><br>
|
<select name="search_type">
|
||||||
Tags: <input type="text" name="tags"><br>
|
<option value="title"{% if search_type == 'title' %} selected{% endif %}>Title</option>
|
||||||
<input type="submit">
|
<option value="author"{% if search_type == 'author' %} selected{% endif %}>Author</option>
|
||||||
|
</select>
|
||||||
|
<br>Tags: <input type="text" name="tags">
|
||||||
|
<br>Order by:
|
||||||
|
<select name="order_by">
|
||||||
|
<option value="creation_date"{% if order_by == 'creation_date' %} selected{% endif %}>Creation date</option>
|
||||||
|
<option value="last_post"{% if order_by == 'last_post' %} selected{% endif %}>Last post</option>
|
||||||
|
<option value="next_live"{% if order_by == 'next_live' %} selected{% endif %}>Next live</option>
|
||||||
|
</select>
|
||||||
|
<select name="order_way">
|
||||||
|
<option value="desc"{% if order_way == 'desc' %} selected{% endif %}>Descending</option>
|
||||||
|
<option value="asc"{% if order_way == 'asc' %} selected{% endif %}>Ascending</option>
|
||||||
|
</select>
|
||||||
|
<br><input type="submit">
|
||||||
</form>
|
</form>
|
||||||
|
<h2>Quests</h2>
|
||||||
{% if results %}
|
{% if results %}
|
||||||
<table id="results">
|
<table id="results">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Title</th>
|
||||||
|
<th>Description</th>
|
||||||
|
<th>Author</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
{% for quest in results %}
|
{% for quest in results %}
|
||||||
<tr>
|
<tr>
|
||||||
<td class="title"><a href="{{ url('quest:quest', args=[quest.id, '0']) }}">{{ quest.title }}</a></td>
|
<td class="title"><a href="{{ url('quest:quest', args=[quest.id, '0']) }}">{{ quest.title }}</a></td>
|
||||||
<td class="summary">{{ quest.description or "" }}</td>
|
<td class="description">{{ quest.description or "" }}</td>
|
||||||
<td class="author"><a href="{{ url('user:profile', args=[quest.owner.id]) }}">{{ quest.owner.username }}</a></td>
|
<td class="author"><a href="{{ url('user:profile', args=[quest.owner.id]) }}">{{ quest.owner.username }}</a></td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
@ -162,7 +162,7 @@
|
||||||
</div>
|
</div>
|
||||||
<div id="QMPage" class="QMPostTabContent" style="display:none;">
|
<div id="QMPage" class="QMPostTabContent" style="display:none;">
|
||||||
<form id="QMPageForm" action="javascript:void(0);" onsubmit="form_post('QMPageForm', 'new_page');">
|
<form id="QMPageForm" action="javascript:void(0);" onsubmit="form_post('QMPageForm', 'new_page');">
|
||||||
New Page: <input type="text" name="page_title" maxlength="200" value="Page {{ pages.count() + 1 }}"><br>
|
New Page: <input type="text" name="page_title" maxlength="200" value="Page {{ pages.last().page_num|int + 1 }}"><br>
|
||||||
Appendix: <input type="checkbox" name="appendix"><br>
|
Appendix: <input type="checkbox" name="appendix"><br>
|
||||||
<input type="submit" name="submit" value="Submit">
|
<input type="submit" name="submit" value="Submit">
|
||||||
</form>
|
</form>
|
||||||
|
|
18
quest/migrations/0009_quest_latest_post_date.py
Normal file
18
quest/migrations/0009_quest_latest_post_date.py
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
# Generated by Django 2.1.1 on 2018-10-05 13:21
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('quest', '0008_auto_20181005_0825'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='quest',
|
||||||
|
name='latest_post_date',
|
||||||
|
field=models.DateTimeField(blank=True, null=True),
|
||||||
|
),
|
||||||
|
]
|
|
@ -21,6 +21,15 @@ class Quest(models.Model):
|
||||||
tags = TaggableManager()
|
tags = TaggableManager()
|
||||||
anonymize = models.BooleanField(default=True)
|
anonymize = models.BooleanField(default=True)
|
||||||
description = models.CharField(max_length=256, blank=True, null=True)
|
description = models.CharField(max_length=256, blank=True, null=True)
|
||||||
|
latest_post_date = models.DateTimeField(null=True, blank=True)
|
||||||
|
|
||||||
|
def update_post_date(self):
|
||||||
|
try:
|
||||||
|
latest_post = self.post_set.order_by('-timestamp')[0]
|
||||||
|
self.latest_post_date = latest_post.timestamp
|
||||||
|
self.save()
|
||||||
|
except:
|
||||||
|
pass # no posts yet
|
||||||
|
|
||||||
|
|
||||||
class Message(models.Model):
|
class Message(models.Model):
|
||||||
|
@ -65,6 +74,14 @@ class Post(models.Model):
|
||||||
post_text = models.TextField()
|
post_text = models.TextField()
|
||||||
timestamp = models.DateTimeField(auto_now=True)
|
timestamp = models.DateTimeField(auto_now=True)
|
||||||
|
|
||||||
|
def save(self, **kwargs):
|
||||||
|
super(Post, self).save(**kwargs)
|
||||||
|
self.quest.update_post_date()
|
||||||
|
|
||||||
|
def delete(self):
|
||||||
|
super(Post, self).delete()
|
||||||
|
self.quest.update_post_date()
|
||||||
|
|
||||||
|
|
||||||
class DiceCall(models.Model):
|
class DiceCall(models.Model):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -10,14 +10,24 @@
|
||||||
border: 1px solid #ccc;
|
border: 1px solid #ccc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
thead {
|
||||||
|
border: 1px solid #ccc;
|
||||||
|
}
|
||||||
|
|
||||||
tr:nth-child(even) {
|
tr:nth-child(even) {
|
||||||
background-color: #DDDDDD;
|
background-color: #DDDDDD;
|
||||||
}
|
}
|
||||||
|
|
||||||
.title {
|
.title {
|
||||||
width: 20%;
|
width: 20%;
|
||||||
|
padding-left: 1em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.description {
|
||||||
|
padding-left: 1em;
|
||||||
}
|
}
|
||||||
|
|
||||||
.author {
|
.author {
|
||||||
width: 20%;
|
width: 20%;
|
||||||
|
text-align: center;
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ import bleach
|
||||||
from django.views.decorators.http import require_POST
|
from django.views.decorators.http import require_POST
|
||||||
from django.contrib import messages
|
from django.contrib import messages
|
||||||
from django.shortcuts import render, redirect
|
from django.shortcuts import render, redirect
|
||||||
|
from django.db.models import F
|
||||||
|
|
||||||
from .models import Quest, DiceRoll, PollOption, PollVote, Page, Post
|
from .models import Quest, DiceRoll, PollOption, PollVote, Page, Post
|
||||||
from .forms import EditQuestForm, QuestForm, PostForm
|
from .forms import EditQuestForm, QuestForm, PostForm
|
||||||
|
@ -15,19 +16,33 @@ from user.models import User
|
||||||
|
|
||||||
def index(request):
|
def index(request):
|
||||||
"""The quest page index."""
|
"""The quest page index."""
|
||||||
author = request.GET.get('author')
|
search_terms = request.GET.get('search_terms')
|
||||||
title = request.GET.get('title')
|
search_type = request.GET.get('search_type')
|
||||||
tags = request.GET.get('tags')
|
tags = request.GET.get('tags')
|
||||||
|
order_by = request.GET.get('order_by')
|
||||||
|
order_way = request.GET.get('order_way', 'desc')
|
||||||
|
|
||||||
|
order_keys = {
|
||||||
|
'ceation_date': 'id',
|
||||||
|
'last_post': 'latest_post_date',
|
||||||
|
'next_live': 'live_time',
|
||||||
|
}
|
||||||
|
order_by = order_keys.get(order_by, 'id')
|
||||||
|
|
||||||
results = Quest.objects.all()
|
results = Quest.objects.all()
|
||||||
if author:
|
if search_terms:
|
||||||
|
if search_type == 'title':
|
||||||
|
results = results.filter(title__unaccent__icontains=search_terms)
|
||||||
|
elif search_type == 'author':
|
||||||
results = results.filter(
|
results = results.filter(
|
||||||
owner__username__unaccent__icontains=author)
|
owner__username__unaccent__icontains=search_terms)
|
||||||
if title:
|
|
||||||
results = results.filter(title__unaccent__icontains=title)
|
|
||||||
if tags:
|
if tags:
|
||||||
results = results.filter(tags__name__in=tags.split())
|
results = results.filter(tags__name__in=tags.split()).distinct()
|
||||||
results = results.distinct().order_by('-id')
|
|
||||||
|
if order_way == 'asc':
|
||||||
|
results = results.order_by(F(order_by).asc(nulls_last=True))
|
||||||
|
elif order_way == 'desc':
|
||||||
|
results = results.order_by(F(order_by).desc(nulls_last=True))
|
||||||
|
|
||||||
context = locals()
|
context = locals()
|
||||||
return render(request, 'quest/index.html', context)
|
return render(request, 'quest/index.html', context)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user