From 96f8fd849d8e06e8d84a3ef1dbdc15b088141f1b Mon Sep 17 00:00:00 2001 From: "Chen, Chien-ting" Date: Sun, 24 Jan 2021 20:40:31 +0800 Subject: [PATCH] add ga_search, password confirm, etc. --- .gitignore | 3 + gazhdict/addentry/views.py | 2 +- gazhdict/gazhdict/settings.py | 8 +++ gazhdict/gazhdict/urls.py | 9 ++- gazhdict/search/views.py | 42 +++++++++----- .../addentry/{index.css => add_entry.css} | 0 .../addentry/{index.js => add_entry.js} | 0 .../addentry/{index.ts => add_entry.ts} | 0 .../addentry/{index.html => add_entry.html} | 13 +++-- gazhdict/templates/addentry/index.js | 4 -- gazhdict/templates/base_generic.html | 11 ++++ gazhdict/templates/ga_search.html | 57 +++++++++++++++++++ gazhdict/templates/hello_world.html | 18 ------ .../templates/registration/logged_out.html | 16 ++++++ gazhdict/templates/registration/login.html | 46 +++++++++++++++ .../registration/password_reset_complete.html | 6 ++ .../registration/password_reset_confirm.html | 31 ++++++++++ .../registration/password_reset_done.html | 5 ++ .../registration/password_reset_email.html | 1 + .../registration/password_reset_form.html | 22 +++++++ 20 files changed, 249 insertions(+), 45 deletions(-) rename gazhdict/static/addentry/{index.css => add_entry.css} (100%) rename gazhdict/static/addentry/{index.js => add_entry.js} (100%) rename gazhdict/static/addentry/{index.ts => add_entry.ts} (100%) rename gazhdict/templates/addentry/{index.html => add_entry.html} (81%) delete mode 100644 gazhdict/templates/addentry/index.js create mode 100644 gazhdict/templates/base_generic.html create mode 100644 gazhdict/templates/ga_search.html delete mode 100644 gazhdict/templates/hello_world.html create mode 100644 gazhdict/templates/registration/logged_out.html create mode 100644 gazhdict/templates/registration/login.html create mode 100644 gazhdict/templates/registration/password_reset_complete.html create mode 100644 gazhdict/templates/registration/password_reset_confirm.html create mode 100644 gazhdict/templates/registration/password_reset_done.html create mode 100644 gazhdict/templates/registration/password_reset_email.html create mode 100644 gazhdict/templates/registration/password_reset_form.html diff --git a/.gitignore b/.gitignore index b6e4761..2baea29 100644 --- a/.gitignore +++ b/.gitignore @@ -127,3 +127,6 @@ dmypy.json # Pyre type checker .pyre/ + +# VScode +.vscode/ diff --git a/gazhdict/addentry/views.py b/gazhdict/addentry/views.py index 53cd563..808bc33 100644 --- a/gazhdict/addentry/views.py +++ b/gazhdict/addentry/views.py @@ -52,6 +52,6 @@ def add_entry(request): } - return render(request, 'addentry/index.html', context) + return render(request, 'addentry/add_entry.html', context) diff --git a/gazhdict/gazhdict/settings.py b/gazhdict/gazhdict/settings.py index 96946d2..1542e70 100644 --- a/gazhdict/gazhdict/settings.py +++ b/gazhdict/gazhdict/settings.py @@ -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 + diff --git a/gazhdict/gazhdict/urls.py b/gazhdict/gazhdict/urls.py index 7485ce1..b595248 100644 --- a/gazhdict/gazhdict/urls.py +++ b/gazhdict/gazhdict/urls.py @@ -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//', ga_search, name="ga_search"), + path('ga_search/', ga_search, name="ga_search"), ] diff --git a/gazhdict/search/views.py b/gazhdict/search/views.py index 9eb731a..c1cf418 100644 --- a/gazhdict/search/views.py +++ b/gazhdict/search/views.py @@ -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): @@ -64,10 +59,29 @@ def ga_search(request, entry): return result 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__) \ No newline at end of file + ''' 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) \ No newline at end of file diff --git a/gazhdict/static/addentry/index.css b/gazhdict/static/addentry/add_entry.css similarity index 100% rename from gazhdict/static/addentry/index.css rename to gazhdict/static/addentry/add_entry.css diff --git a/gazhdict/static/addentry/index.js b/gazhdict/static/addentry/add_entry.js similarity index 100% rename from gazhdict/static/addentry/index.js rename to gazhdict/static/addentry/add_entry.js diff --git a/gazhdict/static/addentry/index.ts b/gazhdict/static/addentry/add_entry.ts similarity index 100% rename from gazhdict/static/addentry/index.ts rename to gazhdict/static/addentry/add_entry.ts diff --git a/gazhdict/templates/addentry/index.html b/gazhdict/templates/addentry/add_entry.html similarity index 81% rename from gazhdict/templates/addentry/index.html rename to gazhdict/templates/addentry/add_entry.html index b0ddaa0..5bbfae1 100644 --- a/gazhdict/templates/addentry/index.html +++ b/gazhdict/templates/addentry/add_entry.html @@ -5,7 +5,7 @@ 增加條目 - + @@ -16,17 +16,20 @@ {% endif %} +

