Skip to content

Commit

Permalink
feat: faqs, special files endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhruv9449 committed Mar 17, 2024
1 parent a3be5dd commit 2a14ec4
Show file tree
Hide file tree
Showing 8 changed files with 235 additions and 8 deletions.
33 changes: 31 additions & 2 deletions student_welfare_backend/student_welfare_backend/core/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.contrib import admin

from student_welfare_backend.core.models import Club, Event, Spotlight, UserClubRelation, Newsletter
from student_welfare_backend.core.models import Club, Event, Spotlight, UserClubRelation, Newsletter, FAQ, SpecialFile


# Register your models here.
Expand Down Expand Up @@ -68,4 +68,33 @@ class NewsletterAdmin(admin.ModelAdmin):
"month",
"link"
]
list_filter = ["year", "month"]
list_filter = ["year", "month"]


@admin.register(FAQ)
class FAQAdmin(admin.ModelAdmin):
"""Admin for FAQ model."""

list_display = [
"id",
"question",
"answer"
]
search_fields = ["question", "answer"]


@admin.register(SpecialFile)
class SpecialFileAdmin(admin.ModelAdmin):
"""Admin for SpecialFile model."""

list_display = [
"id",
"year",
"type",
"file_link"
]
list_filter = ["year", "type"]
search_fields = ["year", "type"]
ordering = ["-year"]


Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
from rest_framework import serializers

from student_welfare_backend.core.models import Event, Club, UserClubRelation, Spotlight, Newsletter
from student_welfare_backend.core.models import (
Event,
Club,
UserClubRelation,
Spotlight,
Newsletter,
FAQ,
SpecialFile,
)
from student_welfare_backend.users.api.serializers import UserLoginSerializer


Expand Down Expand Up @@ -75,3 +83,15 @@ class NewsletterSerializer(serializers.ModelSerializer):
class Meta:
model = Newsletter
fields = "__all__"


class FAQSerializer(serializers.ModelSerializer):
class Meta:
model = FAQ
fields = "__all__"


class SpecialFileSerializer(serializers.ModelSerializer):
class Meta:
model = SpecialFile
fields = "__all__"
30 changes: 29 additions & 1 deletion student_welfare_backend/student_welfare_backend/core/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
NewsletterViewSet,
NewsletterAdminViewSet,
)
from student_welfare_backend.core.api.views.faqs import (
FAQViewSet,
FAQAdminViewSet,
)
from student_welfare_backend.core.api.views.special_files import (
SpecialFileViewSet,
SpecialFileAdminViewSet,
)
from student_welfare_backend.core.api.views.notifications import PushNotificationView

# URL PATTERNS
Expand Down Expand Up @@ -96,4 +104,24 @@

newsletter_admin_router = DefaultRouter()
newsletter_admin_router.register(r"admin/newsletters", NewsletterAdminViewSet, basename="newsletters_admin")
urlpatterns += newsletter_admin_router.urls
urlpatterns += newsletter_admin_router.urls


# FAQ
faq_router = DefaultRouter()
faq_router.register(r"faqs", FAQViewSet, basename="faqs")
urlpatterns += faq_router.urls

faq_admin_router = DefaultRouter()
faq_admin_router.register(r"admin/faqs", FAQAdminViewSet, basename="faqs_admin")
urlpatterns += faq_admin_router.urls


# SPECIAL FILES
special_file_router = DefaultRouter()
special_file_router.register(r"special_files", SpecialFileViewSet, basename="special_files")
urlpatterns += special_file_router.urls

special_file_admin_router = DefaultRouter()
special_file_admin_router.register(r"admin/special_files", SpecialFileAdminViewSet, basename="special_files_admin")
urlpatterns += special_file_admin_router.urls
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,14 @@ def get(request):
status=status.HTTP_400_BAD_REQUEST,
)

