def create_or_update_directive(saas): instance = saas.instance url = urlparse(instance.custom_url) protocol, valid_protocols = saas.PROTOCOL_MAP[url.scheme] account = instance.account # get or create website try: website = Website.objects.get( protocol__in=valid_protocols, domains__name=url.netloc, account=account, ) except Website.DoesNotExist: Domain = Website.domains.field.rel.to domain = Domain.objects.get(name=url.netloc) # Create new website for custom_url website = Website(name=url.netloc, protocol=protocol, account=account) website.save() website.domains.add(domain) # get or create directive try: directive = website.directives.get(name=saas.get_directive_name()) except WebsiteDirective.DoesNotExist: directive = WebsiteDirective(name=saas.get_directive_name(), value=url.path) if not directive.pk or directive.value != url.path: directive.value = url.path directive.website = website directive.save() return directive
def create_or_update_directive(saas): instance = saas.instance url = urlparse(instance.custom_url) protocol, valid_protocols = saas.PROTOCOL_MAP[url.scheme] account = instance.account # get or create website try: website = Website.objects.get( protocol__in=valid_protocols, domains__name=url.netloc, account=account, ) except Website.DoesNotExist: Domain = Website.domains.field.related_model domain = Domain.objects.get(name=url.netloc) # Create new website for custom_url tgt_server = Server.objects.get(name='web.pangea.lan') website = Website(name=url.netloc, protocol=protocol, account=account, target_server=tgt_server) website.save() website.domains.add(domain) # get or create directive try: directive = website.directives.get(name=saas.get_directive_name()) except WebsiteDirective.DoesNotExist: directive = WebsiteDirective(name=saas.get_directive_name(), value=url.path) if not directive.pk or directive.value != url.path: directive.value = url.path directive.website = website directive.save() return directive