線上愛爾蘭語—華語辭典

+

增加項目與解釋

+ {% block content %}
{% csrf_token %}
-

項目

+

項目

{% for field in entry_form %} {{field.label_tag}} {{field}}
{% endfor %}

-

解釋

+

解釋

{% for field in meaning_form %} @@ -41,7 +44,7 @@ {% endblock %} -

詞條一覽

+

詞條一覽

@@ -67,6 +70,6 @@ {% endfor %}
- + diff --git a/gazhdict/templates/addentry/index.js b/gazhdict/templates/addentry/index.js deleted file mode 100644 index baed5ca..0000000 --- a/gazhdict/templates/addentry/index.js +++ /dev/null @@ -1,4 +0,0 @@ - -var options = document.getElementById("id_Entry"); - -options.value = "1"; diff --git a/gazhdict/templates/base_generic.html b/gazhdict/templates/base_generic.html new file mode 100644 index 0000000..8f2f9cf --- /dev/null +++ b/gazhdict/templates/base_generic.html @@ -0,0 +1,11 @@ + + + + {% block title %}愛爾蘭語—華語線上辭典{% endblock %} + + + + +{% block content %}{% endblock %} + + diff --git a/gazhdict/templates/ga_search.html b/gazhdict/templates/ga_search.html new file mode 100644 index 0000000..e03d818 --- /dev/null +++ b/gazhdict/templates/ga_search.html @@ -0,0 +1,57 @@ + + + + 線上愛爾蘭語—漢語辭典 - 愛爾蘭語搜尋 {{entry}} 的結果 + + + + +
+
+ {% if entry == "" %} + + {% elif entry_len == 0 %} + 對不起,找不到條目,請試著檢查有無錯字(或是有沒有輸入)錯字,或是調整語法變化,也有可能您輸入的是用詞變體。
+ {% else %} + 您要找的搜尋結果如下,共 {{entry_len}} 筆: + {% for e in entry_list %} +
+ {{e.Gaeilge}} + {{e.PoS}} + + {%if e.Pronoun != None %} + [{{e.Pronoun}}] + {% endif %} + {%if e.PreScript != None %} + ({{e.PreScript}}) + {% endif %} +
    + {%for exp in e.Explanation %} +
  1. {{exp}}
  2. + {% endfor %} +
+ {%if e.PostScript != None %} + 註:{{e.PostScript}} + {% endif %} +
+ {% endfor %} + {% endif %} +
+ + diff --git a/gazhdict/templates/hello_world.html b/gazhdict/templates/hello_world.html deleted file mode 100644 index e23fea6..0000000 --- a/gazhdict/templates/hello_world.html +++ /dev/null @@ -1,18 +0,0 @@ - - - - I come from template!! - - - -

Hello World!

- {{ current_time }} - - diff --git a/gazhdict/templates/registration/logged_out.html b/gazhdict/templates/registration/logged_out.html new file mode 100644 index 0000000..401cca9 --- /dev/null +++ b/gazhdict/templates/registration/logged_out.html @@ -0,0 +1,16 @@ + + + +線上愛爾蘭語—華語辭典 - 登出 + + +

線上愛爾蘭語—華語辭典

+

您已經登出了。

