REST API for gym member management
Use case
Gym administration (member lists, creation, activation/deactivation, trainer roles, contracts) is currently only available through the server-rendered views in wger/gym/views/; there is no wger/gym/api/ package. I would like to contribute a REST API for the member lifecycle, reusing the existing token/JWT authentication and mirroring the permission model already enforced in the web views.
I am happy to implement this myself in phased PRs; opening this issue first to agree on the design.
Proposal
Motivation
Integrating wger with external systems requires programmatic member lifecycle management. My concrete use case is pairing wger with a subscription billing system (Kill Bill): a failed payment or cancelled subscription should deactivate the member, a successful payment should reactivate them, and signup should create the member account. Today none of that is possible without driving the HTML views.
Related existing issues that an API would help with or should stay consistent with:
- #328
- #230
Current state (from reading master)
- The gym app has no API surface; all administration is in
wger/gym/views/. - The permission pattern to mirror lives in
GymUserListView.dispatch(wger/gym/views/gym.py):gym.manage_gymsgrants access to all gyms;gym.manage_gymandgym.gym_trainerare additionally checked againstrequest.user.userprofile.gym_id. - App-driven user creation infrastructure still exists on the model layer:
UserProfile.can_add_user(whitelist flag, set via theadd-user-restmanagement command) andUserProfile.added_by(tracks which app registered which user). AllowRegisterUserinwger/core/api/permissions.pyis defined but no longer referenced anywhere. Question for the maintainers: was the API registration endpoint intentionally dropped in the allauth headless migration, and should new-member creation go through this mechanism again or stay separate from self-signup?
Proposed design
- New package
wger/gym/api/with serializers, permission classes, and viewsets, registered in the main router. - Two DRF permission classes encoding the existing web rules once:
IsGymManager(gym.manage_gymscoped to own gym, orgym.manage_gyms) andIsGymTrainerOrManager(addsgym.gym_trainer, also gym-scoped). - Authentication: unchanged; the existing token and JWT auth apply automatically to new DRF viewsets.
- Deactivation semantics: set
User.is_active = False, matching the current web views (POST only, per #2415). No data is deleted. - Member creation follows the existing web add-member flow: assign to the gym, create the config records, generate a password if none is supplied and return it once in the response.
added_byis set to the requesting user. - All endpoints documented in the OpenAPI schema via
extend_schema, following the current effort to document the API in the schema itself.
Proposed endpoints (OpenAPI sketch)
openapi: 3.0.3
info:
title: wger gym member management API (proposal)
version: 0.1.0
paths:
/api/v2/gym/:
get:
summary: List gyms visible to the requesting user
description: >
Users with gym.manage_gyms see all gyms. Users with gym.manage_gym
or gym.gym_trainer see only the gym they belong to.
responses:
'200':
description: OK
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Gym'
/api/v2/gym/{gym_id}/members/:
parameters:
- $ref: '#/components/parameters/GymId'
get:
summary: List members and admins of a gym
description: >
Requires gym.manage_gyms, or gym.manage_gym / gym.gym_trainer for
this gym. Mirrors the data of the current member overview view,
including last activity and role information.
parameters:
- name: is_active
in: query
required: false
schema:
type: boolean
responses:
'200':
description: OK
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/GymMember'
'403':
$ref: '#/components/responses/Forbidden'
post:
summary: Create a new member in this gym
description: >
Requires gym.manage_gyms or gym.manage_gym for this gym. If no
password is supplied, one is generated and returned once in the
response, matching the behaviour of the web flow. Sets
UserProfile.added_by to the requesting user.
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/GymMemberCreate'
responses:
'201':
description: Created
content:
application/json:
schema:
$ref: '#/components/schemas/GymMemberCreated'
'403':
$ref: '#/components/responses/Forbidden'
/api/v2/gym/{gym_id}/members/{user_id}/:
parameters:
- $ref: '#/components/parameters/GymId'
- $ref: '#/components/parameters/UserId'
get:
summary: Retrieve a single member
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/GymMember'
'403':
$ref: '#/components/responses/Forbidden'
/api/v2/gym/{gym_id}/members/{user_id}/activate/:
parameters:
- $ref: '#/components/parameters/GymId'
- $ref: '#/components/parameters/UserId'
post:
summary: Reactivate a member
description: Sets User.is_active to true. Requires manager permissions.
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/MemberStatus'
'403':
$ref: '#/components/responses/Forbidden'
/api/v2/gym/{gym_id}/members/{user_id}/deactivate/:
parameters:
- $ref: '#/components/parameters/GymId'
- $ref: '#/components/parameters/UserId'
post:
summary: Deactivate a member
description: >
Sets User.is_active to false. No data is deleted. Requires manager
permissions. POST only, consistent with #2415.
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/MemberStatus'
'403':
$ref: '#/components/responses/Forbidden'
components:
parameters:
GymId:
name: gym_id
in: path
required: true
schema:
type: integer
UserId:
name: user_id
in: path
required: true
schema:
type: integer
responses:
Forbidden:
description: >
The requesting user lacks the required gym permission, or the target
gym is not the gym they belong to.
schemas:
Gym:
type: object
properties:
id:
type: integer
name:
type: string
GymMember:
type: object
properties:
id:
type: integer
username:
type: string
email:
type: string
first_name:
type: string
last_name:
type: string
is_active:
type: boolean
date_joined:
type: string
format: date-time
last_activity:
type: string
format: date-time
nullable: true
roles:
type: array
items:
type: string
enum: [member, trainer, manager, general_manager]
GymMemberCreate:
type: object
required: [username]
properties:
username:
type: string
email:
type: string
first_name:
type: string
last_name:
type: string
password:
type: string
writeOnly: true
description: Optional; generated by the server when omitted.
GymMemberCreated:
allOf:
- $ref: '#/components/schemas/GymMember'
- type: object
properties:
generated_password:
type: string
nullable: true
description: >
Only present when the server generated the password. It is
not retrievable later, matching the web flow.
MemberStatus:
type: object
properties:
id:
type: integer
is_active:
type: booleanOpen questions
- Nesting: is
/api/v2/gym/{gym_id}/members/acceptable, or would you prefer a flat/api/v2/gym-member/resource filtered by gym? - Should member creation reuse
AllowRegisterUser/can_add_user, or is that mechanism considered legacy after the allauth headless migration? - Should trainers (not just managers) be allowed to see
emailin the member list, or should the serializer redact contact fields for the trainer role?
Tests would follow the existing API test patterns (access control per role, per-gym scoping) and all endpoints would ship with extend_schema annotations.
Additional Context
No response
Source: wger-project/wger