Esempio n. 1
0
 def create_campaign_meta(self, campaign, **kwargs):
     if isinstance(campaign, (int, long)):
         campaign = Campaign.query.get(campaign)
     for (k, v) in kwargs.items():
         cm = CampaignMeta()
         cm.key = k
         cm.value = v
         cm.campaign_id = campaign.id
         self.commit_model(cm)
Esempio n. 2
0
 def create_campaign_meta(self, campaign, **kwargs):
     if isinstance(campaign, (int, long)):
         campaign = Campaign.query.get(campaign)
     for (k, v) in kwargs.items():
         cm = CampaignMeta()
         cm.key = k
         cm.value = v
         cm.campaign_id = campaign.id
         self.commit_model(cm)
Esempio n. 3
0
def update(campaign, name=None, description=None, **kwargs):
    """Update name and description of a specified campaign.

    :param campaign: Campaign to update.
    :type campaign: :class:`pooldlib.postgresql.models.Campaign`
    :param name: If specified, the new name for the campaign.
    :type name: string
    :param name: If specified, the new description for the campaign.
    :type name: string
    :param kwargs: key-value pairs to associate with the `User` data model
                   instance as metadata.
    :type kwargs: kwarg dictionary

    :returns: :class:`pooldlib.postgresql.models.Campaign`
    """
    if name is not None:
        campaign.name = name
    if description is not None:
        campaign.description = description

    # TODO :: This is really inefficient, fix it. <*****@*****.**>
    # TODO :: This methodology is mirrored in the user API as well.
    update_meta = [m for m in campaign.metadata if m.key in kwargs]
    create_meta = [(k, v) for (k, v) in kwargs.items()
                   if not hasattr(campaign, k)]

    meta_delta = list()
    meta_remove = list()
    for campaign_meta in update_meta:
        value = kwargs[campaign_meta.key]
        if value is None:
            meta_remove.append(campaign_meta)
        else:
            campaign_meta.value = value
            meta_delta.append(campaign_meta)

    for (k, v) in create_meta:
        m = CampaignMetaModel()
        m.key = k
        m.value = v
        m.campaign_id = campaign.id
        meta_delta.append(m)

    with transaction_session() as session:
        session.add(campaign)
        session.commit()

        for m in meta_delta:
            session.add(m)
            session.flush()
        for m in meta_remove:
            session.delete(m)

        session.commit()

    return campaign
Esempio n. 4
0
def update(campaign, name=None, description=None, **kwargs):
    """Update name and description of a specified campaign.

    :param campaign: Campaign to update.
    :type campaign: :class:`pooldlib.postgresql.models.Campaign`
    :param name: If specified, the new name for the campaign.
    :type name: string
    :param name: If specified, the new description for the campaign.
    :type name: string
    :param kwargs: key-value pairs to associate with the `User` data model
                   instance as metadata.
    :type kwargs: kwarg dictionary

    :returns: :class:`pooldlib.postgresql.models.Campaign`
    """
    if name is not None:
        campaign.name = name
    if description is not None:
        campaign.description = description

    # TODO :: This is really inefficient, fix it. <*****@*****.**>
    # TODO :: This methodology is mirrored in the user API as well.
    update_meta = [m for m in campaign.metadata if m.key in kwargs]
    create_meta = [(k, v) for (k, v) in kwargs.items() if not hasattr(campaign, k)]

    meta_delta = list()
    meta_remove = list()
    for campaign_meta in update_meta:
        value = kwargs[campaign_meta.key]
        if value is None:
            meta_remove.append(campaign_meta)
        else:
            campaign_meta.value = value
            meta_delta.append(campaign_meta)

    for (k, v) in create_meta:
        m = CampaignMetaModel()
        m.key = k
        m.value = v
        m.campaign_id = campaign.id
        meta_delta.append(m)

    with transaction_session() as session:
        session.add(campaign)
        session.commit()

        for m in meta_delta:
            session.add(m)
            session.flush()
        for m in meta_remove:
            session.delete(m)

        session.commit()

    return campaign
Esempio n. 5
0
def create(organizer, name, description, start=None, end=None, **kwargs):
    """Create and return a new instance of
    :class:`pooldlib.postgresql.models.Campaign`.

    :param origanizer: The user to be classified as the campaign's organizing
                       member.
    :type origanizer: :class:`pooldlib.postgresql.models.User`
    :param name: The name of the campaign
    :type name: string
    :param description: The description of the campaign
    :type description: 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: Metadata to associate with the new campaign.
    :type kwargs: kwarg dictionary

    :returns: :class:`pooldlib.postgresql.models.Campaign`
    """
    campaign = CampaignModel()
    campaign.name = name
    campaign.description = description
    campaign.start = start or pytz.UTC.localize(datetime.utcnow())
    campaign.end = end

    with transaction_session() as session:
        session.add(campaign)
        session.commit()

    meta = list()
    for (k, v) in kwargs.items():
        cm = CampaignMetaModel()
        cm.key = k
        cm.value = v
        cm.campaign_id = campaign.id
        meta.append(cm)

    with transaction_session(auto_commit=True) as session:
        for cm in meta:
            session.add(cm)

    associate_user(campaign, organizer, 'organizer', 'participating')
    return campaign
Esempio n. 6
0
def create(organizer, name, description, start=None, end=None, **kwargs):
    """Create and return a new instance of
    :class:`pooldlib.postgresql.models.Campaign`.

    :param origanizer: The user to be classified as the campaign's organizing
                       member.
    :type origanizer: :class:`pooldlib.postgresql.models.User`
    :param name: The name of the campaign
    :type name: string
    :param description: The description of the campaign
    :type description: 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: Metadata to associate with the new campaign.
    :type kwargs: kwarg dictionary

    :returns: :class:`pooldlib.postgresql.models.Campaign`
    """
    campaign = CampaignModel()
    campaign.name = name
    campaign.description = description
    campaign.start = start or pytz.UTC.localize(datetime.utcnow())
    campaign.end = end

    with transaction_session() as session:
        session.add(campaign)
        session.commit()

    meta = list()
    for (k, v) in kwargs.items():
        cm = CampaignMetaModel()
        cm.key = k
        cm.value = v
        cm.campaign_id = campaign.id
        meta.append(cm)

    with transaction_session(auto_commit=True) as session:
        for cm in meta:
            session.add(cm)

    associate_user(campaign, organizer, 'organizer', 'participating')
    return campaign