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

Add Event Sub Topic Model And Endpoints #9119

Open
wants to merge 1 commit into
base: dev-v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Empty file added event_sub_topics/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions event_sub_topics/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.contrib import admin

from .models import EventSubTopic

admin.site.register(EventSubTopic)
6 changes: 6 additions & 0 deletions event_sub_topics/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class EventSubTopicsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'event_sub_topics'
28 changes: 28 additions & 0 deletions event_sub_topics/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -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')},
},
),
]
Original file line number Diff line number Diff line change
@@ -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),
),
]
Empty file.
13 changes: 13 additions & 0 deletions event_sub_topics/models.py
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions event_sub_topics/serializer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from rest_framework import serializers

from .models import EventSubTopic

class EventSubTopicSerializer(serializers.ModelSerializer):
class Meta:
model = EventSubTopic
fields = '__all__'
3 changes: 3 additions & 0 deletions event_sub_topics/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
9 changes: 9 additions & 0 deletions event_sub_topics/urls.py
Original file line number Diff line number Diff line change
@@ -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/<int:pk>/", EventSubTopicRetrieveUpdateDestroy.as_view(), name="event_sub_topic_detail"),
path("event-topics/<int:event_topic_id>/event-sub-topics/", EventSubTopicList.as_view(), name="event_sub_topic_list"),
]
67 changes: 67 additions & 0 deletions event_sub_topics/views.py
Original file line number Diff line number Diff line change
@@ -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)

2 changes: 2 additions & 0 deletions open_event_api/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
"event_topics.apps.EventTopicsConfig",
"notification_contents.apps.NotificationContentsConfig",
"video_channels.apps.VideoChannelsConfig",
"event_sub_topics.apps.EventSubTopicsConfig",

]

MIDDLEWARE = [
Expand Down
2 changes: 2 additions & 0 deletions open_event_api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down