Quiz
58 строк · 2.1 Кб
1import json
2from django.conf import settings
3from django.core.management.base import BaseCommand
4from authapp.models import AuthUser
5from questions.models import Question, Answer, Type, SubType, Category
6
7
8def load_from_json(file_name):
9with open(f'{settings.BASE_DIR}/json/{file_name}.json', 'r', encoding='utf-8') as json_file:
10return json.load(json_file)
11
12
13class Command(BaseCommand):
14
15def handle(self, *args, **options):
16categories = load_from_json('categories')
17types = load_from_json('types')
18subtypes = load_from_json('subtypes')
19answers = load_from_json('answers')
20questions = load_from_json('questions')
21
22Category.objects.all().delete()
23for category in categories:
24Category.objects.create(**category)
25
26Type.objects.all().delete()
27for type_ in types:
28Type.objects.create(**type_)
29
30SubType.objects.all().delete()
31for subtype in subtypes:
32subtype['type'] = Type.objects.get(name=subtype['type'])
33SubType.objects.create(**subtype)
34
35Answer.objects.all().delete()
36for answer in answers:
37answer['subtype'] = SubType.objects.get(name=answer['subtype'])
38Answer.objects.create(**answer)
39
40Question.objects.all().delete()
41for question in questions:
42question['category'] = Category.objects.get(name=question['category'])
43question['answer'] = Answer.objects.get(answer=question['answer'])
44Question.objects.create(**question)
45
46AuthUser.objects.all().delete()
47
48AuthUser.objects.create_superuser(username='pepper', password='pepper123', nickname='pepper',
49birthdate='2019-01-01', is_moderator=True)
50
51for i in range(2, 10):
52AuthUser.objects.create_superuser(username=f'pepper{i}', password='pepper123', nickname=f'pepper{i}',
53birthdate='2019-01-01')
54
55pepper = AuthUser.objects.get(username='pepper')
56pepper2 = AuthUser.objects.get(username='pepper2')
57pepper.friends.add(pepper2)
58pepper2.friends.add(pepper)
59