-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add test - Setup GlobalPermission
- Loading branch information
Showing
32 changed files
with
762 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import strawberry | ||
|
||
from utils.strawberry.enums import get_enum_name_from_django_field | ||
|
||
from .models import GlobalPermission | ||
|
||
GlobalPermissionTypeEnum = strawberry.enum(GlobalPermission.Type, name='GlobalPermissionTypeEnum') | ||
|
||
|
||
enum_map = { | ||
get_enum_name_from_django_field(field): enum | ||
for field, enum in ( | ||
(GlobalPermission.type, GlobalPermissionTypeEnum), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from factory.django import DjangoModelFactory | ||
|
||
from .models import GlobalPermission | ||
|
||
|
||
class GlobalPermissionFactory(DjangoModelFactory): | ||
class Meta: | ||
model = GlobalPermission |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Generated by Django 4.2.5 on 2023-10-10 08:50 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='GlobalPermission', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('type', models.SmallIntegerField(choices=[(1, 'Upload Question Bank'), (2, 'Activate Question Bank')], unique=True)), | ||
('users', models.ManyToManyField(blank=True, to=settings.AUTH_USER_MODEL)), | ||
], | ||
), | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
apps/qbank/migrations/0003_questionbank_ended_at_questionbank_errors_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Generated by Django 4.2.5 on 2023-10-06 05:33 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('qbank', '0002_initial'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='questionbank', | ||
name='ended_at', | ||
field=models.DateTimeField(blank=True, null=True), | ||
), | ||
migrations.AddField( | ||
model_name='questionbank', | ||
name='errors', | ||
field=models.JSONField(default=list), | ||
), | ||
migrations.AddField( | ||
model_name='questionbank', | ||
name='started_at', | ||
field=models.DateTimeField(blank=True, null=True), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import strawberry | ||
from asgiref.sync import sync_to_async | ||
from strawberry.types import Info | ||
from django.core.exceptions import ValidationError | ||
from django.shortcuts import get_object_or_404 | ||
|
||
from utils.strawberry.mutations import ( | ||
MutationResponseType, | ||
mutation_is_not_valid, | ||
) | ||
from utils.strawberry.transformers import convert_serializer_to_type | ||
from utils.strawberry.mutations import process_input_data, generate_error_message | ||
|
||
from apps.common.models import GlobalPermission | ||
from .serializers import CreateQuestionBankSerializer | ||
from .types import ( | ||
QuestionBankType, | ||
) | ||
|
||
CreateQuestionBankInput = convert_serializer_to_type(CreateQuestionBankSerializer, name='CreateQuestionBankInput') | ||
|
||
|
||
@strawberry.type | ||
class PrivateMutation: | ||
|
||
@strawberry.mutation | ||
@sync_to_async | ||
def create_question_bank( | ||
self, | ||
data: CreateQuestionBankInput, | ||
info: Info, | ||
) -> MutationResponseType[QuestionBankType]: | ||
if error := info.context.has_global_perm(GlobalPermission.Type.UPLOAD_QBANK): | ||
return MutationResponseType( | ||
ok=False, | ||
errors=generate_error_message(error), | ||
) | ||
serializer = CreateQuestionBankSerializer( | ||
data=process_input_data(data), | ||
context={'request': info.context.request}, | ||
) | ||
if errors := mutation_is_not_valid(serializer): | ||
return MutationResponseType( | ||
ok=False, | ||
errors=errors, | ||
) | ||
instance = serializer.save() | ||
return MutationResponseType( | ||
result=instance, | ||
) | ||
|
||
@strawberry.mutation | ||
@sync_to_async | ||
def activate_question_bank( | ||
self, | ||
id: strawberry.ID, | ||
info: Info, | ||
) -> MutationResponseType[QuestionBankType]: | ||
if error := info.context.has_global_perm(GlobalPermission.Type.ACTIVATE_QBANK): | ||
return MutationResponseType( | ||
ok=False, | ||
errors=generate_error_message(error), | ||
) | ||
queryset = QuestionBankType.get_queryset(None, None, info) | ||
qbank = get_object_or_404(queryset, id=id) | ||
try: | ||
qbank.activate() | ||
except ValidationError as e: | ||
return MutationResponseType( | ||
ok=False, | ||
errors=generate_error_message(str(e)), | ||
) | ||
return MutationResponseType( | ||
result=qbank, | ||
ok=True, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from django.db import transaction | ||
from rest_framework import serializers | ||
|
||
from apps.common.serializers import UserResourceSerializer | ||
from apps.qbank.models import QuestionBank | ||
from apps.qbank.tasks import import_task | ||
|
||
|
||
class CreateQuestionBankSerializer(UserResourceSerializer): | ||
class Meta: | ||
model = QuestionBank | ||
fields = ( | ||
'title', | ||
'description', | ||
'import_file', | ||
) | ||
|
||
def validate_import_file(self, import_file): | ||
# Basic extension check | ||
if not import_file.name.endswith('.xlsx'): | ||
raise serializers.ValidationError('Only XLSX file allowed. ') | ||
return import_file | ||
|
||
def update(self, _): | ||
raise Exception('Update not allowed') | ||
|
||
def create(self, data): | ||
instance = super().create(data) | ||
# Trigger import | ||
transaction.on_commit( | ||
lambda: import_task.delay(instance.pk) | ||
) | ||
return instance |
Oops, something went wrong.