From 2d4c2443f2247f0ee459283a41729f9178d20421 Mon Sep 17 00:00:00 2001 From: dilshadmohammed Date: Fri, 23 Aug 2024 22:29:08 +0530 Subject: [PATCH] Added solution winner and toprating views --- backend/api/models.py | 10 +++++++- backend/api/solution/serializer.py | 4 +-- backend/api/solution/solution_view.py | 37 +++++++++++++++++++++++++-- backend/api/solution/urls.py | 4 ++- backend/requirements.txt | 19 +++++++++++++- backend/utils/types.py | 8 +++++- 6 files changed, 74 insertions(+), 8 deletions(-) diff --git a/backend/api/models.py b/backend/api/models.py index a451c3f..4ecc797 100644 --- a/backend/api/models.py +++ b/backend/api/models.py @@ -2,6 +2,7 @@ import uuid from .managers import user_manager from django.conf import settings +from utils.types import WinnerType # Copied from mulearnbackend class User(models.Model): @@ -57,10 +58,17 @@ class Meta: db_table = 'mushelf_problem_statements' class Solution(models.Model): + + WINNER_STATUS_CHOICES = [(tag.name, tag.value) for tag in WinnerType] + id = models.CharField(primary_key=True, max_length=36, default=uuid.uuid4) title = models.CharField(max_length=100) description = models.TextField() - is_winner = models.BooleanField(default=False) + winner_status = models.CharField( + max_length=15, + choices=WINNER_STATUS_CHOICES, + default=WinnerType.NOT_WINNER.name, + ) is_sorted = models.BooleanField(default=False) problem_statement = models.ForeignKey(ProblemStatement, related_name="solutions", on_delete=models.CASCADE, db_column="problem_statement") created_at = models.DateTimeField(auto_now_add=True) diff --git a/backend/api/solution/serializer.py b/backend/api/solution/serializer.py index bc7c1a4..beeab70 100644 --- a/backend/api/solution/serializer.py +++ b/backend/api/solution/serializer.py @@ -13,8 +13,8 @@ class Solution_serializer(serializers.ModelSerializer): contributors = Contributor_serializer(many=True) class Meta: model = Solution - fields = ['id', 'title', 'description', 'is_winner', 'problem_statement', 'created_at', 'created_by', 'updated_at', 'contributors'] - read_only_fields = ['is_winner', 'created_by'] + fields = ['id', 'title', 'description', 'winner_status', 'problem_statement', 'created_at', 'created_by', 'updated_at', 'contributors'] + read_only_fields = ['winner_status','created_by'] def create(self, validated_data): contributers_data = validated_data.pop("contributors") diff --git a/backend/api/solution/solution_view.py b/backend/api/solution/solution_view.py index 7656912..5ef20ff 100644 --- a/backend/api/solution/solution_view.py +++ b/backend/api/solution/solution_view.py @@ -3,8 +3,9 @@ from .serializer import Solution_serializer from utils.response import CustomResponse from api.models import User, Solution -from utils.permission import CustomizePermission +from utils.permission import CustomizePermission,role_required from utils.utils import CommonUtils +from utils.types import RoleType,WinnerType class SolutionPostAPI(APIView): @@ -32,9 +33,41 @@ def patch(self, request, pk=None): instance = Solution.objects.filter(id=pk).first() if not instance: return CustomResponse(general_message="Solution not found").get_failure_response() + serializer = Solution_serializer(instance, data=request.data, partial=True) if not serializer.is_valid(): return CustomResponse(general_message=serializer.errors).get_failure_response() serializer.save() return CustomResponse(general_message="Solution updated successfully").get_success_response() - \ No newline at end of file + +class SolutionWinner(APIView): + permission_classes = [CustomizePermission] + + @role_required([RoleType.COMPANY_MEMBER.value,RoleType.ADMIN.value]) + def patch(self, request, pk=None): + new_status = request.data.get('winner_status') + + if new_status not in [tag.name for tag in WinnerType]: + return CustomResponse(general_message="Invalid winner status").get_failure_response() + + instance = Solution.objects.filter(id=pk).first() + if not instance: + return CustomResponse(general_message="Solution not found").get_failure_response() + + instance.winner_status = new_status + instance.save() + return CustomResponse(general_message="Solution winner status changed successfully").get_success_response() + +class SolutionTopRate(APIView): + + permission_classes = [CustomizePermission] + + @role_required([RoleType.COMPANY_MEMBER.value,RoleType.ADMIN.value]) + def patch(self, request, pk=None): + instance = Solution.objects.filter(id=pk).first() + if not instance: + return CustomResponse(general_message="Solution not found").get_failure_response() + + instance.is_sorted = not instance.is_sorted + instance.save() + return CustomResponse(general_message="Solution top rated status changed successfully").get_success_response() \ No newline at end of file diff --git a/backend/api/solution/urls.py b/backend/api/solution/urls.py index ea55fc2..1e3a4be 100644 --- a/backend/api/solution/urls.py +++ b/backend/api/solution/urls.py @@ -1,7 +1,9 @@ from django.urls import path -from .solution_view import SolutionUpdateDeleteAPI, SolutionPostAPI +from .solution_view import SolutionUpdateDeleteAPI, SolutionPostAPI,SolutionWinner,SolutionTopRate urlpatterns = [ path("", SolutionPostAPI.as_view(), name="solution_create"), path("update//", SolutionUpdateDeleteAPI.as_view(), name="solution_update"), + path("winner//", SolutionWinner.as_view(), name="solution_winner"), + path("top-rate//", SolutionTopRate.as_view(), name='solution_toprate') ] diff --git a/backend/requirements.txt b/backend/requirements.txt index 50842bb..d0d00a3 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -1,10 +1,27 @@ +asgiref==3.8.1 +attrs==24.2.0 +certifi==2024.7.4 +charset-normalizer==3.3.2 Django==5.0.6 django-cors-headers==4.4.0 djangorestframework==3.15.2 drf-spectacular==0.27.2 +et-xmlfile==1.1.0 +idna==3.7 +inflection==0.5.1 +jsonschema==4.23.0 +jsonschema-specifications==2023.12.1 mysqlclient==2.2.4 +openpyxl==3.1.5 pip-chill==1.0.3 PyJWT==2.8.0 PyMySQL==1.1.1 python-decouple==3.8 -requests==2.31.0 \ No newline at end of file +pytz==2024.1 +PyYAML==6.0.2 +referencing==0.35.1 +requests==2.31.0 +rpds-py==0.20.0 +sqlparse==0.5.1 +uritemplate==4.1.1 +urllib3==2.2.2 diff --git a/backend/utils/types.py b/backend/utils/types.py index eeb21a0..0b21a8c 100644 --- a/backend/utils/types.py +++ b/backend/utils/types.py @@ -2,4 +2,10 @@ class RoleType(Enum): - COMPANY_MEMBER = "Company Member" \ No newline at end of file + COMPANY_MEMBER = "Company Member" + ADMIN = "Admins" + +class WinnerType(Enum): + WINNER = "Winner" + PARTIAL_WINNER = "Partial Winner" + NOT_WINNER = "Not Winner" \ No newline at end of file