add ga_search, password confirm, etc.

This commit is contained in:
Tan, Kian-ting 2021-01-24 20:40:31 +08:00
parent ad46a18acd
commit 96f8fd849d
20 changed files with 249 additions and 45 deletions

3
.gitignore vendored
View file

@ -127,3 +127,6 @@ dmypy.json
# Pyre type checker
.pyre/
# VScode
.vscode/

View file

@ -52,6 +52,6 @@ def add_entry(request):
}
return render(request, 'addentry/index.html', context)
return render(request, 'addentry/add_entry.html', context)

View file

@ -126,3 +126,11 @@ STATICFILES_DIRS = [
]
STATIC_URL = '/static/'
# Redirect to add_entry after login (Default redirects to /accounts/profile/)
LOGIN_REDIRECT_URL = '/add_entry/'
# for sending password resetting mail
EMAIL_BACKEND = 'django.core.mail.backends.filebased.EmailBackend'
EMAIL_FILE_PATH = '/tmp/app-messages' # change this to a proper location

View file

@ -14,15 +14,18 @@ Including another URLconf
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.urls import path, include
from addentry.views import hello_world, add_entry
from search.views import index_search, ga_search
from search.views import ga_search
urlpatterns = [
path('accounts/', include('django.contrib.auth.urls')), # account verification, login/logout, etc.
path('admin/', admin.site.urls),
path('hello/', hello_world),
path('add_entry/', add_entry),
path('index', index_search),
path('', index_search),
path('index.html', ga_search, name="ga_search"),
path('ga_search/<str:entry>/', ga_search, name="ga_search"),
path('ga_search/', ga_search, name="ga_search"),
]

View file

@ -1,5 +1,6 @@
from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.forms.models import model_to_dict
from addentry.models import Entry, Meaning
from django.db.models.query import QuerySet
@ -10,12 +11,11 @@ from .forms import SearchForm
# index.html-in-chief. as a search tool.
def index_search(request):
search_form = SearchForm()
context = {
'form' : search_form
}
# return Gaeilge search result
def ga_search(request, entry=""):
search_form = SearchForm(initial={'choice_field': 'Gaeilge'})
if request.method == "POST":
regForm = SearchForm(request.POST)
@ -24,22 +24,17 @@ def index_search(request):
search_content = regForm.cleaned_data["search_content"]
if option == 'Gaeilge':
print("123354")
return redirect("ga_search", entry=search_content)
if option == 'Explanation':
# TODO
return HttpResponse("TODO")
return render(request, "index.html", context)
# return Gaeilge search result
def ga_search(request, entry):
entry_list = Entry.objects.filter(Gaeilge=entry)
entry_id_list= list(map(lambda x : x.id, entry_list))
meaning_result_list = list(map(lambda x : Meaning.objects.filter(Entry=x), entry_id_list))
# 攤平例句
def flatten_sentence(e):
result = e.Meaning_content
if e.Exam_Sentence1 != None:
@ -51,7 +46,7 @@ def ga_search(request, entry):
return result
# 將函數作用到 QuerySet 和 List 的樹的每個元素
def tree_mapping(func, tree):
from django.db.models.query import QuerySet
if isinstance(tree, QuerySet) or isinstance(tree, list):
@ -65,9 +60,28 @@ def ga_search(request, entry):
else:
return func(tree)
'''# 攤平眾釋義成一個例句
def flatten_meanings(item):
pass'''
# [['word1 meaning1'], ['word2 meaning1', 'word2 meaning2'], ...]
meaning_result_list_sent_flattened = tree_mapping(flatten_sentence, meaning_result_list)
#TODO
entry_dict_list = list(map(model_to_dict, entry_list))
for i in range(len(entry_dict_list)):
entry_dict_list[i]['Explanation'] = meaning_result_list_sent_flattened[i]
return HttpResponse(meaning_result_list_sent_flattened.__str__)
''' keys of entry_dict :
id (int), Gaeilge, POS, Pronoun, PreScript, Pronoun, PostScript, Explanation (a list)
'''
context = {
'entry' : entry,
'form' : search_form,
'entry_len' : len(entry_dict_list),
'entry_list' : entry_dict_list
}
#return HttpResponse(meaning_result_list_sent_flattened.__str__)
return render(request, 'ga_search.html', context)

View file

