示例#1
0
 def short_description(recipient, tip=0.0):
     if tip > 0:
         return string_length_limit(
             recipient.title, 127 - len(PaymentPlatform.DESC_FRUCTUS_TIP)
         ) + PaymentPlatform.DESC_FRUCTUS_TIP
     else:
         return string_length_limit(recipient.title, 127)
示例#2
0
    def description(self, max_length=-1):
        desc = ""
        if self.type == Entity.TYPE_MUSIC:
            if self.track:
                desc = self._join_limit(" - ", [
                    self.track, self.album, self.artist
                ], max_length)
            elif self.album:
                desc = self._join_limit(" - ", [
                    self.album, self.artist
                ], max_length)
            else:
                desc = string_length_limit(self.artist, max_length)
        else:
            desc = string_length_limit(self.title, max_length)

        return desc
示例#3
0
    def _join_limit(sep, iterable, max_length):
        text = sep.join(iterable)
        total_sep_length = len(iterable) * len(sep)
        if max_length == -1 or len(text) < max_length - total_sep_length:
            return text

        each_limit = (max_length - total_sep_length) / len(iterable)
        trimmed_items = []
        for item in iterable:
            trimmed_items.append(string_length_limit(item, each_limit))
        return sep.join(trimmed_items)
示例#4
0
    def donation_create(self, entity, recipient, payee,
                        amount, tip=0.0, **kwargs):
        amount = float(amount)
        tip = float(tip)
        if payee is None or payee.userservice is None:
            return None, None

        donation = self.db_donation_create(entity, recipient, payee,
                                           amount, tip=tip)

        # Create donation descriptions
        short_description = self.short_description(recipient, tip)
        long_description = self.long_description(recipient, amount, tip)

        params = {
            'account_id': payee.account_id,
            'short_description': short_description,
            'long_description': string_length_limit(long_description,
                                                    max_length=2047),
            'type': 'DONATION',
            'amount': amount + tip,
            'app_fee': tip,
            'fee_payer': 'Payee'
        }

        # TODO: this could do with a cleanup
        if 'base_url' in kwargs:
            if 'redirect_name' in kwargs:
                params['redirect_uri'] = kwargs['base_url'] + reverse(
                    kwargs['redirect_name'],
                    args=[donation.token]
                )

            if 'callback_name' in kwargs:
                params['callback_uri'] = kwargs['base_url'] + reverse(
                    kwargs['callback_name']
                )

        create_result = self.wepay.call(
            '/checkout/create', params,
            token=payee.userservice.data['access_token']
        )

        if 'checkout_id' in create_result:
            donation.checkout_id = create_result['checkout_id']
            donation.save()

        if 'checkout_uri' in create_result:
            return donation, create_result['checkout_uri']
        else:
            return donation, None