From 84f3eb35ddff9538765203a016ac79a2e58529e2 Mon Sep 17 00:00:00 2001 From: cbum-dev Date: Mon, 29 Jan 2024 20:00:40 +0530 Subject: [PATCH] Adds Event Sub Topics Endpoints --- event_sub_topics/__init__.py | 0 event_sub_topics/admin.py | 5 ++ event_sub_topics/apps.py | 6 ++ event_sub_topics/migrations/0001_initial.py | 28 ++++++++ ...tsubtopic_name_alter_eventsubtopic_slug.py | 23 +++++++ event_sub_topics/migrations/__init__.py | 0 event_sub_topics/models.py | 13 ++++ event_sub_topics/serializer.py | 8 +++ event_sub_topics/tests.py | 3 + event_sub_topics/urls.py | 9 +++ event_sub_topics/views.py | 67 +++++++++++++++++++ open_event_api/settings.py | 2 + open_event_api/urls.py | 2 + 13 files changed, 166 insertions(+) create mode 100644 event_sub_topics/__init__.py create mode 100644 event_sub_topics/admin.py create mode 100644 event_sub_topics/apps.py create mode 100644 event_sub_topics/migrations/0001_initial.py create mode 100644 event_sub_topics/migrations/0002_alter_eventsubtopic_name_alter_eventsubtopic_slug.py create mode 100644 event_sub_topics/migrations/__init__.py create mode 100644 event_sub_topics/models.py create mode 100644 event_sub_topics/serializer.py create mode 100644 event_sub_topics/tests.py create mode 100644 event_sub_topics/urls.py create mode 100644 event_sub_topics/views.py diff --git a/event_sub_topics/__init__.py b/event_sub_topics/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/event_sub_topics/admin.py b/event_sub_topics/admin.py new file mode 100644 index 0000000000..7cd3cedc58 --- /dev/null +++ b/event_sub_topics/admin.py @@ -0,0 +1,5 @@ +from django.contrib import admin + +from .models import EventSubTopic + +admin.site.register(EventSubTopic) \ No newline at end of file diff --git a/event_sub_topics/apps.py b/event_sub_topics/apps.py new file mode 100644 index 0000000000..e0139a55fd --- /dev/null +++ b/event_sub_topics/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class EventSubTopicsConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'event_sub_topics' diff --git a/event_sub_topics/migrations/0001_initial.py b/event_sub_topics/migrations/0001_initial.py new file mode 100644 index 0000000000..a76e81adce --- /dev/null +++ b/event_sub_topics/migrations/0001_initial.py @@ -0,0 +1,28 @@ +# Generated by Django 5.0 on 2024-01-28 18:12 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('event_topics', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='EventSubTopic', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(blank=True, max_length=2147483647, null=True)), + ('slug', models.CharField(blank=True, max_length=2147483647, null=True)), + ('event_topic', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='event_topics.eventtopic')), + ], + options={ + 'unique_together': {('slug', 'event_topic')}, + }, + ), + ] diff --git a/event_sub_topics/migrations/0002_alter_eventsubtopic_name_alter_eventsubtopic_slug.py b/event_sub_topics/migrations/0002_alter_eventsubtopic_name_alter_eventsubtopic_slug.py new file mode 100644 index 0000000000..c9bead0693 --- /dev/null +++ b/event_sub_topics/migrations/0002_alter_eventsubtopic_name_alter_eventsubtopic_slug.py @@ -0,0 +1,23 @@ +# Generated by Django 5.0 on 2024-01-29 14:19 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('event_sub_topics', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='eventsubtopic', + name='name', + field=models.CharField(blank=True, max_length=200, null=True), + ), + migrations.AlterField( + model_name='eventsubtopic', + name='slug', + field=models.CharField(blank=True, max_length=200, null=True), + ), + ] diff --git a/event_sub_topics/migrations/__init__.py b/event_sub_topics/migrations/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/event_sub_topics/models.py b/event_sub_topics/models.py new file mode 100644 index 0000000000..f934afda59 --- /dev/null +++ b/event_sub_topics/models.py @@ -0,0 +1,13 @@ +from django.db import models +from event_topics.models import EventTopic + +class EventSubTopic(models.Model): + name = models.CharField(max_length=200, null=True, blank=True) + slug = models.CharField(max_length=200, null=True, blank=True) + event_topic = models.ForeignKey(EventTopic, on_delete=models.CASCADE, null=True, blank=True) + + class Meta: + unique_together = (('slug', 'event_topic'),) + + def __str__(self): + return self.name \ No newline at end of file diff --git a/event_sub_topics/serializer.py b/event_sub_topics/serializer.py new file mode 100644 index 0000000000..3133930adf --- /dev/null +++ b/event_sub_topics/serializer.py @@ -0,0 +1,8 @@ +from rest_framework import serializers + +from .models import EventSubTopic + +class EventSubTopicSerializer(serializers.ModelSerializer): + class Meta: + model = EventSubTopic + fields = '__all__' diff --git a/event_sub_topics/tests.py b/event_sub_topics/tests.py new file mode 100644 index 0000000000..7ce503c2dd --- /dev/null +++ b/event_sub_topics/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/event_sub_topics/urls.py b/event_sub_topics/urls.py new file mode 100644 index 0000000000..650e8cd39a --- /dev/null +++ b/event_sub_topics/urls.py @@ -0,0 +1,9 @@ +from django.urls import path + +from .views import EventSubTopicPost, EventSubTopicList,EventSubTopicRetrieveUpdateDestroy + +urlpatterns = [ + path("event-sub-topics/", EventSubTopicPost.as_view(), name="event_sub_topic_list_create"), + path("event-sub-topics//", EventSubTopicRetrieveUpdateDestroy.as_view(), name="event_sub_topic_detail"), + path("event-topics//event-sub-topics/", EventSubTopicList.as_view(), name="event_sub_topic_list"), +] diff --git a/event_sub_topics/views.py b/event_sub_topics/views.py new file mode 100644 index 0000000000..9bfb225b46 --- /dev/null +++ b/event_sub_topics/views.py @@ -0,0 +1,67 @@ +from rest_framework import generics + +from open_event_api.permissions import IsSuperAdminForUpdate +from rest_framework.exceptions import PermissionDenied + +from .serializer import EventSubTopicSerializer +from .models import EventSubTopic,EventTopic + +class EventSubTopicPost(generics.CreateAPIView): + """ + Create event sub topics + """ + serializer_class = EventSubTopicSerializer + + def perform_create(self, serializer): + """ + Custom method to perform additional actions on object creation + """ + event_topic = self.request.data.get('event_topic') + if not event_topic: + raise serializer.ValidationError({'event_topic': 'This field is required.'}) + serializer.save() + + +class EventSubTopicList(generics.ListAPIView): + """ + List event sub topics + """ + serializer_class = EventSubTopicSerializer + + def get_queryset(self): + queryset = EventSubTopic.objects.all() + + event_topic_id = self.kwargs.get('event_topic_id') + if event_topic_id: + event_topic = EventTopic.objects.all() + if event_topic: + queryset = queryset.all() + + return queryset + +class EventSubTopicRetrieveUpdateDestroy(generics.RetrieveUpdateDestroyAPIView): + """ + Retrieve, update or delete event sub topics + """ + queryset = EventSubTopic.objects.all() + serializer_class = EventSubTopicSerializer + permission_classes = [IsSuperAdminForUpdate] + + def put(self, request, *args, **kwargs): + if not self.request.user.is_staff: + raise PermissionDenied(detail='Admin access is required.') + + return super().put(request, *args, **kwargs) + + def patch(self, request, *args, **kwargs): + if not self.request.user.is_staff: + raise PermissionDenied(detail='Admin access is required.') + + return super().patch(request, *args, **kwargs) + + def delete(self, request, *args, **kwargs): + if not self.request.user.is_staff: + raise PermissionDenied(detail='Admin access is required.') + + return super().delete(request, *args, **kwargs) + diff --git a/open_event_api/settings.py b/open_event_api/settings.py index c164044073..1361c41304 100644 --- a/open_event_api/settings.py +++ b/open_event_api/settings.py @@ -54,6 +54,8 @@ "event_topics.apps.EventTopicsConfig", "notification_contents.apps.NotificationContentsConfig", "video_channels.apps.VideoChannelsConfig", + "event_sub_topics.apps.EventSubTopicsConfig", + ] MIDDLEWARE = [ diff --git a/open_event_api/urls.py b/open_event_api/urls.py index f326ae74f3..2cc8fd6949 100644 --- a/open_event_api/urls.py +++ b/open_event_api/urls.py @@ -32,6 +32,8 @@ # standard api endpoints path("v2/", include("roles.urls")), path("v2/", include("event_topics.urls")), + path("v2/", include("event_sub_topics.urls")), + path("v2/custom-system-roles/", include("custom_sys_roles.urls")), # api docs via spectacular path("v2/schema/", SpectacularAPIView.as_view(), name="schema"),