From 840e575902b7770144e2d35aafc68d623a5c77d7 Mon Sep 17 00:00:00 2001 From: CapooL <2053217188@qq.com> Date: Thu, 7 Mar 2024 11:51:25 +0800 Subject: [PATCH] feat(character): add "type" to Character --- .../word/character/dto/character_all.py | 1 + .../word/character/dto/character_normal.py | 1 + .../word/character/dto/character_simple.py | 1 + hinghwa-dict-backend/word/character/views.py | 8 +++++--- hinghwa-dict-backend/word/forms.py | 1 + .../word/migrations/0007_character_type.py | 15 +++++++++++++++ hinghwa-dict-backend/word/models.py | 1 + 7 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 hinghwa-dict-backend/word/migrations/0007_character_type.py diff --git a/hinghwa-dict-backend/word/character/dto/character_all.py b/hinghwa-dict-backend/word/character/dto/character_all.py index 35214faf..b426c7ea 100644 --- a/hinghwa-dict-backend/word/character/dto/character_all.py +++ b/hinghwa-dict-backend/word/character/dto/character_all.py @@ -17,5 +17,6 @@ def character_all(character: Character, word: Word, source: Pronunciation) -> di "traditional": character.traditional, "word": word, "source": source, + "type": None if character.type is None else character.type, } return response diff --git a/hinghwa-dict-backend/word/character/dto/character_normal.py b/hinghwa-dict-backend/word/character/dto/character_normal.py index 507b25b9..70fa7101 100644 --- a/hinghwa-dict-backend/word/character/dto/character_normal.py +++ b/hinghwa-dict-backend/word/character/dto/character_normal.py @@ -13,5 +13,6 @@ def character_normal(character: Character) -> dict: "county": character.county, "town": character.town, "traditional": character.traditional, + "type": None if character.type is None else character.type, } return response diff --git a/hinghwa-dict-backend/word/character/dto/character_simple.py b/hinghwa-dict-backend/word/character/dto/character_simple.py index c3dd7f7a..ab08149a 100644 --- a/hinghwa-dict-backend/word/character/dto/character_simple.py +++ b/hinghwa-dict-backend/word/character/dto/character_simple.py @@ -29,6 +29,7 @@ def character_simple(character: Character) -> dict: "traditional": character.traditional, "word": {"id": word_id} if word else None, "source": {"source": source_value} if source else None, + "type": None if character.type is None else character.type, } ) diff --git a/hinghwa-dict-backend/word/character/views.py b/hinghwa-dict-backend/word/character/views.py index b69976f2..b493d398 100644 --- a/hinghwa-dict-backend/word/character/views.py +++ b/hinghwa-dict-backend/word/character/views.py @@ -38,6 +38,8 @@ def searchCharacters(request): character_form = CharacterForm(body) if character_form.is_valid(): character = character_form.save(commit=False) + if "type" in body: + character.type = body["type"] character.save() return JsonResponse({"id": character.id}, status=200) else: @@ -75,7 +77,7 @@ def searchCharactersPinyin(request): pinyin_list = [] for item in characters: if ((item.pinyin, item.character) not in result) or ( - item.town == "城里" and item.county == "莆田" + item.town == "城里" and item.county == "莆田" ): result[(item.pinyin, item.character, item.traditional)] = item pinyin_list.append(item.pinyin) @@ -86,13 +88,13 @@ def searchCharactersPinyin(request): words_dict = {} pronunciations_dict = {} for item in Word.objects.filter(standard_pinyin__in=pinyin_list).filter( - visibility=True + visibility=True ): if item.standard_pinyin not in words_dict: words_dict[item.standard_pinyin] = [] words_dict[item.standard_pinyin].append(item) for item in Pronunciation.objects.filter(pinyin__in=pinyin_list).filter( - visibility=True + visibility=True ): if item.pinyin not in pronunciations_dict: pronunciations_dict[item.pinyin] = item diff --git a/hinghwa-dict-backend/word/forms.py b/hinghwa-dict-backend/word/forms.py index 6bf22bd7..5c66a5a5 100644 --- a/hinghwa-dict-backend/word/forms.py +++ b/hinghwa-dict-backend/word/forms.py @@ -36,6 +36,7 @@ class Meta: "county", "town", "traditional", + "type", ) diff --git a/hinghwa-dict-backend/word/migrations/0007_character_type.py b/hinghwa-dict-backend/word/migrations/0007_character_type.py new file mode 100644 index 00000000..ce33d8ef --- /dev/null +++ b/hinghwa-dict-backend/word/migrations/0007_character_type.py @@ -0,0 +1,15 @@ +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ('word', '0006_list_ship'), + ] + + operations = [ + migrations.AddField( + model_name='character', + name='type', + field=models.CharField(max_length=20, null=True, verbose_name='读音类型'), + ), + ] diff --git a/hinghwa-dict-backend/word/models.py b/hinghwa-dict-backend/word/models.py index 30f587d3..22bb98da 100644 --- a/hinghwa-dict-backend/word/models.py +++ b/hinghwa-dict-backend/word/models.py @@ -200,6 +200,7 @@ class Character(models.Model): county = models.CharField(max_length=100, verbose_name="县区") town = models.CharField(max_length=100, verbose_name="乡镇") traditional = models.CharField(max_length=30, verbose_name="繁体字", default="") + type = models.CharField(max_length=20, verbose_name="读音类型", null=True) def clean(self): self.shengmu = self.shengmu.strip()