+ + + + + diff --git a/gazhdict/templates/registration/login.html b/gazhdict/templates/registration/login.html new file mode 100644 index 0000000..0f9690b --- /dev/null +++ b/gazhdict/templates/registration/login.html @@ -0,0 +1,46 @@ + + + +線上愛爾蘭語—華語辭典 - 登入 + + +

線上愛爾蘭語—華語辭典

+

管理後台登入畫面

+ +{% block content %} +{% if form.errors %} +

帳號或密碼不符合,請重新輸入。

+{% endif %} + +{% if next %} + {% if user.is_authenticated %} +

您的帳號沒有此頁的存取權,如果要進行下一步,請登入以另一個具有存取權的帳號。

+ {% else %} +

請登入觀看此頁。

+ {% endif %} +{% endif %} + +
+{% csrf_token %} + +
+ 帳號: + {{ form.username }} +
+
+ 密碼: + {{ form.password }} +
+ +
+ + +
+
+ +{# Assumes you setup the password_reset view in your URLconf #} +

忘記密碼?

+ +{% endblock %} + + diff --git a/gazhdict/templates/registration/password_reset_complete.html b/gazhdict/templates/registration/password_reset_complete.html new file mode 100644 index 0000000..9bff67e --- /dev/null +++ b/gazhdict/templates/registration/password_reset_complete.html @@ -0,0 +1,6 @@ +{% extends "base_generic.html" %} +{% block title %}愛爾蘭語—華語線上辭典 - 密碼更改完成{% endblock %} +{% block content %} +

密碼已經更改。

+

重新登入

+{% endblock %} diff --git a/gazhdict/templates/registration/password_reset_confirm.html b/gazhdict/templates/registration/password_reset_confirm.html new file mode 100644 index 0000000..84c9c54 --- /dev/null +++ b/gazhdict/templates/registration/password_reset_confirm.html @@ -0,0 +1,31 @@ +{% extends "base_generic.html" %} +{% block title %}愛爾蘭語—華語線上辭典 - 重設密碼{% endblock %} +{% block content %} + {% if validlink %} +

請確認您的新密碼。

+
+
+ +
+ + + + + + + + + + + + + +
{{ form.new_password1.errors }} + {{ form.new_password1 }}
{{ form.new_password2.errors }} + {{ form.new_password2 }}
+
+ {% else %} +

密碼重製失效

+

這個密碼重製連結失效,可能是它已經備用過了。請再重新取得新的密碼重設連結

+ {% endif %} +{% endblock %} diff --git a/gazhdict/templates/registration/password_reset_done.html b/gazhdict/templates/registration/password_reset_done.html new file mode 100644 index 0000000..4b085ca --- /dev/null +++ b/gazhdict/templates/registration/password_reset_done.html @@ -0,0 +1,5 @@ +{% extends "base_generic.html" %} +{% block title %}愛爾蘭語—華語線上辭典 - 密碼確認信已寄出{% endblock %} +{% block content %} +

我們已經寄出電子郵件確認信。若是數分鐘內未寄出,請確認您的垃圾郵件匣。

+{% endblock %} diff --git a/gazhdict/templates/registration/password_reset_email.html b/gazhdict/templates/registration/password_reset_email.html new file mode 100644 index 0000000..5248abc --- /dev/null +++ b/gazhdict/templates/registration/password_reset_email.html @@ -0,0 +1 @@ +有人想要為線上愛爾蘭語—漢語辭典重設密碼,所以寄到 {{ email }} 這個電子信箱。如果是您本人,請點選此連結:{{ protocol}}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %} 繼續後續操作。如果不是請忽略此信。 diff --git a/gazhdict/templates/registration/password_reset_form.html b/gazhdict/templates/registration/password_reset_form.html new file mode 100644 index 0000000..59f05af --- /dev/null +++ b/gazhdict/templates/registration/password_reset_form.html @@ -0,0 +1,22 @@ + + + +線上愛爾蘭語—華語辭典 - 忘記密碼 + + +

線上愛爾蘭語—華語辭典

+

忘記密碼

+ +{% block content %} +
+ {% csrf_token %} + {% if form.email.errors %} + 很抱歉,電子郵件格式錯誤,請再輸入一次。 + {% endif %} +

請輸入密碼確認信要寄到的電子郵件:{{ form.email }}

+ +
+{% endblock %} + + +