@ -5,7 +5,7 @@
<meta charset="utf-8">
<title>增加條目</title>
<link rel="stylesheet" href="{% static 'addentry/index.css' %}">
<link rel="stylesheet" href="{% static 'addentry/add_entry.css' %}">
</head>
<body>
@ -16,17 +16,20 @@
</div>
{% endif %}
<h1>線上愛爾蘭語—華語辭典</h1>
<h2>增加項目與解釋</h2>
<div id="sidebar"><a href="../accounts/logout">登出</a></div>
{% block content %}
<form action="" method="POST">
{% csrf_token %}
<div class="form-group">
<div class="form-group-item">
<h2>項目</h2>
<h3>項目</h3>
{% for field in entry_form %}
{{field.label_tag}} {{field}} <br/>
{% endfor %}
<p/>
<h2>解釋</h2>
<h3>解釋</h3>
<div id="form-box">
{% for field in meaning_form %}
@ -41,7 +44,7 @@
</form>
{% endblock %}
<h2>詞條一覽</h2>
<h3>詞條一覽</h3>
<table class="table table-bordered table-striped">
<thead>
<tr>
@ -67,6 +70,6 @@
{% endfor %}
</table>
<script src="{% static 'addentry/index.js' %}"></script>
<script src="{% static 'addentry/add_entry.js' %}"></script>
</html>

View file

@ -1,4 +0,0 @@
var options = document.getElementById("id_Entry");
options.value = "1";

View file

@ -0,0 +1,11 @@
<html lang="zh">
<head>
<meta charset="utf-8">
{% block title %}<title>愛爾蘭語—華語線上辭典</title>{% endblock %}
</head>
<body>
{% block content %}<!-- default content text (typically empty) -->{% endblock %}
</body>
</html>

View file

@ -0,0 +1,57 @@
<!DOCTYPE html>
<html>
<head>
<title>線上愛爾蘭語—漢語辭典 - 愛爾蘭語搜尋 {{entry}} 的結果</title>
</head>
<body>
<div id="header">
<h1>線上愛爾蘭語—華語辭典</h1>
<h2>An Foclóir Gaeilge-Mandairínis ar Líne</h2>
</div>
<div id="search-bar">
<form method="POST">
{% csrf_token %}
<div id="search_content">
<label for="{{form.search_content.id_for_label}}">搜尋內容:</label>{{form.search_content}}
</div>
<div id="options">
{% for option in form.choice_field %}
<li id="{{option.id_for_label}}">{{option.label_tag}} {{option}}</li>
{% endfor %}
</div>
<input type="submit" value="搜尋">
</form>
</div>
<hr/>
<div id="search-result">
{% if entry == "" %}
<!-- Let it blank -->
{% elif entry_len == 0 %}
對不起,找不到條目,請試著檢查有無錯字(或是有沒有輸入)錯字,或是調整語法變化,也有可能您輸入的是用詞變體。<br/>
{% else %}
您要找的搜尋結果如下,共 {{entry_len}} 筆:
{% for e in entry_list %}
<div class="entry">
<span class="gaeilge">{{e.Gaeilge}}</span>
<span class="pos">{{e.PoS}}</span>
<!-- Pronounciation-->
{%if e.Pronoun != None %}
<span class="pronoun">[{{e.Pronoun}}]</span>
{% endif %}
{%if e.PreScript != None %}
<span class="prescript">{{e.PreScript}}</span>
{% endif %}
<ol>
{%for exp in e.Explanation %}
<li class="prescript">{{exp}}</li>
{% endfor %}
</ol>
{%if e.PostScript != None %}
<span class="postscript">註:{{e.PostScript}}</span>
{% endif %}
</div>
{% endfor %}
{% endif %}
</div>
</body>
</html>

View file

@ -1,18 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<title>I come from template!!</title>
<style>
body {
background-color: lightyellow;
}
em {
color: LightSeaGreen;
}
</style>
</head>
<body>
<h1>Hello World!</h1>
<em>{{ current_time }}</em>
</body>
</html>

View file

@ -0,0 +1,16 @@
<body>
<head>
<meta charset="utf-8">
<title>線上愛爾蘭語—華語辭典 - 登出</title>
<head>
<body>
<h1>線上愛爾蘭語—華語辭典</h1>
<h2>您已經登出了。</h2>
<ul>
<li><a href="../login/">重新登入</a></li>
<li><a href="../../index.html">回首頁</a></li>
</ul>
</body>
</html>

