コード例 #1
0
def send_claim_member_email(email, user, group, permission, auth=None, throttle=None, email_template='default'):
    """
    Unregistered user claiming a user account as a group member of an OSFGroup. Send an email for claiming the account.
    Sends to the given email

    :param str email: The address given in the claim user form
    :param User user: The User record to claim.
    :param OSFGroup group: The group where the user claimed their account.
    :return

    """

    claimer_email = email.lower().strip()
    claim_url = user.get_claim_url(group._id, external=True)

    throttle = throttle or settings.GROUP_MEMBER_ADDED_EMAIL_THROTTLE

    mails.send_mail(
        to_addr=claimer_email,
        mail=email_template,
        mimetype='html',
        user=user,
        group_name=group.name,
        referrer_name=auth.user.fullname if auth else '',
        permission=permission,
        claim_url=claim_url,
        osf_contact_email=settings.OSF_CONTACT_EMAIL,
    )
    user.member_added_email_records[group._id]['last_sent'] = get_timestamp()
    user.save()

    return claimer_email
コード例 #2
0
def notify_added_group_member(group, user, permission, auth=None, throttle=None, email_template='default', *args, **kwargs):
    if email_template == 'false':
        return

    throttle = throttle or settings.GROUP_MEMBER_ADDED_EMAIL_THROTTLE

    member_record = user.member_added_email_records.get(group._id, {})
    if member_record:
        timestamp = member_record.get('last_sent', None)
        if timestamp:
            if not throttle_period_expired(timestamp, throttle):
                return
    else:
        user.member_added_email_records[group._id] = {}

    if user.is_registered:
        email_template = mails.GROUP_MEMBER_ADDED
        mails.send_mail(
            to_addr=user.username,
            mail=email_template,
            mimetype='html',
            user=user,
            group_name=group.name,
            permission=permission,
            referrer_name=auth.user.fullname if auth else '',
            osf_contact_email=settings.OSF_CONTACT_EMAIL,
        )
        user.member_added_email_records[group._id]['last_sent'] = get_timestamp()
        user.save()

    else:
        unreg_member_added.send(group, user=user, permission=permission, auth=auth, throttle=throttle, email_template=email_template)
コード例 #3
0
def notify_added_node_group_member(group, node, user, permission, auth, throttle=None):
    throttle = throttle or settings.GROUP_CONNECTED_EMAIL_THROTTLE

    node_group_record = user.group_connected_email_records.get(group._id, {})
    if node_group_record:
        timestamp = node_group_record.get('last_sent', None)
        if timestamp:
            if not throttle_period_expired(timestamp, throttle):
                return
    else:
        user.group_connected_email_records[group._id] = {}

    if (not auth or auth.user != user) and user.is_registered:
        email_template = mails.GROUP_ADDED_TO_NODE
        mails.send_mail(
            to_addr=user.username,
            mail=email_template,
            mimetype='html',
            user=user,
            node=node,
            all_global_subscriptions_none=check_if_all_global_subscriptions_are_none(user),
            group_name=group.name,
            permission=permission,
            referrer_name=auth.user.fullname if auth else '',
            osf_contact_email=settings.OSF_CONTACT_EMAIL,
        )

        user.group_connected_email_records[group._id]['last_sent'] = get_timestamp()
        user.save()
