Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logo model #243

Merged
merged 11 commits into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .vscode/settings.json
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Toto by nemalo byt potrebne pridavat

Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@
},
"editor.defaultFormatter": "ms-python.autopep8",
"editor.formatOnSave": true,
"python.linting.pylintArgs": [
"--load-plugins",
"pylint_django"
]
}

}
11 changes: 10 additions & 1 deletion cms/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.contrib import admin

from cms.models import InfoBanner, MenuItem, MessageTemplate, Post, PostLink
from cms.models import InfoBanner, MenuItem, MessageTemplate, Post, PostLink, Logo


@admin.register(MenuItem)
Expand All @@ -16,6 +16,15 @@ class MenuItemAdmin(admin.ModelAdmin):
)


@admin.register(Logo)
class LogoAdmin(admin.ModelAdmin):
list_display = (
'image',
'name',
'disabled'
)


@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
list_display = (
Expand Down
19 changes: 19 additions & 0 deletions cms/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.core.exceptions import ValidationError
from django.db import models
from django.utils.timezone import now
from base.models import RestrictedFileField

from competition.models import Event, Series

Expand Down Expand Up @@ -111,6 +112,24 @@ class Meta:
# zobrazované aj možno niekde inde


class Logo(models.Model):
"""
Logo
"""
class Meta:
verbose_name = 'logo'
verbose_name_plural = 'logá'

ordering = ['name', ]

name = models.CharField(verbose_name='názov loga', max_length=150)
disabled = models.BooleanField()
image = RestrictedFileField(
content_types=['image/svg+xml', 'image/png'],
upload_to='logo_images/',
verbose_name='Logo', null=True, blank=True)


class InfoBanner(ModelWithVisibility):
class Meta:
verbose_name = 'Informácia v pohyblivom baneri'
Expand Down
6 changes: 6 additions & 0 deletions cms/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ class Meta:
model = models.Post
fields = '__all__'

@ts_interface(context='cms')
class LogoSerializer(serializers.ModelSerializer):
class Meta:
model = models.Logo
fields = '__all__'


@ts_interface(context='cms')
class InfoBannerSerializer(serializers.ModelSerializer):
Expand Down
1 change: 1 addition & 0 deletions cms/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
router.register(r'menu-item', views.MenuItemViewSet)
router.register(r'info-banner', views.InfoBannerViewSet)
router.register(r'message-template', views.MessageTemplateViewSet)
router.register(r'logo', views.LogoViewSet)


urlpatterns = [
Expand Down
49 changes: 46 additions & 3 deletions cms/views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from rest_framework import viewsets
from rest_framework import viewsets, exceptions, status
from rest_framework.decorators import action
from rest_framework.response import Response

from cms.models import InfoBanner, MenuItem, MessageTemplate, Post
from cms.models import InfoBanner, MenuItem, MessageTemplate, Post, Logo
from cms.permissions import PostPermission
from cms.serializers import (InfoBannerSerializer, MenuItemShortSerializer,
MessageTemplateSerializer, PostSerializer)
MessageTemplateSerializer, PostSerializer, LogoSerializer)

from django.http import HttpResponse
from base.utils import mime_type

class MenuItemViewSet(viewsets.ReadOnlyModelViewSet):
"""Položky menu"""
Expand Down Expand Up @@ -45,6 +47,47 @@ def visible(self, request):
return Response(serializer.data)


class LogoViewSet(viewsets.ModelViewSet):
"""Publikácie(výsledky, brožúrky, časopisy, ...)"""
queryset = Logo.objects.all()
serializer_class = LogoSerializer
permission_classes = (PostPermission,)

def perform_create(self, serializer):
'''
Vola sa pri vytvarani objektu,
checkuju sa tu permissions, ci user vie vytvorit publication v danom evente
'''
event = serializer.validated_data['event']
if event.can_user_modify(self.request.user):
serializer.save()
else:
raise exceptions.PermissionDenied(
'Nedostatočné práva na vytvorenie tohoto objektu')

@action(methods=['get'], detail=True, url_path='download')
def download_logo(self, request):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Túto metódu netreba. Stačí ak v response príde link na súbor. Tento súbor sa totiž bude servovať staticky

"""Stiahne logo"""
logo = self.get_object()
response = HttpResponse(
logo.file, content_type=mime_type(logo.file))
response['Content-Disposition'] = f'attachment; filename="{logo.name}"'
return response

@action(methods=['post'], detail=False, url_path='upload')
def upload_publication(self, request):
"""Nahrá súbor publikácie"""
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Toto by sa nemalo volať upload_publication

if 'file' not in request.data:
raise exceptions.ParseError(detail='Request neobsahoval súbor')

file = request.data['file']
if mime_type(file) not in ['application/jpg', 'application/png']:
raise exceptions.ParseError(detail='Nesprávny formát')

return Response(status=status.HTTP_201_CREATED)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tento endpoint iba skontroluje ci tam je file. Ale nevytvori objekt.




class InfoBannerViewSet(viewsets.ModelViewSet):
"""Správy v čiernom info banneri"""
serializer_class = InfoBannerSerializer
Expand Down
Loading