FastComments.com

Django アプリにコメントを追加する


これは FastComments の公式 Django パッケージです。

テンプレートタグを使用した安全な SSO を備えたライブコメントおよびチャットコンポーネントです。

Repository

GitHub で見る


要件 Internal Link


  • Python 3.10+
  • Django 4.2、5.0、5.1、または5.2
  • FastComments のテナント ID(demo を使用してアカウントなしで試す)
  • Secure SSO のみ、API secret が必要です

インストール Internal Link

Install from a release tag (this project is distributed via git tags, not PyPI):

pip install "git+https://github.com/fastcomments/fastcomments-django.git@v0.1.0"

For server-side REST access (the admin() / public_api() helpers), add the api extra, which pulls in the SDK's generated client:

pip install "fastcomments-django[api] @ git+https://github.com/fastcomments/fastcomments-django.git@v0.1.0"

Add the app to INSTALLED_APPS:

INSTALLED_APPS = [
    # ...
    "fastcomments_django",
]

クイックスタート Internal Link


settings.pyでテナントを構成します:

import os

FASTCOMMENTS = {
    "TENANT_ID": os.environ.get("FASTCOMMENTS_TENANT_ID", "demo"),
}

任意のテンプレートにウィジェットを配置します:

{% load fastcomments %}

{% fastcomments url_id="my-page" %}

自動シングルサインオンの前提条件 Internal Link

To pass the logged-in user to the widget automatically, the tags read the current user from the request. Make sure your project has both of these (they are on by default in a standard Django project):

  • django.template.context_processors.request in TEMPLATES["OPTIONS"]["context_processors"]
  • django.contrib.auth.middleware.AuthenticationMiddleware in MIDDLEWARE

Without a request in the template context, widgets render for an anonymous visitor. You can always pass a user explicitly: {% fastcomments user=some_user %}.

ウィジェットタグ Internal Link

Every widget has its own tag. All of them accept **extra keyword arguments, which are merged into the widget config as-is (use camelCase keys) for anything not covered by the named arguments below.

タグウィジェット
{% fastcomments %}Comments
{% fastcomments_live_chat %}Live chat
{% fastcomments_comment_count %}Comment count badge
{% fastcomments_comment_count_bulk %} + {% fastcomments_count_marker %}Bulk comment counts
{% fastcomments_collab_chat target="#el" %}Collaborative (inline) chat
{% fastcomments_image_chat target="#el" %}Image annotation chat
{% fastcomments_recent_comments %}Recent comments
{% fastcomments_recent_discussions %}Recent discussions
{% fastcomments_reviews_summary %}Reviews summary
{% fastcomments_top_pages %}Most-discussed pages
{% fastcomments_user_activity user_id="..." %}A user's activity feed

Named arguments map to the widget's camelCase config keys:

引数設定キータグ
url_idurlIdcomments, live chat, comment count, collab/image chat, recent comments, reviews summary
urlurlcomments, live chat, collab/image chat
readonlyreadonlycomments, live chat, collab/image chat
localelocalecomments, live chat, collab/image chat, user activity
has_dark_backgroundhasDarkBackgroundall
default_sort_directiondefaultSortDirectioncomments, live chat, collab/image chat
number_onlynumberOnlycomment count
is_liveisLivecomment count
countcountrecent comments, recent discussions
target(querySelector, not sent)collab chat, image chat
chat_square_percentagechatSquarePercentageimage chat
user_iduserIduser activity

Examples:

{% load fastcomments %}

{% fastcomments url_id="my-page" locale="en_us" default_sort_direction="MR" %}

{% fastcomments_live_chat url_id="room-1" %}

Comments: {% fastcomments_comment_count url_id="my-page" number_only=True %}

