示例#1
0
    def __init__(self, **kwargs):
        bounty = kwargs['bounty']
        url = kwargs['url']
        user = kwargs['user']
        issuer = kwargs['issuer']
        from_user = kwargs['from_user']
        notification_name = kwargs['notification_name']
        review = kwargs.get('review')
        comment = kwargs.get('comment')
        description = kwargs.get('fulfillment_description', '')
        preview_text = kwargs.get('string_data', '')
        application_message = kwargs.get('application_message', '')
        rejection_message = kwargs.get('rejection_message', '')

        if notification_name.__class__ != int:
            raise TypeError('notification_name must be of type int')
        elif notification_name not in Email.templates:
            raise ValueError(
                'notification_name {} must be a valid notification'.format(
                    notification_name))
        if bounty.__class__ != Bounty:
            raise TypeError('bounty must be of type Bounty')

        remaining = token_decimals(bounty.calculated_balance)
        token_amount = token_decimals(bounty.calculated_fulfillment_amount)

        if len(description) > self.max_description_length:
            # Cut off at the closest word after the limit
            description = wrap(description,
                               self.max_description_length)[0] + ' ...'

        title = bounty.title
        if len(title) > self.max_title_length:
            # Cut off at the closest word after the limit
            title = wrap(title, self.max_title_length)[0] + ' ...'

        if not url or len(url) == 0:
            url = bounty_url_for(bounty.id, bounty.platform)

        remaining_submissions = 0

        if (notification_name == constants.BOUNTY_EXPIRED
                or notification_name == constants.CONTRIBUTION_RECEIVED
                or notification_name == constants.CONTRIBUTION_ADDED):
            remaining_submissions = Fulfillment.objects.filter(
                bounty_id=bounty.id,
                accepted=False,
            ).all().count()
        total_submissions = Fulfillment.objects.filter(
            bounty_id=bounty.id,
            accepted=False,
        ).all().count()
        submissions = ''
        if total_submissions == 1:
            submissions = '{} submission'.format(total_submissions)
        else:
            submissions = '{} submissions'.format(total_submissions)

        remaining_usd = ' unknown'
        if bounty.token_lock_price:
            remaining_usd = usd_decimals(remaining *
                                         usd_decimals(bounty.token_lock_price))
        elif bounty.token and bounty.token.price_usd:
            remaining_usd = usd_decimals(remaining *
                                         usd_decimals(bounty.token.price_usd))

        added_amount = 0
        if (notification_name == constants.CONTRIBUTION_RECEIVED
                or notification_name == constants.CONTRIBUTION_ADDED):
            amount = kwargs['amount']
            added_amount = token_decimals(
                calculate_token_value(int(Decimal(amount)),
                                      bounty.token_decimals))

        rating_url = url
        if notification_name == constants.FULFILLMENT_ACCEPTED_FULFILLER:
            rating_url = '{}?fulfillment_id={}&rating=true'.format(
                url, kwargs['fulfillment_id'])

        user_address_link = (user and profile_url_for(user.public_address,
                                                      bounty.platform))

        ratings = None
        rating_link = None
        if notification_name == constants.RATING_RECEIVED:
            user_reviewees = user.reviewees.filter(platform=bounty.platform)
            rating_link = user_address_link + '?reviews=true'

            if user.public_address == issuer.public_address:
                # Rating for the issuer from the fulfiller
                ratings = user_reviewees.filter(issuer_review__isnull=False)
            else:
                # Rating for the fulfiller from the issuer
                ratings = user_reviewees.filter(
                    fulfillment_review__isnull=False)
                rating_link += '&fulfiller=true'

        rating_count = ratings and ratings.count() or 0
        average_rating = ratings and ratings.aggregate(
            Avg('rating')).get('rating__avg') or 0

        self.__dict__.update({
            'bounty':
            bounty,
            'bounty_title':
            title,
            'url':
            url,
            'preferences_link':
            'https://{}bounties.network/settings'.format(
                '' if ENVIRONMENT == 'production' else 'staging.'),
            'notification_name':
            notification_name,
            'usd_amount':
            usd_decimals(bounty.usd_price),
            'token_amount':
            token_amount,
            'token':
            bounty.token_symbol,
            'token_amount_remaining':
            remaining,
            'usd_amount_remaining':
            remaining_usd,
            'added_amount':
            added_amount,
            'remaining_submissions':
            remaining_submissions,
            'fulfillment_description':
            description,
            'application_message':
            application_message,
            'issuer_name':
            issuer and issuer.name,
            'issuer_address':
            issuer and shorten_address(issuer.public_address),
            'issuer_profile_image': (issuer and issuer.small_profile_image_url
                                     or default_image),
            'issuer_address_link':
            issuer and profile_url_for(issuer.public_address, bounty.platform),
            'user_name':
            user and user.name,
            'user_address':
            user and shorten_address(user.public_address),
            'user_profile_image': (user and user.small_profile_image_url
                                   or default_image),
            'user_address_link':
            user_address_link,
            'from_user_name':
            from_user and from_user.name,
            'from_user_address':
            from_user and shorten_address(from_user.public_address),
            'from_user_profile_image':
            (from_user and from_user.small_profile_image_url or default_image),
            'from_user_address_link':
            from_user
            and profile_url_for(from_user.public_address, bounty.platform),
            'from_user_email':
            from_user and from_user.email,
            'review':
            review and review.review,
            'rating':
            review and '{}/5'.format(review.rating),
            'rating_color':
            review and Email.rating_color(review.rating),
            'comment':
            comment and comment.text,
            'MC_PREVIEW_TEXT':
            preview_text,
            'rating_url':
            rating_url,
            'average_rating':
            usd_decimals(average_rating),
            'rating_count':
            rating_count,
            'rating_link':
            rating_link,
            'contribute_url':
            url + '?contribute=true',
            'submissions':
            submissions,
            'rejection_message':
            rejection_message
        })
