def create_linked_ucr_expression(domain_link, ucr_expression_id): if domain_link.is_remote: raise DomainLinkError(_("Linking expressions to a remote link is not currently supported")) try: ucr_expression = UCRExpression.objects.get(id=ucr_expression_id, domain=domain_link.master_domain) except UCRExpression.DoesNotExist: raise DomainLinkError( _("Expression does not exist in the upstream domain") ) if UCRExpression.objects.filter( name=ucr_expression.name, domain=domain_link.linked_domain, ).exists(): raise DomainLinkError( _("Expression {expression_name} already exists in the downstream domain {domain}").format( name=ucr_expression.name, domain=domain_link.linked_domain ) ) ucr_expression.upstream_id = ucr_expression.id ucr_expression.id = None ucr_expression.domain = domain_link.linked_domain ucr_expression.save() return ucr_expression.id
def link_domains(cls, linked_domain, master_domain, remote_details=None): existing_links = cls.all_objects.filter(linked_domain=linked_domain) active_links_with_other_domains = [l for l in existing_links if not l.deleted and l.master_domain != master_domain] if active_links_with_other_domains: raise DomainLinkError('Domain "{}" is already linked to a different domain ({}).'.format( linked_domain, active_links_with_other_domains[0].master_domain )) deleted_existing_links = [l for l in existing_links if l.deleted and l.master_domain == master_domain] active_links_with_this_domain = [l for l in existing_links if not l.deleted and l.master_domain == master_domain] if deleted_existing_links: # if there was a deleted link, just undelete it link = deleted_existing_links[0] link.deleted = False elif active_links_with_this_domain: # if there is already an active link, just update it with the new information link = active_links_with_this_domain[0] else: # make a new link link = DomainLink(linked_domain=linked_domain, master_domain=master_domain) if remote_details: link.remote_base_url = remote_details.url_base link.remote_username = remote_details.username link.remote_api_key = remote_details.api_key link.save() return link
def link_domains(cls, linked_domain, master_domain, remote_details=None): try: link = cls.all_objects.get(linked_domain=linked_domain) except cls.DoesNotExist: link = DomainLink(linked_domain=linked_domain, master_domain=master_domain) else: if link.master_domain != master_domain: if link.deleted: # create a new link to the new master domain link = DomainLink(linked_domain=linked_domain, master_domain=master_domain) else: raise DomainLinkError( 'Domain "{}" is already linked to a different domain ({}).' .format(linked_domain, link.master_domain)) if remote_details: link.remote_base_url = remote_details.url_base link.remote_username = remote_details.username link.remote_api_key = remote_details.api_key link.deleted = False link.save() return link
def update_linked_ucr_expression(domain_link, ucr_expression_id): try: linked_ucr_expression = UCRExpression.objects.get(id=ucr_expression_id) except UCRExpression.DoesNotExist: raise DomainLinkError( _("Linked Expression could not be found") ) try: upstream_ucr_expression = UCRExpression.objects.get(id=linked_ucr_expression.upstream_id) except UCRExpression.DoesNotExist: raise DomainLinkError( _("Upstream Expression could not be found. Maybe it has been deleted?") ) linked_ucr_expression.update_from_upstream(upstream_ucr_expression)
def _replace_upstream_app_id(haystack, upstream_app_id, downstream_domain): if upstream_app_id in haystack: try: downstream_app_id = get_downstream_app_id(downstream_domain, upstream_app_id) except MultipleDownstreamAppsError: raise DomainLinkError(_("This report cannot be updated because it references an app " "that has multiple linked apps.")) haystack = haystack.replace(upstream_app_id, downstream_app_id) return haystack
def link_domains(cls, linked_domain, master_domain, remote_details=None): """ With the GAing of linked projects in the form of ERM/MRM, this will become an internal method in favor of the link_domains method in linked_domain/views.py to allow for proper validation of domain and user privileges before creating any links """ existing_links = cls.all_objects.filter(linked_domain=linked_domain) active_links_with_other_domains = [ domain_link for domain_link in existing_links if not domain_link.deleted and domain_link.master_domain != master_domain ] if active_links_with_other_domains: already_linked_domain = active_links_with_other_domains[ 0].master_domain raise DomainLinkError( _('{} is already a downstream project space of {}.\nYou must remove the existing link before ' 'creating this new link.').format(linked_domain, already_linked_domain)) deleted_existing_links = [ domain_link for domain_link in existing_links if domain_link.deleted and domain_link.master_domain == master_domain ] active_links_with_this_domain = [ domain_link for domain_link in existing_links if not domain_link.deleted and domain_link.master_domain == master_domain ] if deleted_existing_links: # if there was a deleted link, just undelete it link = deleted_existing_links[0] link.deleted = False elif active_links_with_this_domain: # if there is already an active link, just update it with the new information link = active_links_with_this_domain[0] else: link = DomainLink(linked_domain=linked_domain, master_domain=master_domain) if remote_details: link.remote_base_url = remote_details.url_base link.remote_username = remote_details.username link.remote_api_key = remote_details.api_key link.save() return link
def _update_actions(domain_link, linked_keyword, keyword_actions): linked_keyword.keywordaction_set.all().delete() for keyword_action in keyword_actions: keyword_action.id = None keyword_action.keyword = linked_keyword if keyword_action.app_id is not None: try: keyword_action.app_id = get_master_app_to_linked_app( domain_link.linked_domain)[keyword_action.app_id] except KeyError: raise DomainLinkError( _("Keyword {keyword} references an application " "that has not been linked to {linked_domain}").format( keyword=linked_keyword.keyword, linked_domain=domain_link.linked_domain)) keyword_action.save()
def create_linked_ucr(domain_link, report_config_id): if domain_link.is_remote: remote_configs = remote_get_ucr_config(domain_link, report_config_id) datasource = remote_configs["datasource"] report_config = remote_configs["report"] else: report_config = ReportConfiguration.get(report_config_id) datasource = DataSourceConfiguration.get(report_config.config_id) # grab the linked app this linked report references try: downstream_app_id = get_downstream_app_id(domain_link.linked_domain, datasource.meta.build.app_id) except MultipleDownstreamAppsError: raise DomainLinkError(_("This report cannot be linked because it references an app that has multiple " "downstream apps.")) new_datasource = _get_or_create_datasource_link(domain_link, datasource, downstream_app_id) new_report = _get_or_create_report_link(domain_link, report_config, new_datasource) return LinkedUCRInfo(datasource=new_datasource, report=new_report)