4 changed files with 175 additions and 1 deletions
-
83apps/dobodbi_calendar/admin.py
-
41apps/dobodbi_calendar/serializers_admin.py
-
8apps/dobodbi_calendar/urls.py
-
44apps/dobodbi_calendar/views_admin.py
@ -0,0 +1,41 @@ |
|||||
|
from rest_framework import serializers |
||||
|
|
||||
|
from apps.dobodbi_calendar.models import CalendarOccasions |
||||
|
|
||||
|
|
||||
|
class CalendarOccasionAdminSerializer(serializers.ModelSerializer): |
||||
|
dates = serializers.ListField(child=serializers.DictField(), allow_empty=False) |
||||
|
|
||||
|
class Meta: |
||||
|
model = CalendarOccasions |
||||
|
fields = [ |
||||
|
"id", |
||||
|
"title", |
||||
|
"is_global", |
||||
|
"occasion_type", |
||||
|
"dates", |
||||
|
"is_yearly", |
||||
|
"event_type", |
||||
|
"updated_at", |
||||
|
"created_at", |
||||
|
] |
||||
|
read_only_fields = ["id", "updated_at", "created_at"] |
||||
|
|
||||
|
def validate_dates(self, value): |
||||
|
cleaned_dates = [] |
||||
|
|
||||
|
for index, item in enumerate(value): |
||||
|
day = str(item.get("day", "")).strip() |
||||
|
month = str(item.get("month", "")).strip() |
||||
|
year = str(item.get("year", "")).strip() |
||||
|
|
||||
|
if not day or not month: |
||||
|
raise serializers.ValidationError(f"Date row {index + 1} must include day and month.") |
||||
|
|
||||
|
cleaned_dates.append({ |
||||
|
"day": day, |
||||
|
"month": month, |
||||
|
"year": year, |
||||
|
}) |
||||
|
|
||||
|
return cleaned_dates |
||||
@ -1,8 +1,14 @@ |
|||||
from django.urls import path |
|
||||
|
from django.urls import include, path |
||||
|
from rest_framework.routers import SimpleRouter |
||||
from apps.dobodbi_calendar.views import CalendarList, AdjustmentConfigView, OccasionsList |
from apps.dobodbi_calendar.views import CalendarList, AdjustmentConfigView, OccasionsList |
||||
|
from apps.dobodbi_calendar.views_admin import AdminCalendarOccasionViewSet |
||||
|
|
||||
|
admin_router = SimpleRouter() |
||||
|
admin_router.register(r'occasions', AdminCalendarOccasionViewSet, basename='admin-calendar-occasions') |
||||
|
|
||||
|
|
||||
urlpatterns = [ |
urlpatterns = [ |
||||
|
path('admin/', include(admin_router.urls)), |
||||
path('sync-occasions/', CalendarList.as_view()), |
path('sync-occasions/', CalendarList.as_view()), |
||||
path('adjustemnts/', AdjustmentConfigView.as_view()), |
path('adjustemnts/', AdjustmentConfigView.as_view()), |
||||
path('occasions/', OccasionsList.as_view()), |
path('occasions/', OccasionsList.as_view()), |
||||
|
|||||
@ -0,0 +1,44 @@ |
|||||
|
from django.db.models import Q |
||||
|
from rest_framework.authentication import TokenAuthentication |
||||
|
from rest_framework.permissions import IsAuthenticated |
||||
|
from rest_framework.viewsets import ModelViewSet |
||||
|
|
||||
|
from apps.account.permissions import IsSuperAdmin |
||||
|
from apps.dobodbi_calendar.models import CalendarOccasions |
||||
|
from apps.dobodbi_calendar.serializers_admin import CalendarOccasionAdminSerializer |
||||
|
|
||||
|
|
||||
|
class AdminCalendarOccasionViewSet(ModelViewSet): |
||||
|
serializer_class = CalendarOccasionAdminSerializer |
||||
|
permission_classes = [IsAuthenticated, IsSuperAdmin] |
||||
|
authentication_classes = [TokenAuthentication] |
||||
|
pagination_class = None |
||||
|
|
||||
|
def get_queryset(self): |
||||
|
queryset = CalendarOccasions.objects.all() |
||||
|
search_query = self.request.query_params.get("search") |
||||
|
|
||||
|
if search_query: |
||||
|
queryset = queryset.filter( |
||||
|
Q(title__icontains=search_query) |
||||
|
| Q(event_type__icontains=search_query) |
||||
|
| Q(occasion_type__icontains=search_query) |
||||
|
) |
||||
|
|
||||
|
event_type = self.request.query_params.get("event_type") |
||||
|
if event_type and event_type != "all": |
||||
|
queryset = queryset.filter(event_type=event_type) |
||||
|
|
||||
|
occasion_type = self.request.query_params.get("occasion_type") |
||||
|
if occasion_type and occasion_type != "all": |
||||
|
queryset = queryset.filter(occasion_type=occasion_type) |
||||
|
|
||||
|
yearly = self.request.query_params.get("is_yearly") |
||||
|
if yearly in {"true", "false"}: |
||||
|
queryset = queryset.filter(is_yearly=yearly == "true") |
||||
|
|
||||
|
global_filter = self.request.query_params.get("is_global") |
||||
|
if global_filter in {"true", "false"}: |
||||
|
queryset = queryset.filter(is_global=global_filter == "true") |
||||
|
|
||||
|
return queryset.order_by("-updated_at", "-id") |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue