Quiz

Форк
0
58 строк · 2.1 Кб
1
import json
2
from django.conf import settings
3
from django.core.management.base import BaseCommand
4
from authapp.models import AuthUser
5
from questions.models import Question, Answer, Type, SubType, Category
6

7

8
def load_from_json(file_name):
9
    with open(f'{settings.BASE_DIR}/json/{file_name}.json', 'r', encoding='utf-8') as json_file:
10
        return json.load(json_file)
11

12

13
class Command(BaseCommand):
14

15
    def handle(self, *args, **options):
16
        categories = load_from_json('categories')
17
        types = load_from_json('types')
18
        subtypes = load_from_json('subtypes')
19
        answers = load_from_json('answers')
20
        questions = load_from_json('questions')
21

22
        Category.objects.all().delete()
23
        for category in categories:
24
            Category.objects.create(**category)
25

26
        Type.objects.all().delete()
27
        for type_ in types:
28
            Type.objects.create(**type_)
29

30
        SubType.objects.all().delete()
31
        for subtype in subtypes:
32
            subtype['type'] = Type.objects.get(name=subtype['type'])
33
            SubType.objects.create(**subtype)
34

35
        Answer.objects.all().delete()
36
        for answer in answers:
37
            answer['subtype'] = SubType.objects.get(name=answer['subtype'])
38
            Answer.objects.create(**answer)
39

40
        Question.objects.all().delete()
41
        for question in questions:
42
            question['category'] = Category.objects.get(name=question['category'])
43
            question['answer'] = Answer.objects.get(answer=question['answer'])
44
            Question.objects.create(**question)
45

46
        AuthUser.objects.all().delete()
47

48
        AuthUser.objects.create_superuser(username='pepper', password='pepper123', nickname='pepper',
49
                                          birthdate='2019-01-01', is_moderator=True)
50

51
        for i in range(2, 10):
52
            AuthUser.objects.create_superuser(username=f'pepper{i}', password='pepper123', nickname=f'pepper{i}',
53
                                              birthdate='2019-01-01')
54

55
        pepper = AuthUser.objects.get(username='pepper')
56
        pepper2 = AuthUser.objects.get(username='pepper2')
57
        pepper.friends.add(pepper2)
58
        pepper2.friends.add(pepper)
59

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.