Пример #1
0
    def load_entire_campaign(self, drip_campaign_id):
        """
        helps frontend with loading entire campaigns
        takes drip campaign id and loads all of its components
        in a drip superstructure as frontend wants
        """
        # load campaign
        drip_campaign = DripCampaign.objects(id=drip_campaign_id)[0]
        drip_campaign_frontend = {
            "id": drip_campaign["id"],
            "name": drip_campaign["name"],
            "userListId": drip_campaign["list_id"],
        }
        # load blocks
        blocks = Block.objects(drip_campaign_id=drip_campaign_id)
        blocks_frontend = [
            {
                "id": block["id"],
                "datetime": block["start_time"],
                "nodeIds": block["nodes_id"],
            }
            for block in blocks
        ]
        # load nodes
        nodes_frontend = []
        for node in Node.objects(drip_campaign_id=drip_campaign_id):
            def get_trigger_action_id(trigger):
                if trigger.opened:
                    return self.get_frontend_action_id("open")
                if trigger.any_click:
                    return self.get_frontend_action_id("any click")
                if trigger.default:
                    return self.get_frontend_action_id("default")
                return self.get_frontend_action_id(trigger.clicked)
            triggers = [
                {
                    "id": trigger["id"],
                    "actionId":  get_trigger_action_id(trigger),
                    "nodeId": trigger["node_to"],
                }
                for trigger in Trigger.objects(node_from=node["id"])
            ]
            nodes_frontend.append({
                "id": node["id"],
                "name": node["title"],
                "description": node["description"],
                "templateId": node["content"]["template_id"],
                "triggers": triggers,
            })
        # update and load lists and templates
        lists = self.update_lists()
        lists_frontend = [
            {
                "id": lst["id"],
                "name": lst["name"],
            }
            for lst in lists
        ]
        templates = self.update_templates()
        templates_frontend = [
            {
                "id": tmplt["id"],
                "name": tmplt["name"],
            }
            for tmplt in templates
        ]

        # create actions for frontend
        # set default actions that apply to all templates
        actions = {
            self.get_frontend_action_id(action_type): {
                "id": self.get_frontend_action_id(action_type),
                "name": self.get_frontend_action_name(action_type),
                "templates": [],
            }
            for action_type in self.DEFAULT_ACTIONS
        }
        # iterate over all tempaltes and update actions
        for tmplt in templates:
            # first, add the template to all default actions
            for action_type in self.DEFAULT_ACTIONS:
                action_frontend_id = self.get_frontend_action_id(action_type)
                actions[action_frontend_id]["templates"].append(tmplt["id"])
            # second, add template to all its link click actions
            for link in self.get_links(tmplt["template_id"]):
                action_frontend_id = self.get_frontend_action_id(link)
                # if this link is new, add a new action
                if action_frontend_id not in actions:
                    actions[action_frontend_id] = {
                        "id": action_frontend_id,
                        "name": self.get_frontend_action_name(link),
                        "templates": [],
                    }
                # add the template to this link's click action
                actions[action_frontend_id]["templates"].append(tmplt["id"])
        # ditch the mapping
        actions_frontend = actions.values()

        # form the resulting frontend superstructure
        return {
            "campaign": drip_campaign_frontend,
            "userLists": lists_frontend,
            "templates": templates_frontend,
            "actions": actions_frontend,
            "blocks": blocks_frontend,
            "nodes": nodes_frontend,
        }