Пример #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()
        })