from django.conf import settings from django.core.files import File from wagtail.models import Page def find_available_slug(requested_slug, ignore_page_id=None): """ Finds an available slug within the specified parent. If the requested slug is not available, this adds a number on the end, for example: - 'requested-slug' - 'requested-slug-1' - 'requested-slug-2' And so on, until an available slug is found. The `ignore_page_id` keyword argument is useful for when you are updating a page, you can pass the page being updated here so the page's current slug is not treated as in use by another page. """ pages = Page.objects.filter(slug__startswith=requested_slug) if ignore_page_id: pages = pages.exclude(id=ignore_page_id) existing_slugs = set(pages.values_list("slug", flat=True)) slug = requested_slug number = 1 while slug in existing_slugs: slug = requested_slug + "-" + str(number) number += 1 return slug orgs = { 1: {"de": "andere Broker", "fr": "autres Broker", "it": "altre Broker"}, 2: { "de": "andere Krankenversicherer", "fr": "autres assureurs santé", "it": "altre assicurazioni sanitarie", }, 3: { "de": "andere Privatversicherer", "fr": "autres Assurance privée", "it": "altre Assicurazione privato", }, 4: {"de": "Allianz Suisse", "fr": "Allianz Suisse", "it": "Allianz Suisse"}, 5: {"de": "AON", "fr": "AON", "it": "AON"}, 6: { "de": "AXA Winterthur", "fr": "AXA Assurances SA", "it": "AXA Assicurazioni SA", }, 7: {"de": "Baloise", "fr": "Baloise", "it": "Baloise"}, 8: { "de": "CAP Rechtsschutz", "fr": "CAP Protection juridique", "it": "CAP Protezione giuridica", }, 9: { "de": "Coop Rechtsschutz", "fr": "Coop Protection juridique", "it": "Coop Protezione giuridica", }, 10: {"de": "CSS", "fr": "CSS", "it": "CSS"}, 11: {"de": "Die Mobiliar", "fr": "La Mobilière", "it": "La Mobiliare"}, 12: { "de": "Emmental Versicherung", "fr": "Emmental Assurance", "it": "Emmental Assicurazione", }, 13: { "de": "GENERALI Versicherungen", "fr": "Generali Assurances", "it": "Generali Assicurazioni", }, 14: {"de": "Groupe Mutuel", "fr": "GROUPE MUTUEL", "it": "GROUPE MUTUEL"}, 15: {"de": "Helsana", "fr": "Helsana", "it": "Helsana"}, 16: {"de": "Helvetia", "fr": "Helvetia", "it": "Helvetia"}, 17: {"de": "Kessler & Co AG", "fr": "Kessler & Co AG", "it": "Kessler & Co AG"}, 18: { "de": "Orion Rechtsschutz Versicherung", "fr": "Orion Protection juridique", "it": "Orion Protezione giuridica", }, 19: {"de": "PAX", "fr": "PAX", "it": "PAX"}, 20: {"de": "Sanitas", "fr": "Sanitas", "it": "Sanitas"}, 21: {"de": "SUVA", "fr": "SUVA", "it": "SUVA"}, 22: {"de": "Swica", "fr": "Swica", "it": "Swica"}, 23: {"de": "Swiss Life", "fr": "Swiss Life", "it": "Swiss Life"}, 24: {"de": "Swiss Re", "fr": "Swiss Re", "it": "Swiss Re"}, 25: { "de": "Visana Services AG", "fr": "Visana Services SA", "it": "Visana Services SA", }, 26: { "de": "VZ VermögensZentrum AG", "fr": "VZ VermögensZentrum AG", "it": "VZ VermögensZentrum AG", }, 27: { "de": "Würth Financial Services AG", "fr": "Würth Financial Services SA", "it": "Würth Financial Services SA", }, 28: {"de": "Zürich", "fr": "Zurich", "it": "Zurigo"}, 29: {"de": "VBV", "fr": "AFA", "it": "AFA"}, 30: {"de": "Vaudoise", "fr": "Vaudoise", "it": "Vaudoise"}, 31: { "de": "Keine Firmenzugehörigkeit", "fr": "Pas d'appartenance à une entreprise", "it": "Nessuna affiliazione aziendale", }, } def add_organisations(apps=None, schema_editor=None): if apps is None: # pylint: disable=import-outside-toplevel from vbv_lernwelt.core.models import Organisation else: Organisation = apps.get_model("core", "Organisation") for org_id, org_data in orgs.items(): Organisation.objects.get_or_create( organisation_id=org_id, name_de=org_data["de"], name_fr=org_data["fr"], name_it=org_data["it"], ) def remove_organisations(apps=None, schema_editor=None): if apps is None: # pylint: disable=import-outside-toplevel from vbv_lernwelt.core.models import Organisation else: Organisation = apps.get_model("core", "Organisation") for org_id in orgs.keys(): Organisation.objects.filter( organisation_id=org_id, ).delete() def migrate_avatars(apps=None, schema_editor=None): # pylint: disable=import-outside-toplevel if apps is None: from vbv_lernwelt.core.models import User from vbv_lernwelt.media_files.models import UserImage else: User = apps.get_model("core", "User") UserImage = apps.get_model("media_files", "UserImage") # Models created by Django migration don't contain methods of the parent model. # We need to add them manually. from wagtail.images.models import AbstractImage UserImage.get_upload_to = AbstractImage.get_upload_to avatar_dir = settings.APPS_DIR / "static" / "avatars" for user in User.objects.all().exclude( avatar_url="/static/avatars/myvbv-default-avatar.png" ): if not user.avatar_url: continue avatar_file = user.avatar_url.split("/")[-1] try: with open(avatar_dir / avatar_file, "rb") as f: image = UserImage.objects.create( file=File(f), ) user.avatar = image user.save() except FileNotFoundError: pass