@@ -1,4 +1,6 @@
|
||||
from django.contrib import admin
|
||||
from django.urls import reverse
|
||||
from django.utils.html import format_html
|
||||
|
||||
from common.models import ItemData, ContextButtonType, ItemDataContextButton
|
||||
from common.signals import add_signals
|
||||
@@ -12,19 +14,30 @@ class ItemDataButtonsInline(admin.TabularInline):
|
||||
|
||||
|
||||
class ItemDataAdmin(admin.ModelAdmin):
|
||||
list_display = ('title', "author", 'status', 'created_at', 'updated_at')
|
||||
list_display = ('title', "author", 'status', 'created_at', 'updated_at', 'get_link')
|
||||
search_fields = ('title', 'author')
|
||||
list_filter = ('collection', 'status')
|
||||
ordering = ('-created_at', '-updated_at', 'id')
|
||||
|
||||
readonly_fields = ('get_link',)
|
||||
|
||||
inlines = [ItemDataButtonsInline]
|
||||
|
||||
def get_link(self, obj: ItemData):
|
||||
if obj.collection: # Проверяем, есть ли коллекция
|
||||
url = reverse('item-view', args=(obj.collection.name, obj.id,))
|
||||
return format_html(f'<a href="{url}">Открыть</a>')
|
||||
else:
|
||||
return "-"
|
||||
|
||||
get_link.short_description = "Ссылка на сайте"
|
||||
|
||||
|
||||
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',)
|
||||
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
# Generated by Django 4.2.16 on 2024-11-18 19:22
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('common', '0006_itemdatacontextbutton'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='itemdata',
|
||||
name='image',
|
||||
field=models.ImageField(blank=True, null=True, upload_to='images/', verbose_name='Главное изображение'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='itemdata',
|
||||
name='preview',
|
||||
field=models.ImageField(blank=True, null=True, upload_to='images/previews/', verbose_name='Превью'),
|
||||
),
|
||||
]
|
||||
18
common/migrations/0008_contextbuttontype_host_regex.py
Normal file
18
common/migrations/0008_contextbuttontype_host_regex.py
Normal 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),
|
||||
),
|
||||
]
|
||||
@@ -33,10 +33,10 @@ class ItemDataStatus(models.Model):
|
||||
|
||||
class ItemData(models.Model):
|
||||
title = models.CharField(max_length=100, verbose_name='Название')
|
||||
image = models.ImageField(upload_to='images/', verbose_name='Главное изображение')
|
||||
image = models.ImageField(upload_to='images/', verbose_name='Главное изображение', null=True, blank=True)
|
||||
description = models.TextField(verbose_name='Описание')
|
||||
short_description = models.TextField(max_length=256, verbose_name='Краткое описание')
|
||||
preview = models.ImageField(upload_to='images/previews/', verbose_name='Превью')
|
||||
preview = models.ImageField(upload_to='images/previews/', verbose_name='Превью', null=True, blank=True)
|
||||
|
||||
collection = models.ForeignKey(
|
||||
ItemDataCollection,
|
||||
@@ -72,10 +72,10 @@ class ItemData(models.Model):
|
||||
return {
|
||||
"id": self.id,
|
||||
"title": self.title,
|
||||
"image": self.image.url,
|
||||
"image": None if not self.image else self.image.url,
|
||||
"description": self.description,
|
||||
"shortDescription": self.short_description,
|
||||
"preview": self.preview.url,
|
||||
"preview": None if not self.preview else self.preview.url,
|
||||
"createdAt": self.created_at.timestamp(),
|
||||
"updatedAt": self.updated_at.timestamp(),
|
||||
}
|
||||
@@ -90,7 +90,8 @@ class ItemData(models.Model):
|
||||
author = self.author.id
|
||||
|
||||
return {
|
||||
"image": self.image.url,
|
||||
"id": self.id,
|
||||
"image": None if not self.image else self.image.url,
|
||||
"title": self.title,
|
||||
"shortDescription": self.short_description,
|
||||
"description": self.description,
|
||||
@@ -103,7 +104,7 @@ class ItemData(models.Model):
|
||||
return {
|
||||
"id": self.id,
|
||||
"title": self.title,
|
||||
"preview": self.preview.url
|
||||
"preview": None if not self.preview else self.preview.url
|
||||
}
|
||||
|
||||
@property
|
||||
@@ -111,7 +112,7 @@ class ItemData(models.Model):
|
||||
card = self.card_dict
|
||||
card.update({
|
||||
"status": self.status.verbose,
|
||||
"updatedAt": self.updated_at.timestamp() // .001,
|
||||
"updatedAt": self.updated_at.timestamp(),
|
||||
"shortDescription": self.short_description
|
||||
})
|
||||
|
||||
@@ -122,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
|
||||
|
||||
|
||||
@@ -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}")
|
||||
|
||||
|
||||
@@ -5,4 +5,6 @@ from . import views
|
||||
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'),
|
||||
]
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
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, update_context_buttons
|
||||
|
||||
|
||||
def get_cards(request: HttpRequest, variant: str, collection: str) -> HttpResponse:
|
||||
@@ -40,3 +45,45 @@ def get_item(request: HttpRequest, collection: str, item_id: int) -> HttpRespons
|
||||
500,
|
||||
{"message": repr(e)}
|
||||
)
|
||||
|
||||
|
||||
def update_item(request):
|
||||
data = get_request_data(request)
|
||||
user: User = request.user
|
||||
item: ItemData = ItemData.objects.filter(
|
||||
id=data['id'][0]
|
||||
).first()
|
||||
if item is None:
|
||||
return NovelPlusHttpExceptionResponse(request, "Такого объекта не существует", status=403)
|
||||
|
||||
try:
|
||||
if not (user.id == int(data['author'][0]) == item.author.id):
|
||||
return NovelPlusHttpExceptionResponse(request, "Вы не имеете право на эту операцию", status=403)
|
||||
except KeyError:
|
||||
return NovelPlusHttpExceptionResponse(request, "Вы не имеете право на эту операцию", status=403)
|
||||
except ValueError:
|
||||
return NovelPlusHttpExceptionResponse(request, "Вы не имеете право на эту операцию", status=403)
|
||||
|
||||
for change in data['changes']:
|
||||
if change == "image":
|
||||
try:
|
||||
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":
|
||||
update_context_buttons(data.get(change)[0], item)
|
||||
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()]
|
||||
}
|
||||
)
|
||||
|
||||
Submodule frontend/NovelPlus-frontend updated: 4c467f2325...4bb343af06
@@ -10,7 +10,8 @@
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
{%% csrf_token %%}
|
||||
<meta name="yandex-verification" content="e38d4768470c4ba2" />
|
||||
<link rel="icon" href="{%% static '/misc/icon.ico' %%}">
|
||||
<meta name="google-site-verification" content="ziAqg4RK2M-fsAIziVXrb0SHFuEcpWsGhwjp7H4ODrI" />
|
||||
<link rel="icon" type="image/x-icon" href="{%% static '/misc/icon.ico' %%}">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<script defer="defer" src="{%% static 'frontend/js/%s' %%}"></script>
|
||||
<link rel="stylesheet" href="{%% static 'frontend/css/%s' %%}">
|
||||
|
||||
File diff suppressed because one or more lines are too long
2
frontend/static/frontend/css/main.c8e987ad.css
Normal file
2
frontend/static/frontend/css/main.c8e987ad.css
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
3
frontend/static/frontend/js/main.55f5b4b9.js
Normal file
3
frontend/static/frontend/js/main.55f5b4b9.js
Normal file
File diff suppressed because one or more lines are too long
@@ -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 |
@@ -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 |
@@ -10,10 +10,11 @@
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
{% csrf_token %}
|
||||
<meta name="yandex-verification" content="e38d4768470c4ba2" />
|
||||
<link rel="icon" href="{% static '/misc/icon.ico' %}">
|
||||
<meta name="google-site-verification" content="ziAqg4RK2M-fsAIziVXrb0SHFuEcpWsGhwjp7H4ODrI" />
|
||||
<link rel="icon" type="image/x-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.55f5b4b9.js' %}"></script>
|
||||
<link rel="stylesheet" href="{% static 'frontend/css/main.c8e987ad.css' %}">
|
||||
</head>
|
||||
<body>
|
||||
<noscript>You need to enable JavaScript to run this app.</noscript>
|
||||
|
||||
3
robots.txt
Normal file
3
robots.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
User-agent: *
|
||||
Allow: /
|
||||
Disallow: /admin/
|
||||
@@ -1,5 +1,7 @@
|
||||
from django.contrib import admin
|
||||
from django.contrib.auth.admin import UserAdmin
|
||||
from django.urls import reverse
|
||||
from django.utils.html import format_html
|
||||
|
||||
from users.admin_forms import UserCreationForm, UserChangeForm
|
||||
from users.models import User, UserContextButton
|
||||
@@ -16,10 +18,10 @@ class CustomUserAdmin(UserAdmin):
|
||||
add_form = UserCreationForm
|
||||
form = UserChangeForm
|
||||
model = User
|
||||
list_display = ("username", "email", "is_staff",)
|
||||
list_filter = ("username", "email", "is_staff",)
|
||||
list_display = ("username", "is_staff", "get_link",)
|
||||
list_filter = ("is_staff",)
|
||||
fieldsets = (
|
||||
(None, {"fields": ("username", "first_name", "last_name", "email", "avatar", "password", "description")}),
|
||||
(None, {"fields": ("username", "first_name", "last_name", "email", "avatar", "password", "description", "get_link")}),
|
||||
("Разрешения", {"fields": ("is_staff", "is_active", "groups", "user_permissions")}),
|
||||
)
|
||||
add_fieldsets = (
|
||||
@@ -34,5 +36,13 @@ class CustomUserAdmin(UserAdmin):
|
||||
search_fields = ("email", "username", "email")
|
||||
ordering = ("id",)
|
||||
|
||||
readonly_fields = ('get_link',)
|
||||
|
||||
def get_link(self, obj: User):
|
||||
url = reverse('user', args=(obj.id,))
|
||||
return format_html(f'<a href="{url}">Открыть</a>')
|
||||
|
||||
get_link.short_description = "Ссылка на сайте"
|
||||
|
||||
|
||||
admin.site.register(User, CustomUserAdmin)
|
||||
|
||||
18
users/migrations/0007_alter_user_description.py
Normal file
18
users/migrations/0007_alter_user_description.py
Normal file
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 4.2.16 on 2024-11-18 20:27
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('users', '0006_usercontextbutton'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='user',
|
||||
name='description',
|
||||
field=models.TextField(blank=True, default='', verbose_name='Описание профиля'),
|
||||
),
|
||||
]
|
||||
@@ -9,7 +9,7 @@ class User(AbstractUser):
|
||||
|
||||
avatar = models.ImageField(upload_to='avatars/', blank=True, null=True, verbose_name='Аватарка')
|
||||
vk_user_id = models.IntegerField(blank=True, null=True, unique=True, verbose_name="ID VK")
|
||||
description = models.TextField(default="", verbose_name='Описание профиля')
|
||||
description = models.TextField(default="", verbose_name='Описание профиля', blank=True)
|
||||
|
||||
class Meta:
|
||||
db_table = 'auth_user'
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import json
|
||||
import re
|
||||
import uuid
|
||||
from io import BytesIO
|
||||
|
||||
@@ -6,8 +8,9 @@ from django.contrib.auth import logout, authenticate, login
|
||||
from django.http import JsonResponse, HttpResponse, HttpRequest
|
||||
|
||||
from common.exceptions import NovelPlusHttpExceptionResponse
|
||||
from common.models import ContextButtonType
|
||||
from users.models import User
|
||||
from utils import get_request_data
|
||||
from utils import get_request_data, update_context_buttons
|
||||
|
||||
|
||||
def get_me(request: HttpRequest) -> HttpResponse:
|
||||
@@ -136,19 +139,17 @@ def update_profile(request):
|
||||
except ValueError:
|
||||
return NovelPlusHttpExceptionResponse(request, "Вы не имеете право на эту операцию", status=403)
|
||||
|
||||
if "firstName" in data['changes']:
|
||||
user.first_name = data.get("firstName")[0]
|
||||
if "lastName" in data['changes']:
|
||||
user.last_name = data.get("lastName")[0]
|
||||
if "email" in data['changes']:
|
||||
user.email = data.get("email")[0]
|
||||
if "description" in data['changes']:
|
||||
user.description = data.get("description")[0]
|
||||
if 'avatar' in data['changes']:
|
||||
try:
|
||||
user.avatar.save(user.username + ".jpg", request.FILES['avatar'], save=False)
|
||||
except Exception as e:
|
||||
return NovelPlusHttpExceptionResponse(request, "Произошла ошибка", 500, repr(e))
|
||||
|
||||
for change in data['changes']:
|
||||
if change == "image":
|
||||
try:
|
||||
user.avatar.save(user.username + ".jpg", request.FILES['avatar'], save=False)
|
||||
except Exception as e:
|
||||
return NovelPlusHttpExceptionResponse(request, "Произошла ошибка", 500, repr(e))
|
||||
elif change == "contextButtons":
|
||||
update_context_buttons(data.get(change)[0], user)
|
||||
else:
|
||||
setattr(user, change, data.get(change)[0])
|
||||
|
||||
user.save()
|
||||
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
import json
|
||||
import re
|
||||
import typing
|
||||
|
||||
from django.http import HttpRequest, RawPostDataException
|
||||
|
||||
from common.models import ContextButtonType, ItemData
|
||||
from users.models import User
|
||||
|
||||
|
||||
def get_request_data(request: HttpRequest) -> dict:
|
||||
try:
|
||||
@@ -11,3 +16,23 @@ def get_request_data(request: HttpRequest) -> dict:
|
||||
except RawPostDataException:
|
||||
data = dict(request.GET or request.POST)
|
||||
return data
|
||||
|
||||
def update_context_buttons(buttons_json: str, target: typing.Union[User, ItemData]):
|
||||
buttons = json.loads(buttons_json)
|
||||
for button in buttons:
|
||||
button_type = ContextButtonType.objects.filter(verbose=button["name"]).first()
|
||||
if button_type:
|
||||
if not button["url"]:
|
||||
item_context_button = target.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 = target.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()
|
||||
|
||||
Reference in New Issue
Block a user