コード例 #4
0
def notify_added_contributor(node, contributor, auth=None, throttle=None, email_template='default'):
    if email_template == 'false':
        return

    throttle = throttle or settings.CONTRIBUTOR_ADDED_EMAIL_THROTTLE

    # Email users for projects, or for components where they are not contributors on the parent node.
    if contributor.is_registered and \
            (not node.parent_node or (node.parent_node and not node.parent_node.is_contributor(contributor))):

        mimetype = 'html'
        preprint_provider = None
        logo = None
        if email_template == 'preprint':
            email_template, preprint_provider = find_preprint_provider(node)
            if not email_template or not preprint_provider:
                return
            email_template = getattr(mails, 'CONTRIBUTOR_ADDED_PREPRINT')(email_template, preprint_provider)
            if preprint_provider._id == 'osf':
                logo = settings.OSF_PREPRINTS_LOGO
            else:
                logo = preprint_provider._id
        elif email_template == 'access_request':
            mimetype = 'html'
            email_template = getattr(mails, 'CONTRIBUTOR_ADDED_ACCESS_REQUEST'.format(email_template.upper()))
        elif node.is_preprint:
            email_template = getattr(mails, 'CONTRIBUTOR_ADDED_PREPRINT_NODE_FROM_OSF'.format(email_template.upper()))
            logo = settings.OSF_PREPRINTS_LOGO
        else:
            email_template = getattr(mails, 'CONTRIBUTOR_ADDED_DEFAULT'.format(email_template.upper()))

        contributor_record = contributor.contributor_added_email_records.get(node._id, {})
        if contributor_record:
            timestamp = contributor_record.get('last_sent', None)
            if timestamp:
                if not throttle_period_expired(timestamp, throttle):
                    return
        else:
            contributor.contributor_added_email_records[node._id] = {}

        mails.send_mail(
            contributor.username,
            email_template,
            mimetype=mimetype,
            user=contributor,
            node=node,
            referrer_name=auth.user.fullname if auth else '',
            all_global_subscriptions_none=check_if_all_global_subscriptions_are_none(contributor),
            branded_service=preprint_provider,
            can_change_preferences=False,
            logo=logo if logo else settings.OSF_LOGO,
            osf_contact_email=settings.OSF_CONTACT_EMAIL
        )

        contributor.contributor_added_email_records[node._id]['last_sent'] = get_timestamp()
        contributor.save()

    elif not contributor.is_registered:
        unreg_contributor_added.send(node, contributor=contributor, auth=auth, email_template=email_template)
コード例 #5
0
def notify_added_contributor(node, contributor, auth=None, throttle=None, email_template='default'):
    if email_template == 'false':
        return

    throttle = throttle or settings.CONTRIBUTOR_ADDED_EMAIL_THROTTLE

    # Email users for projects, or for components where they are not contributors on the parent node.
    if contributor.is_registered and \
            (not node.parent_node or (node.parent_node and not node.parent_node.is_contributor(contributor))):

        mimetype = 'html'
        preprint_provider = None
        logo = None
        if email_template == 'preprint':
            email_template, preprint_provider = find_preprint_provider(node)
            if not email_template or not preprint_provider:
                return
            email_template = getattr(mails, 'CONTRIBUTOR_ADDED_PREPRINT')(email_template, preprint_provider)
            if preprint_provider._id == 'osf':
                logo = settings.OSF_PREPRINTS_LOGO
            else:
                logo = preprint_provider._id
        elif email_template == 'access_request':
            mimetype = 'html'
            email_template = getattr(mails, 'CONTRIBUTOR_ADDED_ACCESS_REQUEST'.format(email_template.upper()))
        elif node.is_preprint:
            email_template = getattr(mails, 'CONTRIBUTOR_ADDED_PREPRINT_NODE_FROM_OSF'.format(email_template.upper()))
            logo = settings.OSF_PREPRINTS_LOGO
        else:
            email_template = getattr(mails, 'CONTRIBUTOR_ADDED_DEFAULT'.format(email_template.upper()))

        contributor_record = contributor.contributor_added_email_records.get(node._id, {})
        if contributor_record:
            timestamp = contributor_record.get('last_sent', None)
            if timestamp:
                if not throttle_period_expired(timestamp, throttle):
                    return
        else:
            contributor.contributor_added_email_records[node._id] = {}

        mails.send_mail(
            contributor.username,
            email_template,
            mimetype=mimetype,
            user=contributor,
            node=node,
            referrer_name=auth.user.fullname if auth else '',
            all_global_subscriptions_none=check_if_all_global_subscriptions_are_none(contributor),
            branded_service=preprint_provider,
            can_change_preferences=False,
            logo=logo if logo else settings.OSF_LOGO,
            osf_contact_email=settings.OSF_CONTACT_EMAIL
        )

        contributor.contributor_added_email_records[node._id]['last_sent'] = get_timestamp()
        contributor.save()

    elif not contributor.is_registered:
        unreg_contributor_added.send(node, contributor=contributor, auth=auth, email_template=email_template)
