def create_campaign_goal_meta(self, campaign_goal_id, **kwargs): cg = CampaignGoal.query.get(campaign_goal_id) for (k, v) in kwargs.items(): cgm = CampaignGoalMeta() cgm.key = k cgm.value = v cgm.campaign_goal_id = cg.id self.commit_model(cgm) 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
def update_goal(update_goal, name=None, predecessor=None, description=None, start=None, end=None, **kwargs): """Update an existing goal for a campaign. Only ``goal`` is required. All given goal properties will be updated. Any unspecified keyword arguments will be used to update the goal's metadata. To delete metadata for a campaign goal, pass ``None`` as the value for the to be deleted key in the kwarg key-value pair. :param update_goal: Identifier for the target campaign. :type update_goal: :class:`pooldlib.postgresql.models.Goal` :param name: The name of the newly created campaign goal. :type name: string :param description: The description of the newly created campaign goal. :type name: string :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. """ if name is not None: update_goal.name = name if description is not None: update_goal.description = description if start is not None: update_goal.start = start if end is not None: update_goal.end = end if predecessor is not None: goal.predecessor_id = predecessor.id update_meta = [m for m in update_goal.metadata if m.key in kwargs] create_meta = [(k, v) for (k, v) in kwargs.items() if not hasattr(update_goal, k)] meta_delta = list() meta_remove = list() for goal_meta in update_meta: value = kwargs[m.key] if value is None: meta_remove.append(goal_meta) else: goal_meta.value = value meta_delta.append(goal_meta) for (k, v) in create_meta: goal_meta = CampaignGoalMetaModel() goal_meta.key = k goal_meta.value = v goal_meta.campaign_goal = update_goal meta_delta.append(goal_meta) with transaction_session() as session: session.add(update_goal) # Technically not needed session.flush() for goal_meta in meta_delta: session.add(goal_meta) for goal_meta in meta_remove: session.delete(goal_meta) session.commit() return update_goal