-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(word): create word lists (#322)
* feat(word): create word lists 增加词单功能 * style(word): style: reformat for black * revert #314 --------- Co-authored-by: sheeplin <[email protected]> Co-authored-by: sheeplin <[email protected]>
- Loading branch information
1 parent
533b214
commit 93b0a1c
Showing
13 changed files
with
293 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from ...models import List | ||
from ...word.dto.word_simple import word_simple | ||
from user.dto.user_simple import user_simple | ||
|
||
|
||
def list_all(list: List): | ||
response = { | ||
"name": list.name, | ||
"author": user_simple(list.author), | ||
"createTime": list.createTime, | ||
"updateTime": list.updateTime, | ||
"description": list.description, | ||
"words": [word_simple(x) for x in list.words.all()], | ||
"length": list.words.count(), | ||
"id": list.id, | ||
} | ||
|
||
return response |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from django.urls import path | ||
|
||
from .view.manage_all_list import * | ||
from .view.manage_word_in_list import * | ||
from .view.manage_single_list import * | ||
|
||
app_name = "word.lists" | ||
|
||
urlpatterns = [ | ||
path("", csrf_exempt(ManageAllLists.as_view())), | ||
path("/<str:list_id>", csrf_exempt(ManageSingleLists.as_view())), | ||
path("/<str:list_id>/words", csrf_exempt(ManageListWords.as_view())), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import demjson | ||
from ...models import List, Word | ||
from ..dto.list_all import list_all | ||
from django.http import JsonResponse | ||
from django.views import View | ||
from django.views.decorators.csrf import csrf_exempt | ||
from ...forms import ListForm | ||
from utils.token import token_pass, token_user | ||
from utils.generate_id import generate_list_id | ||
from utils.exception.types.bad_request import BadRequestException | ||
from utils.exception.types.not_found import WordNotFoundException | ||
from django.utils import timezone | ||
|
||
|
||
class ManageAllLists(View): | ||
# WD0601 创建词单 | ||
@csrf_exempt | ||
def post(self, request): | ||
token = token_pass(request.headers, -1) | ||
user = token_user(token) | ||
body = demjson.decode(request.body) | ||
list_form = ListForm(body) | ||
if not list_form.is_valid(): | ||
raise BadRequestException() | ||
list = list_form.save(commit=False) | ||
list.id = generate_list_id() | ||
for id in body["words"]: | ||
word = Word.objects.get(id=id) | ||
print() | ||
print(list) | ||
print(type(word)) | ||
list.words.add(word) | ||
list.author = user | ||
list.createTime = timezone.now() | ||
list.updateTime = timezone.now() | ||
list.save() | ||
return JsonResponse(list_all(list), status=200) | ||
|
||
# WD0605查找词单(多) | ||
def get(self, request): | ||
token_pass(request.headers, 0) | ||
total_list = List.objects.all() | ||
result = [] | ||
for list in total_list: | ||
result.append(list_all(list)) | ||
return JsonResponse({"total": result.count(), "lists": result}) |
49 changes: 49 additions & 0 deletions
49
hinghwa-dict-backend/word/lists/view/manage_single_list.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import demjson | ||
from ...models import List, Word | ||
from ..dto.list_all import list_all | ||
from django.http import JsonResponse | ||
from django.views import View | ||
from django.views.decorators.csrf import csrf_exempt | ||
from ...forms import ListForm | ||
from utils.exception.types.not_found import ListsNotFoundException | ||
from utils.token import token_pass | ||
from utils.generate_id import generate_list_id | ||
from utils.exception.types.bad_request import BadRequestException | ||
|
||
|
||
class ManageSingleLists(View): | ||
# WD0602删除词单 | ||
@csrf_exempt | ||
def delete(self, request): | ||
token_pass(request.headers, -1) | ||
list_id = request.GET["list_id"] | ||
list = List.objects.filter(id=list_id) | ||
if not list.exists(): | ||
raise ListsNotFoundException() | ||
list = list[0] | ||
list.delete() | ||
return JsonResponse({}, status=200) | ||
|
||
# WD0603更改词单信息 | ||
def put(self, request): | ||
token_pass(request.headers, -1) | ||
list_id = request.GET["list_id"] | ||
list = List.objects.filter(id=list_id) | ||
if not list.exists(): | ||
raise ListsNotFoundException() | ||
list = list[0] | ||
body = demjson.decode(request.body) | ||
for key in body: | ||
setattr(list, key, body[key]) | ||
list.save() | ||
return JsonResponse(list_all(list), status=200) | ||
|
||
# WD0604查看词单(单) | ||
def get(self, request): | ||
token_pass(request.headers, 0) | ||
list_id = request.GET["list_id"] | ||
list = List.objects.filter(id=list_id) | ||
if not list.exists(): | ||
raise ListsNotFoundException() | ||
list = list[0] | ||
return JsonResponse(list_all(list), status=200) |
51 changes: 51 additions & 0 deletions
51
hinghwa-dict-backend/word/lists/view/manage_word_in_list.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import demjson | ||
from ...models import List, Word | ||
from ..dto.list_all import list_all | ||
from django.http import JsonResponse | ||
from django.views import View | ||
from django.views.decorators.csrf import csrf_exempt | ||
from utils.exception.types.not_found import ( | ||
ListsNotFoundException, | ||
WordNotFoundException, | ||
) | ||
from utils.token import token_pass | ||
from django.utils import timezone | ||
|
||
|
||
class ManageListWords(View): | ||
# WD0606增加词单词语 | ||
@csrf_exempt | ||
def post(self, request): | ||
token_pass(request.headers, -1) | ||
list_id = request.GET["list_id"] | ||
list = List.objects.filter(id=list_id) | ||
if not list.exists(): | ||
raise ListsNotFoundException() | ||
body = demjson.decode(request) | ||
for id in body["words"]: | ||
word = Word.objects.get(id=id) | ||
if not word: | ||
raise WordNotFoundException() | ||
list.words.add(word) | ||
list.updateTime = timezone.now() | ||
list.save() | ||
return JsonResponse(list_all(list), status=200) | ||
|
||
# WD0607删除词单词语 | ||
@csrf_exempt | ||
def delete(self, request): | ||
token_pass(request.headers, -1) | ||
list_id = request.GET["list_id"] | ||
body = demjson.decode(request) | ||
list = List.objects.filter(id=list_id) | ||
if not list.exists(): | ||
raise ListsNotFoundException() | ||
list = list[0] | ||
for id in body["words"]: | ||
word = Word.objects.get(id=id) | ||
if not word: | ||
raise WordNotFoundException() | ||
list.words.remove(word) | ||
list.updateTime = timezone.now() | ||
list.save() | ||
return JsonResponse({}, status=200) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import demjson | ||
from ..models import List | ||
from .dto.list_all import list_all | ||
from django.http import JsonResponse | ||
from django.views import View | ||
from django.views.decorators.csrf import csrf_exempt | ||
from utils.exception.types.not_found import ListsNotFoundException | ||
from utils.token import token_pass | ||
from utils.generate_id import generate_list_id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
("word", "0003_traditional_character"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="character", | ||
name="traditional", | ||
field=models.CharField(default="", max_length=30, verbose_name="繁体字"), | ||
), | ||
migrations.AlterField( | ||
model_name="word", | ||
name="standard_ipa", | ||
field=models.CharField(blank=True, max_length=100, verbose_name="标准IPA"), | ||
), | ||
migrations.AlterField( | ||
model_name="word", | ||
name="standard_pinyin", | ||
field=models.CharField(blank=True, max_length=100, verbose_name="标准拼音"), | ||
), | ||
migrations.CreateModel( | ||
name="List", | ||
fields=[ | ||
( | ||
"name", | ||
models.CharField(blank=True, max_length=30, verbose_name="类型"), | ||
), | ||
( | ||
"id", | ||
models.CharField( | ||
max_length=20, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
("createTime", models.DateTimeField(blank=True, verbose_name="创建时间")), | ||
("updateTime", models.DateTimeField(blank=True, verbose_name="更新时间")), | ||
( | ||
"description", | ||
models.CharField(blank=True, max_length=100, verbose_name="词单简介"), | ||
), | ||
( | ||
"author", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="list_words", | ||
to=settings.AUTH_USER_MODEL, | ||
verbose_name="词单作者", | ||
), | ||
), | ||
( | ||
"words", | ||
models.ManyToManyField( | ||
blank=True, | ||
related_name="included_word", | ||
to="word.Word", | ||
verbose_name="词语", | ||
), | ||
), | ||
], | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters