Skip to content

Commit

Permalink
feat(word): creag tags filed (#362)
Browse files Browse the repository at this point in the history
* feat(word):adding tags field

* feat(word):add WD0203

* test(word):add test files

* style: reformat word_normal.py

* refactor(word):refactoring code

* file(word):delete unnecessary file

* feat(application): adding tags field

* test(application): fixing test files
  • Loading branch information
CapooL authored Apr 15, 2024
1 parent cca3b1c commit ae322e1
Show file tree
Hide file tree
Showing 11 changed files with 382 additions and 134 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# 用于application_all
import json

from ...models import Application


Expand All @@ -10,6 +12,7 @@ def application_all_content(application: Application) -> dict:
{"id": article.id, "title": article.title}
for article in application.related_articles.all()
]
tags_list = json.loads(application.tags.replace("'", '"'))
response = {
"word": application.content_word,
"definition": application.definition,
Expand All @@ -19,5 +22,6 @@ def application_all_content(application: Application) -> dict:
"related_articles": related_articles,
"standard_ipa": application.standard_ipa,
"standard_pinyin": application.standard_pinyin,
"tags": tags_list,
}
return response
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
# 用于application_simple
import json

from ...models import Application


def application_simple_content(application: Application) -> dict:
tags_list = json.loads(application.tags.replace("'", '"'))
response = {
"word": application.content_word,
"definition": application.definition,
"annotation": application.annotation,
"standard_ipa": application.standard_ipa,
"standard_pinyin": application.standard_pinyin,
"mandarin": eval(application.mandarin) if application.mandarin else [],
"tags": tags_list,
}
return response
2 changes: 2 additions & 0 deletions hinghwa-dict-backend/word/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Meta:
"visibility",
"standard_ipa",
"standard_pinyin",
"tags",
)


Expand Down Expand Up @@ -51,6 +52,7 @@ class Meta:
"mandarin",
"standard_ipa",
"standard_pinyin",
"tags",
)


Expand Down
15 changes: 15 additions & 0 deletions hinghwa-dict-backend/word/migrations/0009_word_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("word", "0008_alter_word_related_words"),
]

operations = [
migrations.AddField(
model_name="word",
name="tags",
field=models.TextField(blank=True, default="[]", verbose_name="标签"),
),
]
16 changes: 16 additions & 0 deletions hinghwa-dict-backend/word/migrations/0010_application_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("word", "0009_word_tags"),
]

operations = [
migrations.AddField(
model_name="application",
name="tags",
field=models.TextField(blank=True, default="[]", verbose_name="标签"),
),
]
5 changes: 4 additions & 1 deletion hinghwa-dict-backend/word/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import re

from django.contrib.auth.models import User
from django.db import models

from article.models import Article
import re


# application是为了修改word而产生的
Expand Down Expand Up @@ -36,6 +37,7 @@ class Word(models.Model):
related_articles = models.ManyToManyField(
Article, related_name="related_words", verbose_name="相关帖子", blank=True
)
tags = models.TextField(verbose_name="标签", blank=True, default="[]")

def __str__(self):
return self.word
Expand Down Expand Up @@ -103,6 +105,7 @@ class Application(models.Model):
verbose_name="相关帖子",
blank=True,
)
tags = models.TextField(verbose_name="标签", blank=True, default="[]")

def granted(self):
return self.verifier is not None
Expand Down
6 changes: 5 additions & 1 deletion hinghwa-dict-backend/word/word/dto/word_all.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# 可用于WD0101 WD0201
from ...models import Word
import json

from user.dto.user_simple import user_simple
from word.word.views import word2pronunciation
from ...models import Word


def word_all(word: Word) -> dict:
Expand All @@ -14,6 +16,7 @@ def word_all(word: Word) -> dict:
]
user = word.contributor
source = word2pronunciation(word)
tags_list = json.loads(word.tags.replace("'", '"'))
response = {
"id": word.id,
"word": word.word,
Expand All @@ -27,5 +30,6 @@ def word_all(word: Word) -> dict:
"related_articles": related_articles,
"views": word.views,
"source": source,
"tags": tags_list,
}
return response
4 changes: 4 additions & 0 deletions hinghwa-dict-backend/word/word/dto/word_normal.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json

from ...models import Word


Expand All @@ -9,6 +11,7 @@ def word_normal(word: Word) -> dict:
{"id": article.id, "title": article.title}
for article in word.related_articles.all()
]
tags_list = json.loads(word.tags.replace("'", '"'))
response = {
"word": word.word,
"definition": word.definition,
Expand All @@ -18,5 +21,6 @@ def word_normal(word: Word) -> dict:
"related_articles": related_articles,
"standard_ipa": word.standard_ipa,
"standard_pinyin": word.standard_pinyin,
"tags": tags_list,
}
return response
4 changes: 4 additions & 0 deletions hinghwa-dict-backend/word/word/dto/word_simple.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# 用于WD0202
import json

from ...models import Word


def word_simple(word: Word) -> dict:
user = word.contributor
tags_list = json.loads(word.tags.replace("'", '"'))
response = {
"id": word.id,
"word": word.word,
Expand All @@ -14,5 +17,6 @@ def word_simple(word: Word) -> dict:
"views": word.views,
"standard_ipa": word.standard_ipa,
"standard_pinyin": word.standard_pinyin,
"tags": tags_list,
}
return response
6 changes: 5 additions & 1 deletion hinghwa-dict-backend/word/word/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def searchWords(request):
words = Word.objects.filter(visibility=True)
if "contributor" in request.GET:
words = words.filter(contributor=request.GET["contributor"])
if "tags" in request.GET:
query = request.GET["tags"]
query = re.findall(r"[\u4e00-\u9fa5]+", query)
words = words.filter(tags__icontains=query)
if "search" in request.GET:
result = []
key = request.GET["search"].replace(" ", "")
Expand Down Expand Up @@ -232,7 +236,7 @@ def load_word(request):
print("load character {}".format(word.id))
else:
raise Exception("add fail in {}".format(dic))
PhoneticOrdering.sign = True # 词语批量上传,音序表需要重建
PhoneticOrdering.sign = True # 词语批量上传,音序表需要重建
return JsonResponse({}, status=200)
except Exception as e:
return JsonResponse({"msg": str(e)}, status=500)
Expand Down
Loading

0 comments on commit ae322e1

Please sign in to comment.