コード例 #6
0
ファイル: contributor.py プロジェクト: mfraezz/osf.io
def send_claim_registered_email(claimer, unclaimed_user, node, throttle=24 * 3600):
    """
    A registered user claiming the unclaimed user account as an contributor to a project.
    Send an email for claiming the account to the referrer and notify the claimer.

    :param claimer: the claimer
    :param unclaimed_user: the user account to claim
    :param node: the project node where the user account is claimed
    :param throttle: the time period in seconds before another claim for the account can be made
    :return:
    :raise: http.BAD_REQUEST
    """

    unclaimed_record = unclaimed_user.get_unclaimed_record(node._primary_key)

    # check throttle
    timestamp = unclaimed_record.get('last_sent')
    if not throttle_period_expired(timestamp, throttle):
        raise HTTPError(http.BAD_REQUEST, data=dict(
            message_long='User account can only be claimed with an existing user once every 24 hours'
        ))

    # roll the valid token for each email, thus user cannot change email and approve a different email address
    verification_key = generate_verification_key(verification_type='claim')
    unclaimed_record['token'] = verification_key['token']
    unclaimed_record['expires'] = verification_key['expires']
    unclaimed_record['claimer_email'] = claimer.username
    unclaimed_user.save()

    referrer = OSFUser.load(unclaimed_record['referrer_id'])
    claim_url = web_url_for(
        'claim_user_registered',
        uid=unclaimed_user._primary_key,
        pid=node._primary_key,
        token=unclaimed_record['token'],
        _external=True,
    )

    # Send mail to referrer, telling them to forward verification link to claimer
    mails.send_mail(
        referrer.username,
        mails.FORWARD_INVITE_REGISTERED,
        user=unclaimed_user,
        referrer=referrer,
        node=node,
        claim_url=claim_url,
        fullname=unclaimed_record['name'],
    )
    unclaimed_record['last_sent'] = get_timestamp()
    unclaimed_user.save()

    # Send mail to claimer, telling them to wait for referrer
    mails.send_mail(
        claimer.username,
        mails.PENDING_VERIFICATION_REGISTERED,
        fullname=claimer.fullname,
        referrer=referrer,
        node=node,
    )
コード例 #7
0
def notify_added_contributor(node, contributor, auth=None, email_template='default', throttle=None, *args, **kwargs):
    logo = settings.OSF_LOGO
    if check_email_throttle(node, contributor, throttle=throttle):
        return
    if email_template == 'false':
        return
    if not getattr(node, 'is_published', True):
        return
    if not contributor.is_registered:
        unreg_contributor_added.send(
            node,
            contributor=contributor,
            auth=auth,
            email_template=email_template
        )
        return

    # Email users for projects, or for components where they are not contributors on the parent node.
    contrib_on_parent_node = isinstance(node, (Preprint, DraftRegistration)) or \
                             (not node.parent_node or (node.parent_node and not node.parent_node.is_contributor(contributor)))
    if contrib_on_parent_node:
        if email_template == 'preprint':
            if node.provider.is_default:
                email_template = mails.CONTRIBUTOR_ADDED_OSF_PREPRINT
                logo = settings.OSF_PREPRINTS_LOGO
            else:
                email_template = mails.CONTRIBUTOR_ADDED_PREPRINT(node.provider)
                logo = node.provider._id
        elif email_template == 'draft_registration':
            email_template = mails.CONTRIBUTOR_ADDED_DRAFT_REGISTRATION
        elif email_template == 'access_request':
            email_template = mails.CONTRIBUTOR_ADDED_ACCESS_REQUEST
        elif node.has_linked_published_preprints:
            # Project holds supplemental materials for a published preprint
            email_template = mails.CONTRIBUTOR_ADDED_PREPRINT_NODE_FROM_OSF
            logo = settings.OSF_PREPRINTS_LOGO
        else:
            email_template = mails.CONTRIBUTOR_ADDED_DEFAULT

        mails.send_mail(
            contributor.username,
            email_template,
            user=contributor,
            node=node,
            referrer_name=auth.user.fullname if auth else '',
            is_initiator=getattr(auth, 'user', False) == contributor,
            all_global_subscriptions_none=check_if_all_global_subscriptions_are_none(contributor),
            branded_service=node.provider,
            can_change_preferences=False,
            logo=logo,
            osf_contact_email=settings.OSF_CONTACT_EMAIL,
            published_preprints=[] if isinstance(node, (Preprint, DraftRegistration)) else serialize_preprints(node, user=None)
        )

        contributor.contributor_added_email_records[node._id]['last_sent'] = get_timestamp()
        contributor.save()
