User

User views for smarter api.

smarter.apps.account.api.v1.views.user.USER_CREATE_FIELDS = ('username', 'password', 'email', 'first_name', 'last_name')

The fields of a new user that the request body may set.

('username', 'password', 'email', 'first_name', 'last_name')
smarter.apps.account.api.v1.views.user.USER_UPDATE_FIELDS = ('username', 'email', 'first_name', 'last_name', 'is_active')

The fields of a user that the request body may update.

('username', 'email', 'first_name', 'last_name', 'is_active')
class smarter.apps.account.api.v1.views.user.UserListView(*args, **kwargs)[source]

Bases: AccountListViewBase

User list view for smarter api.

get(request, *args, **kwargs)[source]

Handle GET requests to retrieve the list of users.

get_list(request)[source]

Get a list of all users the requesting user has access to, as AccountApiUserDetail models.

get_queryset()[source]

Return every User for a superuser, else the Users of the request user’s Accounts.

Returns:

The queryset, or an error Response when the user or its UserProfile is not found.

post(request, *args, **kwargs)[source]

Handle POST requests to create a new user.

serializer_class

alias of UserSerializer

setup(request, *args, **kwargs)[source]

Setup the view.

This is called by Django before dispatch() and is used to initialize attributes that require the request object.

class smarter.apps.account.api.v1.views.user.UserView(*args, **kwargs)[source]

Bases: AccountViewBase

User view for smarter api.

delete(request, user_id)[source]

Delete a user, and send api_request_completed.

Parameters:
  • request – The request.

  • user_id – The User’s id.

Returns:

The response of delete_user().

get(request, user_id)[source]

Return a user as json, and send api_request_completed.

Parameters:
  • request – The request.

  • user_id (int) – The User’s id.

Returns:

The response of get_user().

handle_exception(exc)[source]

Answer an Http404 with a json 404 response, and leave any other exception to DRF.

Parameters:

exc – The exception that the view raised.

Returns:

The response.

patch(request, user_id)[source]

Update a user from the request body, and send api_request_completed.

Parameters:
  • request – The request.

  • user_id (int) – The User’s id.

Returns:

The response of update_user().

post(request, user_id=None)[source]

Create a user from the request body, and send api_request_completed.

Parameters:
  • request – The request.

  • user_id (int | None) – Unused.

Returns:

The response of create_user().

smarter.apps.account.api.v1.views.user.create_user(request)[source]

Create an account from a json representation in the body of the request.

smarter.apps.account.api.v1.views.user.delete_user(request, user_id=None)[source]

Delete a user and its UserProfile, in one transaction.

Parameters:
  • request (Request) – The request.

  • user_id (int | None) – The User’s id, or None for the request user.

Returns:

A redirect to the parent url, or a 404, 401 or 500 JsonResponse.

smarter.apps.account.api.v1.views.user.eval_permissions(request, user_to_update, user_to_update_profile=None)[source]

Check that the request user may modify a user.

A superuser may modify anyone. Anyone else needs a UserProfile, and may only modify users of their own Account; a non-staff user may only modify themself.

Parameters:
  • request – The request.

  • user_to_update (User) – The User to modify.

  • user_to_update_profile (UserProfile | None) – The UserProfile of user_to_update, if it has one.

Returns:

None if the request user may modify the user, else a 401 JsonResponse.

smarter.apps.account.api.v1.views.user.get_user(request, user_id=None)[source]

Get a user’s json representation, an AccountApiUserDetail, by id.

A superuser may get any user, a staff user the users of their own Account, and anyone else only themself.

Parameters:
  • request – The request.

  • user_id (int | None) – The User’s id, or None for the request user.

Returns:

The user’s json response, or a 401 or 404 JsonResponse.

smarter.apps.account.api.v1.views.user.get_user_for_operation(request, user_id)[source]

Look up a User and its UserProfile by the User’s id.

Parameters:
  • request – The request, whose user must be a User.

  • user_id (int) – The User’s id.

Return type:

tuple[User, UserProfile] | JsonResponse

Returns:

The User and its UserProfile, or a 401 or 400 JsonResponse when the request user is not a User, or the User or its UserProfile does not exist.

smarter.apps.account.api.v1.views.user.update_user(request, user_id)[source]

Update a user from a json representation in the body of the request.

Only the fields in USER_UPDATE_FIELDS are changed, and the request user needs the permissions that eval_permissions() checks.

Parameters:
  • request (Request) – The request, whose body is a json dict of the fields to change.

  • user_id (int) – The User’s id.

Returns:

The response: the updated user, or an error JsonResponse.

smarter.apps.account.api.v1.views.user.user_response(user)[source]

The response of a user: its AccountApiUserDetail.

Parameters:

user (django.contrib.auth.models.User) – The user.

Returns:

The response.

Return type:

Response

smarter.apps.account.api.v1.views.user.validate_request_body(request)[source]

Check that the request body is a json dict with a username and a password.

Parameters:

request (Request) – The request.

Returns:

None if the body is valid, else a 400 JsonResponse that explains why not.