diff --git a/api/controllers/account/get_account.py b/api/controllers/account/get_account.py index 68485ff..275d3f9 100644 --- a/api/controllers/account/get_account.py +++ b/api/controllers/account/get_account.py @@ -8,7 +8,7 @@ from django.forms.models import model_to_dict from ...serializers import * -def get_account(account_id:int): +def get_account(account_id:str): try: account = Account.objects.get(account_id=account_id) serialize = AccountSerializer(account) diff --git a/api/controllers/collection/create_collection.py b/api/controllers/collection/create_collection.py index 8894516..f5541bc 100644 --- a/api/controllers/collection/create_collection.py +++ b/api/controllers/collection/create_collection.py @@ -8,7 +8,7 @@ from django.forms.models import model_to_dict from ...serializers import * -def create_collection(account_id:int,request): +def create_collection(account_id:str,request): request.data['creator'] = account_id serialize = CollectionSerializer(data=request.data) diff --git a/api/controllers/collection/get_all_collections_by_account.py b/api/controllers/collection/get_all_collections_by_account.py index b4f64af..7cd16fb 100644 --- a/api/controllers/collection/get_all_collections_by_account.py +++ b/api/controllers/collection/get_all_collections_by_account.py @@ -8,19 +8,27 @@ from django.forms.models import model_to_dict from ...serializers import * -def get_all_collections_by_account(account_id:int): - collections = Collection.objects.filter(creator=account_id).order_by('-updated_date') +def populated_problems(collections: Collection): problemCollections = CollectionProblem.objects.filter(collection__in=collections) populated_collections = [] for collection in collections: - con_probs = problemCollections.filter(collection=collection) - serialize = CollectionSerializer(collection) - collection_data = serialize.data - collection_data['problems'] = CollectionProblemPopulateProblemSecureSerializer(con_probs,many=True).data + collection.problems = problemCollections.filter(collection=collection) + populated_collections.append(collection) - populated_collections.append(collection_data) + return populated_collections + +def get_all_collections_by_account(account:Account): + + collections = Collection.objects.filter(creator=account).order_by('-updated_date') + collections = populated_problems(collections) + serialize = CollectionPopulateCollectionProblemsPopulateProblemSerializer(collections,many=True) + + manageableCollections = Collection.objects.filter(collectiongrouppermission__permission_manage_collections=True,collectiongrouppermission__group__in=GroupMember.objects.filter(account=account).values_list("group",flat=True)).order_by('-updated_date') + manageableCollections = populated_problems(manageableCollections) + manageableSerialize = CollectionPopulateCollectionProblemsPopulateProblemSerializer(manageableCollections,many=True) return Response({ - 'collections': populated_collections + 'collections': serialize.data, + 'manageable_collections': manageableSerialize.data },status=status.HTTP_200_OK) \ No newline at end of file diff --git a/api/controllers/collection/get_collection.py b/api/controllers/collection/get_collection.py index 1814b0c..8e87013 100644 --- a/api/controllers/collection/get_collection.py +++ b/api/controllers/collection/get_collection.py @@ -8,21 +8,16 @@ from django.forms.models import model_to_dict from ...serializers import * -def get_collection(collection_id:int): +def get_collection(collection:Collection): - collection = Collection.objects.get(collection_id=collection_id) - # problems = Problem.objects.filter(collectionproblem__collection_id=collection_id) - collectionProblems = CollectionProblem.objects.filter(collection=collection).order_by('order') + collection.problems = CollectionProblem.objects.filter(collection=collection).order_by('order') + collection.group_permissions = CollectionGroupPermission.objects.filter(collection=collection) + + for cp in collection.problems: + cp.problem.testcases = Testcase.objects.filter(problem=cp.problem,deprecated=False) + cp.problem.group_permissions = ProblemGroupPermission.objects.filter(problem=cp.problem) + + serializer = CollectionPopulateCollectionProblemsPopulateProblemPopulateAccountAndTestcasesAndProblemGroupPermissionsPopulateGroupAndCollectionGroupPermissionsPopulateGroupSerializer(collection) - collection_ser = CollectionSerializer(collection) - collectionProblems_ser = CollectionProblemPopulateProblemSecureSerializer(collectionProblems,many=True) - # populated_problems = [] - # for col_prob in collectionProblems: - # col_prob_serialize = CollectionProblemPopulateProblemSecureSerializer(col_prob) - # # prob_serialize = ProblemSerializer(col_prob.problem) - # populated_problems.append(col_prob_serialize.data) - return Response({ - **collection_ser.data, - 'problems': collectionProblems_ser.data - } ,status=status.HTTP_200_OK) \ No newline at end of file + return Response(serializer.data ,status=status.HTTP_200_OK) \ No newline at end of file diff --git a/api/controllers/collection/update_collection.py b/api/controllers/collection/update_collection.py index 5abdc62..ae33e25 100644 --- a/api/controllers/collection/update_collection.py +++ b/api/controllers/collection/update_collection.py @@ -8,8 +8,7 @@ from django.forms.models import model_to_dict from ...serializers import * -def update_collection(collection_id:int,request): - collection = Collection.objects.get(collection_id=collection_id) +def update_collection(collection:Collection,request): collection.name = request.data.get('name',collection.name) collection.description = request.data.get('description',collection.description) diff --git a/api/controllers/collection/update_group_permissions_collection.py b/api/controllers/collection/update_group_permissions_collection.py new file mode 100644 index 0000000..58577ab --- /dev/null +++ b/api/controllers/collection/update_group_permissions_collection.py @@ -0,0 +1,32 @@ +from api.utility import passwordEncryption +from rest_framework.response import Response +from rest_framework.decorators import api_view +from api.sandbox.grader import PythonGrader +from ...constant import GET,POST,PUT,DELETE +from ...models import * +from rest_framework import status +from django.forms.models import model_to_dict +from ...serializers import * + +def update_group_permissions_collection(collection:Collection,request): + + CollectionGroupPermission.objects.filter(collection=collection).delete() + + print(request.data['groups']) + + collection_group_permissions = [] + for collection_request in request.data['groups']: + group = Group.objects.get(group_id=collection_request['group_id']) + collection_group_permissions.append( + CollectionGroupPermission( + collection=collection, + group=group, + **collection_request + )) + + CollectionGroupPermission.objects.bulk_create(collection_group_permissions) + + collection.group_permissions = collection_group_permissions + serialize = CollectionPopulateCollectionGroupPermissionsPopulateGroupSerializer(collection) + + return Response(serialize.data,status=status.HTTP_202_ACCEPTED) \ No newline at end of file diff --git a/api/controllers/group/create_group.py b/api/controllers/group/create_group.py index 4211b3a..d4e734d 100644 --- a/api/controllers/group/create_group.py +++ b/api/controllers/group/create_group.py @@ -9,6 +9,8 @@ from ...serializers import * def create_group(account:Account,request): + + # print(request.headers["Authorization"]) serialize = GroupSerializer(data={ 'creator':account.account_id, diff --git a/api/controllers/group/get_all_groups_by_account.py b/api/controllers/group/get_all_groups_by_account.py index c3fee84..b9b1f05 100644 --- a/api/controllers/group/get_all_groups_by_account.py +++ b/api/controllers/group/get_all_groups_by_account.py @@ -10,6 +10,11 @@ def get_all_groups_by_account(account:Account,request): + print("GET ALL") + + # Get request headers + headers = request.headers + groups = Group.objects.filter(creator=account).order_by('-updated_date') populate_members = request.GET.get('populate_members',False) diff --git a/api/controllers/problem/create_problem.py b/api/controllers/problem/create_problem.py index 3998d2e..efcf4f6 100644 --- a/api/controllers/problem/create_problem.py +++ b/api/controllers/problem/create_problem.py @@ -8,7 +8,7 @@ from django.forms.models import model_to_dict from ...serializers import * -def create_problem(account_id:int,request): +def create_problem(account_id:str,request): account = Account.objects.get(account_id=account_id) running_result = PythonGrader(request.data['solution'],request.data['testcases'],1,1.5).generate_output() @@ -22,7 +22,8 @@ def create_problem(account_id:int,request): title = request.data['title'], description = request.data['description'], solution = request.data['solution'], - time_limit = request.data['time_limit'] + time_limit = request.data['time_limit'], + allowed_languages = request.data['allowed_languages'], ) problem.save() diff --git a/api/controllers/problem/delete_problem.py b/api/controllers/problem/delete_problem.py index 4140824..fe057f1 100644 --- a/api/controllers/problem/delete_problem.py +++ b/api/controllers/problem/delete_problem.py @@ -8,11 +8,11 @@ from django.forms.models import model_to_dict from ...serializers import * -def delete_problem(problem_id:int): - try: - problem = Problem.objects.get(problem_id=problem_id) - except Problem.DoesNotExist: - return Response({'detail': "Problem doesn't exist!"},status=status.HTTP_404_NOT_FOUND) +def delete_problem(problem:Problem): + # try: + # problem = Problem.objects.get(problem_id=problem_id) + # except Problem.DoesNotExist: + # return Response({'detail': "Problem doesn't exist!"},status=status.HTTP_404_NOT_FOUND) testcases = Testcase.objects.filter(problem_id=problem_id) problem.delete() diff --git a/api/controllers/problem/get_all_problem_with_best_submission.py b/api/controllers/problem/get_all_problem_with_best_submission.py index f2d6741..679fac0 100644 --- a/api/controllers/problem/get_all_problem_with_best_submission.py +++ b/api/controllers/problem/get_all_problem_with_best_submission.py @@ -8,7 +8,7 @@ from django.forms.models import model_to_dict from ...serializers import * -def get_all_problem_with_best_submission(account_id:int): +def get_all_problem_with_best_submission(account_id:str): problems = Problem.objects.all().order_by('-updated_date') diff --git a/api/controllers/problem/get_all_problems.py b/api/controllers/problem/get_all_problems.py index 58a0a98..2737a20 100644 --- a/api/controllers/problem/get_all_problems.py +++ b/api/controllers/problem/get_all_problems.py @@ -13,13 +13,13 @@ def get_all_problems(request): get_private = int(request.query_params.get("private",0)) get_deactive = int(request.query_params.get("deactive",0)) - account_id = int(request.query_params.get("account_id",0)) + account_id = str(request.query_params.get("account_id","")) if not get_private: problem = problem.filter(is_private=False) if not get_deactive: problem = problem.filter(is_active=True) - if account_id != 0: + if account_id != "": problem = problem.filter(creator_id=account_id) problem = problem.order_by('-problem_id') diff --git a/api/controllers/problem/get_all_problems_by_account.py b/api/controllers/problem/get_all_problems_by_account.py index bae9918..d6210d6 100644 --- a/api/controllers/problem/get_all_problems_by_account.py +++ b/api/controllers/problem/get_all_problems_by_account.py @@ -8,11 +8,17 @@ from django.forms.models import model_to_dict from ...serializers import * -def get_all_problems_by_account(account_id:int): - problems = Problem.objects.filter(creator=account_id).order_by('-updated_date') +def get_all_problems_by_account(account:Account): + personalProblems = Problem.objects.filter(creator=account).order_by('-updated_date') + for problem in personalProblems: + problem.testcases = Testcase.objects.filter(problem=problem,deprecated=False) - for problem in problems: + manageableProblems = Problem.objects.filter(problemgrouppermission__permission_manage_problems=True,problemgrouppermission__group__in=GroupMember.objects.filter(account=account).values_list("group",flat=True)).order_by('-updated_date') + for problem in manageableProblems: problem.testcases = Testcase.objects.filter(problem=problem,deprecated=False) - problem_ser = ProblemPopulateTestcaseSerializer(problems,many=True) - return Response({"problems":problem_ser.data},status=status.HTTP_200_OK) \ No newline at end of file + # problem_ser = ProblemPopulateTestcaseSerializer(problems,many=True) + personalSerialize = ProblemPopulateTestcaseSerializer(personalProblems,many=True) + manageableSerialize = ProblemPopulateTestcaseSerializer(manageableProblems,many=True) + + return Response({"problems":personalSerialize.data,"manageable_problems":manageableSerialize.data},status=status.HTTP_200_OK) \ No newline at end of file diff --git a/api/controllers/problem/get_problem.py b/api/controllers/problem/get_problem.py index 56f78f8..0f30ab8 100644 --- a/api/controllers/problem/get_problem.py +++ b/api/controllers/problem/get_problem.py @@ -8,13 +8,20 @@ from django.forms.models import model_to_dict from ...serializers import * -def get_problem(problem_id:int): - try: - problem = Problem.objects.get(problem_id=problem_id) - except Problem.DoesNotExist: - return Response({'detail': "Problem doesn't exist!"},status=status.HTTP_404_NOT_FOUND) - testcases = Testcase.objects.filter(problem_id=problem_id,deprecated=False) - problem_serialize = ProblemPopulateAccountSerializer(problem) - testcases_serialize = TestcaseSerializer(testcases,many=True) - return Response({**problem_serialize.data,'testcases': testcases_serialize.data},status=status.HTTP_200_OK) +def get_problem(problem:Problem): + # try: + # problem = Problem.objects.get(problem_id=problem_id) + # except Problem.DoesNotExist: + # return Response({'detail': "Problem doesn't exist!"},status=status.HTTP_404_NOT_FOUND) + # testcases = Testcase.objects.filter(problem_id=problem_id,deprecated=False) + # problem_serialize = ProblemPopulateAccountSerializer(problem) + # testcases_serialize = TestcaseSerializer(testcases,many=True) + + problem.testcases = Testcase.objects.filter(problem=problem,deprecated=False) + problem.group_permissions = ProblemGroupPermission.objects.filter(problem=problem) + + serialize = ProblemPopulateAccountAndTestcasesAndProblemGroupPermissionsPopulateGroupSerializer(problem) + + + return Response(serialize.data,status=status.HTTP_200_OK) \ No newline at end of file diff --git a/api/controllers/problem/get_problem_in_topic_with_best_submission.py b/api/controllers/problem/get_problem_in_topic_with_best_submission.py index 0be0585..d708d21 100644 --- a/api/controllers/problem/get_problem_in_topic_with_best_submission.py +++ b/api/controllers/problem/get_problem_in_topic_with_best_submission.py @@ -8,7 +8,7 @@ from django.forms.models import model_to_dict from ...serializers import * -def get_problem_in_topic_with_best_submission(account_id:int,topic_id:int,problem:int): +def get_problem_in_topic_with_best_submission(account_id:str,topic_id:str,problem:int): account = Account.objects.get(account_id=account_id) problem = Problem.objects.get(problem_id=problem) diff --git a/api/controllers/problem/get_problem_public.py b/api/controllers/problem/get_problem_public.py new file mode 100644 index 0000000..8dd5e6a --- /dev/null +++ b/api/controllers/problem/get_problem_public.py @@ -0,0 +1,14 @@ +from api.utility import passwordEncryption +from rest_framework.response import Response +from rest_framework.decorators import api_view +from api.sandbox.grader import PythonGrader +from ...constant import GET,POST,PUT,DELETE +from ...models import * +from rest_framework import status +from django.forms.models import model_to_dict +from ...serializers import * + +def get_problem_public(problem:Problem): + serialize = ProblemPopulateAccountSecureSerializer(problem) + return Response(serialize.data,status=status.HTTP_200_OK) + \ No newline at end of file diff --git a/api/controllers/problem/update_group_permission_to_problem.py b/api/controllers/problem/update_group_permission_to_problem.py new file mode 100644 index 0000000..7002fda --- /dev/null +++ b/api/controllers/problem/update_group_permission_to_problem.py @@ -0,0 +1,30 @@ +from api.utility import passwordEncryption +from rest_framework.response import Response +from rest_framework.decorators import api_view +from api.sandbox.grader import PythonGrader +from ...constant import GET,POST,PUT,DELETE +from ...models import * +from rest_framework import status +from django.forms.models import model_to_dict +from ...serializers import * + +def update_group_permission_to_problem(problem:Problem,request): + ProblemGroupPermission.objects.filter(problem=problem).delete() + + problem_group_permissions = [] + for group_request in request.data['groups']: + group = Group.objects.get(group_id=group_request['group_id']) + problem_group_permissions.append( + ProblemGroupPermission( + problem=problem, + group=group, + **group_request + )) + + ProblemGroupPermission.objects.bulk_create(problem_group_permissions) + + problem.group_permissions = problem_group_permissions + problem.testcases = Testcase.objects.filter(problem=problem) + + serialize = ProblemPopulateAccountAndTestcasesAndProblemGroupPermissionsPopulateGroupSerializer(problem) + return Response(serialize.data,status=status.HTTP_202_ACCEPTED) \ No newline at end of file diff --git a/api/controllers/problem/update_problem.py b/api/controllers/problem/update_problem.py index 3380add..cd4a1a5 100644 --- a/api/controllers/problem/update_problem.py +++ b/api/controllers/problem/update_problem.py @@ -9,12 +9,12 @@ from ...serializers import * from django.utils import timezone -def update_problem(problem_id:int,request): - try: - problem = Problem.objects.get(problem_id=problem_id) - except Problem.DoesNotExist: - return Response({'detail': "Problem doesn't exist!"},status=status.HTTP_404_NOT_FOUND) - testcases = Testcase.objects.filter(problem_id=problem_id,deprecated=False) +def update_problem(problem:Problem,request): + # try: + # problem = Problem.objects.get(problem_id=problem_id) + # except Problem.DoesNotExist: + # return Response({'detail': "Problem doesn't exist!"},status=status.HTTP_404_NOT_FOUND) + testcases = Testcase.objects.filter(problem=problem,deprecated=False) problem.title = request.data.get("title",problem.title) problem.language = request.data.get("language",problem.language) @@ -22,6 +22,7 @@ def update_problem(problem_id:int,request): problem.solution = request.data.get("solution",problem.solution) problem.time_limit = request.data.get("time_limit",problem.time_limit) problem.is_private = request.data.get("is_private",problem.is_private) + problem.allowed_languages = request.data.get("allowed_languages",problem.allowed_languages) problem.updated_date = timezone.now() @@ -30,29 +31,27 @@ def update_problem(problem_id:int,request): # if not running_result.runnable: # return Response({'detail': 'Error during editing. Your code may has an error/timeout!'},status=status.HTTP_406_NOT_ACCEPTABLE) - for testcase in testcases: testcase.deprecated = True testcase.save() - testcase_result = [] for unit in running_result.data: - testcase = Testcase( + testcase2 = Testcase( problem = problem, input = unit.input, output = unit.output, runtime_status = unit.runtime_status ) - testcase.save() - testcase_result.append(testcase) + testcase2.save() + testcase_result.append(testcase2) problem.save() - problem_serialize = ProblemSerializer(problem) testcases_serialize = TestcaseSerializer(testcase_result,many=True) return Response({**problem_serialize.data,'testcases': testcases_serialize.data},status=status.HTTP_201_CREATED) - elif 'solution' in request.data: + if 'solution' in request.data: + testcases = Testcase.objects.filter(problem=problem,deprecated=False) program_input = [i.input for i in testcases] running_result = PythonGrader(problem.solution,program_input,1,1.5).generate_output() diff --git a/api/controllers/submission/get_submission_by_quries.py b/api/controllers/submission/get_submission_by_quries.py index 216233e..7fb5401 100644 --- a/api/controllers/submission/get_submission_by_quries.py +++ b/api/controllers/submission/get_submission_by_quries.py @@ -12,20 +12,20 @@ def get_submission_by_quries(request): submissions = Submission.objects.all() # Query params - problem_id = int(request.query_params.get("problem_id", 0)) - account_id = int(request.query_params.get("account_id", 0)) - topic_id = int(request.query_params.get("topic_id", 0)) + problem_id = str(request.query_params.get("problem_id", "")) + account_id = str(request.query_params.get("account_id", "")) + topic_id = str(request.query_params.get("topic_id", "")) passed = int(request.query_params.get("passed", -1)) sort_score = int(request.query_params.get("sort_score", 0)) sort_date = int(request.query_params.get("sort_date", 0)) start = int(request.query_params.get("start", -1)) end = int(request.query_params.get("end", -1)) - if problem_id != 0: + if problem_id != "": submissions = submissions.filter(problem_id=problem_id) - if account_id != 0: + if account_id != "": submissions = submissions.filter(account_id=account_id) - if topic_id != 0: + if topic_id != "": submissions = submissions.filter(problem__topic_id=topic_id) if passed == 0: diff --git a/api/controllers/submission/get_submissions_by_account_problem.py b/api/controllers/submission/get_submissions_by_account_problem.py index 5c5ff9c..7c98e32 100644 --- a/api/controllers/submission/get_submissions_by_account_problem.py +++ b/api/controllers/submission/get_submissions_by_account_problem.py @@ -8,15 +8,15 @@ from django.forms.models import model_to_dict from ...serializers import * -def get_submissions_by_account_problem(account_id:int,problem_id:int): +def get_submissions_by_account_problem(account_id:str,problem_id:str): submissions = Submission.objects.filter(account=account_id,problem=problem_id) if submissions.count() == 0: return Response({"best_submission": None,"submissions": []},status=status.HTTP_204_NO_CONTENT) - submissions = submissions.order_by('-submission_id') + submissions = submissions.order_by('-date') - best_submission_id = submissions.order_by('-passed_ratio','-submission_id').first().submission_id + best_submission_id = submissions.order_by('-passed_ratio','-date').first().submission_id best_submission = None result = [] diff --git a/api/controllers/submission/get_submissions_by_account_problem_in_topic.py b/api/controllers/submission/get_submissions_by_account_problem_in_topic.py index 7e7b91f..5c96f49 100644 --- a/api/controllers/submission/get_submissions_by_account_problem_in_topic.py +++ b/api/controllers/submission/get_submissions_by_account_problem_in_topic.py @@ -8,13 +8,13 @@ from django.forms.models import model_to_dict from ...serializers import * -def get_submissions_by_account_problem_in_topic(account_id:int,problem_id:int,topic_id:int): +def get_submissions_by_account_problem_in_topic(account_id:str,problem_id:str,topic_id:str): submissions = Submission.objects.filter(account=account_id,problem=problem_id,topic_id=topic_id) if submissions.count() == 0: return Response({"best_submission": None,"submissions": []},status=status.HTTP_204_NO_CONTENT) - submissions = submissions.order_by('-submission_id') + submissions = submissions.order_by('-date') result = [] diff --git a/api/controllers/submission/submit_problem.py b/api/controllers/submission/submit_problem.py index 1fa1b8d..9d1e9be 100644 --- a/api/controllers/submission/submit_problem.py +++ b/api/controllers/submission/submit_problem.py @@ -19,7 +19,7 @@ def avaliableQueue(): return i return -1 -def submit_problem_function(account_id:int,problem_id:int,topic_id:int,request): +def submit_problem_function(account_id:str,problem_id:str,topic_id:str,request): global QUEUE problem = Problem.objects.get(problem_id=problem_id) testcases = Testcase.objects.filter(problem=problem,deprecated=False) @@ -101,5 +101,5 @@ def submit_problem_function(account_id:int,problem_id:int,topic_id:int,request): return Response(testser.data,status=status.HTTP_201_CREATED) -def submit_problem(account_id:int,problem_id:int,request): +def submit_problem(account_id:str,problem_id:str,request): return submit_problem_function(account_id,problem_id,None,request) \ No newline at end of file diff --git a/api/controllers/submission/submit_problem_on_topic.py b/api/controllers/submission/submit_problem_on_topic.py index b2940e8..5d7d93f 100644 --- a/api/controllers/submission/submit_problem_on_topic.py +++ b/api/controllers/submission/submit_problem_on_topic.py @@ -11,5 +11,5 @@ from time import sleep from .submit_problem import * -def submit_problem_on_topic(account_id:int,problem_id:int,topic_id:int,request): +def submit_problem_on_topic(account_id:str,problem_id:str,topic_id:str,request): return submit_problem_function(account_id,problem_id,topic_id,request) \ No newline at end of file diff --git a/api/controllers/topic/add_collections_to_topic.py b/api/controllers/topic/add_collections_to_topic.py index 14c85c7..936e05f 100644 --- a/api/controllers/topic/add_collections_to_topic.py +++ b/api/controllers/topic/add_collections_to_topic.py @@ -8,7 +8,7 @@ from django.forms.models import model_to_dict from ...serializers import * -def add_collections_to_topic(topic_id:int,request): +def add_collections_to_topic(topic_id:str,request): topic = Topic.objects.get(topic_id=topic_id) populated_collections = [] diff --git a/api/controllers/topic/create_topic.py b/api/controllers/topic/create_topic.py index aa9d678..12557ce 100644 --- a/api/controllers/topic/create_topic.py +++ b/api/controllers/topic/create_topic.py @@ -8,7 +8,7 @@ from django.forms.models import model_to_dict from ...serializers import * -def create_topic(account_id:int,request): +def create_topic(account_id:str,request): request.data._mutable=True request.data['creator'] = account_id serializer = TopicSerializer(data=request.data) diff --git a/api/controllers/topic/delete_topic.py b/api/controllers/topic/delete_topic.py index 67cb6c2..622c052 100644 --- a/api/controllers/topic/delete_topic.py +++ b/api/controllers/topic/delete_topic.py @@ -8,7 +8,6 @@ from django.forms.models import model_to_dict from ...serializers import * -def delete_topic(topic_id:int): - topic = Topic.objects.get(topic_id=topic_id) +def delete_topic(topic:Topic): topic.delete() return Response(status=status.HTTP_204_NO_CONTENT) diff --git a/api/controllers/topic/get_all_accessed_topics_by_account.py b/api/controllers/topic/get_all_accessed_topics_by_account.py new file mode 100644 index 0000000..68a92fc --- /dev/null +++ b/api/controllers/topic/get_all_accessed_topics_by_account.py @@ -0,0 +1,19 @@ +from api.utility import passwordEncryption +from rest_framework.response import Response +from rest_framework.decorators import api_view +from api.sandbox.grader import PythonGrader +from ...constant import GET,POST,PUT,DELETE +from ...models import * +from rest_framework import status +from django.forms.models import model_to_dict +from ...serializers import * +from django.db.models import Q + +def get_all_accessed_topics_by_account(account:Account): + groups = [gm.group for gm in GroupMember.objects.filter(account=account)] + accessedTopics = TopicGroupPermission.objects.filter(Q(group__in=groups) & (Q(permission_view_topics=True) | Q(permission_manage_topics=True))).distinct() + topics = [at.topic for at in accessedTopics] + + serialize = TopicSerializer(topics,many=True) + + return Response({'topics':serialize.data},status=status.HTTP_200_OK) diff --git a/api/controllers/topic/get_all_topics_by_account.py b/api/controllers/topic/get_all_topics_by_account.py index d9dd97b..417424f 100644 --- a/api/controllers/topic/get_all_topics_by_account.py +++ b/api/controllers/topic/get_all_topics_by_account.py @@ -8,17 +8,25 @@ from django.forms.models import model_to_dict from ...serializers import * -def get_all_topics_by_account(account_id:int,request): - topics = Topic.objects.filter(creator_id=account_id).order_by('-updated_date') +def populated_collections(topics:Topic): topicCollections = TopicCollection.objects.filter(topic__in=topics) - populated_topics = [] for topic in topics: topic.collections = topicCollections.filter(topic=topic) populated_topics.append(topic) + return populated_topics + +def get_all_topics_by_account(account:Account,request): + personalTopics = Topic.objects.filter(creator=account).order_by('-updated_date') + populatedPersonalTopics = populated_collections(personalTopics) + personalSerialize = TopicPopulateTopicCollectionPopulateCollectionSerializer(populatedPersonalTopics,many=True) - serialize = TopicPopulateTopicCollectionPopulateCollectionSerializer(populated_topics,many=True) + # print(GroupMember.objects.all().values_list("group",flat=True)) + manageableTopics = Topic.objects.filter(topicgrouppermission__permission_manage_topics=True,topicgrouppermission__group__in=GroupMember.objects.filter(account=account).values_list("group",flat=True)).order_by('-updated_date') + populatedmanageableTopics = populated_collections(manageableTopics) + manageableSerialize = TopicPopulateTopicCollectionPopulateCollectionSerializer(populatedmanageableTopics,many=True) return Response({ - 'topics': serialize.data + 'topics': personalSerialize.data, + 'manageable_topics': manageableSerialize.data },status=status.HTTP_200_OK) \ No newline at end of file diff --git a/api/controllers/topic/get_topic.py b/api/controllers/topic/get_topic.py index 89f37b4..53f6e86 100644 --- a/api/controllers/topic/get_topic.py +++ b/api/controllers/topic/get_topic.py @@ -8,10 +8,16 @@ from django.forms.models import model_to_dict from ...serializers import * -def get_topic(topic_id:int): - topic = Topic.objects.get(topic_id=topic_id) - topic.collections = TopicCollection.objects.filter(topic_id=topic_id) - serialize = TopicPopulateTopicCollectionPopulateCollectionSerializer(topic) +def get_topic(topic:Topic): + topic.group_permissions = TopicGroupPermission.objects.filter(topic=topic) + + topic.collections = TopicCollection.objects.filter(topic=topic).order_by('order') + + for tp in topic.collections: + tp.collection.problems = CollectionProblem.objects.filter(collection=tp.collection) + tp.collection.group_permissions = CollectionGroupPermission.objects.filter(collection=tp.collection) + + serialize = TopicPopulateTopicCollectionPopulateCollectionPopulateCollectionProblemsPopulateProblemAndCollectionGroupPermissionsPopulateGroupAndTopicGroupPermissionPopulateGroupSerializer(topic) return Response(serialize.data,status=status.HTTP_200_OK) diff --git a/api/controllers/topic/get_topic_public.py b/api/controllers/topic/get_topic_public.py index 177f894..36476b2 100644 --- a/api/controllers/topic/get_topic_public.py +++ b/api/controllers/topic/get_topic_public.py @@ -7,18 +7,42 @@ from rest_framework import status from django.forms.models import model_to_dict from ...serializers import * +from django.db.models import Q -def get_topic_public(topic_id:int,request): +def get_topic_public(topic_id:str,request): account_id = request.query_params.get('account_id',None) topic = Topic.objects.get(topic_id=topic_id) account = Account.objects.get(account_id=account_id) - topicCollections = TopicCollection.objects.filter(topic=topic) + + + topicCollections = TopicCollection.objects.filter( + topic=topic, + collection__in= + CollectionGroupPermission.objects.filter( + Q(group__in=GroupMember.objects.filter(account=account).values_list("group",flat=True)) & + ( + Q(permission_view_collections=True) | Q(permission_manage_collections=True) + ) + ).values_list("collection",flat=True)) for tp in topicCollections: # tp.collection.problems = CollectionProblem.objects.filter(collection=tp.collection) - collectionProblems = CollectionProblem.objects.filter(collection=tp.collection) + + # viewPermission = CollectionGroupPermission.objects.filter(collection=tp.collection,group__in=GroupMember.objects.filter(account=account).values_list("group",flat=True),permission_view_collections=True) + # print(len(viewPermission)) + # if len(viewPermission) == 0: + # tp.collection.problems = [] + # continue + collectionProblems = CollectionProblem.objects.filter( + collection=tp.collection, + problem__in=ProblemGroupPermission.objects.filter( + Q(group__in=GroupMember.objects.filter(account=account).values_list("group",flat=True)) & + (Q(permission_view_problems=True) | + Q(permission_manage_problems=True)) + ).values_list("problem",flat=True)) + for cp in collectionProblems: try: best_submission = BestSubmission.objects.get(problem=cp.problem,account=account,topic=topic) diff --git a/api/controllers/topic/remove_collections_from_topic.py b/api/controllers/topic/remove_collections_from_topic.py index 1275956..74ac26e 100644 --- a/api/controllers/topic/remove_collections_from_topic.py +++ b/api/controllers/topic/remove_collections_from_topic.py @@ -8,7 +8,7 @@ from django.forms.models import model_to_dict from ...serializers import * -def remove_collections_from_topic(topic_id:int,request): +def remove_collections_from_topic(topic_id:str,request): TopicCollection.objects.filter(topic_id=topic_id,collection_id__in=request.data['collection_ids']).delete() # collections = Collection.objects.filter(collection_id__in=request.data['collection_ids']) # problems = Problem.objects.filter(problem_id__in=request.data['problems_id']) diff --git a/api/controllers/topic/update_groups_permission_to_topic.py b/api/controllers/topic/update_groups_permission_to_topic.py new file mode 100644 index 0000000..608791b --- /dev/null +++ b/api/controllers/topic/update_groups_permission_to_topic.py @@ -0,0 +1,31 @@ +from api.utility import passwordEncryption +from rest_framework.response import Response +from rest_framework.decorators import api_view +from api.sandbox.grader import PythonGrader +from ...constant import GET,POST,PUT,DELETE +from ...models import * +from rest_framework import status +from django.forms.models import model_to_dict +from ...serializers import * + +def update_groups_permission_to_topic(topic:Topic,request): + + TopicGroupPermission.objects.filter(topic=topic).delete() + + topic_group_permissions = [] + for group_request in request.data['groups']: + print(group_request) + group = Group.objects.get(group_id=group_request['group_id']) + topic_group_permissions.append( + TopicGroupPermission( + topic=topic, + group=group, + **group_request + )) + + TopicGroupPermission.objects.bulk_create(topic_group_permissions) + + topic.group_permissions = topic_group_permissions + serialize = TopicPopulateTopicGroupPermissionsSerializer(topic) + + return Response(serialize.data,status=status.HTTP_202_ACCEPTED) \ No newline at end of file diff --git a/api/controllers/topic/update_topic.py b/api/controllers/topic/update_topic.py index 3a76351..1d95c9f 100644 --- a/api/controllers/topic/update_topic.py +++ b/api/controllers/topic/update_topic.py @@ -8,9 +8,7 @@ from django.forms.models import model_to_dict from ...serializers import * -def update_topic(topic_id:int,request): - topic = Topic.objects.get(topic_id=topic_id) - +def update_topic(topic:Topic,request): topic_ser = TopicSerializer(topic,data=request.data,partial=True) if topic_ser.is_valid(): topic_ser.save() diff --git a/api/migrations/0029_remove_account_is_admin_remove_collection_owner_and_more.py b/api/migrations/0029_remove_account_is_admin_remove_collection_owner_and_more.py index 2638044..94d36f3 100644 --- a/api/migrations/0029_remove_account_is_admin_remove_collection_owner_and_more.py +++ b/api/migrations/0029_remove_account_is_admin_remove_collection_owner_and_more.py @@ -30,19 +30,19 @@ class Migration(migrations.Migration): migrations.AddField( model_name='collection', name='creator', - field=models.ForeignKey(db_column='creator_id', default=1, on_delete=django.db.models.deletion.CASCADE, to='api.account'), + field=models.ForeignKey(db_column='creator_id', default=4, on_delete=django.db.models.deletion.CASCADE, to='api.account'), preserve_default=False, ), migrations.AddField( model_name='problem', name='creator', - field=models.ForeignKey(db_column='creator_id', default=1, on_delete=django.db.models.deletion.CASCADE, to='api.account'), + field=models.ForeignKey(db_column='creator_id', default=4, on_delete=django.db.models.deletion.CASCADE, to='api.account'), preserve_default=False, ), migrations.AddField( model_name='topic', name='creator', - field=models.ForeignKey(db_column='creator_id', default=1, on_delete=django.db.models.deletion.CASCADE, to='api.account'), + field=models.ForeignKey(db_column='creator_id', default=4, on_delete=django.db.models.deletion.CASCADE, to='api.account'), preserve_default=False, ), ] diff --git a/api/migrations/0045_collection_sharing_and_more.py b/api/migrations/0045_collection_sharing_and_more.py new file mode 100644 index 0000000..ccd65f3 --- /dev/null +++ b/api/migrations/0045_collection_sharing_and_more.py @@ -0,0 +1,95 @@ +# Generated by Django 4.1.2 on 2023-12-30 13:02 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0044_remove_groupmember_updated_date_group_color_and_more'), + ] + + operations = [ + migrations.AddField( + model_name='collection', + name='sharing', + field=models.BooleanField(blank=True, default=False), + ), + migrations.AddField( + model_name='group', + name='permission_manage_collections', + field=models.BooleanField(blank=True, default=False), + ), + migrations.AddField( + model_name='group', + name='permission_manage_problems', + field=models.BooleanField(blank=True, default=False), + ), + migrations.AddField( + model_name='group', + name='permission_manage_topics', + field=models.BooleanField(blank=True, default=False), + ), + migrations.AddField( + model_name='group', + name='permission_view_collections', + field=models.BooleanField(blank=True, default=False), + ), + migrations.AddField( + model_name='group', + name='permission_view_problems', + field=models.BooleanField(blank=True, default=False), + ), + migrations.AddField( + model_name='group', + name='permission_view_topics', + field=models.BooleanField(blank=True, default=False), + ), + migrations.AddField( + model_name='group', + name='permission_view_topics_log', + field=models.BooleanField(blank=True, default=False), + ), + migrations.AddField( + model_name='problem', + name='sharing', + field=models.BooleanField(blank=True, default=False), + ), + migrations.AddField( + model_name='topic', + name='sharing', + field=models.BooleanField(blank=True, default=False), + ), + migrations.CreateModel( + name='TopicGroupPermission', + fields=[ + ('topic_group_permission_id', models.AutoField(primary_key=True, serialize=False)), + ('permission_manage_topics', models.BooleanField(blank=True, default=False)), + ('permission_view_topics', models.BooleanField(blank=True, default=False)), + ('permission_view_topics_log', models.BooleanField(blank=True, default=False)), + ('group', models.ForeignKey(db_column='group_id', on_delete=django.db.models.deletion.CASCADE, to='api.group')), + ('topic', models.ForeignKey(db_column='topic_id', on_delete=django.db.models.deletion.CASCADE, to='api.topic')), + ], + ), + migrations.CreateModel( + name='ProblemGroupPermission', + fields=[ + ('problem_group_permission_id', models.AutoField(primary_key=True, serialize=False)), + ('permission_manage_problems', models.BooleanField(blank=True, default=False)), + ('permission_view_problems', models.BooleanField(blank=True, default=False)), + ('group', models.ForeignKey(db_column='group_id', on_delete=django.db.models.deletion.CASCADE, to='api.group')), + ('problem', models.ForeignKey(db_column='problem_id', on_delete=django.db.models.deletion.CASCADE, to='api.problem')), + ], + ), + migrations.CreateModel( + name='CollectionGroupPermission', + fields=[ + ('collection_group_permission_id', models.AutoField(primary_key=True, serialize=False)), + ('permission_manage_collections', models.BooleanField(blank=True, default=False)), + ('permission_view_collections', models.BooleanField(blank=True, default=False)), + ('collection', models.ForeignKey(db_column='collection_id', on_delete=django.db.models.deletion.CASCADE, to='api.collection')), + ('group', models.ForeignKey(db_column='group_id', on_delete=django.db.models.deletion.CASCADE, to='api.group')), + ], + ), + ] diff --git a/api/migrations/0046_alter_account_account_id_and_more.py b/api/migrations/0046_alter_account_account_id_and_more.py new file mode 100644 index 0000000..d3ba1d2 --- /dev/null +++ b/api/migrations/0046_alter_account_account_id_and_more.py @@ -0,0 +1,78 @@ +# Generated by Django 4.1.2 on 2023-12-31 06:41 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0045_collection_sharing_and_more'), + ] + + operations = [ + migrations.AlterField( + model_name='account', + name='account_id', + field=models.IntegerField(blank=True, default=21169482018841058607917871046905516796, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='bestsubmission', + name='best_submission_id', + field=models.IntegerField(blank=True, default=149805424067452344256336652203968127971, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='collection', + name='collection_id', + field=models.IntegerField(blank=True, default=268667638844507429395683049205342878308, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='collectiongrouppermission', + name='collection_group_permission_id', + field=models.IntegerField(blank=True, default=109233292530947572577678851192443289212, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='group', + name='group_id', + field=models.IntegerField(blank=True, default=122445526515845715335481945122286027175, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='groupmember', + name='group_member_id', + field=models.IntegerField(blank=True, default=166402800061360647258671358541528594815, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='problem', + name='problem_id', + field=models.IntegerField(blank=True, default=319367364084708849680820356972231050063, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='problemgrouppermission', + name='problem_group_permission_id', + field=models.IntegerField(blank=True, default=287900638121684221482942373316315838568, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='submission', + name='submission_id', + field=models.IntegerField(blank=True, default=333913260807308465398586534607890822150, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='submissiontestcase', + name='submission_testcase_id', + field=models.IntegerField(blank=True, default=60359747276246222989657681614453318391, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='testcase', + name='testcase_id', + field=models.IntegerField(blank=True, default=67781662549803771653563714324350833289, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='topic', + name='topic_id', + field=models.IntegerField(blank=True, default=277680624917405823307023916753225961537, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='topicgrouppermission', + name='topic_group_permission_id', + field=models.IntegerField(blank=True, default=268422871249108757857865287128153578738, primary_key=True, serialize=False, unique=True), + ), + ] diff --git a/api/migrations/0047_alter_account_account_id_and_more.py b/api/migrations/0047_alter_account_account_id_and_more.py new file mode 100644 index 0000000..5d7eb2f --- /dev/null +++ b/api/migrations/0047_alter_account_account_id_and_more.py @@ -0,0 +1,78 @@ +# Generated by Django 4.1.2 on 2023-12-31 06:42 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0046_alter_account_account_id_and_more'), + ] + + operations = [ + migrations.AlterField( + model_name='account', + name='account_id', + field=models.IntegerField(blank=True, default=255025766878975110361680320380373860653, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='bestsubmission', + name='best_submission_id', + field=models.IntegerField(blank=True, default=258922610152306027347722825318799690393, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='collection', + name='collection_id', + field=models.IntegerField(blank=True, default=99617119408089154479842742330779925899, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='collectiongrouppermission', + name='collection_group_permission_id', + field=models.IntegerField(blank=True, default=81014014139093807433038406409328592680, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='group', + name='group_id', + field=models.IntegerField(blank=True, default=16555706619906256397756149457406660852, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='groupmember', + name='group_member_id', + field=models.IntegerField(blank=True, default=327764340289070410599813626795123765411, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='problem', + name='problem_id', + field=models.IntegerField(blank=True, default=158188918196591434367913126653525869241, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='problemgrouppermission', + name='problem_group_permission_id', + field=models.IntegerField(blank=True, default=253268741446266731297890328391178611475, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='submission', + name='submission_id', + field=models.IntegerField(blank=True, default=24540818810954611023886936486853063157, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='submissiontestcase', + name='submission_testcase_id', + field=models.IntegerField(blank=True, default=264396448540576706768031613977560334190, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='testcase', + name='testcase_id', + field=models.IntegerField(blank=True, default=85088409562075225550176092152049933143, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='topic', + name='topic_id', + field=models.IntegerField(blank=True, default=326588415083665992335616674836859249347, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='topicgrouppermission', + name='topic_group_permission_id', + field=models.IntegerField(blank=True, default=224958087767984655481777651457017521859, primary_key=True, serialize=False, unique=True), + ), + ] diff --git a/api/migrations/0048_alter_account_account_id_and_more.py b/api/migrations/0048_alter_account_account_id_and_more.py new file mode 100644 index 0000000..356111b --- /dev/null +++ b/api/migrations/0048_alter_account_account_id_and_more.py @@ -0,0 +1,78 @@ +# Generated by Django 4.1.2 on 2023-12-31 06:50 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0047_alter_account_account_id_and_more'), + ] + + operations = [ + migrations.AlterField( + model_name='account', + name='account_id', + field=models.CharField(blank=True, default='4256b03cc08048ef9aa3c0348b3271ce', max_length=32, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='bestsubmission', + name='best_submission_id', + field=models.CharField(blank=True, default='a6a17c8c118947ddbe19ead82fadcf13', max_length=32, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='collection', + name='collection_id', + field=models.CharField(blank=True, default='0826e62bc2eb485bac000c5a6874354e', max_length=32, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='collectiongrouppermission', + name='collection_group_permission_id', + field=models.CharField(blank=True, default='99f216a4857a47bebf46585de5ba10b4', max_length=32, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='group', + name='group_id', + field=models.CharField(blank=True, default='cc530cb9f1ee4852b1eecd54edbc1ca0', max_length=32, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='groupmember', + name='group_member_id', + field=models.CharField(blank=True, default='4a87ce6643ea4e46ba7a5949f80a9bfe', max_length=32, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='problem', + name='problem_id', + field=models.CharField(blank=True, default='b8f79eec56bd42a78a993888721d2966', max_length=32, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='problemgrouppermission', + name='problem_group_permission_id', + field=models.CharField(blank=True, default='d91f4c61dda94f2b93728feccb8441d5', max_length=32, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='submission', + name='submission_id', + field=models.CharField(blank=True, default='c494a2d002af4c439c258553ab41fd32', max_length=32, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='submissiontestcase', + name='submission_testcase_id', + field=models.CharField(blank=True, default='092f2c6cd8004cd68f8ae138b65d6c8e', max_length=32, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='testcase', + name='testcase_id', + field=models.CharField(blank=True, default='2f39f938182542d6b7d5df70a4c275a2', max_length=32, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='topic', + name='topic_id', + field=models.CharField(blank=True, default='9c5390db0ce942ac9562ec3422222b32', max_length=32, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='topicgrouppermission', + name='topic_group_permission_id', + field=models.CharField(blank=True, default='35981bc9854a45dd9456f4958332466b', max_length=32, primary_key=True, serialize=False, unique=True), + ), + ] diff --git a/api/migrations/0049_alter_account_account_id_and_more.py b/api/migrations/0049_alter_account_account_id_and_more.py new file mode 100644 index 0000000..efa9328 --- /dev/null +++ b/api/migrations/0049_alter_account_account_id_and_more.py @@ -0,0 +1,88 @@ +# Generated by Django 4.1.2 on 2023-12-31 06:51 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0048_alter_account_account_id_and_more'), + ] + + operations = [ + migrations.AlterField( + model_name='account', + name='account_id', + field=models.CharField(blank=True, default='fe492e8f6bbd4df3b7d8518d66cc4141', max_length=32, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='bestsubmission', + name='best_submission_id', + field=models.CharField(blank=True, default='fb2ee51251b144e0980aaaab7de3e2ed', max_length=32, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='collection', + name='collection_id', + field=models.CharField(blank=True, default='f70ffe44bd84425480ea4c73cc1eccf3', max_length=32, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='collectiongrouppermission', + name='collection_group_permission_id', + field=models.CharField(blank=True, default='e32d76e246844cf1834c711ac1d9f2c3', max_length=32, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='collectionproblem', + name='id', + field=models.CharField(blank=True, default='9df47ab61f73413da2667553854563a7', max_length=32, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='group', + name='group_id', + field=models.CharField(blank=True, default='eeb90b4a7ee046e1be3e26967ed815b6', max_length=32, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='groupmember', + name='group_member_id', + field=models.CharField(blank=True, default='b046b347361a4e839828ff8dc64fbf1c', max_length=32, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='problem', + name='problem_id', + field=models.CharField(blank=True, default='9926855227344794b60bf52d107012c7', max_length=32, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='problemgrouppermission', + name='problem_group_permission_id', + field=models.CharField(blank=True, default='b5eeb3a47121426e930587793bfe173f', max_length=32, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='submission', + name='submission_id', + field=models.CharField(blank=True, default='bafecc07aa194ca8a292f387c21af764', max_length=32, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='submissiontestcase', + name='submission_testcase_id', + field=models.CharField(blank=True, default='0188a3eb130c480d95504eae32e1b912', max_length=32, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='testcase', + name='testcase_id', + field=models.CharField(blank=True, default='e47da5f04bdf4b599a6d59d439508e22', max_length=32, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='topic', + name='topic_id', + field=models.CharField(blank=True, default='9d84e241dd0643479412cac53c408657', max_length=32, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='topiccollection', + name='id', + field=models.CharField(blank=True, default='823d76779a1d4455aa3437ae7157fde3', max_length=32, primary_key=True, serialize=False, unique=True), + ), + migrations.AlterField( + model_name='topicgrouppermission', + name='topic_group_permission_id', + field=models.CharField(blank=True, default='9ade2833620640cda259dbc40d3638ac', max_length=32, primary_key=True, serialize=False, unique=True), + ), + ] diff --git a/api/migrations/0050_alter_account_account_id_and_more.py b/api/migrations/0050_alter_account_account_id_and_more.py new file mode 100644 index 0000000..c3087be --- /dev/null +++ b/api/migrations/0050_alter_account_account_id_and_more.py @@ -0,0 +1,88 @@ +# Generated by Django 4.1.2 on 2023-12-31 07:27 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0049_alter_account_account_id_and_more'), + ] + + operations = [ + migrations.AlterField( + model_name='account', + name='account_id', + field=models.CharField(blank=True, default='c61cc4ec15424865814d8403791fe7de', max_length=32, primary_key=True, serialize=False), + ), + migrations.AlterField( + model_name='bestsubmission', + name='best_submission_id', + field=models.CharField(blank=True, default='5de4232945d34426a0b7dc51c1671cbc', max_length=32, primary_key=True, serialize=False), + ), + migrations.AlterField( + model_name='collection', + name='collection_id', + field=models.CharField(blank=True, default='c178f001e1e5408fa42263289589c1ae', max_length=32, primary_key=True, serialize=False), + ), + migrations.AlterField( + model_name='collectiongrouppermission', + name='collection_group_permission_id', + field=models.CharField(blank=True, default='f504bf60f7d3421389bd8f939dd49caf', max_length=32, primary_key=True, serialize=False), + ), + migrations.AlterField( + model_name='collectionproblem', + name='id', + field=models.CharField(blank=True, default='2a36719d3b224b62ad031c94da0d253d', max_length=32, primary_key=True, serialize=False), + ), + migrations.AlterField( + model_name='group', + name='group_id', + field=models.CharField(blank=True, default='497e3357fd344fe5bdd31788c211f5e7', max_length=32, primary_key=True, serialize=False), + ), + migrations.AlterField( + model_name='groupmember', + name='group_member_id', + field=models.CharField(blank=True, default='9fe857f7c4bf41aca173f07320779da4', max_length=32, primary_key=True, serialize=False), + ), + migrations.AlterField( + model_name='problem', + name='problem_id', + field=models.CharField(blank=True, default='1851825ec238474dabb80d4a03a05d2d', max_length=32, primary_key=True, serialize=False), + ), + migrations.AlterField( + model_name='problemgrouppermission', + name='problem_group_permission_id', + field=models.CharField(blank=True, default='8adfff7ea11645d88a5b97cb56979165', max_length=32, primary_key=True, serialize=False), + ), + migrations.AlterField( + model_name='submission', + name='submission_id', + field=models.CharField(blank=True, default='0ec695421ff341e086c9a37a662be3e1', max_length=32, primary_key=True, serialize=False), + ), + migrations.AlterField( + model_name='submissiontestcase', + name='submission_testcase_id', + field=models.CharField(blank=True, default='9bbd6f2b00234676b0b661e74768a830', max_length=32, primary_key=True, serialize=False), + ), + migrations.AlterField( + model_name='testcase', + name='testcase_id', + field=models.CharField(blank=True, default='81fd1d215c6e41a5b5d99608ab580c7b', max_length=32, primary_key=True, serialize=False), + ), + migrations.AlterField( + model_name='topic', + name='topic_id', + field=models.CharField(blank=True, default='950c695bdef043c0b52e79bda497423e', max_length=32, primary_key=True, serialize=False), + ), + migrations.AlterField( + model_name='topiccollection', + name='id', + field=models.CharField(blank=True, default='0ddc6a8372ef4db59adf00aa24b39429', max_length=32, primary_key=True, serialize=False), + ), + migrations.AlterField( + model_name='topicgrouppermission', + name='topic_group_permission_id', + field=models.CharField(blank=True, default='8fdc25dc2fcf40588e76e71c1bed234b', max_length=32, primary_key=True, serialize=False), + ), + ] diff --git a/api/migrations/0051_alter_account_account_id_and_more.py b/api/migrations/0051_alter_account_account_id_and_more.py new file mode 100644 index 0000000..feb04e6 --- /dev/null +++ b/api/migrations/0051_alter_account_account_id_and_more.py @@ -0,0 +1,89 @@ +# Generated by Django 4.1.2 on 2023-12-31 07:30 + +import api.models +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0050_alter_account_account_id_and_more'), + ] + + operations = [ + migrations.AlterField( + model_name='account', + name='account_id', + field=models.CharField(blank=True, default=api.models.generate_uuid4_hex, max_length=32, primary_key=True, serialize=False), + ), + migrations.AlterField( + model_name='bestsubmission', + name='best_submission_id', + field=models.CharField(blank=True, default=api.models.generate_uuid4_hex, max_length=32, primary_key=True, serialize=False), + ), + migrations.AlterField( + model_name='collection', + name='collection_id', + field=models.CharField(blank=True, default=api.models.generate_uuid4_hex, max_length=32, primary_key=True, serialize=False), + ), + migrations.AlterField( + model_name='collectiongrouppermission', + name='collection_group_permission_id', + field=models.CharField(blank=True, default=api.models.generate_uuid4_hex, max_length=32, primary_key=True, serialize=False), + ), + migrations.AlterField( + model_name='collectionproblem', + name='id', + field=models.CharField(blank=True, default=api.models.generate_uuid4_hex, max_length=32, primary_key=True, serialize=False), + ), + migrations.AlterField( + model_name='group', + name='group_id', + field=models.CharField(blank=True, default=api.models.generate_uuid4_hex, max_length=32, primary_key=True, serialize=False), + ), + migrations.AlterField( + model_name='groupmember', + name='group_member_id', + field=models.CharField(blank=True, default=api.models.generate_uuid4_hex, max_length=32, primary_key=True, serialize=False), + ), + migrations.AlterField( + model_name='problem', + name='problem_id', + field=models.CharField(blank=True, default=api.models.generate_uuid4_hex, max_length=32, primary_key=True, serialize=False), + ), + migrations.AlterField( + model_name='problemgrouppermission', + name='problem_group_permission_id', + field=models.CharField(blank=True, default=api.models.generate_uuid4_hex, max_length=32, primary_key=True, serialize=False), + ), + migrations.AlterField( + model_name='submission', + name='submission_id', + field=models.CharField(blank=True, default=api.models.generate_uuid4_hex, max_length=32, primary_key=True, serialize=False), + ), + migrations.AlterField( + model_name='submissiontestcase', + name='submission_testcase_id', + field=models.CharField(blank=True, default=api.models.generate_uuid4_hex, max_length=32, primary_key=True, serialize=False), + ), + migrations.AlterField( + model_name='testcase', + name='testcase_id', + field=models.CharField(blank=True, default=api.models.generate_uuid4_hex, max_length=32, primary_key=True, serialize=False), + ), + migrations.AlterField( + model_name='topic', + name='topic_id', + field=models.CharField(blank=True, default=api.models.generate_uuid4_hex, max_length=32, primary_key=True, serialize=False), + ), + migrations.AlterField( + model_name='topiccollection', + name='id', + field=models.CharField(blank=True, default=api.models.generate_uuid4_hex, max_length=32, primary_key=True, serialize=False), + ), + migrations.AlterField( + model_name='topicgrouppermission', + name='topic_group_permission_id', + field=models.CharField(blank=True, default=api.models.generate_uuid4_hex, max_length=32, primary_key=True, serialize=False), + ), + ] diff --git a/api/migrations/0052_problem_allowed_languages.py b/api/migrations/0052_problem_allowed_languages.py new file mode 100644 index 0000000..7f940c1 --- /dev/null +++ b/api/migrations/0052_problem_allowed_languages.py @@ -0,0 +1,18 @@ +# Generated by Django 4.1.2 on 2024-01-12 05:01 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0051_alter_account_account_id_and_more'), + ] + + operations = [ + migrations.AddField( + model_name='problem', + name='allowed_languages', + field=models.CharField(blank=True, default='', max_length=1000), + ), + ] diff --git a/api/models.py b/api/models.py index 38e08f5..e30c9a1 100644 --- a/api/models.py +++ b/api/models.py @@ -7,6 +7,10 @@ from django.utils import timezone from django.contrib.auth.base_user import AbstractBaseUser,BaseUserManager from .utility import uploadTopic +from uuid import uuid4 + +def generate_uuid4_hex(): + return uuid4().hex # Create your models here. class ProgrammingLanguage(models.TextChoices): @@ -15,7 +19,7 @@ class ProgrammingLanguage(models.TextChoices): CPP = 'cpp',_('C++') class Account(models.Model): - account_id = models.AutoField(primary_key=True) + account_id = models.CharField(primary_key=True,blank=True,default=generate_uuid4_hex,max_length=32) email = models.EmailField(max_length=50,unique=True,null=True) username = models.CharField(max_length=32,unique=True) password = models.CharField(max_length=128) @@ -26,7 +30,7 @@ class Account(models.Model): is_private = models.BooleanField(default=True) class Problem(models.Model): - problem_id = models.AutoField(primary_key=True) + problem_id = models.CharField(primary_key=True,blank=True,default=generate_uuid4_hex,max_length=32) creator = models.ForeignKey(Account,on_delete=models.CASCADE,db_column="creator_id") language = models.CharField(max_length=15) # ,choices=ProgrammingLanguage.choices,default=ProgrammingLanguage.PYTHON) title = models.CharField(max_length=50) @@ -38,10 +42,11 @@ class Problem(models.Model): submission_regex = models.CharField(max_length=1000,null=True,blank=True,default=".*") created_date = models.DateTimeField(default=timezone.now) updated_date = models.DateTimeField(default=timezone.now) - + sharing = models.BooleanField(default=False,blank=True) + allowed_languages = models.CharField(max_length=1000,blank=True,default="") class Testcase(models.Model): - testcase_id = models.AutoField(primary_key=True) + testcase_id = models.CharField(primary_key=True,blank=True,default=generate_uuid4_hex,max_length=32) problem = models.ForeignKey(Problem,on_delete=models.CASCADE,db_column="problem_id") input = models.CharField(max_length=100000) output = models.CharField(max_length=100000,null=True) @@ -49,7 +54,7 @@ class Testcase(models.Model): deprecated = models.BooleanField(default=False,blank=True) class Collection(models.Model): - collection_id = models.AutoField(primary_key=True) + collection_id = models.CharField(primary_key=True,blank=True,default=generate_uuid4_hex,max_length=32) creator = models.ForeignKey(Account,on_delete=models.CASCADE,db_column="creator_id") name = models.CharField(max_length=100) description = models.CharField(max_length=100000,null=True,blank=True,default=None) @@ -57,9 +62,11 @@ class Collection(models.Model): is_private = models.BooleanField(default=False,blank=True) created_date = models.DateTimeField(default=timezone.now) updated_date = models.DateTimeField(default=timezone.now) + sharing = models.BooleanField(default=False,blank=True) + class Topic(models.Model): - topic_id = models.AutoField(primary_key=True) + topic_id = models.CharField(primary_key=True,blank=True,default=generate_uuid4_hex,max_length=32) creator = models.ForeignKey(Account,on_delete=models.CASCADE,db_column="creator_id") name = models.CharField(max_length=100) description = models.CharField(max_length=100000,null=True,blank=True,default=None) @@ -68,13 +75,16 @@ class Topic(models.Model): is_private = models.BooleanField(default=False,blank=True) created_date = models.DateTimeField(default=timezone.now) updated_date = models.DateTimeField(default=timezone.now) + sharing = models.BooleanField(default=False,blank=True) class TopicCollection(models.Model): + id = models.CharField(primary_key=True,blank=True,default=generate_uuid4_hex,max_length=32) topic = models.ForeignKey(Topic,on_delete=models.CASCADE,db_column="topic_id") collection = models.ForeignKey(Collection,on_delete=models.CASCADE,db_column="collection_id") order = models.IntegerField(blank=True,default=0) class CollectionProblem(models.Model): + id = models.CharField(primary_key=True,blank=True,default=generate_uuid4_hex,max_length=32) collection = models.ForeignKey(Collection,on_delete=models.CASCADE,db_column="collection_id") problem = models.ForeignKey(Problem,on_delete=models.CASCADE,db_column="problem_id") order = models.IntegerField(blank=True,default=0) @@ -89,7 +99,7 @@ class TopicAccountAccess(models.Model): account = models.ForeignKey(Account,on_delete=models.CASCADE,db_column="account_id") class Submission(models.Model): - submission_id = models.AutoField(primary_key=True) + submission_id = models.CharField(primary_key=True,blank=True,default=generate_uuid4_hex,max_length=32) problem = models.ForeignKey(Problem,on_delete=models.CASCADE,db_column="problem_id") topic = models.ForeignKey(Topic,on_delete=models.CASCADE,db_column="topic_id",null=True) account = models.ForeignKey(Account,on_delete=models.CASCADE,db_column="account_id") @@ -102,7 +112,7 @@ class Submission(models.Model): passed_ratio = models.FloatField(default=0) class SubmissionTestcase(models.Model): - submission_testcase_id = models.AutoField(primary_key=True) + submission_testcase_id = models.CharField(primary_key=True,blank=True,default=generate_uuid4_hex,max_length=32) submission = models.ForeignKey(Submission,on_delete=models.CASCADE,db_column="submission_id") testcase = models.ForeignKey(Testcase,on_delete=models.CASCADE,db_column="testcase_id") output = models.CharField(max_length=100000,blank=True,null=True) @@ -110,14 +120,14 @@ class SubmissionTestcase(models.Model): runtime_status = models.CharField(max_length=10) class BestSubmission(models.Model): - best_submission_id = models.AutoField(primary_key=True) + best_submission_id = models.CharField(primary_key=True,blank=True,default=generate_uuid4_hex,max_length=32) problem = models.ForeignKey(Problem,on_delete=models.CASCADE,db_column="problem_id") topic = models.ForeignKey(Topic,on_delete=models.CASCADE,db_column="topic_id",null=True) account = models.ForeignKey(Account,on_delete=models.CASCADE,db_column="account_id") submission = models.ForeignKey(Submission,on_delete=models.CASCADE,db_column="submission_id") class Group(models.Model): - group_id = models.AutoField(primary_key=True) + group_id = models.CharField(primary_key=True,blank=True,default=generate_uuid4_hex,max_length=32) creator = models.ForeignKey(Account,on_delete=models.CASCADE,db_column="creator_id") name = models.CharField(max_length=100) description = models.CharField(max_length=100000,null=True,blank=True,default=None) @@ -125,12 +135,42 @@ class Group(models.Model): created_date = models.DateTimeField(default=timezone.now) updated_date = models.DateTimeField(default=timezone.now) + permission_manage_topics = models.BooleanField(default=False,blank=True) + permission_view_topics = models.BooleanField(default=False,blank=True) + permission_view_topics_log = models.BooleanField(default=False,blank=True) + permission_manage_collections = models.BooleanField(default=False,blank=True) + permission_view_collections = models.BooleanField(default=False,blank=True) + permission_manage_problems = models.BooleanField(default=False,blank=True) + permission_view_problems = models.BooleanField(default=False,blank=True) + class GroupMember(models.Model): - group_member_id = models.AutoField(primary_key=True) + group_member_id = models.CharField(primary_key=True,blank=True,default=generate_uuid4_hex,max_length=32) group = models.ForeignKey(Group,on_delete=models.CASCADE,db_column="group_id") account = models.ForeignKey(Account,on_delete=models.CASCADE,db_column="account_id") created_date = models.DateTimeField(default=timezone.now) +class ProblemGroupPermission(models.Model): + problem_group_permission_id = models.CharField(primary_key=True,blank=True,default=generate_uuid4_hex,max_length=32) + problem = models.ForeignKey(Problem,on_delete=models.CASCADE,db_column="problem_id") + group = models.ForeignKey(Group,on_delete=models.CASCADE,db_column="group_id") + permission_manage_problems = models.BooleanField(default=False,blank=True) + permission_view_problems = models.BooleanField(default=False,blank=True) + +class CollectionGroupPermission(models.Model): + collection_group_permission_id = models.CharField(primary_key=True,blank=True,default=generate_uuid4_hex,max_length=32) + collection = models.ForeignKey(Collection,on_delete=models.CASCADE,db_column="collection_id") + group = models.ForeignKey(Group,on_delete=models.CASCADE,db_column="group_id") + permission_manage_collections = models.BooleanField(default=False,blank=True) + permission_view_collections = models.BooleanField(default=False,blank=True) + +class TopicGroupPermission(models.Model): + topic_group_permission_id = models.CharField(primary_key=True,blank=True,default=generate_uuid4_hex,max_length=32) + topic = models.ForeignKey(Topic,on_delete=models.CASCADE,db_column="topic_id") + group = models.ForeignKey(Group,on_delete=models.CASCADE,db_column="group_id") + permission_manage_topics = models.BooleanField(default=False,blank=True) + permission_view_topics = models.BooleanField(default=False,blank=True) + permission_view_topics_log = models.BooleanField(default=False,blank=True) + # class TopicGroupPermission(models.Model): # topic = models.ForeignKey(Topic,on_delete=models.CASCADE,db_column="topic_id") # group = models.ForeignKey(Group,on_delete=models.CASCADE,db_column="group_id") diff --git a/api/permissions/topic.py b/api/permissions/topic.py new file mode 100644 index 0000000..ee389cc --- /dev/null +++ b/api/permissions/topic.py @@ -0,0 +1,6 @@ +from ..models import * +def canManageTopic(topic:Topic,account:Account): + is_creator = topic.creator.account_id == account.account_id + has_permission = len(TopicGroupPermission.objects.filter(permission_manage_topics=True,topic=topic,group__in=[gm.group for gm in GroupMember.objects.filter(account=account)])) > 0 + print(TopicGroupPermission.objects.filter(topic=topic,group__in=[gm.group for gm in GroupMember.objects.filter(account=account)])) + return is_creator or has_permission \ No newline at end of file diff --git a/api/sandbox/section1/runner.c b/api/sandbox/section1/runner.c deleted file mode 100644 index bf28f2e..0000000 --- a/api/sandbox/section1/runner.c +++ /dev/null @@ -1 +0,0 @@ -n = int(input('N: '));print(2/) \ No newline at end of file diff --git a/api/sandbox/section1/runner.cpp b/api/sandbox/section1/runner.cpp deleted file mode 100644 index 2999e16..0000000 --- a/api/sandbox/section1/runner.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include -using namespace std; - -int main() { - cout << "Hello World!\n"; -} \ No newline at end of file diff --git a/api/serializers.py b/api/serializers.py index 2a30842..a4f9030 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -48,7 +48,7 @@ class Meta: class ProblemSecureSerializer(serializers.ModelSerializer): class Meta: model = Problem - fields = ['problem_id','title','description','is_active','is_private','updated_date','created_date'] + exclude = ['solution','submission_regex','is_private','is_active','sharing'] class ProblemPopulateAccountSerializer(serializers.ModelSerializer): creator = AccountSecureSerializer() @@ -56,6 +56,15 @@ class Meta: model = Problem fields = "__all__" + +class ProblemPopulateAccountSecureSerializer(serializers.ModelSerializer): + creator = AccountSecureSerializer() + class Meta: + model = Problem + exclude = ['solution','submission_regex','is_private','is_active','sharing'] + + + class TopicCollectionSerializer(serializers.ModelSerializer): class Meta: model = TopicCollection @@ -138,11 +147,11 @@ class Meta: 'testcases' ] -class ProblemPopulateAccountSecureSerializer(serializers.ModelSerializer): - creator = AccountSecureSerializer() - class Meta: - model = Problem - fields = ['problem_id','title','description','creator'] +# class ProblemPopulateAccountSecureSerializer(serializers.ModelSerializer): +# creator = AccountSecureSerializer() +# class Meta: +# model = Problem +# fields = ['problem_id','title','description','creator'] class SubmissionPoplulateProblemSecureSerializer(serializers.ModelSerializer): problem = ProblemPopulateAccountSecureSerializer() @@ -199,7 +208,8 @@ class TopicPopulateTopicCollectionPopulateCollectionSerializer(serializers.Model collections = TopicCollectionPopulateCollectionSerializer(many=True) class Meta: model = Topic - fields = ['topic_id','name','description','image_url','is_active','is_private','created_date','updated_date','creator','collections'] + fields = "__all__" + include = ['collections'] class TopicPopulateTopicCollectionPopulateCollectionProblemPopulateProblemSerializer(serializers.ModelSerializer): @@ -250,4 +260,124 @@ class GroupPopulateGroupMemberPopulateAccountSecureSerializer(serializers.ModelS members = GroupMemberPopulateAccountSecureSerializer(many=True) class Meta: model = Group - fields = ['group_id','name','description','color','created_date','updated_date','creator','members'] \ No newline at end of file + fields = "__all__" + include = ['members'] + +class TopicGroupPermissionsSerializer(serializers.ModelSerializer): + class Meta: + model = TopicGroupPermission + fields = "__all__" + +class TopicGroupPermissionPopulateGroupSerializer(serializers.ModelSerializer): + group = GroupSerializer() + class Meta: + model = TopicGroupPermission + fields = "__all__" + +class TopicPopulateTopicGroupPermissionsSerializer(serializers.ModelSerializer): + group_permissions = TopicGroupPermissionsSerializer(many=True) + class Meta: + model = Topic + fields = "__all__" + include = ['group_permissions'] + +class TopicPopulateTopicCollectionPopulateCollectionAndTopicGroupPermissionPopulateGroupSerializer(serializers.ModelSerializer): + collections = TopicCollectionPopulateCollectionSerializer(many=True) + group_permissions = TopicGroupPermissionPopulateGroupSerializer(many=True) + class Meta: + model = Topic + fields = "__all__" + include = ['collections','group_permissions'] + +class CollectionPopulateCollectionProblemPopulateProblemSerializer(serializers.ModelSerializer): + problems = CollectionProblemPopulateProblemSerializer(many=True) + class Meta: + model = Collection + fields = "__all__" + +class TopicCollectionPopulateCollectionPopulateCollectionProblemPopulateProblemSerializer(serializers.ModelSerializer): + collection = CollectionPopulateCollectionProblemPopulateProblemSerializer() + class Meta: + model = TopicCollection + fields = "__all__" + +class TopicPopulateTopicCollectionPopulateCollectionPopulateCollectionProblemPopulateProblemAndTopicGroupPermissionPopulateGroupSerializer(serializers.ModelSerializer): + collections = TopicCollectionPopulateCollectionPopulateCollectionProblemPopulateProblemSerializer(many=True) + group_permissions = TopicGroupPermissionPopulateGroupSerializer(many=True) + + class Meta: + model = Topic + fields = "__all__" + include = ['collections','group_permissions'] + +class CollectionGroupPermissionPopulateGroupSerializer(serializers.ModelSerializer): + group = GroupSerializer() + class Meta: + model = CollectionGroupPermission + fields = "__all__" +class CollectionPopulateCollectionGroupPermissionsPopulateGroupSerializer(serializers.ModelSerializer): + group_permissions = CollectionGroupPermissionPopulateGroupSerializer(many=True) + class Meta: + model = Collection + fields = "__all__" + include = ['group_permissions'] + +class CollectionPopulateCollectionProblemsPopulateProblemAndCollectionGroupPermissionsPopulateGroupSerializer(serializers.ModelSerializer): + problems = CollectionProblemPopulateProblemSerializer(many=True) + group_permissions = CollectionGroupPermissionPopulateGroupSerializer(many=True) + class Meta: + model = Collection + fields = "__all__" + include = ['problems','group_permissions'] + + + +class CollectionPopulateCollectionProblemsPopulateProblemSerializer(serializers.ModelSerializer): + problems = CollectionProblemPopulateProblemSerializer(many=True) + class Meta: + model = Collection + fields = "__all__" + include = ['problems'] + +class TopicCollectionPopulateCollectionPopulateCollectionProblemsPopulateProblemAndCollectionGroupPermissionsPopulateGroupSerializer(serializers.ModelSerializer): + collection = CollectionPopulateCollectionProblemsPopulateProblemAndCollectionGroupPermissionsPopulateGroupSerializer() + class Meta: + model = TopicCollection + fields = "__all__" +class TopicPopulateTopicCollectionPopulateCollectionPopulateCollectionProblemsPopulateProblemAndCollectionGroupPermissionsPopulateGroupAndTopicGroupPermissionPopulateGroupSerializer(serializers.ModelSerializer): + collections = TopicCollectionPopulateCollectionPopulateCollectionProblemsPopulateProblemAndCollectionGroupPermissionsPopulateGroupSerializer(many=True) + group_permissions = TopicGroupPermissionPopulateGroupSerializer(many=True) + + class Meta: + model = Topic + fields = "__all__" + include = ['collections','group_permissions'] + +class ProblemGroupPermissionsPopulateGroupSerializer(serializers.ModelSerializer): + group = GroupSerializer() + class Meta: + model = ProblemGroupPermission + fields = "__all__" + +class ProblemPopulateAccountAndTestcasesAndProblemGroupPermissionsPopulateGroupSerializer(serializers.ModelSerializer): + creator = AccountSecureSerializer() + group_permissions = ProblemGroupPermissionsPopulateGroupSerializer(many=True) + testcases = TestcaseSerializer(many=True) + class Meta: + model = Problem + fields = "__all__" + include = ['creator','group_permissions','testcases'] + + +class CollectionProblemsPopulateProblemPopulateAccountAndTestcasesAndProblemGroupPermissionsPopulateGroupSerializer(serializers.ModelSerializer): + problem = ProblemPopulateAccountAndTestcasesAndProblemGroupPermissionsPopulateGroupSerializer() + class Meta: + model = CollectionProblem + fields = "__all__" +class CollectionPopulateCollectionProblemsPopulateProblemPopulateAccountAndTestcasesAndProblemGroupPermissionsPopulateGroupAndCollectionGroupPermissionsPopulateGroupSerializer(serializers.ModelSerializer): + problems = CollectionProblemsPopulateProblemPopulateAccountAndTestcasesAndProblemGroupPermissionsPopulateGroupSerializer(many=True) + group_permissions = CollectionGroupPermissionPopulateGroupSerializer(many=True) + class Meta: + model = Collection + fields = "__all__" + include = ['problems','group_permissions'] \ No newline at end of file diff --git a/api/urls.py b/api/urls.py index 68591c5..f4c9a13 100644 --- a/api/urls.py +++ b/api/urls.py @@ -8,39 +8,44 @@ path('token',auth.authorization_view), path("accounts",account.all_accounts_view), - path("accounts/",account.one_creator_view), - path("accounts//daily-submissions",account.get_daily_submission), - path("accounts//password",account.change_password), - - path('accounts//problems',problem.all_problems_creator_view), - path('accounts//problems/',problem.one_problem_creator_view), - path("accounts//problems//submissions",submission.account_problem_submission_view), - path("accounts//topics//problems//submissions",submission.topic_account_problem_submission_view), - - path('accounts//collections',collection.all_collections_creator_view), - path('accounts//collections/',collection.one_collection_creator_view), + path("accounts/",account.one_creator_view), + path("accounts//daily-submissions",account.get_daily_submission), + path("accounts//password",account.change_password), + + path('accounts//problems',problem.all_problems_creator_view), + path('accounts//problems/',problem.one_problem_creator_view), + path('accounts//problems//groups',problem.problem_group_view), + path("accounts//problems//submissions",submission.account_problem_submission_view), + path("accounts//topics//problems//submissions",submission.topic_account_problem_submission_view), + + path('accounts//collections',collection.all_collections_creator_view), + path('accounts//collections/',collection.one_collection_creator_view), + path('accounts//collections//groups',collection.collection_groups_view), - path('accounts//topics',topic.all_topics_creator_view), - path('accounts//topics/',topic.one_topic_creator_view), + path('accounts//topics',topic.all_topics_creator_view), + path('accounts//topics/',topic.one_topic_creator_view), + path('accounts//topics//groups',topic.topic_groups_view), - path('accounts//groups',group.all_groups_creator_view), + path('accounts//access/topics',topic.all_topics_access_view), + + path('accounts//groups',group.all_groups_creator_view), path('problems',problem.all_problems_view), path('problems/validate',problem.validation_view), - path('problems/',problem.one_problem_view), - path('topics//problems//accounts/',problem.problem_in_topic_account_view), + path('problems/',problem.one_problem_view), + path('topics//problems//accounts/',problem.problem_in_topic_account_view), path('collections',collection.all_collections_view), - path('collections/',collection.one_collection_view), - path('collections//problems/',collection.collection_problems_view), + path('collections/',collection.one_collection_view), + path('collections//problems/',collection.collection_problems_view), path('topics',topic.all_topics_view), - path('topics/',topic.one_topic_view), - path('topics//access',topic.account_access), - path('topics//collections/',topic.topic_collections_view), + path('topics/',topic.one_topic_view), + path('topics//access',topic.account_access), + path('topics//collections/',topic.topic_collections_view), - path('groups/',group.one_group_view), - path('groups//members',group.group_members_view), + path('groups/',group.one_group_view), + path('groups//members',group.group_members_view), path('submissions',submission.all_submission_view), diff --git a/api/views/account.py b/api/views/account.py index d9f4caa..41785b9 100644 --- a/api/views/account.py +++ b/api/views/account.py @@ -31,7 +31,7 @@ def change_password(request,account_id): return Response({'message':"Your password has been changed"}) @api_view([GET]) -def get_daily_submission(request,account_id:int): +def get_daily_submission(request,account_id:str): submissions = Submission.objects.filter(account_id=account_id) serializes = SubmissionSerializer(submissions,many=True) diff --git a/api/views/collection.py b/api/views/collection.py index 1213b1c..424050c 100644 --- a/api/views/collection.py +++ b/api/views/collection.py @@ -17,30 +17,33 @@ from ..controllers.collection.remove_problems_from_collection import * from ..controllers.collection.get_all_collections_by_account import * from ..controllers.collection.update_problems_to_collection import * +from ..controllers.collection.update_group_permissions_collection import * + @api_view([POST,GET]) -def all_collections_creator_view(request,account_id:int): +def all_collections_creator_view(request,account_id:str): if request.method == POST: return create_collection(account_id,request) if request.method == GET: return get_all_collections_by_account(account_id) @api_view([GET,PUT,DELETE]) -def one_collection_creator_view(request,collection_id:int): +def one_collection_creator_view(request,account_id:int,collection_id:str): + collection = Collection.objects.get(collection_id=collection_id) if request.method == GET: - return get_collection(collection_id) + return get_collection(collection) if request.method == PUT: - return update_collection(collection_id,request) + return update_collection(collection,request) if request.method == DELETE: - return delete_collection(collection_id) + return delete_collection(collection) @api_view([GET]) def all_collections_view(request): return get_all_collections(request) @api_view([GET,PUT,DELETE]) -def one_collection_view(request,collection_id:int): +def one_collection_view(request,collection_id:str): if request.method == GET: return get_collection(collection_id) if request.method == PUT: @@ -49,7 +52,7 @@ def one_collection_view(request,collection_id:int): return delete_collection(collection_id) @api_view([PUT]) -def collection_problems_view(request,collection_id:int,method:str): +def collection_problems_view(request,collection_id:str,method:str): collection = Collection.objects.get(collection_id=collection_id) @@ -58,4 +61,10 @@ def collection_problems_view(request,collection_id:int,method:str): if method == "remove": return remove_problems_from_collection(collection,request) if method == "update": - return update_problems_to_collection(collection,request) \ No newline at end of file + return update_problems_to_collection(collection,request) + +@api_view([PUT]) +def collection_groups_view(request,account_id:int,collection_id:str): + collection = Collection.objects.get(collection_id=collection_id) + if request.method == PUT: + return update_group_permissions_collection(collection,request) \ No newline at end of file diff --git a/api/views/group.py b/api/views/group.py index e9d95d3..9e2aa0d 100644 --- a/api/views/group.py +++ b/api/views/group.py @@ -15,7 +15,7 @@ from ..controllers.group.get_all_groups_by_account import get_all_groups_by_account @api_view([POST,GET]) -def all_groups_creator_view(request,account_id:int): +def all_groups_creator_view(request,account_id:str): account = Account.objects.get(account_id=account_id) if request.method == POST: return create_group(account,request) @@ -23,7 +23,7 @@ def all_groups_creator_view(request,account_id:int): return get_all_groups_by_account(account,request) @api_view([PUT,DELETE,GET]) -def one_group_view(request,group_id:int): +def one_group_view(request,group_id:str): group = Group.objects.get(group_id=group_id) if request.method == PUT: return update_group(group,request) @@ -33,7 +33,7 @@ def one_group_view(request,group_id:int): return get_group(group,request) @api_view([PUT]) -def group_members_view(request,group_id:int): +def group_members_view(request,group_id:str): group = Group.objects.get(group_id=group_id) if request.method == PUT: return update_members_to_group(group,request) \ No newline at end of file diff --git a/api/views/problem.py b/api/views/problem.py index 8c7224b..7016010 100644 --- a/api/views/problem.py +++ b/api/views/problem.py @@ -18,6 +18,9 @@ from ..controllers.problem.validate_program import * from ..controllers.problem.get_all_problem_with_best_submission import * from ..controllers.problem.get_problem_in_topic_with_best_submission import * +from ..controllers.problem.update_group_permission_to_problem import * +from ..controllers.problem.get_problem_public import * + # Create your views here. @api_view([POST,GET]) @@ -28,13 +31,14 @@ def all_problems_creator_view(request,account_id): return get_all_problems_by_account(account_id) @api_view([GET,PUT,DELETE]) -def one_problem_creator_view(problem_id:int,request): +def one_problem_creator_view(request,problem_id:str,account_id:str): + problem = Problem.objects.get(problem_id=problem_id) if request.method == GET: - return get_problem(problem_id) + return get_problem(problem) elif request.method == PUT: - return update_problem(problem_id,request) + return update_problem(problem,request) elif request.method == DELETE: - return delete_problem(problem_id) + return delete_problem(problem) @api_view([GET,DELETE]) def all_problems_view(request): @@ -45,8 +49,9 @@ def all_problems_view(request): @api_view([GET,PUT,DELETE]) def one_problem_view(request,problem_id: int): + problem = Problem.objects.get(problem_id=problem_id) if request.method == GET: - return get_problem(problem_id) + return get_problem_public(problem) elif request.method == PUT: return update_problem(problem_id,request) elif request.method == DELETE: @@ -58,6 +63,12 @@ def validation_view(request): return validate_program(request) @api_view([GET]) -def problem_in_topic_account_view(request,account_id:int,topic_id:int,problem_id:int): +def problem_in_topic_account_view(request,account_id:str,topic_id:str,problem_id:str): if request.method == GET: - return get_problem_in_topic_with_best_submission(account_id,topic_id,problem_id) \ No newline at end of file + return get_problem_in_topic_with_best_submission(account_id,topic_id,problem_id) + +@api_view([PUT]) +def problem_group_view(request,account_id:int,problem_id:int): + problem = Problem.objects.get(problem_id=problem_id) + if request.method == PUT: + return update_group_permission_to_problem(problem,request) \ No newline at end of file diff --git a/api/views/script.py b/api/views/script.py index afc396b..bfc5b45 100644 --- a/api/views/script.py +++ b/api/views/script.py @@ -6,7 +6,7 @@ from rest_framework import status from django.forms.models import model_to_dict from ..serializers import * - +from ..controllers.script.generate_submission_score import generate_submission_score # @api_view([POST]) # def run_script(request): @@ -51,8 +51,9 @@ @api_view([POST]) def run_script(request): - collections = Collection.objects.all() - for collection in collections: - collection.description = '[{"id":"1","type":"p","children":[{"text":"Just course"}]}]' - collection.save() + # collections = Collection.objects.all() + # for collection in collections: + # collection.description = '[{"id":"1","type":"p","children":[{"text":"Just course"}]}]' + # collection.save() + generate_submission_score(request) return Response({'message': 'Success!'},status=status.HTTP_201_CREATED) diff --git a/api/views/submission.py b/api/views/submission.py index 5973897..4672259 100644 --- a/api/views/submission.py +++ b/api/views/submission.py @@ -37,5 +37,5 @@ def topic_account_problem_submission_view(request,topic_id,account_id,problem_id return get_submissions_by_account_problem_in_topic(account_id,problem_id,topic_id) # @api_view([GET]) -# def submission_account_problem_view(request,account_id:int,problem_id:int): +# def submission_account_problem_view(request,account_id:str,problem_id:str): # return get_submissions_by_account_problem(account_id,problem_id) \ No newline at end of file diff --git a/api/views/topic.py b/api/views/topic.py index b8932e3..55be2bf 100644 --- a/api/views/topic.py +++ b/api/views/topic.py @@ -18,30 +18,38 @@ from ..controllers.topic.get_all_topics_by_account import * from ..controllers.topic.update_collections_to_topic import * from ..controllers.topic.get_topic_public import * +from ..controllers.topic.update_groups_permission_to_topic import * +from ..controllers.topic.get_all_accessed_topics_by_account import * +from ..permissions.topic import * @api_view([POST,GET]) @parser_classes([MultiPartParser,FormParser]) def all_topics_creator_view(request,account_id :int): + account = Account.objects.get(account_id=account_id) if request.method == POST: return create_topic(account_id,request) elif request.method == GET: - return get_all_topics_by_account(account_id,request) + return get_all_topics_by_account(account,request) @api_view([GET,PUT,DELETE]) -def one_topic_creator_view(request,account_id:int,topic_id:int): +def one_topic_creator_view(request,account_id:str,topic_id:str): + topic = Topic.objects.get(topic_id=topic_id) + account = Account.objects.get(account_id=account_id) + if not canManageTopic(topic,account): + return Response(status=status.HTTP_401_UNAUTHORIZED) if request.method == GET: - return get_topic(topic_id) + return get_topic(topic) elif request.method == PUT: - return update_topic(topic_id,request) + return update_topic(topic,request) elif request.method == DELETE: - return delete_topic(topic_id) + return delete_topic(topic) @api_view([GET]) def all_topics_view(request): return get_all_topics(request) @api_view([GET,PUT,DELETE]) -def one_topic_view(request,topic_id:int): +def one_topic_view(request,topic_id:str): if request.method == GET: return get_topic_public(topic_id,request) elif request.method == PUT: @@ -50,7 +58,7 @@ def one_topic_view(request,topic_id:int): return delete_topic(topic_id) @api_view([PUT]) -def topic_collections_view(request,topic_id:int,method:str): +def topic_collections_view(request,topic_id:str,method:str): topic = Topic.objects.get(topic_id=topic_id) @@ -62,7 +70,7 @@ def topic_collections_view(request,topic_id:int,method:str): return update_collections_to_topic(topic,request) @api_view([POST,PUT]) -def account_access(request,topic_id:int): +def account_access(request,topic_id:str): topic = Topic.objects.get(topic_id=topic_id) target_accounts = Account.objects.filter(account_id__in=request.data['account_ids']) @@ -86,3 +94,13 @@ def account_access(request,topic_id:int): topicAccountAccesses = TopicAccountAccess.objects.filter(account_id__in=request.data['account_ids']) topicAccountAccesses.delete() return Response(status=status.HTTP_204_NO_CONTENT) + +@api_view([PUT]) +def topic_groups_view(request,account_id:int,topic_id:str): + topic = Topic.objects.get(topic_id=topic_id) + return update_groups_permission_to_topic(topic,request) + +@api_view([GET]) +def all_topics_access_view(request,account_id:str): + account = Account.objects.get(account_id=account_id) + return get_all_accessed_topics_by_account(account) \ No newline at end of file