Beispiel #1
0
 def create_drip_campaign(self, name, list_id, description=None):
     """
     save drip campaign to mongo
     return object id
     """
     new_drip_campaign = DripCampaign(
         shop_url=self.shop_url,
         name=name,
         list_id=list_id,
         description=description,
         active=False,
     )
     new_drip_campaign.save()
     return new_drip_campaign.id
Beispiel #2
0
 def create_drip_campaign(self, name, list_id, description=None):
     """
     save drip campaign to mongo
     return object id
     """
     new_drip_campaign = DripCampaign(
         shop_url=self.shop_url,
         name=name,
         list_id=list_id,
         description=description,
         active=False,
     )
     new_drip_campaign.save()
     return new_drip_campaign.id
Beispiel #3
0
 def create_node_campaign(self, node_oid):
     """
     create mailchimp campaign for given node
     node must be fully processed (we have all content and segment info)
     returns campaign id, or None if failed
     (usually fails if segment is empty - pointless campaign)
     """
     node = Node.objects(id=node_oid)[0]
     list_id = DripCampaign.objects(
         id=node["drip_campaign_id"])[0]["list_id"]
     segment = Segment.objects(id=node["segment_oid"])[0]
     if segment["members_euid"]:
         campaign_id = self.mw.create_campaign(
             list_id=list_id,
             segment_id=segment["segment_id"],
             template_id=node["content"]["template_id"],
             subject=node["content"]["subject"],
             from_email=node["content"]["from_email"],
             from_name=node["content"]["from_name"],
             folder_id=self.folder_id,
         )
         node.update(set__campaign_id=campaign_id,
                     set__updated_at=datetime.utcnow())
         return campaign_id
     else:
         return None
Beispiel #4
0
    def form_segment(self, node_oid):
        """
        for given drip campaign node
        get the set of applicable members for this node
        and create a segment based on it
        there are two cases:
        1. node is initial node - then the segment is the whole list
        2. node is not initial node - gather the set based on segments of
           previous nodes by applying the trigger filters
        """
        # init empty segment and stuff
        new_segment = Segment()
        new_segment.save()
        name = "%s_seg_%s" % (self.PREFIX, new_segment.id)
        node = Node.objects(id=node_oid)[0]
        list_id = DripCampaign.objects(
            id=node["drip_campaign_id"])[0]["list_id"]
        node.update(set__segment_oid=new_segment.id,
                    set__updated_at=datetime.utcnow())

        # gather all users that apply for this node after triggers on previous nodes
        all_euids = set()
        if node["initial"]:
            all_euids = set(List.objects(list_id=list_id)[0]["members_euid"])
        else:
            for trg in Trigger.objects(node_to=node_oid):
                for euids, to_node_oid in self.segment_by_triggers(
                        trg["node_from"]):
                    if to_node_oid == node_oid:
                        all_euids.update(set(euids))

        # # intersect euids with current state of the list
        # # it might be the case that some people are removed from the list since previous email
        # self.fetch_members_for_list(list_id)
        # all_euids = all_euids - set(List.objects(list_id=list_id)[0]["members_euid"])

        all_euids = list(all_euids)

        # apply the user list to segment n stuff
        # if user list is empty, save only meta info and don't actually work with mailchimp
        if all_euids:
            segment_id = self.mw.create_segment(list_id, name)
            self.mw.update_segment_members(list_id, segment_id, all_euids)
        else:
            segment_id = None
        new_segment.update(set__segment_id=segment_id,
                           set__name=name,
                           members_euid=all_euids,
                           set__updated_at=datetime.utcnow())
