Exemplo n.º 1
0
def test_get_default_site(settings, site_key, throws):
    """Test that get_default_site works as expected"""
    authenticated_site = AuthenticatedSiteFactory.create(key="key")
    settings.OPEN_DISCUSSIONS_DEFAULT_SITE_KEY = site_key
    if not throws:
        assert get_default_site() == authenticated_site
    else:
        with pytest.raises(ImproperlyConfigured):
            get_default_site()
Exemplo n.º 2
0
def context_for_user(*, user=None, extra_context=None):
    """
    Returns an email context for the given user

    Args:
        user (User): user this email is being sent to
        extra_context (dict): additional per-user context

    Returns:
        dict: the context for this user
    """

    context = {
        "base_url": settings.SITE_BASE_URL,
        "site_name": get_default_site().title,
    }

    if user:
        context.update({
            "user":
            user,
            "anon_token":
            get_encoded_and_signed_subscription_token(user),
        })

    if extra_context is not None:
        context.update(extra_context)

    return context
Exemplo n.º 3
0
def test_context_for_user(settings, test_user, extra_context):
    """Tests that context_for_user returns the expected values"""
    user_ctx = ({
        "user": test_user,
        "anon_token": any_instance_of(str)
    } if test_user else {})

    assert context_for_user(user=test_user, extra_context=extra_context) == {
        "base_url": settings.SITE_BASE_URL,
        "site_name": get_default_site().title,
        **(extra_context or {}),
        **user_ctx,
    }
Exemplo n.º 4
0
    def post(self, request):
        """
        Renders a test email
        """
        form = self.form_cls(request.POST)

        if not form.is_valid():
            return JsonResponse({"error": "invalid input"})

        email_type = form.cleaned_data["email_type"]
        context = {
            "base_url": settings.SITE_BASE_URL,
            "anon_token": "abc123",
            "site_name": get_default_site().title,
        }

        # static, dummy data
        if email_type == "password_reset":
            context.update({"uid": "abc-def", "token": "abc-def"})
        elif email_type == "verification":
            context.update(
                {"confirmation_url": "http://www.example.com/comfirm/url"})
        elif email_type == "comments":
            context.update({
                "post":
                SimpleNamespace(
                    id="abc",
                    title="Batman Rules!",
                    slug="batman_rules",
                    channel_name="channel_name",
                    channel_title="Favorite Superheros",
                ),
                "comment":
                SimpleNamespace(id="def", text="Your post is really awesome!"),
            })
        elif email_type == "frontpage":
            context.update({
                "posts": [
                    SimpleNamespace(
                        id="abc",
                        author_name="Steve Brown",
                        author_headline="Physics Professor",
                        author_id="njksdfg",
                        title="Batman Rules!",
                        url="http://example.com/batman.jpg",
                        url_domain="example.com",
                        slug="batman_rules",
                        created="2018-09-19T18:50:32+00:00",
                        channel_name="channel_name",
                        channel_title="Favorite Superheros",
                    ),
                    SimpleNamespace(
                        id="def",
                        author_name="Casey Adams",
                        author_headline="Graduate Student",
                        author_id="ghjkl",
                        title="I, however, do not concur",
                        slug="i_however_do_not_concur",
                        created="2018-09-19T18:50:32+00:00",
                        channel_name="channel_name",
                        channel_title="Favorite Superheros",
                    ),
                ]
            })

        subject, text_body, html_body = api.render_email_templates(
            email_type, context)

        return JsonResponse({
            "subject": subject,
            "html_body": html_body,
            "text_body": text_body
        })
Exemplo n.º 5
0
def index(request, **kwargs):  # pylint: disable=unused-argument
    """Render the react app"""
    if request.META.get("HTTP_USER_AGENT",
                        "").startswith("facebookexternalhit"):
        return render(
            request,
            "social.html",
            context={
                "url": request.build_absolute_uri(),
                "description_value": "MIT Open Learning's discussion platform",
            },
        )

    username = None
    user_full_name = None
    user_email = None
    user_is_superuser = False
    user_id = None

    if request.user.is_authenticated:
        user = request.user
        username = user.username
        user_full_name = user.profile.name
        user_email = user.email
        user_is_superuser = user.is_superuser
        user_id = user.id

    site = get_default_site()

    article_ui_enabled = (features.is_enabled(features.ARTICLE_UI)
                          if settings.CKEDITOR_ENVIRONMENT_ID
                          and settings.CKEDITOR_SECRET_KEY
                          and settings.CKEDITOR_UPLOAD_URL else False)

    livestream_ui_enabled = (features.is_enabled(features.LIVESTREAM_UI)
                             if settings.LIVESTREAM_ACCOUNT_ID
                             and settings.LIVESTREAM_SECRET_KEY else False)

    js_settings = {
        "gaTrackingID":
        settings.GA_TRACKING_ID,
        "environment":
        settings.ENVIRONMENT,
        "sentry_dsn":
        settings.SENTRY_DSN,
        "release_version":
        settings.VERSION,
        "public_path":
        public_path(request),
        "site_url":
        settings.SITE_BASE_URL,
        "max_comment_depth":
        settings.OPEN_DISCUSSIONS_MAX_COMMENT_DEPTH,
        "username":
        username,
        "user_full_name":
        user_full_name,
        "user_email":
        user_email,
        "user_id":
        user_id,
        "is_admin":
        user_is_superuser,
        "authenticated_site": {
            "title": site.title,
            "base_url": site.base_url,
            "login_url": site.login_url,
            "session_url": site.session_url,
            "tos_url": site.tos_url,
        },
        "support_email":
        settings.EMAIL_SUPPORT,
        "is_authenticated":
        bool(request.user.is_authenticated),
        "profile_ui_enabled":
        features.is_enabled(features.PROFILE_UI),
        "allow_saml_auth":
        features.is_enabled(features.SAML_AUTH),
        "allow_related_posts_ui":
        features.is_enabled(features.RELATED_POSTS_UI),
        "embedlyKey":
        settings.EMBEDLY_KEY,
        "recaptchaKey":
        settings.RECAPTCHA_SITE_KEY,
        "search_page_size":
        settings.ELASTICSEARCH_DEFAULT_PAGE_SIZE,
        "search_min_length":
        settings.ELASTICSEARCH_MIN_QUERY_SIZE,
        "accepted_social_sites":
        list(SOCIAL_SITE_NAME_MAP.values()),
        "article_ui_enabled":
        article_ui_enabled,
        "ckeditor_upload_url":
        settings.CKEDITOR_UPLOAD_URL,
        "algolia_appId":
        settings.ALGOLIA_APP_ID,
        "algolia_apiKey":
        settings.ALGOLIA_API_KEY,
        "course_ui_enabled":
        features.is_enabled(features.COURSE_UI),
        "file_search_enabled":
        features.is_enabled(features.COURSE_FILE_SEARCH),
        "livestream_ui_enabled":
        livestream_ui_enabled,
        "podcast_frontpage_enabled":
        features.is_enabled(features.PODCAST_FRONTPAGE),
    }

    return render(request,
                  "index.html",
                  context={"js_settings_json": json.dumps(js_settings)})