NP-8 Возможность создавать новую страницу

This commit is contained in:
Mikan
2024-11-25 17:29:48 +03:00
parent efb3c52e6b
commit 6160f94e73
4 changed files with 31 additions and 5 deletions

View File

@@ -15,7 +15,7 @@ class ItemDataButtonsInline(admin.TabularInline):
class ItemDataAdmin(admin.ModelAdmin):
list_display = ('title', "author", 'status', 'created_at', 'updated_at', 'get_link')
search_fields = ('title', 'author')
search_fields = ('title', 'author__username')
list_filter = ('collection', 'status')
ordering = ('-created_at', '-updated_at', 'id')

View File

@@ -22,6 +22,13 @@ class ItemDataStatus(models.Model):
name = models.CharField(max_length=20, primary_key=True)
verbose = models.CharField(max_length=20)
@property
def select_data_dict(self) -> typing.Dict[str, str]:
return {
"verbose": self.verbose,
"name": self.name
}
def __str__(self):
return self.verbose
@@ -96,7 +103,8 @@ class ItemData(models.Model):
"shortDescription": self.short_description,
"description": self.description,
"author": author,
"contextButtons": context_buttons
"contextButtons": context_buttons,
"status": self.status.select_data_dict
}
@property
@@ -111,7 +119,7 @@ class ItemData(models.Model):
def mini_card_dict(self) -> typing.Dict[str, typing.Union[str, int, float, None]]:
card = self.card_dict
card.update({
"status": self.status.verbose,
"status": self.status.select_data_dict,
"updatedAt": self.updated_at.timestamp(),
"shortDescription": self.short_description
})

View File

@@ -7,4 +7,5 @@ urlpatterns = [
path('get-item/<str:collection>/<int:item_id>', views.get_item, name='get-collection'),
path('update/item/', views.update_item, name='update-item'),
path('get/available-context-buttons/', views.context_buttons_list, name='available-context-buttons'),
path('get/available-item-statuses/', views.item_statuses_list, name='available-item-statuses'),
]

View File

@@ -5,7 +5,7 @@ from django.db.models import QuerySet
from django.http import HttpResponse, HttpRequest, JsonResponse
from common.exceptions import NovelPlusHttpExceptionResponse
from common.models import ItemData, ContextButtonType, ItemDataCollection
from common.models import ItemData, ContextButtonType, ItemDataCollection, ItemDataStatus
from users.models import User
from utils import get_request_data, update_context_buttons
@@ -71,6 +71,7 @@ def update_item(request):
title=created_data.get("title", "Unnamed"),
author=user,
collection=collection,
status=ItemDataStatus.objects.first(),
is_passed_moderation=True #TODO автомодерация на первых этапах
)
@@ -93,12 +94,17 @@ def update_item(request):
return NovelPlusHttpExceptionResponse(request, "Произошла ошибка", 500, repr(e))
elif change == "contextButtons":
update_context_buttons(data.get(change)[0], item)
elif change == "status":
try:
item.status = ItemDataStatus.objects.get(name=data.get(change)[0])
except ItemDataStatus.DoesNotExist:
return NovelPlusHttpExceptionResponse(request, "Такого статуса не существует", status=404)
else:
setattr(item, change, data.get(change)[0])
item.save()
return JsonResponse({"success": True})
return JsonResponse({"success": True, "id": item.id})
def context_buttons_list(request):
@@ -108,3 +114,14 @@ def context_buttons_list(request):
"context_buttons": [cb.verbose for cb in ContextButtonType.objects.all()]
}
)
def item_statuses_list(request):
return JsonResponse(
{
"success": True,
"item_status": [
status.select_data_dict for status in ItemDataStatus.objects.all()
]
}
)