2017-03-19 14:46:08 +01:00
|
|
|
from textwrap import dedent
|
2018-10-28 10:04:52 +01:00
|
|
|
from typing import Optional
|
2017-03-19 14:46:08 +01:00
|
|
|
|
|
|
|
from django.contrib.auth.hashers import make_password
|
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
2017-08-09 14:27:39 +02:00
|
|
|
from django.db import transaction
|
|
|
|
from django.db.utils import IntegrityError
|
2017-03-19 14:46:08 +01:00
|
|
|
from django.utils.crypto import get_random_string
|
|
|
|
|
2019-08-08 12:47:47 +02:00
|
|
|
from openslides.agenda.models import Item, ListOfSpeakers
|
2017-03-19 14:46:08 +01:00
|
|
|
from openslides.assignments.models import Assignment
|
|
|
|
from openslides.motions.models import Motion
|
|
|
|
from openslides.topics.models import Topic
|
|
|
|
from openslides.users.models import Group, User
|
2019-08-20 12:00:54 +02:00
|
|
|
from openslides.utils.startup import run_startup_hooks
|
2017-03-19 14:46:08 +01:00
|
|
|
|
2018-07-09 23:22:26 +02:00
|
|
|
|
2017-03-19 14:46:08 +01:00
|
|
|
MOTION_NUMBER_OF_PARAGRAPHS = 4
|
|
|
|
|
|
|
|
LOREM_IPSUM = [
|
|
|
|
"""\
|
|
|
|
<p>Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod
|
|
|
|
tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim
|
|
|
|
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea
|
|
|
|
commodi consequat. Quis aute iure reprehenderit in voluptate velit esse
|
|
|
|
cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat
|
|
|
|
cupiditat non proident, sunt in culpa qui officia deserunt mollit anim
|
2019-01-06 16:22:33 +01:00
|
|
|
id est laborum.</p>""".replace(
|
|
|
|
"\n", " "
|
|
|
|
),
|
2017-03-19 14:46:08 +01:00
|
|
|
"""\
|
|
|
|
<p>Sed ut perspiciatis, unde omnis iste natus error sit voluptatem
|
|
|
|
accusantium doloremque laudantium, totam rem aperiam eaque ipsa, quae
|
|
|
|
ab illo inventore veritatis et quasi architecto beatae vitae dicta
|
|
|
|
sunt, explicabo. Nemo enim ipsam voluptatem, quia voluptas sit,
|
|
|
|
aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos,
|
|
|
|
qui ratione voluptatem sequi nesciunt, neque porro quisquam est, qui
|
|
|
|
dolorem ipsum, quia dolor sit amet consectetur adipisci[ng] velit, sed
|
|
|
|
quia non numquam [do] eius modi tempora inci[di]dunt, ut labore et
|
|
|
|
dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam,
|
|
|
|
quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut
|
|
|
|
aliquid ex ea commodi consequatur? Quis autem vel eum iure
|
|
|
|
reprehenderit, qui in ea voluptate velit esse, quam nihil molestiae
|
|
|
|
consequatur, vel illum, qui dolorem eum fugiat, quo voluptas nulla
|
2019-01-06 16:22:33 +01:00
|
|
|
pariatur?</p>""".replace(
|
|
|
|
"\n", " "
|
|
|
|
),
|
2017-03-19 14:46:08 +01:00
|
|
|
"""\
|
|
|
|
<p>At vero eos et accusamus et iusto odio dignissimos ducimus, qui
|
|
|
|
blanditiis praesentium voluptatum deleniti atque corrupti, quos dolores
|
|
|
|
et quas molestias excepturi sint, obcaecati cupiditate non provident,
|
|
|
|
similique sunt in culpa, qui officia deserunt mollitia animi, id est
|
|
|
|
laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita
|
|
|
|
distinctio. Nam libero tempore, cum soluta nobis est eligendi optio,
|
|
|
|
cumque nihil impedit, quo minus id, quod maxime placeat, facere
|
|
|
|
possimus, omnis voluptas assumenda est, omnis dolor repellendus.
|
|
|
|
Temporibus autem quibusdam et aut officiis debitis aut rerum
|
|
|
|
necessitatibus saepe eveniet, ut et voluptates repudiandae sint et
|
|
|
|
molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente
|
|
|
|
delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut
|
2019-01-06 16:22:33 +01:00
|
|
|
perferendis doloribus asperiores repellat…</p>""".replace(
|
|
|
|
"\n", " "
|
|
|
|
),
|
2017-03-19 14:46:08 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
DEFAULT_NUMBER = 100
|
2019-01-06 16:22:33 +01:00
|
|
|
STAFF_USER_USERNAME = "admin{}"
|
|
|
|
DEFAULT_USER_USERNAME = "user{}"
|
|
|
|
PASSWORD = "password"
|
2017-03-19 14:46:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
"""
|
|
|
|
Command to create example data for OpenSlides.
|
|
|
|
"""
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
help = "Create example data for OpenSlides."
|
|
|
|
|
|
|
|
chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
|
2017-03-19 14:46:08 +01:00
|
|
|
|
|
|
|
def add_arguments(self, parser):
|
|
|
|
"""
|
|
|
|
Adds arguments to the command parser. The default values for the apps
|
|
|
|
are set by DEFAULT_NUMBER.
|
|
|
|
"""
|
|
|
|
parser.add_argument(
|
2019-01-06 16:22:33 +01:00
|
|
|
"--only",
|
|
|
|
action="store_true",
|
2020-05-13 16:16:03 +02:00
|
|
|
help="Only the given objects are created i. e. all defaults are set to 0.",
|
2017-03-19 14:46:08 +01:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2019-01-06 16:22:33 +01:00
|
|
|
"-t",
|
|
|
|
"--topics",
|
2017-03-19 14:46:08 +01:00
|
|
|
type=int,
|
2019-01-12 23:01:42 +01:00
|
|
|
help=f"Number of topics to be created (default {DEFAULT_NUMBER}).",
|
2017-03-19 14:46:08 +01:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2019-01-06 16:22:33 +01:00
|
|
|
"-m",
|
|
|
|
"--motions",
|
2017-03-19 14:46:08 +01:00
|
|
|
type=int,
|
2019-01-12 23:01:42 +01:00
|
|
|
help=f"Number of motions to be created (default {DEFAULT_NUMBER}).",
|
2017-03-19 14:46:08 +01:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2019-01-06 16:22:33 +01:00
|
|
|
"-a",
|
|
|
|
"--assignments",
|
2017-03-19 14:46:08 +01:00
|
|
|
type=int,
|
2019-01-12 23:01:42 +01:00
|
|
|
help=f"Number of assignments to be created (default {DEFAULT_NUMBER}).",
|
2017-03-19 14:46:08 +01:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2019-01-06 16:22:33 +01:00
|
|
|
"-u",
|
|
|
|
"--users",
|
2017-03-19 14:46:08 +01:00
|
|
|
nargs=2,
|
|
|
|
type=int,
|
2020-05-13 16:16:03 +02:00
|
|
|
help=dedent(
|
|
|
|
f"""
|
|
|
|
Number of users to be created. The first number of users is added \
|
|
|
|
to the group "Staff" (default {DEFAULT_NUMBER}). The second number \
|
|
|
|
of users is not added to any group (default {DEFAULT_NUMBER}).
|
|
|
|
"""
|
|
|
|
),
|
2017-03-19 14:46:08 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
2019-08-20 12:00:54 +02:00
|
|
|
run_startup_hooks()
|
2017-03-19 14:46:08 +01:00
|
|
|
self.create_topics(options)
|
|
|
|
self.create_motions(options)
|
|
|
|
self.create_assignments(options)
|
|
|
|
self.create_users(options)
|
|
|
|
|
2017-08-09 14:27:39 +02:00
|
|
|
@transaction.atomic
|
2017-03-19 14:46:08 +01:00
|
|
|
def create_topics(self, options):
|
2019-01-06 16:22:33 +01:00
|
|
|
number_of_topics = options["topics"]
|
|
|
|
if number_of_topics is None and not options["only"]:
|
2017-03-19 14:46:08 +01:00
|
|
|
number_of_topics = DEFAULT_NUMBER
|
|
|
|
if number_of_topics is not None and number_of_topics > 0:
|
2019-01-12 23:01:42 +01:00
|
|
|
self.stdout.write(f"Start creating {number_of_topics} topcis ...")
|
2019-01-06 16:22:33 +01:00
|
|
|
current_topics = list(Topic.objects.values_list("id", flat=True))
|
2017-03-19 14:46:08 +01:00
|
|
|
new_topics = []
|
|
|
|
for i in range(number_of_topics):
|
|
|
|
new_topics.append(Topic(title=get_random_string(20, self.chars)))
|
|
|
|
Topic.objects.bulk_create(new_topics)
|
|
|
|
items = []
|
2019-08-08 12:47:47 +02:00
|
|
|
lists_of_speakers = []
|
2017-03-19 14:46:08 +01:00
|
|
|
for topic in Topic.objects.exclude(pk__in=current_topics):
|
|
|
|
items.append(Item(content_object=topic, type=Item.AGENDA_ITEM))
|
2019-08-08 12:47:47 +02:00
|
|
|
lists_of_speakers.append(ListOfSpeakers(content_object=topic))
|
2017-03-19 14:46:08 +01:00
|
|
|
Item.objects.bulk_create(items)
|
2019-08-08 12:47:47 +02:00
|
|
|
ListOfSpeakers.objects.bulk_create(lists_of_speakers)
|
2019-01-06 16:22:33 +01:00
|
|
|
self.stdout.write(
|
2019-01-12 23:01:42 +01:00
|
|
|
self.style.SUCCESS(f"{number_of_topics} topcis successfully created.")
|
2019-01-06 16:22:33 +01:00
|
|
|
)
|
2017-03-19 14:46:08 +01:00
|
|
|
elif number_of_topics is not None and number_of_topics < 0:
|
2019-01-06 16:22:33 +01:00
|
|
|
raise CommandError("Number for topics must not be negative.")
|
2017-03-19 14:46:08 +01:00
|
|
|
|
2017-08-09 14:27:39 +02:00
|
|
|
@transaction.atomic
|
2017-03-19 14:46:08 +01:00
|
|
|
def create_motions(self, options):
|
2019-01-06 16:22:33 +01:00
|
|
|
number_of_motions = options["motions"]
|
|
|
|
if number_of_motions is None and not options["only"]:
|
2017-03-19 14:46:08 +01:00
|
|
|
number_of_motions = DEFAULT_NUMBER
|
|
|
|
if number_of_motions is not None and number_of_motions > 0:
|
2019-01-12 23:01:42 +01:00
|
|
|
self.stdout.write(f"Start creating {number_of_motions} motions ...")
|
2019-01-06 16:22:33 +01:00
|
|
|
text = ""
|
2017-03-19 14:46:08 +01:00
|
|
|
for i in range(MOTION_NUMBER_OF_PARAGRAPHS):
|
|
|
|
text += dedent(LOREM_IPSUM[i % 3])
|
|
|
|
for i in range(number_of_motions):
|
2019-01-06 16:22:33 +01:00
|
|
|
motion = Motion(title=get_random_string(20, self.chars), text=text)
|
2017-03-19 14:46:08 +01:00
|
|
|
motion.save(skip_autoupdate=True)
|
2019-01-06 16:22:33 +01:00
|
|
|
self.stdout.write(
|
2019-01-12 23:01:42 +01:00
|
|
|
self.style.SUCCESS(f"{number_of_motions} motions successfully created.")
|
2019-01-06 16:22:33 +01:00
|
|
|
)
|
2017-03-19 14:46:08 +01:00
|
|
|
elif number_of_motions is not None and number_of_motions < 0:
|
2019-01-06 16:22:33 +01:00
|
|
|
raise CommandError("Number for motions must not be negative.")
|
2017-03-19 14:46:08 +01:00
|
|
|
|
2017-08-09 14:27:39 +02:00
|
|
|
@transaction.atomic
|
2017-03-19 14:46:08 +01:00
|
|
|
def create_assignments(self, options):
|
2019-01-06 16:22:33 +01:00
|
|
|
number_of_assignments = options["assignments"]
|
|
|
|
if number_of_assignments is None and not options["only"]:
|
2017-03-19 14:46:08 +01:00
|
|
|
number_of_assignments = DEFAULT_NUMBER
|
|
|
|
if number_of_assignments is not None and number_of_assignments > 0:
|
2019-01-12 23:01:42 +01:00
|
|
|
self.stdout.write(f"Start creating {number_of_assignments} assignments ...")
|
2019-01-06 16:22:33 +01:00
|
|
|
current_assignments = list(Assignment.objects.values_list("id", flat=True))
|
2017-03-19 14:46:08 +01:00
|
|
|
new_assignments = []
|
|
|
|
for i in range(number_of_assignments):
|
2019-01-06 16:22:33 +01:00
|
|
|
new_assignments.append(
|
|
|
|
Assignment(title=get_random_string(20, self.chars), open_posts=1)
|
|
|
|
)
|
2017-03-19 14:46:08 +01:00
|
|
|
Assignment.objects.bulk_create(new_assignments)
|
|
|
|
items = []
|
2019-08-08 12:47:47 +02:00
|
|
|
lists_of_speakers = []
|
2017-03-19 14:46:08 +01:00
|
|
|
for assignment in Assignment.objects.exclude(pk__in=current_assignments):
|
|
|
|
items.append(Item(content_object=assignment))
|
2019-08-08 12:47:47 +02:00
|
|
|
lists_of_speakers.append(ListOfSpeakers(content_object=assignment))
|
2017-03-19 14:46:08 +01:00
|
|
|
Item.objects.bulk_create(items)
|
2019-08-08 12:47:47 +02:00
|
|
|
ListOfSpeakers.objects.bulk_create(lists_of_speakers)
|
2019-01-06 16:22:33 +01:00
|
|
|
self.stdout.write(
|
|
|
|
self.style.SUCCESS(
|
2019-01-12 23:01:42 +01:00
|
|
|
f"{number_of_assignments} assignments successfully created."
|
2019-01-06 16:22:33 +01:00
|
|
|
)
|
|
|
|
)
|
2017-03-19 14:46:08 +01:00
|
|
|
elif number_of_assignments is not None and number_of_assignments < 0:
|
2019-01-06 16:22:33 +01:00
|
|
|
raise CommandError("Number for assignments must not be negative.")
|
2017-03-19 14:46:08 +01:00
|
|
|
|
|
|
|
def create_users(self, options):
|
|
|
|
self.create_staff_users(options)
|
|
|
|
self.create_default_users(options)
|
|
|
|
|
2017-08-09 14:27:39 +02:00
|
|
|
@transaction.atomic
|
2017-03-19 14:46:08 +01:00
|
|
|
def create_staff_users(self, options):
|
2019-01-06 16:22:33 +01:00
|
|
|
if options["users"] is None and not options["only"]:
|
2018-10-28 10:04:52 +01:00
|
|
|
staff_users: Optional[int] = DEFAULT_NUMBER
|
2019-01-06 16:22:33 +01:00
|
|
|
elif options["users"] is None:
|
2017-03-19 14:46:08 +01:00
|
|
|
staff_users = None
|
|
|
|
else:
|
2019-01-06 16:22:33 +01:00
|
|
|
staff_users = options["users"][0]
|
2017-03-19 14:46:08 +01:00
|
|
|
if staff_users is not None and staff_users > 0:
|
2019-01-12 23:01:42 +01:00
|
|
|
self.stdout.write(f"Start creating {staff_users} staff users ...")
|
2019-01-06 16:22:33 +01:00
|
|
|
group_staff = Group.objects.get(name="Staff")
|
2017-09-12 12:52:37 +02:00
|
|
|
hashed_password = make_password(PASSWORD)
|
2019-01-06 16:22:33 +01:00
|
|
|
current_users = list(User.objects.values_list("id", flat=True))
|
2017-03-19 14:46:08 +01:00
|
|
|
new_users = []
|
|
|
|
for i in range(staff_users):
|
2019-01-06 16:22:33 +01:00
|
|
|
new_users.append(
|
|
|
|
User(
|
|
|
|
username=STAFF_USER_USERNAME.format(i),
|
|
|
|
default_password=PASSWORD,
|
|
|
|
password=hashed_password,
|
|
|
|
)
|
|
|
|
)
|
2017-08-09 14:27:39 +02:00
|
|
|
try:
|
|
|
|
User.objects.bulk_create(new_users)
|
|
|
|
except IntegrityError:
|
2019-01-06 16:22:33 +01:00
|
|
|
self.stdout.write(
|
|
|
|
"FAILED: The requested staff users to create are already existing..."
|
|
|
|
)
|
2017-08-09 14:27:39 +02:00
|
|
|
else:
|
|
|
|
for user in User.objects.exclude(pk__in=current_users):
|
|
|
|
user.groups.add(group_staff)
|
2019-01-06 16:22:33 +01:00
|
|
|
self.stdout.write(
|
|
|
|
self.style.SUCCESS(
|
2019-01-12 23:01:42 +01:00
|
|
|
f"{staff_users} staff users successfully created."
|
2019-01-06 16:22:33 +01:00
|
|
|
)
|
|
|
|
)
|
2017-03-19 14:46:08 +01:00
|
|
|
elif staff_users is not None and staff_users < 0:
|
2019-01-06 16:22:33 +01:00
|
|
|
raise CommandError("Number for staff users must not be negative.")
|
2017-03-19 14:46:08 +01:00
|
|
|
|
2017-08-09 14:27:39 +02:00
|
|
|
@transaction.atomic
|
2017-03-19 14:46:08 +01:00
|
|
|
def create_default_users(self, options):
|
2019-01-06 16:22:33 +01:00
|
|
|
if options["users"] is None and not options["only"]:
|
2018-10-28 10:04:52 +01:00
|
|
|
default_users: Optional[int] = DEFAULT_NUMBER
|
2019-01-06 16:22:33 +01:00
|
|
|
elif options["users"] is None:
|
2017-03-19 14:46:08 +01:00
|
|
|
default_users = None
|
|
|
|
else:
|
2019-01-06 16:22:33 +01:00
|
|
|
default_users = options["users"][1]
|
2017-03-19 14:46:08 +01:00
|
|
|
if default_users is not None and default_users > 0:
|
2019-01-12 23:01:42 +01:00
|
|
|
self.stdout.write(f"Start creating {default_users} default users ...")
|
2017-09-12 12:52:37 +02:00
|
|
|
hashed_password = make_password(PASSWORD)
|
2017-03-19 14:46:08 +01:00
|
|
|
new_users = []
|
|
|
|
for i in range(default_users):
|
2019-01-06 16:22:33 +01:00
|
|
|
new_users.append(
|
|
|
|
User(
|
|
|
|
username=DEFAULT_USER_USERNAME.format(i),
|
|
|
|
default_password=PASSWORD,
|
|
|
|
password=hashed_password,
|
|
|
|
)
|
|
|
|
)
|
2017-08-09 14:27:39 +02:00
|
|
|
try:
|
|
|
|
User.objects.bulk_create(new_users)
|
|
|
|
except IntegrityError:
|
2019-01-06 16:22:33 +01:00
|
|
|
self.stdout.write(
|
|
|
|
"FAILED: The requested staff users to create are already existing..."
|
|
|
|
)
|
2017-08-09 14:27:39 +02:00
|
|
|
else:
|
2019-01-06 16:22:33 +01:00
|
|
|
self.stdout.write(
|
|
|
|
self.style.SUCCESS(
|
2019-01-12 23:01:42 +01:00
|
|
|
f"{default_users} default users successfully created."
|
2019-01-06 16:22:33 +01:00
|
|
|
)
|
|
|
|
)
|
2017-03-19 14:46:08 +01:00
|
|
|
elif default_users is not None and default_users < 0:
|
2019-01-06 16:22:33 +01:00
|
|
|
raise CommandError("Number for default users must not be negative.")
|