From cac6066ea71fb669794450faa85475819dbc229a Mon Sep 17 00:00:00 2001 From: KanonKC Date: Sun, 31 Dec 2023 14:34:57 +0700 Subject: [PATCH 01/15] Change ID from sequence number to random uuid4 --- api/controllers/account/get_account.py | 2 +- .../collection/create_collection.py | 2 +- .../get_all_collections_by_account.py | 2 +- api/controllers/collection/get_collection.py | 2 +- .../collection/update_collection.py | 2 +- api/controllers/problem/create_problem.py | 2 +- api/controllers/problem/delete_problem.py | 2 +- .../get_all_problem_with_best_submission.py | 2 +- .../problem/get_all_problems_by_account.py | 2 +- api/controllers/problem/get_problem.py | 2 +- ...t_problem_in_topic_with_best_submission.py | 2 +- api/controllers/problem/update_problem.py | 25 +++-- .../get_submissions_by_account_problem.py | 6 +- ...submissions_by_account_problem_in_topic.py | 4 +- api/controllers/submission/submit_problem.py | 4 +- .../submission/submit_problem_on_topic.py | 2 +- .../topic/add_collections_to_topic.py | 2 +- api/controllers/topic/create_topic.py | 2 +- api/controllers/topic/delete_topic.py | 2 +- .../get_all_accessed_topics_by_account.py | 18 ++++ .../topic/get_all_topics_by_account.py | 2 +- api/controllers/topic/get_topic.py | 2 +- api/controllers/topic/get_topic_public.py | 2 +- .../topic/remove_collections_from_topic.py | 2 +- .../update_groups_permission_to_topic.py | 29 ++++++ api/controllers/topic/update_topic.py | 2 +- .../0045_collection_sharing_and_more.py | 95 +++++++++++++++++++ .../0046_alter_account_account_id_and_more.py | 78 +++++++++++++++ .../0047_alter_account_account_id_and_more.py | 78 +++++++++++++++ .../0048_alter_account_account_id_and_more.py | 78 +++++++++++++++ .../0049_alter_account_account_id_and_more.py | 88 +++++++++++++++++ .../0050_alter_account_account_id_and_more.py | 88 +++++++++++++++++ .../0051_alter_account_account_id_and_more.py | 89 +++++++++++++++++ api/models.py | 61 +++++++++--- api/serializers.py | 13 ++- api/urls.py | 45 +++++---- api/views/account.py | 2 +- api/views/collection.py | 8 +- api/views/group.py | 6 +- api/views/problem.py | 4 +- api/views/submission.py | 2 +- api/views/topic.py | 20 +++- 42 files changed, 798 insertions(+), 83 deletions(-) create mode 100644 api/controllers/topic/get_all_accessed_topics_by_account.py create mode 100644 api/controllers/topic/update_groups_permission_to_topic.py create mode 100644 api/migrations/0045_collection_sharing_and_more.py create mode 100644 api/migrations/0046_alter_account_account_id_and_more.py create mode 100644 api/migrations/0047_alter_account_account_id_and_more.py create mode 100644 api/migrations/0048_alter_account_account_id_and_more.py create mode 100644 api/migrations/0049_alter_account_account_id_and_more.py create mode 100644 api/migrations/0050_alter_account_account_id_and_more.py create mode 100644 api/migrations/0051_alter_account_account_id_and_more.py 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..be88a0b 100644 --- a/api/controllers/collection/get_all_collections_by_account.py +++ b/api/controllers/collection/get_all_collections_by_account.py @@ -8,7 +8,7 @@ from django.forms.models import model_to_dict from ...serializers import * -def get_all_collections_by_account(account_id:int): +def get_all_collections_by_account(account_id:str): collections = Collection.objects.filter(creator=account_id).order_by('-updated_date') problemCollections = CollectionProblem.objects.filter(collection__in=collections) diff --git a/api/controllers/collection/get_collection.py b/api/controllers/collection/get_collection.py index 1814b0c..b3e0a77 100644 --- a/api/controllers/collection/get_collection.py +++ b/api/controllers/collection/get_collection.py @@ -8,7 +8,7 @@ from django.forms.models import model_to_dict from ...serializers import * -def get_collection(collection_id:int): +def get_collection(collection_id:str): collection = Collection.objects.get(collection_id=collection_id) # problems = Problem.objects.filter(collectionproblem__collection_id=collection_id) diff --git a/api/controllers/collection/update_collection.py b/api/controllers/collection/update_collection.py index 5abdc62..aa2df34 100644 --- a/api/controllers/collection/update_collection.py +++ b/api/controllers/collection/update_collection.py @@ -8,7 +8,7 @@ from django.forms.models import model_to_dict from ...serializers import * -def update_collection(collection_id:int,request): +def update_collection(collection_id:str,request): collection = Collection.objects.get(collection_id=collection_id) collection.name = request.data.get('name',collection.name) diff --git a/api/controllers/problem/create_problem.py b/api/controllers/problem/create_problem.py index 3998d2e..16fb7a5 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() diff --git a/api/controllers/problem/delete_problem.py b/api/controllers/problem/delete_problem.py index 4140824..bcc75f2 100644 --- a/api/controllers/problem/delete_problem.py +++ b/api/controllers/problem/delete_problem.py @@ -8,7 +8,7 @@ from django.forms.models import model_to_dict from ...serializers import * -def delete_problem(problem_id:int): +def delete_problem(problem_id:str): try: problem = Problem.objects.get(problem_id=problem_id) except Problem.DoesNotExist: 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_by_account.py b/api/controllers/problem/get_all_problems_by_account.py index bae9918..27dd562 100644 --- a/api/controllers/problem/get_all_problems_by_account.py +++ b/api/controllers/problem/get_all_problems_by_account.py @@ -8,7 +8,7 @@ from django.forms.models import model_to_dict from ...serializers import * -def get_all_problems_by_account(account_id:int): +def get_all_problems_by_account(account_id:str): problems = Problem.objects.filter(creator=account_id).order_by('-updated_date') for problem in problems: diff --git a/api/controllers/problem/get_problem.py b/api/controllers/problem/get_problem.py index 56f78f8..664a957 100644 --- a/api/controllers/problem/get_problem.py +++ b/api/controllers/problem/get_problem.py @@ -8,7 +8,7 @@ from django.forms.models import model_to_dict from ...serializers import * -def get_problem(problem_id:int): +def get_problem(problem_id:str): try: problem = Problem.objects.get(problem_id=problem_id) except Problem.DoesNotExist: 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/update_problem.py b/api/controllers/problem/update_problem.py index 3380add..d60f98a 100644 --- a/api/controllers/problem/update_problem.py +++ b/api/controllers/problem/update_problem.py @@ -9,13 +9,15 @@ from ...serializers import * from django.utils import timezone -def update_problem(problem_id:int,request): +def update_problem(problem_id:str,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) + print("AAA") + problem.title = request.data.get("title",problem.title) problem.language = request.data.get("language",problem.language) problem.description = request.data.get("description",problem.description) @@ -25,40 +27,47 @@ def update_problem(problem_id:int,request): problem.updated_date = timezone.now() + print("BBB") + if 'testcases' in request.data: running_result = PythonGrader(problem.solution,request.data['testcases'],1,1.5).generate_output() # if not running_result.runnable: # return Response({'detail': 'Error during editing. Your code may has an error/timeout!'},status=status.HTTP_406_NOT_ACCEPTABLE) - + print("ZZZZZ") for testcase in testcases: testcase.deprecated = True testcase.save() - + print("ZZZZZ") testcase_result = [] for unit in running_result.data: - testcase = Testcase( + print("YYYYY") + testcase2 = Testcase( problem = problem, input = unit.input, output = unit.output, runtime_status = unit.runtime_status ) - testcase.save() - testcase_result.append(testcase) + print("YYYYY",testcase2.testcase_id) + testcase2.save() + print("YYYYY") + testcase_result.append(testcase2) problem.save() - + print("ZZZZZ") 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_id=problem_id,deprecated=False) program_input = [i.input for i in testcases] running_result = PythonGrader(problem.solution,program_input,1,1.5).generate_output() if not running_result.runnable: return Response({'detail': 'Error during editing. Your code may has an error/timeout!'},status=status.HTTP_406_NOT_ACCEPTABLE) + print("CCC") problem.save() problem_serialize = ProblemSerializer(problem) return Response(problem_serialize.data,status=status.HTTP_201_CREATED) 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..74938fc 100644 --- a/api/controllers/topic/delete_topic.py +++ b/api/controllers/topic/delete_topic.py @@ -8,7 +8,7 @@ from django.forms.models import model_to_dict from ...serializers import * -def delete_topic(topic_id:int): +def delete_topic(topic_id:str): topic = Topic.objects.get(topic_id=topic_id) 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..019b175 --- /dev/null +++ b/api/controllers/topic/get_all_accessed_topics_by_account.py @@ -0,0 +1,18 @@ +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_all_accessed_topics_by_account(account:Account): + groups = [gm.group for gm in GroupMember.objects.filter(account=account)] + accessedTopics = TopicGroupPermission.objects.filter(group__in=groups).distinct() + topics = [at.topic for at in accessedTopics] + + serialize = TopicSerializer(topics,many=True) + + return Response({'topics':serialize.data},status=status.HTTP_204_NO_CONTENT) diff --git a/api/controllers/topic/get_all_topics_by_account.py b/api/controllers/topic/get_all_topics_by_account.py index d9dd97b..5d71a84 100644 --- a/api/controllers/topic/get_all_topics_by_account.py +++ b/api/controllers/topic/get_all_topics_by_account.py @@ -8,7 +8,7 @@ from django.forms.models import model_to_dict from ...serializers import * -def get_all_topics_by_account(account_id:int,request): +def get_all_topics_by_account(account_id:str,request): topics = Topic.objects.filter(creator_id=account_id).order_by('-updated_date') topicCollections = TopicCollection.objects.filter(topic__in=topics) diff --git a/api/controllers/topic/get_topic.py b/api/controllers/topic/get_topic.py index 89f37b4..6a17ce1 100644 --- a/api/controllers/topic/get_topic.py +++ b/api/controllers/topic/get_topic.py @@ -8,7 +8,7 @@ from django.forms.models import model_to_dict from ...serializers import * -def get_topic(topic_id:int): +def get_topic(topic_id:str): topic = Topic.objects.get(topic_id=topic_id) topic.collections = TopicCollection.objects.filter(topic_id=topic_id) serialize = TopicPopulateTopicCollectionPopulateCollectionSerializer(topic) diff --git a/api/controllers/topic/get_topic_public.py b/api/controllers/topic/get_topic_public.py index 177f894..b05513e 100644 --- a/api/controllers/topic/get_topic_public.py +++ b/api/controllers/topic/get_topic_public.py @@ -8,7 +8,7 @@ from django.forms.models import model_to_dict from ...serializers import * -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) 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..5831898 --- /dev/null +++ b/api/controllers/topic/update_groups_permission_to_topic.py @@ -0,0 +1,29 @@ +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): + + 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..f094c05 100644 --- a/api/controllers/topic/update_topic.py +++ b/api/controllers/topic/update_topic.py @@ -8,7 +8,7 @@ from django.forms.models import model_to_dict from ...serializers import * -def update_topic(topic_id:int,request): +def update_topic(topic_id:str,request): topic = Topic.objects.get(topic_id=topic_id) topic_ser = TopicSerializer(topic,data=request.data,partial=True) 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/models.py b/api/models.py index 38e08f5..1f45db5 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,10 @@ 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) 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 +53,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 +61,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 +74,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 +98,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 +111,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 +119,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 +134,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/serializers.py b/api/serializers.py index 2a30842..0f63c90 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -250,4 +250,15 @@ 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 = ['group_id','name','description','color','created_date','updated_date','creator','members'] + +class TopicGroupPermissionsSerializer(serializers.ModelSerializer): + class Meta: + model = TopicGroupPermission + fields = "__all__" + +class TopicPopulateTopicGroupPermissionsSerializer(serializers.ModelSerializer): + group_permissions = TopicGroupPermissionsSerializer(many=True) + class Meta: + model = Topic + fields = ['topic_id','name','description','image_url','is_active','is_private','created_date','updated_date','creator','group_permissions'] \ No newline at end of file diff --git a/api/urls.py b/api/urls.py index 68591c5..f704418 100644 --- a/api/urls.py +++ b/api/urls.py @@ -8,39 +8,42 @@ 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/",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//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//collections',collection.all_collections_creator_view), + path('accounts//collections/',collection.one_collection_creator_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//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('topics//groups',topic.topic_groups_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..e66c8cc 100644 --- a/api/views/collection.py +++ b/api/views/collection.py @@ -20,14 +20,14 @@ @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,collection_id:str): if request.method == GET: return get_collection(collection_id) if request.method == PUT: @@ -40,7 +40,7 @@ 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 +49,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) 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..d42fb6c 100644 --- a/api/views/problem.py +++ b/api/views/problem.py @@ -28,7 +28,7 @@ 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(problem_id:str,request): if request.method == GET: return get_problem(problem_id) elif request.method == PUT: @@ -58,6 +58,6 @@ 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 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..ecaf583 100644 --- a/api/views/topic.py +++ b/api/views/topic.py @@ -18,6 +18,8 @@ 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 * @api_view([POST,GET]) @parser_classes([MultiPartParser,FormParser]) @@ -28,7 +30,7 @@ def all_topics_creator_view(request,account_id :int): return get_all_topics_by_account(account_id,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): if request.method == GET: return get_topic(topic_id) elif request.method == PUT: @@ -41,7 +43,7 @@ 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 +52,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 +64,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 +88,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,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 From c92efc5ddcae5f3bedaa8de57bc09e8a6e7a841b Mon Sep 17 00:00:00 2001 From: KanonKC Date: Sun, 31 Dec 2023 20:24:50 +0700 Subject: [PATCH 02/15] Get topics by accessible --- api/controllers/problem/get_all_problems.py | 4 ++-- .../submission/get_submission_by_quries.py | 12 ++++++------ .../topic/get_all_accessed_topics_by_account.py | 4 ++-- .../topic/update_groups_permission_to_topic.py | 2 ++ 4 files changed, 12 insertions(+), 10 deletions(-) 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/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/topic/get_all_accessed_topics_by_account.py b/api/controllers/topic/get_all_accessed_topics_by_account.py index 019b175..28753e0 100644 --- a/api/controllers/topic/get_all_accessed_topics_by_account.py +++ b/api/controllers/topic/get_all_accessed_topics_by_account.py @@ -10,9 +10,9 @@ def get_all_accessed_topics_by_account(account:Account): groups = [gm.group for gm in GroupMember.objects.filter(account=account)] - accessedTopics = TopicGroupPermission.objects.filter(group__in=groups).distinct() + accessedTopics = TopicGroupPermission.objects.filter(group__in=groups,permission_view_topics=True).distinct() topics = [at.topic for at in accessedTopics] serialize = TopicSerializer(topics,many=True) - return Response({'topics':serialize.data},status=status.HTTP_204_NO_CONTENT) + return Response({'topics':serialize.data},status=status.HTTP_200_OK) diff --git a/api/controllers/topic/update_groups_permission_to_topic.py b/api/controllers/topic/update_groups_permission_to_topic.py index 5831898..608791b 100644 --- a/api/controllers/topic/update_groups_permission_to_topic.py +++ b/api/controllers/topic/update_groups_permission_to_topic.py @@ -9,6 +9,8 @@ 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']: From 422745d763e56fd570a135a59f6160096d1b3c73 Mon Sep 17 00:00:00 2001 From: KanonKC Date: Wed, 3 Jan 2024 15:27:26 +0700 Subject: [PATCH 03/15] TopicPopulateTopicCollectionPopulateCollection --- api/controllers/topic/get_topic.py | 3 ++- api/serializers.py | 23 ++++++++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/api/controllers/topic/get_topic.py b/api/controllers/topic/get_topic.py index 6a17ce1..d09be37 100644 --- a/api/controllers/topic/get_topic.py +++ b/api/controllers/topic/get_topic.py @@ -11,7 +11,8 @@ def get_topic(topic_id:str): topic = Topic.objects.get(topic_id=topic_id) topic.collections = TopicCollection.objects.filter(topic_id=topic_id) - serialize = TopicPopulateTopicCollectionPopulateCollectionSerializer(topic) + topic.group_permissions = TopicGroupPermission.objects.filter(topic=topic) + serialize = TopicPopulateTopicCollectionPopulateCollectionAndTopicGroupPermissionPopulateGroupSerializer(topic) return Response(serialize.data,status=status.HTTP_200_OK) diff --git a/api/serializers.py b/api/serializers.py index 0f63c90..2fd3d9a 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -199,7 +199,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,15 +251,31 @@ class GroupPopulateGroupMemberPopulateAccountSecureSerializer(serializers.ModelS members = GroupMemberPopulateAccountSecureSerializer(many=True) class Meta: model = Group - fields = ['group_id','name','description','color','created_date','updated_date','creator','members'] + 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 = ['topic_id','name','description','image_url','is_active','is_private','created_date','updated_date','creator','group_permissions'] \ No newline at end of file + 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'] From d7f2297d7dddfb9f294c086e9b91bc88268a6903 Mon Sep 17 00:00:00 2001 From: KanonKC Date: Wed, 3 Jan 2024 22:45:46 +0700 Subject: [PATCH 04/15] Migration to main account --- ..._admin_remove_collection_owner_and_more.py | 6 ++-- api/sandbox/section1/runner.cpp | 28 +++++++++++++++++-- api/views/script.py | 11 ++++---- 3 files changed, 34 insertions(+), 11 deletions(-) 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/sandbox/section1/runner.cpp b/api/sandbox/section1/runner.cpp index 2999e16..e03836d 100644 --- a/api/sandbox/section1/runner.cpp +++ b/api/sandbox/section1/runner.cpp @@ -1,6 +1,28 @@ -#include +#include + + + using namespace std; -int main() { - cout << "Hello World!\n"; + + + + +void rec(int t){ + + cout << "HEllo"; + return rec(t-1); + +} + + + +int main(){ + + // int t; cin >> t; + + rec(5); + + return 0; + } \ 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) From fbc227102e80284a95bff3d5a2aea1fa49a4a87a Mon Sep 17 00:00:00 2001 From: KanonKC Date: Wed, 3 Jan 2024 22:51:33 +0700 Subject: [PATCH 05/15] Removed runner code --- api/sandbox/section1/runner.c | 1 - api/sandbox/section1/runner.cpp | 28 ---------------------------- 2 files changed, 29 deletions(-) delete mode 100644 api/sandbox/section1/runner.c delete mode 100644 api/sandbox/section1/runner.cpp 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 e03836d..0000000 --- a/api/sandbox/section1/runner.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include - - - -using namespace std; - - - - - -void rec(int t){ - - cout << "HEllo"; - return rec(t-1); - -} - - - -int main(){ - - // int t; cin >> t; - - rec(5); - - return 0; - -} \ No newline at end of file From 40ad8746329cb18a1669ff207d5735ce07d577b0 Mon Sep 17 00:00:00 2001 From: KanonKC Date: Thu, 4 Jan 2024 22:47:33 +0700 Subject: [PATCH 06/15] Update topic apis --- api/controllers/topic/delete_topic.py | 4 ++-- .../topic/get_all_topics_by_account.py | 18 +++++++++++++----- api/controllers/topic/get_topic.py | 5 ++--- api/controllers/topic/update_topic.py | 4 +--- api/permissions/topic.py | 6 ++++++ api/views/topic.py | 14 ++++++++++---- 6 files changed, 34 insertions(+), 17 deletions(-) create mode 100644 api/permissions/topic.py diff --git a/api/controllers/topic/delete_topic.py b/api/controllers/topic/delete_topic.py index 74938fc..6d1db33 100644 --- a/api/controllers/topic/delete_topic.py +++ b/api/controllers/topic/delete_topic.py @@ -8,7 +8,7 @@ from django.forms.models import model_to_dict from ...serializers import * -def delete_topic(topic_id:str): - topic = Topic.objects.get(topic_id=topic_id) +def delete_topic(topic:Topic): + topic = Topic.objects.get(topic=topic) topic.delete() return Response(status=status.HTTP_204_NO_CONTENT) diff --git a/api/controllers/topic/get_all_topics_by_account.py b/api/controllers/topic/get_all_topics_by_account.py index 5d71a84..49ce8c8 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:str,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.all().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 d09be37..617898a 100644 --- a/api/controllers/topic/get_topic.py +++ b/api/controllers/topic/get_topic.py @@ -8,9 +8,8 @@ from django.forms.models import model_to_dict from ...serializers import * -def get_topic(topic_id:str): - topic = Topic.objects.get(topic_id=topic_id) - topic.collections = TopicCollection.objects.filter(topic_id=topic_id) +def get_topic(topic:Topic): + topic.collections = TopicCollection.objects.filter(topic=topic) topic.group_permissions = TopicGroupPermission.objects.filter(topic=topic) serialize = TopicPopulateTopicCollectionPopulateCollectionAndTopicGroupPermissionPopulateGroupSerializer(topic) diff --git a/api/controllers/topic/update_topic.py b/api/controllers/topic/update_topic.py index f094c05..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:str,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/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/views/topic.py b/api/views/topic.py index ecaf583..f3cc177 100644 --- a/api/views/topic.py +++ b/api/views/topic.py @@ -20,23 +20,29 @@ 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: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): From 1a74decc23d8272bf560a1972eb9105abd3aa134 Mon Sep 17 00:00:00 2001 From: KanonKC Date: Fri, 5 Jan 2024 01:04:52 +0700 Subject: [PATCH 07/15] Get topic populate problems --- .../get_all_collections_by_account.py | 2 +- api/controllers/topic/get_topic.py | 9 ++++++-- api/serializers.py | 21 +++++++++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/api/controllers/collection/get_all_collections_by_account.py b/api/controllers/collection/get_all_collections_by_account.py index be88a0b..4b3bd0a 100644 --- a/api/controllers/collection/get_all_collections_by_account.py +++ b/api/controllers/collection/get_all_collections_by_account.py @@ -17,7 +17,7 @@ def get_all_collections_by_account(account_id:str): con_probs = problemCollections.filter(collection=collection) serialize = CollectionSerializer(collection) collection_data = serialize.data - collection_data['problems'] = CollectionProblemPopulateProblemSecureSerializer(con_probs,many=True).data + collection_data['problems'] = CollectionProblemPopulateProblemSerializer(con_probs,many=True).data populated_collections.append(collection_data) diff --git a/api/controllers/topic/get_topic.py b/api/controllers/topic/get_topic.py index 617898a..19d5f41 100644 --- a/api/controllers/topic/get_topic.py +++ b/api/controllers/topic/get_topic.py @@ -9,9 +9,14 @@ from ...serializers import * def get_topic(topic:Topic): - topic.collections = TopicCollection.objects.filter(topic=topic) topic.group_permissions = TopicGroupPermission.objects.filter(topic=topic) - serialize = TopicPopulateTopicCollectionPopulateCollectionAndTopicGroupPermissionPopulateGroupSerializer(topic) + + topic.collections = TopicCollection.objects.filter(topic=topic) + + for tp in topic.collections: + tp.collection.problems = CollectionProblem.objects.filter(collection=tp.collection) + + serialize = TopicPopulateTopicCollectionPopulateCollectionPopulateCollectionProblemPopulateProblemAndTopicGroupPermissionPopulateGroupSerializer(topic) return Response(serialize.data,status=status.HTTP_200_OK) diff --git a/api/serializers.py b/api/serializers.py index 2fd3d9a..8073a6b 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -279,3 +279,24 @@ 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'] \ No newline at end of file From 5fdc9d2a6d2704f3b289b68ec4954f39a4b67763 Mon Sep 17 00:00:00 2001 From: KanonKC Date: Sat, 6 Jan 2024 14:08:34 +0700 Subject: [PATCH 08/15] Collection permission --- api/controllers/collection/get_collection.py | 20 +++++-------- .../update_group_permissions_collection.py | 30 +++++++++++++++++++ api/serializers.py | 22 +++++++++++++- api/urls.py | 3 +- api/views/collection.py | 12 ++++++-- api/views/topic.py | 2 +- 6 files changed, 72 insertions(+), 17 deletions(-) create mode 100644 api/controllers/collection/update_group_permissions_collection.py diff --git a/api/controllers/collection/get_collection.py b/api/controllers/collection/get_collection.py index b3e0a77..3a01e51 100644 --- a/api/controllers/collection/get_collection.py +++ b/api/controllers/collection/get_collection.py @@ -12,17 +12,13 @@ def get_collection(collection_id:str): 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) - 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) + # collection_ser = CollectionSerializer(collection) + # collectionProblems_ser = CollectionProblemPopulateProblemSecureSerializer(collectionProblems,many=True) - return Response({ - **collection_ser.data, - 'problems': collectionProblems_ser.data - } ,status=status.HTTP_200_OK) \ No newline at end of file + serializer = CollectionPopulateCollectionProblemsPopulateProblemAndCollectionGroupPermissionsPopulateGroupSerializer(collection) + + + return Response(serializer.data ,status=status.HTTP_200_OK) \ No newline at end of file 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..d2d8061 --- /dev/null +++ b/api/controllers/collection/update_group_permissions_collection.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_permissions_collection(collection:Collection,request): + + CollectionGroupPermission.objects.filter(collection=collection).delete() + + 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/serializers.py b/api/serializers.py index 8073a6b..92816fc 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -299,4 +299,24 @@ class TopicPopulateTopicCollectionPopulateCollectionPopulateCollectionProblemPop class Meta: model = Topic fields = "__all__" - include = ['collections','group_permissions'] \ No newline at end of file + 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'] \ No newline at end of file diff --git a/api/urls.py b/api/urls.py index f704418..1e6aa11 100644 --- a/api/urls.py +++ b/api/urls.py @@ -19,9 +19,11 @@ 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//groups',topic.topic_groups_view), path('accounts//access/topics',topic.all_topics_access_view), @@ -40,7 +42,6 @@ path('topics/',topic.one_topic_view), path('topics//access',topic.account_access), path('topics//collections/',topic.topic_collections_view), - path('topics//groups',topic.topic_groups_view), path('groups/',group.one_group_view), path('groups//members',group.group_members_view), diff --git a/api/views/collection.py b/api/views/collection.py index e66c8cc..3db6fcf 100644 --- a/api/views/collection.py +++ b/api/views/collection.py @@ -17,6 +17,8 @@ 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]) @@ -27,7 +29,7 @@ def all_collections_creator_view(request,account_id:str): return get_all_collections_by_account(account_id) @api_view([GET,PUT,DELETE]) -def one_collection_creator_view(request,collection_id:str): +def one_collection_creator_view(request,account_id:int,collection_id:str): if request.method == GET: return get_collection(collection_id) if request.method == PUT: @@ -58,4 +60,10 @@ def collection_problems_view(request,collection_id:str,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/topic.py b/api/views/topic.py index f3cc177..55be2bf 100644 --- a/api/views/topic.py +++ b/api/views/topic.py @@ -96,7 +96,7 @@ def account_access(request,topic_id:str): return Response(status=status.HTTP_204_NO_CONTENT) @api_view([PUT]) -def topic_groups_view(request,topic_id:str): +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) From 0f584e712fa8ce73a16bf5bb33726e0edb62d7c6 Mon Sep 17 00:00:00 2001 From: KanonKC Date: Sun, 7 Jan 2024 16:07:03 +0700 Subject: [PATCH 09/15] Public collections show only permitted collections --- .../get_all_collections_by_account.py | 24 ++++++++++++------- api/controllers/group/create_group.py | 2 ++ .../group/get_all_groups_by_account.py | 5 ++++ .../topic/get_all_topics_by_account.py | 2 +- api/controllers/topic/get_topic_public.py | 8 ++++++- api/serializers.py | 9 ++++++- 6 files changed, 39 insertions(+), 11 deletions(-) diff --git a/api/controllers/collection/get_all_collections_by_account.py b/api/controllers/collection/get_all_collections_by_account.py index 4b3bd0a..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:str): - 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'] = CollectionProblemPopulateProblemSerializer(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/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/topic/get_all_topics_by_account.py b/api/controllers/topic/get_all_topics_by_account.py index 49ce8c8..417424f 100644 --- a/api/controllers/topic/get_all_topics_by_account.py +++ b/api/controllers/topic/get_all_topics_by_account.py @@ -22,7 +22,7 @@ def get_all_topics_by_account(account:Account,request): personalSerialize = TopicPopulateTopicCollectionPopulateCollectionSerializer(populatedPersonalTopics,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.all().values_list("group",flat=True)).order_by('-updated_date') + 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) diff --git a/api/controllers/topic/get_topic_public.py b/api/controllers/topic/get_topic_public.py index b05513e..0961df4 100644 --- a/api/controllers/topic/get_topic_public.py +++ b/api/controllers/topic/get_topic_public.py @@ -14,10 +14,16 @@ def get_topic_public(topic_id:str,request): 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(group__in=GroupMember.objects.filter(account=account).values_list("group",flat=True),permission_view_collections=True).values_list("collection",flat=True)) for tp in topicCollections: # tp.collection.problems = 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) for cp in collectionProblems: try: diff --git a/api/serializers.py b/api/serializers.py index 92816fc..6f0bcd2 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -319,4 +319,11 @@ class CollectionPopulateCollectionProblemsPopulateProblemAndCollectionGroupPermi class Meta: model = Collection fields = "__all__" - include = ['problems','group_permissions'] \ No newline at end of file + include = ['problems','group_permissions'] + +class CollectionPopulateCollectionProblemsPopulateProblemSerializer(serializers.ModelSerializer): + problems = CollectionProblemPopulateProblemSerializer(many=True) + class Meta: + model = Collection + fields = "__all__" + include = ['problems'] \ No newline at end of file From 288a1eb0f2d61b7ddb8286e9b26c991cb9da62d4 Mon Sep 17 00:00:00 2001 From: KanonKC Date: Tue, 9 Jan 2024 03:01:23 +0700 Subject: [PATCH 10/15] Get topic populate for collection group permissions --- .../update_group_permissions_collection.py | 2 ++ api/controllers/topic/get_topic.py | 3 ++- api/serializers.py | 16 +++++++++++++++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/api/controllers/collection/update_group_permissions_collection.py b/api/controllers/collection/update_group_permissions_collection.py index d2d8061..58577ab 100644 --- a/api/controllers/collection/update_group_permissions_collection.py +++ b/api/controllers/collection/update_group_permissions_collection.py @@ -12,6 +12,8 @@ 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']) diff --git a/api/controllers/topic/get_topic.py b/api/controllers/topic/get_topic.py index 19d5f41..d8f2b39 100644 --- a/api/controllers/topic/get_topic.py +++ b/api/controllers/topic/get_topic.py @@ -15,8 +15,9 @@ def get_topic(topic:Topic): 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 = TopicPopulateTopicCollectionPopulateCollectionPopulateCollectionProblemPopulateProblemAndTopicGroupPermissionPopulateGroupSerializer(topic) + serialize = TopicPopulateTopicCollectionPopulateCollectionPopulateCollectionProblemsPopulateProblemAndCollectionGroupPermissionsPopulateGroupAndTopicGroupPermissionPopulateGroupSerializer(topic) return Response(serialize.data,status=status.HTTP_200_OK) diff --git a/api/serializers.py b/api/serializers.py index 6f0bcd2..289b4dd 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -326,4 +326,18 @@ class CollectionPopulateCollectionProblemsPopulateProblemSerializer(serializers. class Meta: model = Collection fields = "__all__" - include = ['problems'] \ No newline at end of file + 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'] \ No newline at end of file From 355bdf48971ee75f519a94678ee04c5cb2e5904e Mon Sep 17 00:00:00 2001 From: KanonKC Date: Thu, 11 Jan 2024 01:47:06 +0700 Subject: [PATCH 11/15] Get problem included group permissions --- api/controllers/problem/get_problem.py | 25 ++++++++++++++++--------- api/serializers.py | 19 ++++++++++++++++++- api/views/problem.py | 5 +++-- 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/api/controllers/problem/get_problem.py b/api/controllers/problem/get_problem.py index 664a957..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:str): - 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/serializers.py b/api/serializers.py index 289b4dd..c4f8154 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -56,6 +56,8 @@ class Meta: model = Problem fields = "__all__" + + class TopicCollectionSerializer(serializers.ModelSerializer): class Meta: model = TopicCollection @@ -340,4 +342,19 @@ class TopicPopulateTopicCollectionPopulateCollectionPopulateCollectionProblemsPo class Meta: model = Topic fields = "__all__" - include = ['collections','group_permissions'] \ No newline at end of file + 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'] \ No newline at end of file diff --git a/api/views/problem.py b/api/views/problem.py index d42fb6c..732c6af 100644 --- a/api/views/problem.py +++ b/api/views/problem.py @@ -28,9 +28,10 @@ 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:str,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) elif request.method == DELETE: From 2ebb12d221fc6a9b4d5e81898f58f49c2f966069 Mon Sep 17 00:00:00 2001 From: KanonKC Date: Thu, 11 Jan 2024 10:20:03 +0700 Subject: [PATCH 12/15] Manage problem permissions api --- .../problem/get_all_problems_by_account.py | 16 ++++++---- .../update_group_permission_to_problem.py | 30 +++++++++++++++++++ api/urls.py | 1 + api/views/problem.py | 9 +++++- 4 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 api/controllers/problem/update_group_permission_to_problem.py diff --git a/api/controllers/problem/get_all_problems_by_account.py b/api/controllers/problem/get_all_problems_by_account.py index 27dd562..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:str): - 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/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/urls.py b/api/urls.py index 1e6aa11..f4c9a13 100644 --- a/api/urls.py +++ b/api/urls.py @@ -14,6 +14,7 @@ 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), diff --git a/api/views/problem.py b/api/views/problem.py index 732c6af..69439fd 100644 --- a/api/views/problem.py +++ b/api/views/problem.py @@ -18,6 +18,7 @@ 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 * # Create your views here. @api_view([POST,GET]) @@ -61,4 +62,10 @@ def validation_view(request): @api_view([GET]) 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 From 6fcafa8a30f61eeaa00e8e1b57013af5e3c429fe Mon Sep 17 00:00:00 2001 From: KanonKC Date: Thu, 11 Jan 2024 12:41:23 +0700 Subject: [PATCH 13/15] Get problems in collection populated problem groups permissions --- api/controllers/collection/get_collection.py | 13 ++++++------- api/controllers/topic/get_topic.py | 2 +- api/serializers.py | 18 +++++++++++++++++- api/views/collection.py | 3 ++- 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/api/controllers/collection/get_collection.py b/api/controllers/collection/get_collection.py index 3a01e51..8e87013 100644 --- a/api/controllers/collection/get_collection.py +++ b/api/controllers/collection/get_collection.py @@ -8,17 +8,16 @@ from django.forms.models import model_to_dict from ...serializers import * -def get_collection(collection_id:str): +def get_collection(collection:Collection): - collection = Collection.objects.get(collection_id=collection_id) - # problems = Problem.objects.filter(collectionproblem__collection_id=collection_id) collection.problems = CollectionProblem.objects.filter(collection=collection).order_by('order') collection.group_permissions = CollectionGroupPermission.objects.filter(collection=collection) - - # collection_ser = CollectionSerializer(collection) - # collectionProblems_ser = CollectionProblemPopulateProblemSecureSerializer(collectionProblems,many=True) - serializer = CollectionPopulateCollectionProblemsPopulateProblemAndCollectionGroupPermissionsPopulateGroupSerializer(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) return Response(serializer.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 d8f2b39..53f6e86 100644 --- a/api/controllers/topic/get_topic.py +++ b/api/controllers/topic/get_topic.py @@ -11,7 +11,7 @@ def get_topic(topic:Topic): topic.group_permissions = TopicGroupPermission.objects.filter(topic=topic) - topic.collections = TopicCollection.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) diff --git a/api/serializers.py b/api/serializers.py index c4f8154..45e83e5 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -323,6 +323,8 @@ class Meta: fields = "__all__" include = ['problems','group_permissions'] + + class CollectionPopulateCollectionProblemsPopulateProblemSerializer(serializers.ModelSerializer): problems = CollectionProblemPopulateProblemSerializer(many=True) class Meta: @@ -357,4 +359,18 @@ class ProblemPopulateAccountAndTestcasesAndProblemGroupPermissionsPopulateGroupS class Meta: model = Problem fields = "__all__" - include = ['creator','group_permissions','testcases'] \ No newline at end of file + 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/views/collection.py b/api/views/collection.py index 3db6fcf..661644d 100644 --- a/api/views/collection.py +++ b/api/views/collection.py @@ -30,8 +30,9 @@ def all_collections_creator_view(request,account_id:str): @api_view([GET,PUT,DELETE]) 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) if request.method == DELETE: From b55a676be6cc589bdd509f406305bd9762acc14e Mon Sep 17 00:00:00 2001 From: KanonKC Date: Thu, 11 Jan 2024 14:02:39 +0700 Subject: [PATCH 14/15] Get topic public can check permission at problem level --- api/controllers/topic/get_topic_public.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/api/controllers/topic/get_topic_public.py b/api/controllers/topic/get_topic_public.py index 0961df4..bf948ec 100644 --- a/api/controllers/topic/get_topic_public.py +++ b/api/controllers/topic/get_topic_public.py @@ -24,7 +24,8 @@ def get_topic_public(topic_id:str,request): # if len(viewPermission) == 0: # tp.collection.problems = [] # continue - collectionProblems = CollectionProblem.objects.filter(collection=tp.collection) + collectionProblems = CollectionProblem.objects.filter(collection=tp.collection,problem__in=ProblemGroupPermission.objects.filter(group__in=GroupMember.objects.filter(account=account).values_list("group",flat=True),permission_view_problems=True).values_list("problem",flat=True)) + for cp in collectionProblems: try: best_submission = BestSubmission.objects.get(problem=cp.problem,account=account,topic=topic) From 15b11f956ef38b23da5b18cc19b1f61b16055df9 Mon Sep 17 00:00:00 2001 From: KanonKC Date: Fri, 12 Jan 2024 20:20:13 +0700 Subject: [PATCH 15/15] Update delete api --- .../collection/update_collection.py | 3 +-- api/controllers/problem/create_problem.py | 3 ++- api/controllers/problem/delete_problem.py | 10 +++---- api/controllers/problem/get_problem_public.py | 14 ++++++++++ api/controllers/problem/update_problem.py | 26 ++++++------------- api/controllers/topic/delete_topic.py | 1 - .../get_all_accessed_topics_by_account.py | 3 ++- api/controllers/topic/get_topic_public.py | 21 +++++++++++++-- .../0052_problem_allowed_languages.py | 18 +++++++++++++ api/models.py | 1 + api/serializers.py | 19 +++++++++----- api/views/collection.py | 4 +-- api/views/problem.py | 9 ++++--- 13 files changed, 91 insertions(+), 41 deletions(-) create mode 100644 api/controllers/problem/get_problem_public.py create mode 100644 api/migrations/0052_problem_allowed_languages.py diff --git a/api/controllers/collection/update_collection.py b/api/controllers/collection/update_collection.py index aa2df34..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:str,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/problem/create_problem.py b/api/controllers/problem/create_problem.py index 16fb7a5..efcf4f6 100644 --- a/api/controllers/problem/create_problem.py +++ b/api/controllers/problem/create_problem.py @@ -22,7 +22,8 @@ def create_problem(account_id:str,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 bcc75f2..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:str): - 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_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_problem.py b/api/controllers/problem/update_problem.py index d60f98a..cd4a1a5 100644 --- a/api/controllers/problem/update_problem.py +++ b/api/controllers/problem/update_problem.py @@ -9,14 +9,12 @@ from ...serializers import * from django.utils import timezone -def update_problem(problem_id:str,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) - - print("AAA") +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) @@ -24,50 +22,42 @@ def update_problem(problem_id:str,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() - print("BBB") - if 'testcases' in request.data: running_result = PythonGrader(problem.solution,request.data['testcases'],1,1.5).generate_output() # if not running_result.runnable: # return Response({'detail': 'Error during editing. Your code may has an error/timeout!'},status=status.HTTP_406_NOT_ACCEPTABLE) - print("ZZZZZ") for testcase in testcases: testcase.deprecated = True testcase.save() - print("ZZZZZ") testcase_result = [] for unit in running_result.data: - print("YYYYY") testcase2 = Testcase( problem = problem, input = unit.input, output = unit.output, runtime_status = unit.runtime_status ) - print("YYYYY",testcase2.testcase_id) testcase2.save() - print("YYYYY") testcase_result.append(testcase2) problem.save() - print("ZZZZZ") 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) if 'solution' in request.data: - testcases = Testcase.objects.filter(problem_id=problem_id,deprecated=False) + 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() if not running_result.runnable: return Response({'detail': 'Error during editing. Your code may has an error/timeout!'},status=status.HTTP_406_NOT_ACCEPTABLE) - print("CCC") problem.save() problem_serialize = ProblemSerializer(problem) return Response(problem_serialize.data,status=status.HTTP_201_CREATED) diff --git a/api/controllers/topic/delete_topic.py b/api/controllers/topic/delete_topic.py index 6d1db33..622c052 100644 --- a/api/controllers/topic/delete_topic.py +++ b/api/controllers/topic/delete_topic.py @@ -9,6 +9,5 @@ from ...serializers import * def delete_topic(topic:Topic): - topic = Topic.objects.get(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 index 28753e0..68a92fc 100644 --- a/api/controllers/topic/get_all_accessed_topics_by_account.py +++ b/api/controllers/topic/get_all_accessed_topics_by_account.py @@ -7,10 +7,11 @@ 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(group__in=groups,permission_view_topics=True).distinct() + 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) diff --git a/api/controllers/topic/get_topic_public.py b/api/controllers/topic/get_topic_public.py index bf948ec..36476b2 100644 --- a/api/controllers/topic/get_topic_public.py +++ b/api/controllers/topic/get_topic_public.py @@ -7,6 +7,7 @@ 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:str,request): @@ -14,7 +15,17 @@ def get_topic_public(topic_id:str,request): topic = Topic.objects.get(topic_id=topic_id) account = Account.objects.get(account_id=account_id) - topicCollections = TopicCollection.objects.filter(topic=topic,collection__in=CollectionGroupPermission.objects.filter(group__in=GroupMember.objects.filter(account=account).values_list("group",flat=True),permission_view_collections=True).values_list("collection",flat=True)) + + + 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) @@ -24,7 +35,13 @@ def get_topic_public(topic_id:str,request): # if len(viewPermission) == 0: # tp.collection.problems = [] # continue - collectionProblems = CollectionProblem.objects.filter(collection=tp.collection,problem__in=ProblemGroupPermission.objects.filter(group__in=GroupMember.objects.filter(account=account).values_list("group",flat=True),permission_view_problems=True).values_list("problem",flat=True)) + 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: 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 1f45db5..e30c9a1 100644 --- a/api/models.py +++ b/api/models.py @@ -43,6 +43,7 @@ class Problem(models.Model): 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.CharField(primary_key=True,blank=True,default=generate_uuid4_hex,max_length=32) diff --git a/api/serializers.py b/api/serializers.py index 45e83e5..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() @@ -57,6 +57,13 @@ class Meta: 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: @@ -140,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() diff --git a/api/views/collection.py b/api/views/collection.py index 661644d..424050c 100644 --- a/api/views/collection.py +++ b/api/views/collection.py @@ -34,9 +34,9 @@ def one_collection_creator_view(request,account_id:int,collection_id:str): if request.method == GET: 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): diff --git a/api/views/problem.py b/api/views/problem.py index 69439fd..7016010 100644 --- a/api/views/problem.py +++ b/api/views/problem.py @@ -19,6 +19,8 @@ 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]) @@ -34,9 +36,9 @@ def one_problem_creator_view(request,problem_id:str,account_id:str): if request.method == GET: 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): @@ -47,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: