Beispiel #1
0
    def get(self):
        client_id = int(self.request.headers.get('Client-Id') or 0)
        if not client_id:
            self.abort(400)
        client = Client.get(client_id)
        if not client:
            self.abort(400)

        if not config.SHARE_INVITATION_ENABLED:
            self.abort(403)
        share = Share.query(Share.sender == client.key, Share.status == Share.ACTIVE,
                            Share.share_type == branch_io.INVITATION).get()
        logging.info('Found share: %s' % share)
        if share and not share.promo_code and not share.channel_urls:
            self.abort(412)
        if not share:
            share = Share(share_type=branch_io.INVITATION, sender=client.key)
            share.put()  # need share id

            if 'iOS' in self.request.headers["User-Agent"]:
                user_agent = 'ios'
            elif 'Android' in self.request.headers["User-Agent"]:
                user_agent = 'android'
            else:
                user_agent = 'unknown'
            urls = [{
                'url': branch_io.create_url(share.key.id(), branch_io.INVITATION, channel, user_agent),
                'channel': channel
            } for channel in branch_io.CHANNELS]
            share.channel_urls = [ChannelUrl(url=url['url'], channel=url['channel']) for url in urls]
            group_promo_codes = PromoCodeGroup()
            group_promo_codes.put()
            promo_code = PromoCode.create(group_promo_codes, KIND_SHARE_INVITATION, 100000, title=u'Переход по совету друга')
            share.promo_code = promo_code.key
            share.put()

        self.render_json({
            'text': config.SHARE_INVITATION_MODULE.invitation_text,
            'urls': [channel_url.dict() for channel_url in share.channel_urls],
            'image': config.SHARE_INVITATION_MODULE.invitation_image,
            'promo_code': share.promo_code.id()
        })
Beispiel #2
0
 def success(self, sender, gift, promo_code, sender_phone, sender_email, reciptient_name, recipient_phone):
     share = Share(share_type=branch_io.GIFT, sender=sender.key)
     share.put()
     if 'iOS' in self.request.headers["User-Agent"]:
         user_agent = 'ios'
     elif 'Android' in self.request.headers["User-Agent"]:
         user_agent = 'android'
     else:
         user_agent = 'unknown'
     recipient = {
         'name': reciptient_name,
         'phone': recipient_phone
     }
     url = branch_io.create_url(share.key.id(), branch_io.INVITATION, branch_io.SMS, user_agent, recipient=recipient)
     share.channel_urls = [ChannelUrl(channel=SMS, url=url)]
     share.put()
     gift.share_id = share.key.id()
     gift.put()
     text = u'Вам подарок от %s в приложении %s! Установите его: %s или введите в нем промо-код %s' % \
            (sender_phone, config.APP_NAME, url, promo_code.key.id())
     self.render_json({
         'success': True,
         'sms_text': text
     })