def main():
    # 获取所有出生在当前日期的用户
    for user in get_users_by_birthday(date.today().strftime('%m%d')):
        distribute_welfare_gift(user, happy_birthday_package)

        # 向用户手机发送提醒短信
        if user.has_mobile():
            sms = ShortMessage.create(user.mobile, birthday_package_remind_sms)
            sms.send_async()
Example #2
0
def reserve_spring_gift(mobile_phone):
    """预约体验金."""
    # 给老用户派发礼包
    is_new = SpringGift.get_id_by_mobile_phone(mobile_phone) is None
    user = Account.get_by_alias(mobile_phone)

    # 给新老用户预约体验金
    gift = SpringGift.add(mobile_phone)

    # 为老用户发放礼包并创建通知
    if gift and is_new and user:
        distribute_welfare_gift(user, spring_2016_package)
        notify_spring_gift(user, gift)

    return gift
Example #3
0
    def award(self):
        """向手机号对应用户发放礼包"""
        if self.is_awarded:
            return

        # lock it
        sql = 'select is_awarded from {.table_name} where mobile_phone = %s for update'.format(self)
        params = (self.mobile_phone,)
        rs = db.execute(sql, params)
        if rs and rs[0][0]:
            db.rollback()  # release lock
            return

        try:
            user = Account.get_by_alias(self.mobile_phone)
            sql = 'update {.table_name} set is_awarded=%s where id=%s'.format(self)
            params = (True, self.id_)
            db.execute(sql, params)
        except:
            db.rollback()  # release lock
            raise
        else:
            db.commit()

        self.is_awarded = True

        # distribute package and record
        package = distribute_welfare_gift(
            user, self.rank.award, allow_piling_firewood=bool(Identity.get(user.id_)))
        sql = 'update {.table_name} set awarded_package_id=%s where id=%s'.format(self)
        params = (package.id_, self.id_)
        db.execute(sql, params)
        db.commit()

        self.clear_cache(self.id_)
        self.clear_cache_by_mobile_phone(self.mobile_phone)
Example #4
0
 def _redeem_normal_package(self, user, redeem_code_usage):
     distribute_welfare_gift(user,
                             self.activity.reward_welfare_package_kind,
                             redeem_code_usage)
Example #5
0
 def send(self, user_id):
     user = Account.get(user_id)
     distribute_welfare_gift(user, self.package)