organizations = Club.objects.filter(type=organization_type).all()
organizations = Club.objects.filter(type=organization_type).order_by("-name").all()
if len(organizations) == 0:
return Response(
{"detail": "No such organizations found"},
status=status.HTTP_404_NOT_FOUND,
)
organization = organizations[0]
return Response(
ClubDetailSerializer(organization).data,
ClubDetailSerializer(organizations, many=True).data,
status=status.HTTP_200_OK,
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from rest_framework.viewsets import ModelViewSet, ReadOnlyModelViewSet
from rest_framework.filters import SearchFilter, OrderingFilter
from rest_framework.permissions import IsAuthenticated
from rest_framework_simplejwt.authentication import JWTAuthentication
from django_filters.rest_framework import DjangoFilterBackend


from student_welfare_backend.core.models import FAQ
from student_welfare_backend.core.api.serializers import FAQSerializer
from student_welfare_backend.customs.pagination import CustomPagination
from student_welfare_backend.customs.permissions import IsDSW, IsADSW


class FAQViewSet(ReadOnlyModelViewSet):
authentication_classes = []
permission_classes = []
queryset = FAQ.objects.all()
serializer_class = FAQSerializer
pagination_class = CustomPagination
filter_backends = [SearchFilter, OrderingFilter, DjangoFilterBackend]
search_fields = ["question", "answer"]
ordering_fields = ["question"]
ordering = ["question"]


class FAQAdminViewSet(ModelViewSet):
authentication_classes = [JWTAuthentication]
permission_classes = [IsAuthenticated, IsDSW | IsADSW]
queryset = FAQ.objects.all()
serializer_class = FAQSerializer
pagination_class = CustomPagination
filter_backends = [SearchFilter, OrderingFilter, DjangoFilterBackend]
search_fields = ["question", "answer"]
ordering_fields = ["question"]
ordering = ["question"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from rest_framework.viewsets import ModelViewSet, ReadOnlyModelViewSet
from rest_framework.filters import SearchFilter, OrderingFilter
from rest_framework.permissions import IsAuthenticated
from rest_framework_simplejwt.authentication import JWTAuthentication
from django_filters.rest_framework import DjangoFilterBackend


from student_welfare_backend.core.models import SpecialFile
from student_welfare_backend.core.api.serializers import SpecialFileSerializer
from student_welfare_backend.customs.pagination import CustomPagination
from student_welfare_backend.customs.permissions import IsDSW, IsADSW


class SpecialFileViewSet(ReadOnlyModelViewSet):
authentication_classes = []
permission_classes = []
queryset = SpecialFile.objects.all()
serializer_class = SpecialFileSerializer
pagination_class = CustomPagination
filter_backends = [SearchFilter, OrderingFilter, DjangoFilterBackend]
filterset_fields = ["type"]
search_fields = ["year", "type"]
ordering_fields = ["year"]
ordering = ["-year"]


class SpecialFileAdminViewSet(ModelViewSet):
authentication_classes = [JWTAuthentication]
permission_classes = [IsAuthenticated, IsDSW | IsADSW]
queryset = SpecialFile.objects.all()
serializer_class = SpecialFileSerializer
pagination_class = CustomPagination
filter_backends = [SearchFilter, OrderingFilter, DjangoFilterBackend]
filterset_fields = ["type"]
search_fields = ["year", "type"]
ordering_fields = ["year"]
ordering = ["-year"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Generated by Django 4.0.10 on 2024-03-17 12:50

import django.core.validators
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('core', '0011_newsletter'),
]

operations = [
migrations.CreateModel(
name='FAQ',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('question', models.CharField(max_length=255)),
('answer', models.TextField()),
],
options={
'verbose_name': 'FAQ',
'verbose_name_plural': 'FAQs',
},
),
migrations.CreateModel(
name='SpecialFile',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('year', models.CharField(max_length=4, validators=[django.core.validators.RegexValidator(message='Year must be 4 digits long', regex='^\\d{4}$')])),
('type', models.CharField(choices=[('events', 'Events'), ('program_representatives', 'Program Representatives')], max_length=100)),
('file_link', models.CharField(max_length=255)),
],
options={
'verbose_name': 'Special File',
'verbose_name_plural': 'Special Files',
},
),
migrations.AlterField(
model_name='club',
name='type',
field=models.CharField(choices=[('student_welfare', 'Student Welfare'), ('student_council', 'Student Council'), ('club', 'Club'), ('chapter', 'Chapter'), ('team', 'Team'), ('greviance_cell', 'Greviance Cell'), ('counseling_division', 'Counseling Division'), ('other', 'Other')], default='club', max_length=50, verbose_name='Type'),
),
]
37 changes: 36 additions & 1 deletion student_welfare_backend/student_welfare_backend/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Club(models.Model):
("chapter", "Chapter"),
("team", "Team"),
("greviance_cell", "Greviance Cell"),
("counseling_division", "Counseling Division"),
("other", "Other"),
]

Expand Down Expand Up @@ -151,4 +152,38 @@ class Meta:
)
month = models.CharField(max_length=10, choices=month_names)
cover_page = models.CharField(max_length=255, null=True, blank=True)
link = models.CharField(max_length=255, null=True, blank=True)
link = models.CharField(max_length=255, null=True, blank=True)


class SpecialFile(models.Model):
"""
Model containing all the data of special files
"""

file_type_options = [
("events", "Events"),
("program_representatives", "Program Representatives"),
]

class Meta:
verbose_name = "Special File"
verbose_name_plural = "Special Files"

year = models.CharField(
max_length=4, validators=[RegexValidator(regex="^\d{4}$", message="Year must be 4 digits long")]
)
type = models.CharField(max_length=100, choices=file_type_options)
file_link = models.CharField(max_length=255)


class FAQ(models.Model):
"""
Model containing all the data of FAQs
"""

class Meta:
verbose_name = "FAQ"
verbose_name_plural = "FAQs"

question = models.CharField(max_length=255)
answer = models.TextField()

0 comments on commit 2a14ec4

Please sign in to comment.