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
def create_campaign(self, name, description, start=None, end=None): if start is None: start = datetime.utcnow() if end is None: end = start + timedelta(days=30) c = Campaign() c.name = name c.description = description c.enabled = True c.start = start c.end = end self.commit_model(c) return c