{# Collab chat attaches to an existing element on the page #}
<article id="post-body">...</article>
{% fastcomments_collab_chat target="#post-body" %}

{# Bulk counts: place markers, then one bulk loader fills them all in #}
{% for post in posts %}
    <a href="\{{ post.url }}">\{{ post.title }}</a>
    {% fastcomments_count_marker url_id=post.url_id %}
{% endfor %}
{% fastcomments_comment_count_bulk %}

SSO(シングルサインオン) Internal Link

Enable SSO and choose a mode in settings.py. Secure SSO signs the user server-side with HMAC‑SHA256 using your API secret and is recommended.

FASTCOMMENTS = {
    "TENANT_ID": os.environ["FASTCOMMENTS_TENANT_ID"],
    "API_KEY": os.environ["FASTCOMMENTS_API_KEY"],   # あなたのAPIシークレット; Secure SSOに署名します
    "SSO": {
        "ENABLED": True,
        "MODE": "secure",                            # "secure" | "simple"
        # FastComments のフィールドをユーザーモデルにマップします。値は属性
        # 名、ドット区切りパス("profile.avatar_url")、callable(user)、または None です。
        "USER_MAP": {
            "id": "id",
            "email": "email",
            "username": "username",
            "avatar": None,
            "display_name": None,
            "website_url": None,
        },
        "IS_ADMIN": lambda user: user.is_staff,      # callable(user) -> bool、またはドット区切りパス
        "IS_MODERATOR": None,
        "GROUP_IDS": None,                           # callable(user) -> list、またはドット区切りパス
    },
}

SSO の id を慎重に選択してください。 FastComments の id はユーザーのコメント履歴の永続的なハンドルです。デフォルトの USER_MAP は設定不要の便利さのためにあなたの Django の主キーにマップしますが、連続した整数 PK は列挙可能で後で変更が困難です(ユーザーの id を変更すると履歴が新しいアカウントに分割されます)。デモを超えるすべての場合、id を事前に選択した安定した不透明な値(UUID または専用の公開 ID)にマップし、決してプライベートデータを格納しないでください。この理由でサンプルアプリはユーザー名ベースの id を使用しています。

SSO は現在のユーザーに対して {% fastcomments %}, {% fastcomments_live_chat %}, {% fastcomments_collab_chat %}, {% fastcomments_image_chat %}, および {% fastcomments_user_activity %} に自動的に注入されます。

Login/logout URLs shown to signed-out visitors default to reverse("login") / reverse("logout"); override them with SSO["LOGIN_URL"] / SSO["LOGOUT_URL"].

カスタムマッピング

USER_MAP よりも優先度の高いオプションが2つあります:

  • ユーザーモデルのメソッド(インターフェースの Python的類似):

    class User(AbstractUser):
        def to_fastcomments_user_data(self):
            return {"id": self.pk, "email": self.email, "username": self.get_username()}
  • グローバルマッパーcallable(user) -> dict へのドット区切りパス:

    FASTCOMMENTS = {"SSO": {"USER_MAPPER": "myapp.sso.map_user"}}

優先順位は USER_MAPPER > to_fastcomments_user_data() > USER_MAP です。

サーバーサイド API アクセス Internal Link

[api] エクストラがインストールされている状態で、SDK を通じて FastComments REST API を呼び出します。API キーとリージョンが事前に設定されています:

from fastcomments_django import admin, public_api, get_manager

admin().get_comments("YOUR_TENANT_ID", ...)     # 認証済み (DefaultApi)
public_api().get_comments_public(...)            # 公開 (PublicApi)

# API 呼び出しまたはクライアントハンドオフ用に SSO トークンを生成:
token = get_manager().sso().token_for(request.user)

EU リージョン Internal Link


REGION を設定して、ウィジェットと API を EU にルーティングします:

FASTCOMMENTS = {"TENANT_ID": "...", "REGION": "eu"}

埋め込みマークアップのカスタマイズ Internal Link


fastcomments/widget.html を、テンプレート検索パスの上位に自分のコピーを早めに配置することで上書きします
テンプレート検索パス(プロジェクトの templates/fastcomments/widget.html)。これは
Laravel の vendor:publish --tag=fastcomments-views に相当する Django のアナログです。

設定リファレンス Internal Link

キーデフォルト説明
TENANT_ID""FastComments のテナント ID (demo はテスト用)。
API_KEY""API シークレット。Secure SSO に署名し、admin() を認証します。
REGIONNoneNone は米国、"eu" は EU リージョンです。
SSO.ENABLEDFalseSSO をオンにします。
SSO.MODE"secure""secure" (HMAC) または "simple" (無署名)。
SSO.LOGIN_URL / SSO.LOGOUT_URLNoneサインアウトした訪問者に表示されます。デフォルトは reverse("login"/"logout") です。
SSO.USER_MAPid/email/usernameFastComments のフィールドをユーザー属性/パス/呼び出し可能オブジェクトにマッピングします。
SSO.IS_ADMIN / IS_MODERATOR / GROUP_IDSNonecallable(user) またはドット区切りパス。
SSO.USER_MAPPERNonecallable(user) -> dict へのドット区切りパス。最も高い優先度です。
WIDGET_DEFAULTS{}すべてのウィジェットにマージされる設定(camelCase キー)。

サンプルプロジェクト Internal Link

A runnable showcase lives in example/: a left-rail + main-stage
app with a page per widget and a sign-in page listing pre-seeded demo users.
Sign in as any of them and the comment and live‑chat widgets authenticate that
identity via Secure SSO. From that directory:

python manage.py migrate
# Secure SSO を実際に見るために自分のテナントを使用してください(API シークレットが必要です):
FASTCOMMENTS_TENANT_ID=... FASTCOMMENTS_API_KEY=... python manage.py runserver

Without an API secret it falls back to the public demo tenant (anonymous).
example/browser_smoke.py is a Playwright e2e
that loads the page in a real browser and posts a comment as the Secure‑SSO
user。

助けが必要ですか?

Django パッケージに関して問題が発生したり質問がある場合は、以下をご利用ください:

貢献

貢献は歓迎します!貢献ガイドラインについては、GitHub リポジトリをご覧ください。