def create_campaign_goal(self, campaign, name, description, type=None, start=None, end=None): if type is None: type = 'project' if start is None: start = datetime.utcnow() if end is None: end = start + timedelta(days=7) if not isinstance(campaign, Campaign): campaign = Campaign.query.get(campaign) cg = CampaignGoal() #campaign.goals.append(cg) cg.campaign_id = campaign.id cg.enabled = True cg.name = name cg.description = description cg.start = start cg.end = end cg.type = type self.commit_model(cg) return cg
def add_goal(campaign, name, description, type, predecessor=None, start=None, end=None, **kwargs): """Add a goal to an existing campaign. ``name`` and ``description`` are required. Any key-value pair will be assumed to be metadata to be added to the goal instance. :param campaign: The campaign goal which the add a new goal. :type campaign: :class:`pooldlib.postgresql.models.Campaign` :param name: The name of the newly created campaign goal. :type name: string :param description: The description of the newly created campaign goal. :type description: string :param type: The type of goal to add (fund-raiser, project or group-purchase) :type type: string :param start: Active start datetime for the campaign in UTC, defaults to `datetime.utcnow` :type start: :class:`datetime.datetime` :param end: Active end datetime for the campaign in UTC, optional :type end: :class:`datetime.datetime` or `None` :param kwargs: Keyword arguments consisting of key-value pairs to be added to the newly created goal as metadata. :type kwargs: unspecified keyword arguments to the function. :returns: :class:`pooldlib.postgresql.models.CommunitiyGoal` """ goal = CampaignGoalModel() goal.name = name goal.description = description goal.start = start or pytz.UTC.localize(datetime.utcnow()) goal.end = end goal.type = type if predecessor is not None: goal.predecessor = predecessor campaign.goals.append(goal) with transaction_session() as session: session.add(goal) session.commit() meta = list() for (k, v) in kwargs.items(): goal_meta = CampaignGoalMetaModel() goal_meta.key = k goal_meta.value = v goal_meta.campaign_goal = goal meta.append(goal_meta) with transaction_session(auto_commit=True) as session: for goal_meta in meta: session.add(goal_meta) return goal