diff --git a/README.md b/README.md index d5be08c..c75e99c 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ It is important to set the proper --schema parameter to load the data into the r python manage.py tenant_command loaddata --schema=localhost bakeup/shop/fixtures/demo_point_of_sale.json python manage.py tenant_command loaddata --schema=localhost bakeup/users/fixtures/demo_users.json + python manage.py tenant_command loaddata --schema=localhost bakeup/users/fixtures/groups.json python manage.py tenant_command loaddata --schema=localhost bakeup/workshop/fixtures/demo_categories.json python manage.py tenant_command loaddata --schema=localhost bakeup/workshop/fixtures/demo_products.json diff --git a/bakeup/pages/management/commands/create_initial_wagtail_pages.py b/bakeup/pages/management/commands/setup_tenant.py similarity index 72% rename from bakeup/pages/management/commands/create_initial_wagtail_pages.py rename to bakeup/pages/management/commands/setup_tenant.py index f2ae0bb..f8f2e8b 100644 --- a/bakeup/pages/management/commands/create_initial_wagtail_pages.py +++ b/bakeup/pages/management/commands/setup_tenant.py @@ -1,5 +1,8 @@ +import getpass from pathlib import Path +from django.conf import settings +from django.contrib.auth.models import Group from django.contrib.contenttypes.models import ContentType from django.core.files import File from django.core.management.base import BaseCommand @@ -9,14 +12,20 @@ from wagtail.models import Page, Site from wagtailmenus.conf import settings as wagtailmenu_settings +from bakeup.core.models import RegistrationFieldOption +from bakeup.newsletter.models import Audience from bakeup.pages.models import ContentPage, ShopPage +from bakeup.users.models import User APP_DIR = Path(__file__).resolve().parent.parent.parent FIXTURES_DIR = APP_DIR.joinpath("fixtures") class Command(InteractiveTenantOption, BaseCommand): - help = "Creates initial pages for the wagtail site" + help = ( + "Setup default data for tenant. Includes creating pages and menus, adding a" + " user and default settings." + ) def _boolean_input(self, question, default=None): self.stdout.write(f"{question} ", ending="") @@ -245,7 +254,77 @@ def handle(self, *args, **options): # finally, create the menus self._create_main_menu() self._create_flat_menus() + self.stdout.write( + self.style.SUCCESS("All pages created for tenant {}".format(tenant)) + ) + do_account = self._boolean_input( + "Would you like to create an baker account? [y/N]", + default=True, + ) + if do_account: + # create a user + username = input("Username: ") + password = getpass.getpass() + email = input("Email: ") + first_name = input("First name: ") + last_name = input("Last name: ") + user = User.objects.create_user( + username=username, + email=email, + first_name=first_name, + last_name=last_name, + password=password, + is_staff=True, + is_active=True, + ) + self.stdout.write( + self.style.SUCCESS( + "Account {} created for tenant {}".format(user, tenant) + ) + ) + # Add user to Wagtail groups + editors_group = Group.objects.filter(name="Editors").first() + moderators_group = Group.objects.filter(name="Moderators").first() - self.stdout.write( - self.style.SUCCESS("All pages created for tenant {}".format(tenant)) + if editors_group: + user.groups.add(editors_group) + self.stdout.write(self.style.SUCCESS("Added user to 'Editors' group.")) + else: + self.stderr.write( + "Editors group not found. Please ensure Wagtail is set up." + ) + + if moderators_group: + user.groups.add(moderators_group) + self.stdout.write( + self.style.SUCCESS("Added user to 'Moderators' group.") + ) + else: + self.stderr.write( + "Moderators group not found. Please ensure Wagtail is set up." + ) + do_settings = self._boolean_input( + "Would you really like to create default settings? [y/N]", + default=True, ) + if do_settings: + tenant_settings = tenant.clientsetting + tenant_settings.default_from_email = settings.TENANT_DEFAULT_EMAIL + tenant_settings.email_host = settings.TENANT_DEFAULT_EMAIL_HOST + tenant_settings.email_host_password = settings.TENANT_DEFAULT_EMAIL_PASSWORD + tenant_settings.email_host_user = settings.TENANT_DEFAULT_EMAIL_USER + tenant_settings.email_host_port = settings.TENANT_DEFAULT_EMAIL_PORT + tenant_settings.email_use_tls = settings.TENANT_DEFAULT_EMAIL_USE_TLS + tenant_settings.show_full_name_delivery_bill = True + tenant_settings.show_remaining_products = True + tenant_settings.user_registration_fields = RegistrationFieldOption.values + is_newsletter_enabled = self._boolean_input( + "Enable newsletter? [y/N]", default=False + ) + if is_newsletter_enabled: + tenant_settings.is_newsletter_enabled = is_newsletter_enabled + Audience.objects.create( + name="Alle Kunden", + is_default=True, + ) + tenant_settings.save() diff --git a/bakeup/users/fixtures/groups.json b/bakeup/users/fixtures/groups.json new file mode 100644 index 0000000..cbda30e --- /dev/null +++ b/bakeup/users/fixtures/groups.json @@ -0,0 +1 @@ +[{"model": "auth.group", "pk": 1, "fields": {"name": "Moderators", "permissions": [109, 105, 106, 107, 101, 102, 103]}}, {"model": "auth.group", "pk": 2, "fields": {"name": "Editors", "permissions": [154, 155, 156, 320, 321, 322, 324, 325, 326, 344, 345, 346, 336, 337, 338, 313, 289, 309, 297, 301, 268, 269, 270, 109, 5, 6, 7, 65, 66, 67, 69, 70, 71, 105, 106, 107, 101, 102, 103, 368, 369, 370, 376, 377, 378, 166, 167, 168]}}, {"model": "wagtailcore.groupcollectionpermission", "pk": 1, "fields": {"group": 1, "collection": 1, "permission": 101}}, {"model": "wagtailcore.groupcollectionpermission", "pk": 2, "fields": {"group": 2, "collection": 1, "permission": 101}}, {"model": "wagtailcore.groupcollectionpermission", "pk": 3, "fields": {"group": 1, "collection": 1, "permission": 102}}, {"model": "wagtailcore.groupcollectionpermission", "pk": 4, "fields": {"group": 2, "collection": 1, "permission": 102}}, {"model": "wagtailcore.groupcollectionpermission", "pk": 5, "fields": {"group": 1, "collection": 1, "permission": 105}}, {"model": "wagtailcore.groupcollectionpermission", "pk": 6, "fields": {"group": 2, "collection": 1, "permission": 105}}, {"model": "wagtailcore.groupcollectionpermission", "pk": 7, "fields": {"group": 1, "collection": 1, "permission": 106}}, {"model": "wagtailcore.groupcollectionpermission", "pk": 8, "fields": {"group": 2, "collection": 1, "permission": 106}}, {"model": "wagtailcore.groupcollectionpermission", "pk": 9, "fields": {"group": 2, "collection": 1, "permission": 108}}, {"model": "wagtailcore.groupcollectionpermission", "pk": 10, "fields": {"group": 2, "collection": 1, "permission": 104}}, {"model": "wagtailcore.grouppagepermission", "pk": 1, "fields": {"group": 1, "page": 1, "permission": 33}}, {"model": "wagtailcore.grouppagepermission", "pk": 2, "fields": {"group": 1, "page": 1, "permission": 34}}, {"model": "wagtailcore.grouppagepermission", "pk": 3, "fields": {"group": 1, "page": 1, "permission": 39}}, {"model": "wagtailcore.grouppagepermission", "pk": 4, "fields": {"group": 2, "page": 1, "permission": 33}}, {"model": "wagtailcore.grouppagepermission", "pk": 5, "fields": {"group": 2, "page": 1, "permission": 34}}, {"model": "wagtailcore.grouppagepermission", "pk": 6, "fields": {"group": 1, "page": 1, "permission": 38}}, {"model": "wagtailcore.grouppagepermission", "pk": 7, "fields": {"group": 1, "page": 1, "permission": 40}}, {"model": "wagtailcore.grouppagepermission", "pk": 8, "fields": {"group": 2, "page": 1, "permission": 39}}, {"model": "wagtailcore.grouppagepermission", "pk": 9, "fields": {"group": 2, "page": 1, "permission": 40}}, {"model": "wagtailcore.grouppagepermission", "pk": 10, "fields": {"group": 2, "page": 1, "permission": 37}}, {"model": "wagtailcore.grouppagepermission", "pk": 11, "fields": {"group": 2, "page": 1, "permission": 38}}] \ No newline at end of file diff --git a/config/settings/base.py b/config/settings/base.py index 965cb1e..8236b15 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -526,3 +526,10 @@ NEWSLETTER_SUBSCRIBE_FORM_MSG_FAILURE = ( "Es ist ein Fehler aufgetreten. Bitte versuche es erneut." ) + +TENANT_DEFAULT_EMAIL = "noreply@bakeup.org" +TENANT_DEFAULT_EMAIL_HOST = "v027131.kasserver.com" +TENANT_DEFAULT_EMAIL_PASSWORD = env("TENANT_DEFAULT_EMAIL_PASSWORD", default="") +TENANT_DEFAULT_EMAIL_USER = env("TENANT_DEFAULT_EMAIL_USER", default="") +TENANT_DEFAULT_EMAIL_PORT = env("TENANT_DEFAULT_EMAIL_PORT", default="587") +TENANT_DEFAULT_EMAIL_USE_TLS = env("TENANT_DEFAULT_EMAIL_USE_TLS", default=True)