Skip to content

Commit

Permalink
feat(word): create word lists (#322)
Browse files Browse the repository at this point in the history
* 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
3 people authored Sep 3, 2023
1 parent 533b214 commit 93b0a1c
Show file tree
Hide file tree
Showing 13 changed files with 293 additions and 2 deletions.
1 change: 1 addition & 0 deletions hinghwa-dict-backend/HinghwaDict/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,5 @@
include("rewards.transactions.urls", namespace="rewards.transactions"),
),
path("orders", include("rewards.orders.urls", namespace="rewards.orders")),
path("lists", include("word.lists.urls", namespace="word.lists")),
]
2 changes: 1 addition & 1 deletion hinghwa-dict-backend/utils/exception/types/not_found.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def __init__(self, id=""):
self.msg = "订单{}不存在".format(id)


class ThesaurusNotFoundException(NotFoundException):
class ListsNotFoundException(NotFoundException):
"""
词单不存在
param id:订单id
Expand Down
11 changes: 11 additions & 0 deletions hinghwa-dict-backend/utils/generate_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from rewards.titles.models.title import Title
from rewards.products.models.product import Product
from rewards.orders.models.order import Order
from word.models import List


def generate_transaction_id():
Expand Down Expand Up @@ -42,3 +43,13 @@ def generate_order_id():
else:
new_id = 1
return f"DD{new_id:06d}"


def generate_list_id():
last_order = List.objects.order_by("-id").first()
if last_order:
last_id = int(last_order.id[2:])
new_id = last_id + 1
else:
new_id = 1
return f"CD{new_id:06d}"
8 changes: 7 additions & 1 deletion hinghwa-dict-backend/word/forms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django import forms

from .models import Word, Pronunciation, Character, Application
from .models import Word, Pronunciation, Character, Application, List


class WordForm(forms.ModelForm):
Expand Down Expand Up @@ -51,3 +51,9 @@ class Meta:
"standard_ipa",
"standard_pinyin",
)


class ListForm(forms.ModelForm):
class Meta:
model = List
fields = ("name", "description")
18 changes: 18 additions & 0 deletions hinghwa-dict-backend/word/lists/dto/list_all.py
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.
13 changes: 13 additions & 0 deletions hinghwa-dict-backend/word/lists/urls.py
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())),
]
46 changes: 46 additions & 0 deletions hinghwa-dict-backend/word/lists/view/manage_all_list.py
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 hinghwa-dict-backend/word/lists/view/manage_single_list.py
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 hinghwa-dict-backend/word/lists/view/manage_word_in_list.py
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)
9 changes: 9 additions & 0 deletions hinghwa-dict-backend/word/lists/views.py
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
70 changes: 70 additions & 0 deletions hinghwa-dict-backend/word/migrations/0004_word_list.py
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="词语",
),
),
],
),
]
17 changes: 17 additions & 0 deletions hinghwa-dict-backend/word/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,20 @@ def save(self, *args, **kwargs):
class Meta:
verbose_name_plural = "单字"
verbose_name = "单字"


class List(models.Model):
name = models.CharField(blank=True, max_length=30, verbose_name="类型")
author = models.ForeignKey(
User, on_delete=models.CASCADE, related_name="list_words", verbose_name="词单作者"
)
words = models.ManyToManyField(
Word,
related_name="included_word",
verbose_name="词语",
blank=True,
)
id = models.CharField(max_length=20, verbose_name="ID", primary_key=True)
createTime = models.DateTimeField(blank=True, verbose_name="创建时间")
updateTime = models.DateTimeField(blank=True, verbose_name="更新时间")
description = models.CharField(blank=True, max_length=100, verbose_name="词单简介")

0 comments on commit 93b0a1c

Please sign in to comment.