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

Added solution winner and toprating views #3

Open
wants to merge 1 commit into
base: main
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
10 changes: 9 additions & 1 deletion backend/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions backend/api/solution/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
37 changes: 35 additions & 2 deletions backend/api/solution/solution_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()


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()
4 changes: 3 additions & 1 deletion backend/api/solution/urls.py
Original file line number Diff line number Diff line change
@@ -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/<str:pk>/", SolutionUpdateDeleteAPI.as_view(), name="solution_update"),
path("winner/<str:pk>/", SolutionWinner.as_view(), name="solution_winner"),
path("top-rate/<str:pk>/", SolutionTopRate.as_view(), name='solution_toprate')
]
19 changes: 18 additions & 1 deletion backend/requirements.txt
Original file line number Diff line number Diff line change
@@ -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
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
8 changes: 7 additions & 1 deletion backend/utils/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@


class RoleType(Enum):
COMPANY_MEMBER = "Company Member"
COMPANY_MEMBER = "Company Member"
ADMIN = "Admins"

class WinnerType(Enum):
WINNER = "Winner"
PARTIAL_WINNER = "Partial Winner"
NOT_WINNER = "Not Winner"