Beispiel #5
0
    def form_segment(self, node_oid):
        """
        for given drip campaign node
        get the set of applicable members for this node
        and create a segment based on it
        there are two cases:
        1. node is initial node - then the segment is the whole list
        2. node is not initial node - gather the set based on segments of
           previous nodes by applying the trigger filters
        """
        # init empty segment and stuff
        new_segment = Segment()
        new_segment.save()
        name = "%s_seg_%s" % (self.PREFIX, new_segment.id)
        node = Node.objects(id=node_oid)[0]
        list_id = DripCampaign.objects(id=node["drip_campaign_id"])[0]["list_id"]
        node.update(set__segment_oid=new_segment.id, set__updated_at=datetime.utcnow())

        # gather all users that apply for this node after triggers on previous nodes
        all_euids = set()
        if node["initial"]:
            all_euids = set(List.objects(list_id=list_id)[0]["members_euid"])
        else:
            for trg in Trigger.objects(node_to=node_oid):
                for euids, to_node_oid in self.segment_by_triggers(trg["node_from"]):
                    if to_node_oid == node_oid:
                        all_euids.update(set(euids))

        # # intersect euids with current state of the list
        # # it might be the case that some people are removed from the list since previous email
        # self.fetch_members_for_list(list_id)
        # all_euids = all_euids - set(List.objects(list_id=list_id)[0]["members_euid"])

        all_euids = list(all_euids)

        # apply the user list to segment n stuff
        # if user list is empty, save only meta info and don't actually work with mailchimp
        if all_euids:
            segment_id = self.mw.create_segment(list_id, name)
            self.mw.update_segment_members(list_id, segment_id, all_euids)
        else:
            segment_id = None
        new_segment.update(set__segment_id=segment_id, set__name=name, members_euid=all_euids,
                           set__updated_at=datetime.utcnow())
Beispiel #6
0
def process_campaigns(mw):
    """
    processes all campaigns
    looks for nodes that have to be processed
    (start time in the past but nod processed yet)
    form segments for the nodes, send emails, makes nodes as done
    """
    now = datetime.utcnow()
    print "it's", now, "now! checking drip campaigns.."
    # iterate over all active drip campaigns
    for drip_campaign in DripCampaign.objects(active=True):
        print "   ", "checking drip campaign", drip_campaign.name, drip_campaign.id
        # initialize data captain for this shop
        dc = DataCaptain(drip_campaign["shop_url"], mw)
        dc.update_lists()
        dc.get_folder()
        # update lists
        dc.fetch_members_for_list(drip_campaign["list_id"])
        print "   ", "checking nodes"
        # iterate over all unprocessed drip campaign's nodes that have already surpassed start_time
        for node in list(
                Node.objects(drip_campaign_id=drip_campaign.id,
                             done=False,
                             start_time__lte=now)):
            print "   ", "   ", "processing node", node.title, node.start_time, node.id
            # create user segment for the node
            dc.form_segment(node.id)
            # prepare a mailchimp campaign for the node
            mc_campaign_id = dc.create_node_campaign(node.id)
            print "   ", "   ", "node segment formed:", mc_campaign_id
            # if successful, send the emails
            if mc_campaign_id is not None:
                print "   ", "   ", "sending node emails"
                dc.send_campaign(mc_campaign_id)
            # mark node as processed
            node.update(set__done=True, set__updated_at=datetime.utcnow())
            print "   ", "   ", "node processing finished!"
Beispiel #7
0
 def create_node_campaign(self, node_oid):
     """
     create mailchimp campaign for given node
     node must be fully processed (we have all content and segment info)
     returns campaign id, or None if failed
     (usually fails if segment is empty - pointless campaign)
     """
     node = Node.objects(id=node_oid)[0]
     list_id = DripCampaign.objects(id=node["drip_campaign_id"])[0]["list_id"]
     segment = Segment.objects(id=node["segment_oid"])[0]
     if segment["members_euid"]:
         campaign_id = self.mw.create_campaign(
             list_id=list_id,
             segment_id=segment["segment_id"],
             template_id=node["content"]["template_id"],
             subject=node["content"]["subject"],
             from_email=node["content"]["from_email"],
             from_name=node["content"]["from_name"],
             folder_id=self.folder_id,
         )
         node.update(set__campaign_id=campaign_id, set__updated_at=datetime.utcnow())
         return campaign_id
     else:
         return None
Beispiel #8
0
 def get_drip_campaigns(self):
     """
     return list of all drip campaigns for this shop
     """
     return list(DripCampaign.objects(shop_url=self.shop_url))
Beispiel #9
0
 def deactivate_drip_campaign(self, id):
     """
     set campaign with given id to inactive
     """
     DripCampaign.objects(id=id).update(set__active=False, set__updated_at=datetime.utcnow())
Beispiel #10
0
 def get_drip_campaigns(self):
     """
     return list of all drip campaigns for this shop
     """
     return list(DripCampaign.objects(shop_url=self.shop_url))
Beispiel #11
0
 def deactivate_drip_campaign(self, id):
     """
     set campaign with given id to inactive
     """
     DripCampaign.objects(id=id).update(set__active=False,
                                        set__updated_at=datetime.utcnow())