Example #1
0
def access_token_post_save_cb(sender, instance, created, raw, update_fields,
                              **kwargs):
    """
    Email a user to notify them of any new incoming connections.

    This signal is only used for projects using our deprecated OAuth2 method:
    American Gut, Harvard Personal Genome Project, and Wild Life of Our Homes.
    """
    if raw or not created:
        return

    # This separates our custom OAuth2 apps from direct sharing projects.
    try:
        app_label, _ = [
            x for x in get_source_labels_and_configs()
            if x[1].verbose_name == instance.application.name
        ][0]
    except IndexError:
        return

    # only notify the user the first time they connect
    if AccessToken.objects.filter(application=instance.application,
                                  user=instance.user).count() > 1:
        return

    url_slug = source_to_url_slug(app_label)
    activity_url = full_url(
        reverse('activity-management', kwargs={'source': url_slug}))

    send_connection_email(user=instance.user,
                          connection_name=instance.application.name,
                          activity_url=activity_url)
Example #2
0
def send_welcome_email(email_address):
    """
    Send a welcome email. Rendered as a separate function to enable testing.
    """
    sources = get_source_labels_and_configs()

    params = {
        'newsletter':
        email_address.user.member.newsletter,
        'public_sharing_url':
        full_url(reverse('public-data:home')),
        'sources': [
            s for s in sources if s[0] != 'data_selfie'
            and not getattr(email_address.user, s[0]).is_connected
        ],
        'research_data_management_url':
        full_url(reverse('my-member-research-data')),
        'data_selfie_url':
        full_url(reverse('activities:data-selfie:upload')),
    }

    plain = render_to_string('email/welcome.txt', params)
    html = render_to_string('email/welcome.html', params)

    send_mail('Welcome to Open Humans!',
              plain,
              settings.DEFAULT_FROM_EMAIL, [email_address.email],
              html_message=html)
Example #3
0
    def get_context_data(self, **kwargs):
        context = super(SourcesContextMixin, self).get_context_data(**kwargs)

        context.update({
            'sources':
            dict(get_source_labels_and_configs()),
            'activities': [
                activity for activity in get_activities()
                if not activity[1].in_development
            ],
            'in_development_activities': [
                activity for activity in get_activities()
                if activity[1].in_development
            ],
        })

        return context
Example #4
0
    def get_context_data(self, **kwargs):
        context = super(SourcesContextMixin, self).get_context_data(**kwargs)

        context.update(
            {
                "sources": dict(get_source_labels_and_configs()),
                "activities": [
                    activity
                    for activity in get_activities()
                    if not activity[1].in_development
                ],
                "in_development_activities": [
                    activity
                    for activity in get_activities()
                    if activity[1].in_development
                ],
            }
        )

        return context
Example #5
0
def user_social_auth_post_save_cb(sender, instance, created, raw,
                                  update_fields, **kwargs):
    """
    Email a user to notify them of any new outgoing connections.
    """
    if raw or not created:
        return

    # only notify the user the first time they connect
    if UserSocialAuth.objects.filter(provider=instance.provider,
                                     user=instance.user).count() > 1:
        return

    # Look up the related name and URL. Note, we've used app names that match
    # the UserSocialAuth 'provider' field in Python Social Auth.
    app_config = dict(get_source_labels_and_configs())[instance.provider]
    url_slug = source_to_url_slug(app_config.label)
    activity_url = full_url(
        reverse('activity-management', kwargs={'source': url_slug}))
    send_connection_email(user=instance.user,
                          connection_name=app_config.verbose_name,
                          activity_url=activity_url)
Example #6
0
def get_sources(user=None):
    """
    Create and return activity definitions for all of the activities in the
    'study' and 'activity' modules.
    """
    activities = defaultdict(dict)

    for label, source in get_source_labels_and_configs():
        user_data_model = app_label_to_user_data_model(label)
        is_connected = False

        if user:
            if hasattr(user_data_model, 'objects'):
                is_connected = user_data_model.objects.get(
                    user=user).is_connected
            elif hasattr(source, 'user_data'):
                is_connected = source.user_data(user=user).is_connected

        if hasattr(source, 'url_slug'):
            url_slug = source.url_slug
        else:
            url_slug = label

        activity = {
            'verbose_name':
            source.verbose_name,
            'badge': {
                'label':
                label,
                'name':
                source.verbose_name,
                'url':
                label + '/images/badge.png',
                'href':
                reverse('activity-management', kwargs={'source': url_slug}),
            },
            'data_source':
            True,
            'labels':
            get_labels('data-source'),
            'leader':
            source.leader,
            'organization':
            source.organization,
            'description':
            source.description,
            'long_description':
            source.long_description,
            'data_description':
            source.data_description['description'],
            'in_development':
            bool(source.in_development),
            'active':
            True,
            'info_url':
            source.href_learn,
            'product_website':
            source.product_website,
            'connect_verb':
            source.connect_verb,
            'add_data_text':
            '{} {}'.format(source.connect_verb.title(), source.verbose_name),
            'add_data_url':
            source.href_add_data
            if source.href_add_data else source.href_connect,
            'url_slug':
            url_slug,
            'has_files':
            (user and
             DataFile.objects.for_user(user).filter(source=label).count() > 0),
            'is_connected':
            is_connected,
            'members':
            badge_counts().get(label, 0),
            'type':
            'internal',
        }

        if hasattr(source, 'href_next'):
            activity['href_next'] = source.href_next

        if not (source.leader or source.organization):
            activity['organization'] = 'Open Humans'
            activity['contact_email'] = '*****@*****.**'

        if activity['leader'] and activity['organization']:
            activity['labels'].update(get_labels('study'))

        activities[label] = activity

    return activities