def distribute_welfare_gift(user, welfare_package_kind, voucher=None, allow_piling_firewood=True): # voucher是获取礼包的凭证根据(如用户、订单、资产等),亦可无条件发放 from core.models.group import welfare_reminder_group from core.models.notification import Notification from core.models.notification.kind import welfare_gift_notification package = welfare_package_kind.distributor.bestow(user, voucher) if package: # 拆包发礼券 # 针对新结算产品,动态分配抵扣金 package.unpack(user, allow_piling_firewood=allow_piling_firewood) # 本地记录 rsyslog.send('\t'.join([user.id_, package.id_]), tag='welfare_package_distribution') notification_voucher = (welfare_package_kind.coupon_wrappers or welfare_package_kind.firewood_wrapper.worth) if notification_voucher: # 提醒用户新增福利 welfare_reminder_group.add_member(user.id_) # 添加消息通知 Notification.create(user.id_, welfare_gift_notification, dict(welfare_package_id=package.id_)) return package
def get_all_notifications(): if not g.user: abort(401) all_unreads = Notification.get_multi_unreads_by_user(g.user.id_) welfare_unreads = [] spring_reserved_unreads = [] spring_obtained_unreads = [] for notice in all_unreads: if notice.kind is welfare_gift_notification: welfare_unreads.append(notice) elif notice.kind is spring_gift_reserved_notification: spring_reserved_unreads.append(notice) elif notice.kind is spring_gift_obtained_notification: spring_obtained_unreads.append(notice) targets = spring_obtained_unreads or spring_reserved_unreads or welfare_unreads if not targets: return jsonify(template=None) template = Notification.get_merged_popout_template(targets) # simply mark all unreads read once here requested for t in all_unreads: t.mark_as_read() return jsonify(template=template)
def notify_spring_gift(user, gift): """创建体验金通知.""" if gift.status is SpringGift.Status.reserved: Notification.create(user, spring_gift_reserved_notification, dict(spring_gift_id=gift.id_)) elif gift.status is SpringGift.Status.obtained: Notification.create(user, spring_gift_obtained_notification, dict(spring_gift_id=gift.id_))
def main(): """获取在两天后过期的礼券用户情况(用户X有Y张礼券在1天后过期)""" expiration_date = date.today() + timedelta(days=1) for summary in get_user_coupon_expiration(expiration_date): # 添加消息通知 Notification.create( summary.user_id, coupon_expiring_notification, dict(expiring_coupon_count=summary.expiring_coupon_count))
def notification(): notices = Notification.get_multi_by_user(g.user.id_) origin_bulletins = Bulletin.get_multi() bulletins = sorted(origin_bulletins, key=lambda x: x.creation_time, reverse=True) # 当用户打开通知页面则默认全部消息为已读 unreads = Notification.get_multi_unreads_by_user(g.user.id_) for u in unreads: u.mark_as_read() return render_template('notification/index.html', notices=notices, bulletins=bulletins[:1])
def index(): if not g.user: return redirect(url_for('accounts.login.login', next=request.path)) notices = Notification.get_multi_by_user(g.user.id_) # 当用户打开通知页面则默认全部消息为已读 unreads = Notification.get_multi_unreads_by_user(g.user.id_) for u in unreads: u.mark_as_read() return render_template('notification/index.html', notices=notices)
def notification_unicast_push(notification_id): """通知(Notification)的单播 适用于用户行为触发性、面向用户、与用户个体有关的通知 """ from core.models.notification import Notification from core.models.pusher import DeviceBinding, UserPushRecord from core.models.pusher.element import SingleDeviceAudience notice = Notification.get(notification_id) if not notice.allow_push: return bindings = DeviceBinding.get_multi_by_user(notice.user_id) bindings = [b for b in bindings if b.platform in notice.push_platforms] for binding in bindings: # 如已推送,则跳过 record = UserPushRecord.get_by_device_and_notification( binding.device_id, notice.id_) if record and record.is_pushed: continue pack = notice.make_push_pack(audience=SingleDeviceAudience( binding.device_id), platform=binding.platform) # 如未创建记录则新建推送 if not record: record = UserPushRecord.create(notice.user, binding, notice) response = jpush.push(**pack.payload) record.mark_as_pushed(response.msg_id)
def initialize(): if not g.user: return redirect(url_for('accounts.login.login', next=request.path)) # 圣诞游戏获取红包的用户如果没有身份信息则跳转 game_gift = ChristmasGift.get_by_mobile_phone(g.user.mobile) if not has_real_identity( g.user) and game_gift and game_gift.rank.award.firewood_wrapper: return redirect(url_for('profile.auth.supply', next=request.path)) # 礼券管理 g.coupon_manager = CouponManager(g.user.id_) # 为用户创建抵扣金账户 g.firewood_flow = FirewoodWorkflow(g.user.id_) # 用户浏览任意福利相关页,则关闭福利提醒提示 welfare_reminder_group.remove_member(g.user.id_) # 用户红包记录 pileds = FirewoodPiling.get_multi_by_user(g.user.id_) burneds = FirewoodBurning.get_multi_by_user(g.user.id_) g.records = sorted(pileds + burneds, key=attrgetter('creation_time'), reverse=True) # 临时:用户访问该页面时默认将相关通知置为已读 unread_notices = Notification.get_multi_unreads_by_user(g.user.id_) unread_notices = [ n for n in unread_notices if n.kind is welfare_gift_notification ] for un in unread_notices: un.mark_as_read()
def has_new_notifications(): """用户通知. :reqheader Authorization: OAuth 2.0 Bearer Token :status 200: 返回 :class:`NewNotificationCheckSchema` """ schema = NewNotificationCheckSchema(strict=True) data = { 'has_new_notification': bool(Notification.get_multi_unreads_by_user(request.oauth.user.id_)) } return jsonify(success=True, data=schema.dump(data).data)
def notification(self): from core.models.notification import Notification return Notification.get(self.notification_id)