add async
This commit is contained in:
18
task/migrations/0002_alter_task_display.py
Normal file
18
task/migrations/0002_alter_task_display.py
Normal file
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 5.1.6 on 2025-03-10 10:38
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('task', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='task',
|
||||
name='display',
|
||||
field=models.IntegerField(db_index=True, unique=True),
|
||||
),
|
||||
]
|
||||
@@ -8,7 +8,7 @@ class Task(TimeStampedModel):
|
||||
("tutorial", "Tutorial"),
|
||||
]
|
||||
|
||||
display = models.IntegerField(unique=True)
|
||||
display = models.IntegerField(unique=True, db_index=True)
|
||||
title = models.CharField(max_length=100)
|
||||
content = models.TextField()
|
||||
task_type = models.CharField(
|
||||
|
||||
@@ -20,11 +20,11 @@ def get_all_public_display(request):
|
||||
|
||||
|
||||
@router.get("/{display}", response=TutorialAll)
|
||||
def get(request, display: int):
|
||||
async def get(request, display: int):
|
||||
try:
|
||||
return Tutorial.objects.get(display=display)
|
||||
return await Tutorial.objects.aget(display=display)
|
||||
except Tutorial.DoesNotExist:
|
||||
return HttpError(404, "此序号无教程")
|
||||
raise HttpError(404, "此序号无教程")
|
||||
|
||||
|
||||
@router.post("/")
|
||||
@@ -35,7 +35,7 @@ def create_or_update(request, payload: TutorialIn):
|
||||
item.title = payload.title
|
||||
item.content = payload.content
|
||||
item.is_public = payload.is_public
|
||||
item.save()
|
||||
item.asave()
|
||||
return {"message": "更新成功"}
|
||||
except Tutorial.DoesNotExist:
|
||||
Tutorial.objects.create(**payload.dict())
|
||||
@@ -48,11 +48,11 @@ def toggle_public(request, display: int):
|
||||
try:
|
||||
item = Tutorial.objects.get(display=display)
|
||||
item.is_public = not item.is_public
|
||||
item.save()
|
||||
item.asave()
|
||||
label = "公开" if item.is_public else "隐藏"
|
||||
return {"message": f"【{item.display}】{item.title} 已{label}"}
|
||||
except Tutorial.DoesNotExist:
|
||||
return HttpError(404, "此序号无教程")
|
||||
raise HttpError(404, "此序号无教程")
|
||||
|
||||
|
||||
@router.delete("/{display}")
|
||||
|
||||
Reference in New Issue
Block a user