コード例 #8
0
ファイル: contributor.py プロジェクト: aaxelb/osf.io
def send_claim_email(email, unclaimed_user, node, notify=True, throttle=24 * 3600, email_template='default'):
    """
    Unregistered user claiming a user account as an contributor to a project. Send an email for claiming the account.
    Either sends to the given email or the referrer's email, depending on the email address provided.

    :param str email: The address given in the claim user form
    :param User unclaimed_user: The User record to claim.
    :param Node node: The node where the user claimed their account.
    :param bool notify: If True and an email is sent to the referrer, an email
        will also be sent to the invited user about their pending verification.
    :param int throttle: Time period (in seconds) after the referrer is
        emailed during which the referrer will not be emailed again.
    :param str email_template: the email template to use
    :return
    :raise http.BAD_REQUEST

    """

    claimer_email = email.lower().strip()
    unclaimed_record = unclaimed_user.get_unclaimed_record(node._primary_key)
    referrer = OSFUser.load(unclaimed_record['referrer_id'])
    claim_url = unclaimed_user.get_claim_url(node._primary_key, external=True)

    # Option 1:
    #   When adding the contributor, the referrer provides both name and email.
    #   The given email is the same provided by user, just send to that email.
    preprint_provider = None
    logo = None
    if unclaimed_record.get('email') == claimer_email:
        # check email template for branded preprints
        if email_template == 'preprint':
            email_template, preprint_provider = find_preprint_provider(node)
            if not email_template or not preprint_provider:
                return
            mail_tpl = getattr(mails, 'INVITE_PREPRINT')(email_template, preprint_provider)
            if preprint_provider._id == 'osf':
                logo = settings.OSF_PREPRINTS_LOGO
            else:
                logo = preprint_provider._id
        else:
            mail_tpl = getattr(mails, 'INVITE_DEFAULT'.format(email_template.upper()))

        to_addr = claimer_email
        unclaimed_record['claimer_email'] = claimer_email
        unclaimed_user.save()
    # Option 2:
    # TODO: [new improvement ticket] this option is disabled from preprint but still available on the project page
    #   When adding the contributor, the referred only provides the name.
    #   The account is later claimed by some one who provides the email.
    #   Send email to the referrer and ask her/him to forward the email to the user.
    else:
        # check throttle
        timestamp = unclaimed_record.get('last_sent')
        if not throttle_period_expired(timestamp, throttle):
            raise HTTPError(http.BAD_REQUEST, data=dict(
                message_long='User account can only be claimed with an existing user once every 24 hours'
            ))
        # roll the valid token for each email, thus user cannot change email and approve a different email address
        verification_key = generate_verification_key(verification_type='claim')
        unclaimed_record['last_sent'] = get_timestamp()
        unclaimed_record['token'] = verification_key['token']
        unclaimed_record['expires'] = verification_key['expires']
        unclaimed_record['claimer_email'] = claimer_email
        unclaimed_user.save()

        claim_url = unclaimed_user.get_claim_url(node._primary_key, external=True)
        # send an email to the invited user without `claim_url`
        if notify:
            pending_mail = mails.PENDING_VERIFICATION
            mails.send_mail(
                claimer_email,
                pending_mail,
                user=unclaimed_user,
                referrer=referrer,
                fullname=unclaimed_record['name'],
                node=node,
                can_change_preferences=False,
                osf_contact_email=settings.OSF_CONTACT_EMAIL,
            )
        mail_tpl = mails.FORWARD_INVITE
        to_addr = referrer.username

    # Send an email to the claimer (Option 1) or to the referrer (Option 2) with `claim_url`
    mails.send_mail(
        to_addr,
        mail_tpl,
        user=unclaimed_user,
        referrer=referrer,
        node=node,
        claim_url=claim_url,
        email=claimer_email,
        fullname=unclaimed_record['name'],
        branded_service=preprint_provider,
        can_change_preferences=False,
        logo=logo if logo else settings.OSF_LOGO,
        osf_contact_email=settings.OSF_CONTACT_EMAIL,
    )

    return to_addr
