def create_notification(bounty,
                        notification_name,
                        user,
                        notification_created,
                        string_data,
                        subject,
                        is_activity=True,
                        email=False):
    bounty_url = bounty_url_for(bounty.bounty_id, bounty.platform)
    notification = Notification.objects.create(
        notification_name=notification_name,
        user=user,
        notification_created=notification_created,
        email=email,
        dashboard=True)
    DashboardNotification.objects.create(
        notification=notification,
        string_data=string_data,
        is_activity=is_activity,
        data={'link': bounty_url},
    )
    username = bounty.user.name or 'bounty hunter'
    email_html = render_to_string('base_notification.html',
                                  context={
                                      'link': bounty_url,
                                      'username': username,
                                      'message_string': string_data
                                  })
    email_txt = 'Hello {}! \n {} \n View in app: {}'.format(
        username, string_data, bounty_url)
    if bounty.platform != 'gitcoin':
        send_email(bounty.user.email, subject, email_txt, email_html)
def bounty_fulfilled(bounty_id, **kwargs):
    fulfillment_id = kwargs.get('fulfillment_id')
    bounty = Bounty.objects.get(bounty_id=bounty_id)
    bounty_client.fulfill_bounty(bounty, **kwargs)
    notification_client.fulfillment_submitted(bounty_id, fulfillment_id)
    slack_client.bounty_fulfilled(bounty, fulfillment_id)

    if bounty.platform == 'colorado':
        email_url = bounty_url_for(bounty_id, platform='colorado')
    else:
        email_url = bounty_url_for(bounty_id)

    if bounty.platform != 'gitcoin':
        send_email(
            bounty.issuer_email, 'Bounty Contribution Received',
            'Hey there! You received a contribution for your bounty: {}. {}'.
            format(bounty.title, email_url))
def create_notification(**kwargs):
    uid = kwargs['uid']
    notification_name = kwargs['notification_name']
    string_data = kwargs['string_data']
    user = kwargs['user']
    from_user = kwargs['from_user']
    notification_created = kwargs['notification_created']
    bounty = kwargs.get('bounty')
    subject = kwargs['subject']
    platform = kwargs.get('platform', '')
    is_activity = kwargs.get('is_activity', True)
    url = kwargs.get('url', '')

    notification, created = Notification.objects.get_or_create(
        uid=str(uid),
        defaults={
            'notification_name': notification_name,
            'user': user,
            'from_user': from_user,
            'notification_created': notification_created,
            'dashboard': True,
            'platform': platform,
        },
    )

    # this function is atomic, so this is a good way to be sure
    # we never notify more than once
    if not created:
        return

    DashboardNotification.objects.create(
        notification=notification,
        string_data=string_data,
        is_activity=is_activity,
        data={
            'link': url,
            'bounty_title': bounty and bounty.title or kwargs.get('subject')
        },
    )

    # from here on out, all we care about is email
    if not user.email:
        return

    email_settings = user.settings.emails
    activity_emails = email_settings['activity']

    if is_activity and not activity_emails:
        return

    if (not is_activity and notification_name
            not in user.settings.accepted_email_settings()):
        return

    if platform not in settings.PLATFORM_MAPPING:
        return

    if notification.email_sent:
        return

    if notification_name not in Email.templates:
        return

    email = Email(**kwargs)

    email_html = email.render()

    send_email(user.email, subject, email_html)
    notification.email_sent = True
    notification.save()