feat [P-227]: картинка для теста
This commit is contained in:
1 parent
c68af12398
commit
765ebc481b
5 files changed
+191
-10
No files matched your search
@@ -137,9 +137,8 @@ class TestCreate(APIView):
|
|||||||
user = request.user
|
user = request.user
|
||||||
|
|
||||||
test_image = (
|
test_image = (
|
||||||
request.data.get["image"]["name"]
|
request.FILES.get("image")
|
||||||
if "image" in request.data and request.data["image"] is not None
|
if "image" in request.FILES else None
|
||||||
else None
|
|
||||||
)
|
)
|
||||||
|
|
||||||
test = Test.objects.create(
|
test = Test.objects.create(
|
||||||
|
|||||||
@@ -35,8 +35,16 @@
|
|||||||
hide-details
|
hide-details
|
||||||
label="Картинка"
|
label="Картинка"
|
||||||
variant="outlined"
|
variant="outlined"
|
||||||
|
@update:model-value="() => previewAnswerImage(answer, aIndex)"
|
||||||
></v-file-input>
|
></v-file-input>
|
||||||
|
|
||||||
|
<!-- Превью изображения ответа, если оно загружено -->
|
||||||
|
<div v-if="answer.imagePreview" class="ml-2">
|
||||||
|
<v-avatar size="32">
|
||||||
|
<v-img :src="answer.imagePreview" alt="Превью"></v-img>
|
||||||
|
</v-avatar>
|
||||||
|
</div>
|
||||||
|
|
||||||
<v-btn
|
<v-btn
|
||||||
class="ms-1"
|
class="ms-1"
|
||||||
color="error"
|
color="error"
|
||||||
@@ -95,9 +103,31 @@ export default {
|
|||||||
answer.right_wrong = !answer.right_wrong;
|
answer.right_wrong = !answer.right_wrong;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Функция для предпросмотра изображения ответа
|
||||||
|
const previewAnswerImage = (answer, index) => {
|
||||||
|
if (answer.image) {
|
||||||
|
const reader = new FileReader();
|
||||||
|
reader.onload = (e) => {
|
||||||
|
// Vue не всегда реактивно отслеживает новые свойства,
|
||||||
|
// поэтому используем явную установку свойства
|
||||||
|
if (!answer.hasOwnProperty('imagePreview')) {
|
||||||
|
answer.imagePreview = e.target.result;
|
||||||
|
} else {
|
||||||
|
answer.imagePreview = e.target.result;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
reader.readAsDataURL(answer.image);
|
||||||
|
} else {
|
||||||
|
if (answer.hasOwnProperty('imagePreview')) {
|
||||||
|
answer.imagePreview = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
isTestRoute,
|
isTestRoute,
|
||||||
toggleCorrect,
|
toggleCorrect,
|
||||||
|
previewAnswerImage,
|
||||||
isTest: props.isTest
|
isTest: props.isTest
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,14 +4,25 @@
|
|||||||
<QuestionHeader :question="question" :rules="rules" :index="index" :removeQuestion="removeQuestion" />
|
<QuestionHeader :question="question" :rules="rules" :index="index" :removeQuestion="removeQuestion" />
|
||||||
|
|
||||||
<!-- Загрузка изображения для вопроса -->
|
<!-- Загрузка изображения для вопроса -->
|
||||||
|
<div class="d-flex align-center">
|
||||||
<v-file-input
|
<v-file-input
|
||||||
v-model="question.image"
|
v-model="question.image"
|
||||||
accept="image/*"
|
accept="image/*"
|
||||||
density="comfortable"
|
density="comfortable"
|
||||||
label="Картинка для вопроса"
|
label="Картинка для вопроса"
|
||||||
variant="outlined"
|
variant="outlined"
|
||||||
|
class="flex-grow-1"
|
||||||
|
@update:model-value="previewQuestionImage"
|
||||||
></v-file-input>
|
></v-file-input>
|
||||||
|
|
||||||
|
<!-- Превью изображения вопроса, если оно загружено -->
|
||||||
|
<div v-if="questionImagePreview" class="ml-3">
|
||||||
|
<v-avatar size="40" class="ml-2">
|
||||||
|
<v-img :src="questionImagePreview" alt="Превью вопроса"></v-img>
|
||||||
|
</v-avatar>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<v-divider></v-divider>
|
<v-divider></v-divider>
|
||||||
|
|
||||||
<!-- Настройки вопроса -->
|
<!-- Настройки вопроса -->
|
||||||
@@ -145,7 +156,7 @@
|
|||||||
import QuestionHeader from '@/components/vote/ConstructorVote/QuestionHeader.vue';
|
import QuestionHeader from '@/components/vote/ConstructorVote/QuestionHeader.vue';
|
||||||
import QuestionSettings from '@/components/vote/ConstructorVote/QuestionSettings.vue';
|
import QuestionSettings from '@/components/vote/ConstructorVote/QuestionSettings.vue';
|
||||||
import AnswerList from '@/components/vote/ConstructorVote/AnswerList.vue';
|
import AnswerList from '@/components/vote/ConstructorVote/AnswerList.vue';
|
||||||
import { computed } from 'vue';
|
import { computed, ref } from 'vue';
|
||||||
import { useRoute } from 'vue-router';
|
import { useRoute } from 'vue-router';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
@@ -212,12 +223,26 @@ export default {
|
|||||||
emits: ['update:question'],
|
emits: ['update:question'],
|
||||||
setup(props, { emit }) {
|
setup(props, { emit }) {
|
||||||
const route = useRoute();
|
const route = useRoute();
|
||||||
|
const questionImagePreview = ref(null);
|
||||||
|
|
||||||
const resetAnswers = () => {
|
const resetAnswers = () => {
|
||||||
props.question.answers = [];
|
props.question.answers = [];
|
||||||
emit('update:question', props.question);
|
emit('update:question', props.question);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Функция предпросмотра изображения вопроса
|
||||||
|
const previewQuestionImage = () => {
|
||||||
|
if (props.question.image) {
|
||||||
|
const reader = new FileReader();
|
||||||
|
reader.onload = (e) => {
|
||||||
|
questionImagePreview.value = e.target.result;
|
||||||
|
};
|
||||||
|
reader.readAsDataURL(props.question.image);
|
||||||
|
} else {
|
||||||
|
questionImagePreview.value = null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const addAnswer = () => {
|
const addAnswer = () => {
|
||||||
props.question.answers.push({ text: '', image: null });
|
props.question.answers.push({ text: '', image: null });
|
||||||
emit('update:question', props.question);
|
emit('update:question', props.question);
|
||||||
@@ -263,7 +288,9 @@ export default {
|
|||||||
toggleSelectAll,
|
toggleSelectAll,
|
||||||
removeEmployeeAnswer,
|
removeEmployeeAnswer,
|
||||||
onEmployeesChange,
|
onEmployeesChange,
|
||||||
isTestRoute
|
isTestRoute,
|
||||||
|
questionImagePreview,
|
||||||
|
previewQuestionImage
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -6,8 +6,7 @@
|
|||||||
</v-btn>
|
</v-btn>
|
||||||
</CreatedVote>
|
</CreatedVote>
|
||||||
<template v-else>
|
<template v-else>
|
||||||
<div class="v-col flex-grow-1 flex-shrink-0">
|
<div class="v-col flex-grow-1" ><v-stepper v-model="step" class="d-flex flex-column fill-height" editable>
|
||||||
<v-stepper v-model="step" class="d-flex flex-column fill-height" editable>
|
|
||||||
<v-stepper-header class="flex-grow-0 flex-shrink-1">
|
<v-stepper-header class="flex-grow-0 flex-shrink-1">
|
||||||
<v-stepper-item :complete="step > 0" step="1">Основная информация</v-stepper-item>
|
<v-stepper-item :complete="step > 0" step="1">Основная информация</v-stepper-item>
|
||||||
<v-divider></v-divider>
|
<v-divider></v-divider>
|
||||||
@@ -63,6 +62,38 @@
|
|||||||
class="ml-2"
|
class="ml-2"
|
||||||
></v-text-field>
|
></v-text-field>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- Загрузка основного изображения теста -->
|
||||||
|
<div class="mt-4">
|
||||||
|
<v-file-input
|
||||||
|
v-model="testImage"
|
||||||
|
accept="image/*"
|
||||||
|
density="comfortable"
|
||||||
|
label="Изображение теста"
|
||||||
|
variant="outlined"
|
||||||
|
prepend-icon="mdi-image"
|
||||||
|
placeholder="Выберите изображение для теста"
|
||||||
|
hint="Логотип или изображение для теста"
|
||||||
|
:show-size="true"
|
||||||
|
@update:model-value="previewImage"
|
||||||
|
></v-file-input>
|
||||||
|
|
||||||
|
<!-- Превью изображения -->
|
||||||
|
<div v-if="imagePreview" class="d-flex justify-center mt-3">
|
||||||
|
<v-img
|
||||||
|
:src="imagePreview"
|
||||||
|
max-height="200"
|
||||||
|
max-width="300"
|
||||||
|
contain
|
||||||
|
>
|
||||||
|
<template v-slot:placeholder>
|
||||||
|
<v-row class="fill-height ma-0" align="center" justify="center">
|
||||||
|
<v-progress-circular indeterminate color="grey-lighten-5"></v-progress-circular>
|
||||||
|
</v-row>
|
||||||
|
</template>
|
||||||
|
</v-img>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</v-card-text>
|
</v-card-text>
|
||||||
</template>
|
</template>
|
||||||
<v-card-actions>
|
<v-card-actions>
|
||||||
@@ -141,6 +172,29 @@
|
|||||||
<strong class="px-2">Дата окончания:</strong>
|
<strong class="px-2">Дата окончания:</strong>
|
||||||
{{ form.endDate.toLocaleDateString() }}
|
{{ form.endDate.toLocaleDateString() }}
|
||||||
</p>
|
</p>
|
||||||
|
<!-- Показываем информацию о тесте, если это тест -->
|
||||||
|
<template v-if="isTest">
|
||||||
|
<p><strong class="px-2">Минимальный процент для сдачи:</strong> {{ testSettings.pass_perc }}%</p>
|
||||||
|
<p><strong class="px-2">Время прохождения (мин):</strong> {{ testSettings.time }}</p>
|
||||||
|
<p><strong class="px-2">Количество вопросов:</strong> {{ testSettings.question_count }}</p>
|
||||||
|
<p><strong class="px-2">Количество попыток:</strong> {{ testSettings.attemps_number }}</p>
|
||||||
|
|
||||||
|
<!-- Превью изображения теста на странице подтверждения -->
|
||||||
|
<div class="mt-2 mb-4">
|
||||||
|
<p><strong class="px-2">Изображение теста:</strong></p>
|
||||||
|
<div v-if="imagePreview" class="d-flex justify-center mt-2">
|
||||||
|
<v-img
|
||||||
|
:src="imagePreview"
|
||||||
|
max-height="150"
|
||||||
|
max-width="250"
|
||||||
|
contain
|
||||||
|
class="border rounded"
|
||||||
|
></v-img>
|
||||||
|
</div>
|
||||||
|
<p v-else class="text-grey text-center mt-2">Не выбрано</p>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
<p><strong class="px-2">Вопросы:</strong></p>
|
<p><strong class="px-2">Вопросы:</strong></p>
|
||||||
|
|
||||||
<v-card
|
<v-card
|
||||||
@@ -154,13 +208,44 @@
|
|||||||
question.type
|
question.type
|
||||||
}})
|
}})
|
||||||
</p>
|
</p>
|
||||||
|
<!-- Показываем превью изображения вопроса, если оно есть -->
|
||||||
|
<div v-if="question.image" class="mt-2 mb-2">
|
||||||
|
<img
|
||||||
|
v-if="typeof question.image === 'object' && question.image !== null"
|
||||||
|
:src="getImagePreviewUrl(question.image)"
|
||||||
|
alt="Изображение вопроса"
|
||||||
|
style="max-width: 150px; max-height: 100px; object-fit: contain;"
|
||||||
|
class="ml-3 border-thin"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div v-if="question.settings.choice === 'free' && question.answers.length">
|
<div v-if="question.settings.choice === 'free' && question.answers.length">
|
||||||
<p><strong>Варианты ответов:</strong></p>
|
<p><strong>Варианты ответов:</strong></p>
|
||||||
|
|
||||||
<ul>
|
<ul>
|
||||||
<li v-for="(answer, answerIndex) in question.answers" :key="answerIndex">
|
<li v-for="(answer, answerIndex) in question.answers" :key="answerIndex" class="mb-2">
|
||||||
{{ answer.text }}
|
<div class="d-flex align-center">
|
||||||
|
<span>{{ answer.text }}</span>
|
||||||
|
<!-- Для тестов показываем правильность ответа -->
|
||||||
|
<v-icon
|
||||||
|
v-if="isTest"
|
||||||
|
:color="answer.right_wrong ? 'green' : 'red'"
|
||||||
|
class="ml-2"
|
||||||
|
small
|
||||||
|
>
|
||||||
|
{{ answer.right_wrong ? 'mdi-check-circle' : 'mdi-close-circle' }}
|
||||||
|
</v-icon>
|
||||||
|
<!-- Показываем превью изображения ответа, если оно есть -->
|
||||||
|
<div v-if="answer.image" class="ml-2">
|
||||||
|
<img
|
||||||
|
v-if="typeof answer.image === 'object' && answer.image !== null"
|
||||||
|
:src="getImagePreviewUrl(answer.image)"
|
||||||
|
alt="Изображение ответа"
|
||||||
|
style="max-width: 100px; max-height: 60px; object-fit: contain;"
|
||||||
|
class="ml-1 border-thin"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
@@ -241,6 +326,9 @@ export default {
|
|||||||
attemps_number: '3',
|
attemps_number: '3',
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const testImage = ref(null); // Для хранения основного изображения теста
|
||||||
|
const imagePreview = ref(null); // Для превью изображения
|
||||||
|
|
||||||
const rules = {
|
const rules = {
|
||||||
required: (value) => !!value || 'Это поле обязательно для заполнения',
|
required: (value) => !!value || 'Это поле обязательно для заполнения',
|
||||||
positive: (value) => parseInt(value) > 0 || 'Должно быть положительное число',
|
positive: (value) => parseInt(value) > 0 || 'Должно быть положительное число',
|
||||||
@@ -361,7 +449,7 @@ export default {
|
|||||||
description: form.value.description,
|
description: form.value.description,
|
||||||
datestart: form.value.startDate,
|
datestart: form.value.startDate,
|
||||||
dateend: form.value.endDate,
|
dateend: form.value.endDate,
|
||||||
image: null,
|
image: testImage.value, // Используем загруженное изображение теста
|
||||||
pass_perc: Number(testSettings.value.pass_perc),
|
pass_perc: Number(testSettings.value.pass_perc),
|
||||||
time: Number(testSettings.value.time),
|
time: Number(testSettings.value.time),
|
||||||
question_count: Number(testSettings.value.question_count),
|
question_count: Number(testSettings.value.question_count),
|
||||||
@@ -391,6 +479,19 @@ export default {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Функция для предпросмотра загруженного изображения
|
||||||
|
const previewImage = () => {
|
||||||
|
if (testImage.value) {
|
||||||
|
const reader = new FileReader();
|
||||||
|
reader.onload = (e) => {
|
||||||
|
imagePreview.value = e.target.result;
|
||||||
|
};
|
||||||
|
reader.readAsDataURL(testImage.value);
|
||||||
|
} else {
|
||||||
|
imagePreview.value = null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const resetForm = () => {
|
const resetForm = () => {
|
||||||
form.value = {
|
form.value = {
|
||||||
title: '',
|
title: '',
|
||||||
@@ -407,6 +508,8 @@ export default {
|
|||||||
question_count: '10',
|
question_count: '10',
|
||||||
attemps_number: '3',
|
attemps_number: '3',
|
||||||
};
|
};
|
||||||
|
testImage.value = null;
|
||||||
|
imagePreview.value = null;
|
||||||
step.value = 0;
|
step.value = 0;
|
||||||
isVoteCreated.value = false;
|
isVoteCreated.value = false;
|
||||||
};
|
};
|
||||||
@@ -463,11 +566,28 @@ export default {
|
|||||||
|
|
||||||
const quests = computed(() => form.value.questions.map((question) => question.text.length > 0));
|
const quests = computed(() => form.value.questions.map((question) => question.text.length > 0));
|
||||||
|
|
||||||
|
// Функция для получения URL превью изображения для объектов File
|
||||||
|
const getImagePreviewUrl = (file) => {
|
||||||
|
if (file instanceof File) {
|
||||||
|
// Если у нас уже есть кэшированный URL для этого файла, вернем его
|
||||||
|
if (!file._cachedUrl) {
|
||||||
|
file._cachedUrl = URL.createObjectURL(file);
|
||||||
|
}
|
||||||
|
return file._cachedUrl;
|
||||||
|
}
|
||||||
|
return '';
|
||||||
|
};
|
||||||
|
|
||||||
// Добавляем начальный вопрос при монтировании компонента, если мы в режиме теста
|
// Добавляем начальный вопрос при монтировании компонента, если мы в режиме теста
|
||||||
if (props.isTest && form.value.questions.length === 0) {
|
if (props.isTest && form.value.questions.length === 0) {
|
||||||
addQuestion();
|
addQuestion();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Автоматически обновляем превью при смене testImage
|
||||||
|
watch(testImage, () => {
|
||||||
|
previewImage();
|
||||||
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
quests,
|
quests,
|
||||||
onlyDepdict,
|
onlyDepdict,
|
||||||
@@ -489,6 +609,10 @@ export default {
|
|||||||
handleSelectAll,
|
handleSelectAll,
|
||||||
isAllSelected,
|
isAllSelected,
|
||||||
testSettings,
|
testSettings,
|
||||||
|
testImage,
|
||||||
|
imagePreview,
|
||||||
|
previewImage,
|
||||||
|
getImagePreviewUrl,
|
||||||
isTest: props.isTest
|
isTest: props.isTest
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -203,6 +203,7 @@ async function createTest(testData) {
|
|||||||
try {
|
try {
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
|
|
||||||
|
// Добавляем основное изображение теста, если есть
|
||||||
if (testData.image) {
|
if (testData.image) {
|
||||||
formData.append('image', testData.image);
|
formData.append('image', testData.image);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user