Skip to main content

Django integration for Lark billing

Project description

django-lark

Django integration for Lark billing.

Installation

pip install django-lark

Quick Start

  1. Add django_lark to your INSTALLED_APPS:
INSTALLED_APPS = [
    ...
    'django_lark',
]
  1. Configure your Lark API key in settings.py:
LARK_API_KEY = "lark_api_..."

Or set the LARK_API_KEY environment variable.

  1. Include the URLs (optional, for customer portal):
urlpatterns = [
    ...
    path('billing/', include('django_lark.urls')),
]

Configuration

All settings are prefixed with LARK_:

Setting Default Description
LARK_API_KEY Required Your Lark API key
LARK_BASE_URL https://api.uselark.ai Lark API base URL
LARK_TIMEOUT 60.0 Request timeout in seconds
LARK_MAX_RETRIES 2 Max retry attempts
LARK_USER_SUBJECT_FIELD email User field to use as external_id
LARK_AUTO_CREATE_SUBJECTS False Auto-create Lark subjects for users
LARK_BILLING_STATE_CACHE_TIMEOUT 300 Cache timeout for billing state (seconds)

How It Works

django-lark uses Lark's external_id feature to identify users without maintaining a local database mapping. When you create a subject in Lark with an external_id, you can use that same external_id in any API call that accepts a subject_id.

By default, django-lark uses the user's email as the external_id. You can customize this with the LARK_USER_SUBJECT_FIELD setting:

# Use user's primary key
LARK_USER_SUBJECT_FIELD = "id"  # Results in "django_user_{pk}"

# Use email (default)
LARK_USER_SUBJECT_FIELD = "email"

# Use a custom field
LARK_USER_SUBJECT_FIELD = "uuid"

# Use a callable for full control
LARK_USER_SUBJECT_FIELD = lambda user: f"myapp_{user.organization_id}_{user.id}"

Usage

Client Access

from django_lark import get_lark_client, get_async_lark_client

# Sync
client = get_lark_client()
subjects = client.subjects.list()

# Async
async def my_view(request):
    client = get_async_lark_client()
    subjects = await client.subjects.list()

Get External ID for a User

from django_lark.utils import get_external_id_for_user

external_id = get_external_id_for_user(user)
# Use this external_id in any Lark API call

Create Lark Subjects for Users

from django_lark.utils import get_or_create_subject_for_user

# Get or create a Lark subject for a Django user
subject, created = get_or_create_subject_for_user(user)

Check Subscription Status

from django_lark.utils import get_billing_state_for_user

billing_state = get_billing_state_for_user(user)
if billing_state.has_active_subscription:
    print("User has active subscription!")

View Decorators

from django_lark.decorators import subscription_required

@subscription_required()
def premium_view(request):
    return render(request, 'premium.html')

@subscription_required(rate_card_ids=['rc_pro', 'rc_enterprise'])
def pro_view(request):
    return render(request, 'pro.html')

@subscription_required(redirect_url='/pricing/')
def feature_view(request):
    return render(request, 'feature.html')

Template Tags

{% load lark_tags %}

{% has_active_subscription as is_subscribed %}
{% if is_subscribed %}
    <p>Welcome, premium member!</p>
{% else %}
    <a href="/pricing/">Upgrade now</a>
{% endif %}

{% has_subscription_to_rate_card "rc_pro" as has_pro %}
{% if has_pro %}
    <p>Pro features unlocked!</p>
{% endif %}

{% get_subscriptions as subscriptions %}
{% for sub in subscriptions %}
    <p>{{ sub.rate_card_id }} -
       <span class="badge {{ sub.status|lark_subscription_status_badge }}">
           {{ sub.status }}
       </span>
    </p>
{% endfor %}

{% get_lark_external_id as external_id %}

Customer Portal

Redirect users to the Lark customer portal:

<a href="{% url 'django_lark:customer_portal' %}?return_url={{ request.path }}">
    Manage Subscription
</a>

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_lark-0.1.0.tar.gz (14.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_lark-0.1.0-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file django_lark-0.1.0.tar.gz.

File metadata

  • Download URL: django_lark-0.1.0.tar.gz
  • Upload date:
  • Size: 14.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_lark-0.1.0.tar.gz
Algorithm Hash digest
SHA256 23a8f6bcc2473ed065c013552f85938f15c2738c3d9f25faf1f3a2aaf5108fe9
MD5 47f445c35b04ba7d79be4725b215b118
BLAKE2b-256 d31e465a657a7b945632c8337bb3fe08431e9ef10978ed7e7d824c062caf5144

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_lark-0.1.0.tar.gz:

Publisher: publish.yml on uselark/django-lark

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_lark-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: django_lark-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_lark-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b57f7e4e037830e8071f48a59cdd1a5bb0a10517532f017a8fdb77d4d3b7815a
MD5 751cef2464ab049e3804f06ea7f0e1e8
BLAKE2b-256 44d8efc3ebb516212c6f73a3b6da5950317ed57ef0c9652637bf5ff061017024

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_lark-0.1.0-py3-none-any.whl:

Publisher: publish.yml on uselark/django-lark

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page