2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-09-14 06:31:27 +00:00

User token table (#9954)

* Tweak layout of Token dialog

* Render user details in API token table

* Bump API version
This commit is contained in:
Oliver
2025-07-04 11:38:21 +10:00
committed by GitHub
parent ada346d339
commit 6453abb974
3 changed files with 67 additions and 47 deletions

View File

@@ -1,12 +1,15 @@
"""InvenTree API version information."""
# InvenTree API version
INVENTREE_API_VERSION = 362
INVENTREE_API_VERSION = 363
"""Increment this API version number whenever there is a significant change to the API that any clients need to know about."""
INVENTREE_API_TEXT = """
v363 -> 2025-07-04 : https://github.com/inventree/InvenTree/pull/9954
- Adds "user_detail" field to the ApiToken serializer
v362 -> 2025-07-02 : https://github.com/inventree/InvenTree/pull/9939
- Allow filtering of BuildItem API by "location" of StockItem
- Allow filtering of SalesOrderAllocation API by "location" of StockItem

View File

@@ -134,46 +134,6 @@ def generate_roles_dict(roles) -> dict:
return role_dict
class ApiTokenSerializer(InvenTreeModelSerializer):
"""Serializer for the ApiToken model."""
in_use = serializers.SerializerMethodField(read_only=True)
user = serializers.PrimaryKeyRelatedField(
queryset=User.objects.all(), required=False
)
def get_in_use(self, token: ApiToken) -> bool:
"""Return True if the token is currently used to call the endpoint."""
from InvenTree.middleware import get_token_from_request
request = self.context.get('request')
rq_token = get_token_from_request(request)
return token.key == rq_token
class Meta:
"""Meta options for ApiTokenSerializer."""
model = ApiToken
fields = [
'created',
'expiry',
'id',
'last_seen',
'name',
'token',
'active',
'revoked',
'user',
'in_use',
]
def validate(self, data):
"""Validate the data for the serializer."""
if 'user' not in data:
data['user'] = self.context['request'].user
return super().validate(data)
class GetAuthTokenSerializer(serializers.Serializer):
"""Serializer for the GetAuthToken API endpoint."""
@@ -248,6 +208,49 @@ class UserSerializer(InvenTreeModelSerializer):
)
class ApiTokenSerializer(InvenTreeModelSerializer):
"""Serializer for the ApiToken model."""
in_use = serializers.SerializerMethodField(read_only=True)
user = serializers.PrimaryKeyRelatedField(
queryset=User.objects.all(), required=False
)
def get_in_use(self, token: ApiToken) -> bool:
"""Return True if the token is currently used to call the endpoint."""
from InvenTree.middleware import get_token_from_request
request = self.context.get('request')
rq_token = get_token_from_request(request)
return token.key == rq_token
class Meta:
"""Meta options for ApiTokenSerializer."""
model = ApiToken
fields = [
'created',
'expiry',
'id',
'last_seen',
'name',
'token',
'active',
'revoked',
'user',
'user_detail',
'in_use',
]
def validate(self, data):
"""Validate the data for the serializer."""
if 'user' not in data:
data['user'] = self.context['request'].user
return super().validate(data)
user_detail = UserSerializer(source='user', read_only=True)
class GroupSerializer(InvenTreeModelSerializer):
"""Serializer for a 'Group'."""