diff --git a/backend/settings_base.py b/backend/settings_base.py index c06f6049..44385665 100644 --- a/backend/settings_base.py +++ b/backend/settings_base.py @@ -52,6 +52,7 @@ 'external.apps.ExternalConfig', 'alumni.apps.AlumniConfig', 'community.apps.CommunityConfig', + 'bans.apps.BansConfig', 'notifications', 'markdownify', diff --git a/backend/urls.py b/backend/urls.py index 65cd47ce..40773a62 100644 --- a/backend/urls.py +++ b/backend/urls.py @@ -68,6 +68,7 @@ def api_base(prefix=None): path(api_base(), include('external.urls')), path(api_base(), include('community.urls')), path(api_base(), include('buyandsell.urls')), + path(api_base(), include('bans.urls')), path(api_base('venter'), include("venter.urls")), diff --git a/bans/__init__.py b/bans/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/bans/admin.py b/bans/admin.py new file mode 100644 index 00000000..e19683f5 --- /dev/null +++ b/bans/admin.py @@ -0,0 +1,9 @@ +from django.contrib import admin +from bans.models import SSOBans +# Register your models here. + + +class SSOBansAdmin(admin.ModelAdmin): + list_display = ('banned_user', 'banned_by','id') + +admin.site.register(SSOBans, SSOBansAdmin) \ No newline at end of file diff --git a/bans/apps.py b/bans/apps.py new file mode 100644 index 00000000..70a30bae --- /dev/null +++ b/bans/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class BansConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'bans' diff --git a/bans/migrations/0001_initial.py b/bans/migrations/0001_initial.py new file mode 100644 index 00000000..5a671073 --- /dev/null +++ b/bans/migrations/0001_initial.py @@ -0,0 +1,29 @@ +# Generated by Django 3.2.16 on 2023-10-10 12:29 + +from django.db import migrations, models +import django.db.models.deletion +import uuid + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('users', '0040_remove_userprofile_followed_communities'), + ] + + operations = [ + migrations.CreateModel( + name='SSOBans', + fields=[ + ('id', models.UUIDField(default=uuid.uuid4, primary_key=True, serialize=False)), + ('time_of_creation', models.DateTimeField(auto_now_add=True)), + ('reason', models.CharField(choices=[('IDF', 'Unappropriate Comment'), ('Buy&Sell', 'Unappropriate Activity in Buy and Sell'), ('Graduated ', 'Passed out from Institute'), ('InstiBan', 'Banned by Insittute Authority')], max_length=30)), + ('detailed_reason', models.TextField(blank=True)), + ('duration_of_ban', models.CharField(choices=[('1 month', 'One Month'), ('3 months', 'Three Months'), ('6 months', 'Six Months'), ('12 months', 'Twelve Months'), ('Permanent', 'Permanent')], max_length=20)), + ('banned_by', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='banned_by', to='users.userprofile')), + ('banned_user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='banned_user', to='users.userprofile')), + ], + ), + ] diff --git a/bans/migrations/__init__.py b/bans/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/bans/models.py b/bans/models.py new file mode 100644 index 00000000..9a790284 --- /dev/null +++ b/bans/models.py @@ -0,0 +1,42 @@ +from collections.abc import Iterable +from django.db import models +from uuid import uuid4 +# Create your models here. + + +BAN_REASON_CHOICHES = [ + ('IDF', 'Unappropriate Comment'), + ('Buy&Sell', 'Unappropriate Activity in Buy and Sell'), + ('Graduated ', 'Passed out from Institute'), + ('InstiBan', 'Banned by Insittute Authority') +] + +BAN_DURATION_CHOICES = [ + ('1 month', 'One Month'), + ('3 months', 'Three Months'), + ('6 months', 'Six Months'), + ('12 months', 'Twelve Months'), + ('Permanent', 'Permanent') +] + + +class SSOBans(models.Model): + """Bans imposed on students to access any SSO required View.""" + id = models.UUIDField(primary_key=True, default=uuid4, blank=False) + banned_user = models.ForeignKey(to='users.UserProfile', related_name='banned_user', on_delete=models.CASCADE) + time_of_creation = models.DateTimeField(auto_now_add=True) + reason = models.CharField(max_length=30, choices = BAN_REASON_CHOICHES) + detailed_reason = models.TextField(blank=True) + duration_of_ban = models.CharField(max_length=20, choices = BAN_DURATION_CHOICES) + banned_by = models.ForeignKey(to = 'users.UserProfile', related_name='banned_by', on_delete=models.SET_NULL, null=True) + + def __str__(self) -> str: + return self.user + + def save(self): + self.banned_user = self.banned_user.ldap_id + + super.save() + + + diff --git a/bans/serializers.py b/bans/serializers.py new file mode 100644 index 00000000..d16d3fe7 --- /dev/null +++ b/bans/serializers.py @@ -0,0 +1,11 @@ +"""Serializers for Bans """ +from rest_framework import serializers +from users.serializers import UserProfileSerializer +from .models import SSOBans + +class SSOBansSerializer(serializers.ModelSerializer): + banned_by = UserProfileSerializer(read_only = False, source = 'name') + + class Meta: + model = SSOBans + fields = '__all__' diff --git a/bans/tests.py b/bans/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/bans/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/bans/urls.py b/bans/urls.py new file mode 100644 index 00000000..b801e879 --- /dev/null +++ b/bans/urls.py @@ -0,0 +1,12 @@ +"""URLs for Bans""" +from django.urls import path +from bans.views import SSOBansViewSet + +urlpatterns =[ + path('bans/', SSOBansViewSet.as_view( + {'get':'list', 'post':'create'} + )), + path('bans//', SSOBansViewSet.as_view({ + 'put': 'update', 'delete': 'destroy', 'get': 'retrieve', 'patch': 'update' + })) +] \ No newline at end of file diff --git a/bans/views.py b/bans/views.py new file mode 100644 index 00000000..9ffe7027 --- /dev/null +++ b/bans/views.py @@ -0,0 +1,91 @@ +"""Views for achievements models.""" +from uuid import UUID +from django.shortcuts import get_object_or_404 +from rest_framework import viewsets, status +from rest_framework.response import Response + +from roles.helpers import login_required_ajax, user_has_insti_privilege, forbidden_no_privileges + + +from users.models import UserProfile +from .models import SSOBans +from .serializers import SSOBansSerializer + + +# Create your views here. + + +class SSOBansViewSet(viewsets.ModelViewSet): + queryset = SSOBans.objects.all() + serializer_class = SSOBansSerializer + + @login_required_ajax + def list(self, request): + """List all the banned Accounts.""" + if user_has_insti_privilege(request.user.profile, 'RoleB'): + queryset = self.get_queryset() + serializer = self.get_serializer(queryset, many=True) + return Response(serializer.data) + else: + return forbidden_no_privileges + + @login_required_ajax + def retrieve(self, request, pk): + + if user_has_insti_privilege(request.user.profile, 'RoleB'): + instance = get_object_or_404(self.queryset, pk=pk) + serializer = self.get_serializer(instance) + return Response(serializer.data) + else: + return forbidden_no_privileges + + @login_required_ajax + def create(self, request): + if user_has_insti_privilege(request.user.profile, 'RoleB'): + serializer = self.get_serializer(data=request.data) + if serializer.is_valid(): + + banned_user = serializer.validated_data.get('banned_user') + duration_of_ban = serializer.validated_data.get('duration_of_ban') + + + if banned_user: + banned_user_model = UserProfile.objects.filter(ldap_id = banned_user).first() + if not banned_user_model: + return Response({'user': ['This field is Invalid.']}, status=status.HTTP_400_BAD_REQUEST) + + if not duration_of_ban: + return Response({'duration_of_ban': ['This field is required.']}, status=status.HTTP_400_BAD_REQUEST) + + serializer.validated_date['banned_by'] = request.user + serializer.save() + return Response(serializer.data, status=status.HTTP_201_CREATED) + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + else: + return forbidden_no_privileges + + @login_required_ajax + def update(self, request, pk=None, *args, **kwargs): + if user_has_insti_privilege(request.user.profile, 'RoleB'): + instance = get_object_or_404(self.queryset, pk=pk) + serializer = self.get_serializer(instance, data=request.data, partial=True) + + if serializer.is_valid(): + serializer.validated_data['banned_by'] = request.user + serializer.save() + return Response(serializer.data) + + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + else: + return forbidden_no_privileges + + @login_required_ajax + def destroy(self, request, pk=None, *args, **kwargs): + if user_has_insti_privilege(request.user.profile, 'RoleB'): + instance = get_object_or_404(self.queryset, pk=pk) + instance.delete() + return Response(status=status.HTTP_204_NO_CONTENT) + else: + return forbidden_no_privileges + + \ No newline at end of file