NP-20 Добавить возможность редактировать контекстные кнопки

This commit is contained in:
Mikan
2024-11-23 00:35:17 +03:00
parent c1b0778f0a
commit c518246b39
13 changed files with 76 additions and 18 deletions

View File

@@ -24,7 +24,7 @@ admin.site.register(ItemData, ItemDataAdmin)
class ContextButtonTypeAdmin(admin.ModelAdmin):
list_display = ('verbose', 'name')
list_display = ('verbose', 'name', 'host_regex')
search_fields = ('verbose', 'name')
ordering = ('verbose',)

View File

@@ -0,0 +1,18 @@
# Generated by Django 4.2.16 on 2024-11-22 21:12
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('common', '0007_alter_itemdata_image_alter_itemdata_preview'),
]
operations = [
migrations.AddField(
model_name='contextbuttontype',
name='host_regex',
field=models.CharField(blank=True, default='', max_length=20),
),
]

View File

@@ -123,6 +123,8 @@ class ContextButtonType(models.Model):
name = models.CharField(max_length=20, primary_key=True)
verbose = models.CharField(max_length=20)
host_regex = models.CharField(max_length=20, default="", blank=True)
def __str__(self):
return self.verbose

View File

@@ -42,16 +42,16 @@ def create_status_types(sender, **kwargs):
@receiver(post_migrate)
def create_context_button_types(sender, **kwargs):
default_context_button_types = (
("telegram", "Телеграм"),
("vk", "Вконтакте"),
("email", "E-MAIL"),
("steam", "Страница в стиме"),
("itchio", "Страница в itch.io"),
("telegram", "Телеграм", r"https://t\.me/"),
("vk", "Вконтакте", "https://vk.com/"),
("email", "E-MAIL", "mailto://"),
("steam", "Страница в стиме", r"https://(?store\.steampowered|steamcommunity)\.com/"),
("itchio", "Страница в itch.io", r"https://\w+\.itch\.io/"),
)
for name, verbose in default_context_button_types:
for name, verbose, host_regex in default_context_button_types:
try:
ContextButtonType.objects.get_or_create(name=name, verbose=verbose)
ContextButtonType.objects.get_or_create(name=name, verbose=verbose, host_regex=host_regex)
except Exception as e:
print(f"[-] {repr(e)} - {name} {verbose}")

View File

@@ -6,4 +6,5 @@ urlpatterns = [
path('get-collection/<str:variant>/<str:collection>', views.get_cards, name='get-collection'),
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'),
]

View File

@@ -1,8 +1,11 @@
import json
import re
from django.db.models import QuerySet
from django.http import HttpResponse, HttpRequest, JsonResponse
from common.exceptions import NovelPlusHttpExceptionResponse
from common.models import ItemData
from common.models import ItemData, ContextButtonType
from users.models import User
from utils import get_request_data
@@ -43,6 +46,7 @@ def get_item(request: HttpRequest, collection: str, item_id: int) -> HttpRespons
{"message": repr(e)}
)
def update_item(request):
data = get_request_data(request)
user: User = request.user
@@ -66,10 +70,37 @@ def update_item(request):
item.image.save(user.username + str(item.id) + ".jpg", request.FILES['image'], save=False)
except Exception as e:
return NovelPlusHttpExceptionResponse(request, "Произошла ошибка", 500, repr(e))
elif change == "contextButtons":
buttons = json.loads(data.get(change)[0])
for button in buttons:
button_type = ContextButtonType.objects.filter(verbose=button["name"]).first()
if button_type:
if not button["url"]:
item_context_button = item.context_buttons.filter(button_type=button_type).first()
if item_context_button:
item_context_button.delete()
elif re.match(button_type.host_regex, button["url"]):
item_context_button, create = item.context_buttons.get_or_create(
button_type=button_type,
defaults={
"url": button["url"]
}
)
if not create:
item_context_button.url = button["url"]
item_context_button.save()
else:
setattr(item, change, data.get(change)[0])
item.save()
return JsonResponse({"success": True})
def context_buttons_list(request):
return JsonResponse(
{
"success": True,
"context_buttons": [cb.verbose for cb in ContextButtonType.objects.all()]
}
)

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,3 @@
<svg width="500" height="350" viewBox="0 0 500 350" fill="none" xmlns="http://www.w3.org/2000/svg">
<rect width="500" height="350" fill="#EAE4E4"/>
</svg>

After

Width:  |  Height:  |  Size: 155 B

View File

@@ -0,0 +1,3 @@
<svg width="350" height="200" viewBox="0 0 350 200" fill="none" xmlns="http://www.w3.org/2000/svg">
<rect width="350" height="200" fill="#D9D9D9"/>
</svg>

After

Width:  |  Height:  |  Size: 155 B

View File

@@ -12,8 +12,8 @@
<meta name="yandex-verification" content="e38d4768470c4ba2" />
<link rel="icon" href="{% static '/misc/icon.ico' %}">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script defer="defer" src="{% static 'frontend/js/main.4a9be667.js' %}"></script>
<link rel="stylesheet" href="{% static 'frontend/css/main.3c1cb610.css' %}">
<script defer="defer" src="{% static 'frontend/js/main.270f1aca.js' %}"></script>
<link rel="stylesheet" href="{% static 'frontend/css/main.0634f992.css' %}">
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>