コード例 #9
0
ファイル: contributor.py プロジェクト: sergiy-neurohub/osf.io
def send_claim_email(email,
                     unclaimed_user,
                     node,
                     notify=True,
                     throttle=24 * 3600,
                     email_template='default'):
    """
    Unregistered user claiming a user account as an contributor to a project. Send an email for claiming the account.
    Either sends to the given email or the referrer's email, depending on the email address provided.

    :param str email: The address given in the claim user form
    :param User unclaimed_user: The User record to claim.
    :param Node node: The node where the user claimed their account.
    :param bool notify: If True and an email is sent to the referrer, an email
        will also be sent to the invited user about their pending verification.
    :param int throttle: Time period (in seconds) after the referrer is
        emailed during which the referrer will not be emailed again.
    :param str email_template: the email template to use
    :return
    :raise http_status.HTTP_400_BAD_REQUEST

    """

    claimer_email = email.lower().strip()
    unclaimed_record = unclaimed_user.get_unclaimed_record(node._primary_key)
    referrer = OSFUser.load(unclaimed_record['referrer_id'])
    claim_url = unclaimed_user.get_claim_url(node._primary_key, external=True)

    # Option 1:
    #   When adding the contributor, the referrer provides both name and email.
    #   The given email is the same provided by user, just send to that email.
    preprint_provider = None
    logo = None
    if unclaimed_record.get('email') == claimer_email:
        # check email template for branded preprints
        if email_template == 'preprint':
            email_template, preprint_provider = find_preprint_provider(node)
            if not email_template or not preprint_provider:
                return
            mail_tpl = getattr(mails, 'INVITE_PREPRINT')(email_template,
                                                         preprint_provider)
            if preprint_provider._id == 'osf':
                logo = settings.OSF_PREPRINTS_LOGO
            else:
                logo = preprint_provider._id
        else:
            mail_tpl = getattr(mails,
                               'INVITE_DEFAULT'.format(email_template.upper()))

        to_addr = claimer_email
        unclaimed_record['claimer_email'] = claimer_email
        unclaimed_user.save()
    # Option 2:
    # TODO: [new improvement ticket] this option is disabled from preprint but still available on the project page
    #   When adding the contributor, the referred only provides the name.
    #   The account is later claimed by some one who provides the email.
    #   Send email to the referrer and ask her/him to forward the email to the user.
    else:
        # check throttle
        timestamp = unclaimed_record.get('last_sent')
        if not throttle_period_expired(timestamp, throttle):
            raise HTTPError(
                http_status.HTTP_400_BAD_REQUEST,
                data=dict(
                    message_long=
                    'User account can only be claimed with an existing user once every 24 hours'
                ))
        # roll the valid token for each email, thus user cannot change email and approve a different email address
        verification_key = generate_verification_key(verification_type='claim')
        unclaimed_record['last_sent'] = get_timestamp()
        unclaimed_record['token'] = verification_key['token']
        unclaimed_record['expires'] = verification_key['expires']
        unclaimed_record['claimer_email'] = claimer_email
        unclaimed_user.save()

        claim_url = unclaimed_user.get_claim_url(node._primary_key,
                                                 external=True)
        # send an email to the invited user without `claim_url`
        if notify:
            pending_mail = mails.PENDING_VERIFICATION
            mails.send_mail(
                claimer_email,
                pending_mail,
                user=unclaimed_user,
                referrer=referrer,
                fullname=unclaimed_record['name'],
                node=node,
                can_change_preferences=False,
                osf_contact_email=settings.OSF_CONTACT_EMAIL,
            )
        mail_tpl = mails.FORWARD_INVITE
        to_addr = referrer.username

    # Send an email to the claimer (Option 1) or to the referrer (Option 2) with `claim_url`
    mails.send_mail(
        to_addr,
        mail_tpl,
        user=unclaimed_user,
        referrer=referrer,
        node=node,
        claim_url=claim_url,
        email=claimer_email,
        fullname=unclaimed_record['name'],
        branded_service=preprint_provider,
        can_change_preferences=False,
        logo=logo if logo else settings.OSF_LOGO,
        osf_contact_email=settings.OSF_CONTACT_EMAIL,
    )

    return to_addr