示例#2
0
    def contribution_added(self, bounty_id, event_date, inputs,
                           transaction_from, uid, **kwargs):
        bounty = Bounty.objects.get(id=bounty_id)

        try:
            from_user = transaction_from and User.objects.get(
                public_address=transaction_from.lower())
        except User.DoesNotExist:
            logger.error('No user for address: {}'.format(
                transaction_from.lower()))
            return

        amount = '{} {}'.format(
            bounty.tokenSymbol,
            token_decimals(
                calculate_token_value(int(Decimal(inputs['value'])),
                                      bounty.tokenDecimals)))

        added_string_data = notification_templates['ContributionAdded'].format(
            bounty_title=bounty.title, amount=amount)
        received_string_data = notification_templates[
            'ContributionReceived'].format(bounty_title=bounty.title,
                                           amount=amount)

        if bounty.user == from_user:
            # activity to bounty issuer
            create_bounty_notification(
                bounty=bounty,
                uid='{}-{}-activity'.format(uid, bounty.user.public_address),
                notification_name=notifications['ContributionAdded'],
                user=bounty.user,
                from_user=None,
                string_data=added_string_data,
                notification_created=event_date,
                inputs=inputs,
                subject='Contribution Added',
                is_activity=True)
        else:
            # notification to bounty issuer
            create_bounty_notification(
                bounty=bounty,
                uid='{}-{}-notification'.format(uid,
                                                bounty.user.public_address),
                notification_name=notifications['ContributionReceived'],
                user=bounty.user,
                from_user=from_user,
                string_data=received_string_data,
                notification_created=event_date,
                inputs=inputs,
                subject='Contribution Received',
                is_activity=False)
            # activity to user contributing to the bounty
            create_bounty_notification(
                bounty=bounty,
                uid='{}-{}-activity'.format(uid, from_user.public_address),
                notification_name=notifications['ContributionAdded'],
                user=from_user,
                from_user=None,
                string_data=added_string_data,
                notification_created=event_date,
                inputs=inputs,
                subject='Contribution Added',
                is_activity=True)