View file

@ -0,0 +1,46 @@
<body>
<head>
<meta charset="utf-8">
<title>線上愛爾蘭語—華語辭典 - 登入</title>
<head>
<body>
<h1>線上愛爾蘭語—華語辭典</h1>
<h2>管理後台登入畫面</h2>
{% block content %}
{% if form.errors %}
<p>帳號或密碼不符合,請重新輸入。</p>
{% endif %}
{% if next %}
{% if user.is_authenticated %}
<p>您的帳號沒有此頁的存取權,如果要進行下一步,請登入以另一個具有存取權的帳號。</p>
{% else %}
<p>請登入觀看此頁。</p>
{% endif %}
{% endif %}
<form method="post" action="{% url 'login' %}">
{% csrf_token %}
<div>
<td>帳號:<!-- {{ form.username.label_tag }} --></td>
<td>{{ form.username }}</td>
</div>
<div>
<td>密碼:<!-- {{ form.password.label_tag }}--></td>
<td>{{ form.password }}</td>
</div>
<div>
<input type="submit" value="登入" />
<input type="hidden" name="next" value="{{ next }}" />
</div>
</form>
{# Assumes you setup the password_reset view in your URLconf #}
<p><a href="{% url 'password_reset' %}">忘記密碼?</a></p>
{% endblock %}
</body>
</html>

View file

@ -0,0 +1,6 @@
{% extends "base_generic.html" %}
{% block title %}<title>愛爾蘭語—華語線上辭典 - 密碼更改完成</title>{% endblock %}
{% block content %}
<p>密碼已經更改。</p>
<p><a href="{% url 'login' %}">重新登入</a></p>
{% endblock %}

View file

@ -0,0 +1,31 @@
{% extends "base_generic.html" %}
{% block title %}<title>愛爾蘭語—華語線上辭典 - 重設密碼</title>{% endblock %}
{% block content %}
{% if validlink %}
<p>請確認您的新密碼。</p>
<form action="" method="post">
<div style="display:none">
<input type="hidden" value="{{ csrf_token }}" name="csrfmiddlewaretoken">
</div>
<table>
<tr>
<td>{{ form.new_password1.errors }}
<label for="id_new_password1">新密碼:</label></td>
<td>{{ form.new_password1 }}</td>
</tr>
<tr>
<td>{{ form.new_password2.errors }}
<label for="id_new_password2">確認密碼:</label></td>
<td>{{ form.new_password2 }}</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="更改密碼" /></td>
</tr>
</table>
</form>
{% else %}
<h1>密碼重製失效</h1>
<p>這個密碼重製連結失效,可能是它已經備用過了。請<a href="{% url 'password_reset' %}">再重新取得新的密碼重設連結</a></p>
{% endif %}
{% endblock %}

View file

@ -0,0 +1,5 @@
{% extends "base_generic.html" %}
{% block title %}<title>愛爾蘭語—華語線上辭典 - 密碼確認信已寄出</title>{% endblock %}
{% block content %}
<p>我們已經寄出電子郵件確認信。若是數分鐘內未寄出,請確認您的垃圾郵件匣。</p>
{% endblock %}

View file

@ -0,0 +1 @@
有人想要為線上愛爾蘭語—漢語辭典重設密碼,所以寄到 {{ email }} 這個電子信箱。如果是您本人,請點選此連結:<a href="{{ protocol}}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}">{{ protocol}}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}</a> 繼續後續操作。如果不是請忽略此信。

View file

@ -0,0 +1,22 @@
<body>
<head>
<meta charset="utf-8">
<title>線上愛爾蘭語—華語辭典 - 忘記密碼</title>
<head>
<body>
<h1>線上愛爾蘭語—華語辭典</h1>
<h2>忘記密碼</h2>
{% block content %}
<form action="" method="post">
{% csrf_token %}
{% if form.email.errors %}
很抱歉,電子郵件格式錯誤,請再輸入一次。<!--{{ form.email.errors }}-->
{% endif %}
<p>請輸入密碼確認信要寄到的電子郵件:{{ form.email }}</p>
<input type="submit" class="btn btn-default btn-lg" value="重設密碼">
</form>
{% endblock %}
</body>
</html>