コード例 #10
0
ファイル: contributor.py プロジェクト: sergiy-neurohub/osf.io
def send_claim_registered_email(claimer,
                                unclaimed_user,
                                node,
                                throttle=24 * 3600):
    """
    A registered user claiming the unclaimed user account as an contributor to a project.
    Send an email for claiming the account to the referrer and notify the claimer.

    :param claimer: the claimer
    :param unclaimed_user: the user account to claim
    :param node: the project node where the user account is claimed
    :param throttle: the time period in seconds before another claim for the account can be made
    :return:
    :raise: http_status.HTTP_400_BAD_REQUEST
    """

    unclaimed_record = unclaimed_user.get_unclaimed_record(node._primary_key)

    # check throttle
    timestamp = unclaimed_record.get('last_sent')
    if not throttle_period_expired(timestamp, throttle):
        raise HTTPError(
            http_status.HTTP_400_BAD_REQUEST,
            data=dict(
                message_long=
                'User account can only be claimed with an existing user once every 24 hours'
            ))

    # roll the valid token for each email, thus user cannot change email and approve a different email address
    verification_key = generate_verification_key(verification_type='claim')
    unclaimed_record['token'] = verification_key['token']
    unclaimed_record['expires'] = verification_key['expires']
    unclaimed_record['claimer_email'] = claimer.username
    unclaimed_user.save()

    referrer = OSFUser.load(unclaimed_record['referrer_id'])
    claim_url = web_url_for(
        'claim_user_registered',
        uid=unclaimed_user._primary_key,
        pid=node._primary_key,
        token=unclaimed_record['token'],
        _absolute=True,
    )

    # Send mail to referrer, telling them to forward verification link to claimer
    mails.send_mail(
        referrer.username,
        mails.FORWARD_INVITE_REGISTERED,
        user=unclaimed_user,
        referrer=referrer,
        node=node,
        claim_url=claim_url,
        fullname=unclaimed_record['name'],
        can_change_preferences=False,
        osf_contact_email=settings.OSF_CONTACT_EMAIL,
    )
    unclaimed_record['last_sent'] = get_timestamp()
    unclaimed_user.save()

    # Send mail to claimer, telling them to wait for referrer
    mails.send_mail(
        claimer.username,
        mails.PENDING_VERIFICATION_REGISTERED,
        fullname=claimer.fullname,
        referrer=referrer,
        node=node,
        can_change_preferences=False,
        osf_contact_email=settings.OSF_CONTACT_EMAIL,
    )