2018-12-10 15:58:35 +00:00
|
|
|
"""passbook core user views"""
|
2020-05-23 18:22:23 +00:00
|
|
|
from typing import Any, Dict
|
|
|
|
|
2019-03-11 10:25:59 +00:00
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
2020-10-18 13:38:28 +00:00
|
|
|
from django.contrib.auth.mixins import (
|
|
|
|
PermissionRequiredMixin as DjangoPermissionRequiredMixin,
|
|
|
|
)
|
2019-02-27 14:09:05 +00:00
|
|
|
from django.contrib.messages.views import SuccessMessageMixin
|
2020-10-18 13:38:28 +00:00
|
|
|
from django.db.models.query import QuerySet
|
|
|
|
from django.http.response import HttpResponse
|
2019-02-27 14:09:05 +00:00
|
|
|
from django.urls import reverse_lazy
|
2018-12-10 15:58:35 +00:00
|
|
|
from django.utils.translation import gettext as _
|
2020-10-18 13:38:28 +00:00
|
|
|
from django.views.generic import ListView, UpdateView
|
|
|
|
from guardian.mixins import PermissionListMixin, PermissionRequiredMixin
|
|
|
|
from guardian.shortcuts import get_objects_for_user
|
2018-12-10 15:58:35 +00:00
|
|
|
|
2020-10-18 13:38:28 +00:00
|
|
|
from passbook.admin.views.utils import (
|
|
|
|
DeleteMessageView,
|
|
|
|
SearchListMixin,
|
|
|
|
UserPaginateListMixin,
|
|
|
|
)
|
|
|
|
from passbook.core.forms.token import UserTokenForm
|
2020-05-10 22:49:48 +00:00
|
|
|
from passbook.core.forms.users import UserDetailForm
|
2020-10-18 13:38:28 +00:00
|
|
|
from passbook.core.models import Token, TokenIntents
|
2020-05-23 18:22:23 +00:00
|
|
|
from passbook.flows.models import Flow, FlowDesignation
|
2020-10-18 13:38:28 +00:00
|
|
|
from passbook.lib.views import CreateAssignPermView
|
2018-12-10 15:58:35 +00:00
|
|
|
|
|
|
|
|
2019-03-11 10:25:59 +00:00
|
|
|
class UserSettingsView(SuccessMessageMixin, LoginRequiredMixin, UpdateView):
|
2018-12-10 15:58:35 +00:00
|
|
|
"""Update User settings"""
|
2019-02-26 09:57:05 +00:00
|
|
|
|
2019-12-31 11:51:16 +00:00
|
|
|
template_name = "user/settings.html"
|
2018-12-10 15:58:35 +00:00
|
|
|
form_class = UserDetailForm
|
|
|
|
|
2019-12-31 11:51:16 +00:00
|
|
|
success_message = _("Successfully updated user.")
|
|
|
|
success_url = reverse_lazy("passbook_core:user-settings")
|
2019-02-27 14:09:05 +00:00
|
|
|
|
2018-12-10 15:58:35 +00:00
|
|
|
def get_object(self):
|
|
|
|
return self.request.user
|
2020-05-23 18:22:23 +00:00
|
|
|
|
2020-07-05 21:49:33 +00:00
|
|
|
def get_context_data(self, **kwargs: Any) -> Dict[str, Any]:
|
2020-05-23 18:22:23 +00:00
|
|
|
kwargs = super().get_context_data(**kwargs)
|
|
|
|
unenrollment_flow = Flow.with_policy(
|
|
|
|
self.request, designation=FlowDesignation.UNRENOLLMENT
|
|
|
|
)
|
|
|
|
kwargs["unenrollment_enabled"] = bool(unenrollment_flow)
|
|
|
|
return kwargs
|
2020-10-18 13:38:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TokenListView(
|
|
|
|
LoginRequiredMixin,
|
|
|
|
PermissionListMixin,
|
|
|
|
UserPaginateListMixin,
|
|
|
|
SearchListMixin,
|
|
|
|
ListView,
|
|
|
|
):
|
|
|
|
"""Show list of all tokens"""
|
|
|
|
|
|
|
|
model = Token
|
|
|
|
ordering = "expires"
|
|
|
|
permission_required = "passbook_core.view_token"
|
|
|
|
|
|
|
|
template_name = "user/token_list.html"
|
|
|
|
search_fields = [
|
|
|
|
"identifier",
|
|
|
|
"intent",
|
|
|
|
"description",
|
|
|
|
]
|
|
|
|
|
|
|
|
def get_queryset(self) -> QuerySet:
|
|
|
|
return super().get_queryset().filter(intent=TokenIntents.INTENT_API)
|
|
|
|
|
|
|
|
|
|
|
|
class TokenCreateView(
|
|
|
|
SuccessMessageMixin,
|
|
|
|
LoginRequiredMixin,
|
|
|
|
DjangoPermissionRequiredMixin,
|
|
|
|
CreateAssignPermView,
|
|
|
|
):
|
|
|
|
"""Create new Token"""
|
|
|
|
|
|
|
|
model = Token
|
|
|
|
form_class = UserTokenForm
|
|
|
|
permission_required = "passbook_core.add_token"
|
|
|
|
|
|
|
|
template_name = "generic/create.html"
|
|
|
|
success_url = reverse_lazy("passbook_core:user-tokens")
|
|
|
|
success_message = _("Successfully created Token")
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs: Any) -> Dict[str, Any]:
|
|
|
|
kwargs = super().get_context_data(**kwargs)
|
|
|
|
kwargs["container_template"] = "user/base.html"
|
|
|
|
return kwargs
|
|
|
|
|
|
|
|
def form_valid(self, form: UserTokenForm) -> HttpResponse:
|
|
|
|
form.instance.user = self.request.user
|
|
|
|
form.instance.intent = TokenIntents.INTENT_API
|
|
|
|
return super().form_valid(form)
|
|
|
|
|
|
|
|
|
|
|
|
class TokenUpdateView(
|
|
|
|
SuccessMessageMixin, LoginRequiredMixin, PermissionRequiredMixin, UpdateView
|
|
|
|
):
|
|
|
|
"""Update token"""
|
|
|
|
|
|
|
|
model = Token
|
|
|
|
form_class = UserTokenForm
|
|
|
|
permission_required = "passbook_core.update_token"
|
|
|
|
template_name = "generic/update.html"
|
|
|
|
success_url = reverse_lazy("passbook_core:user-tokens")
|
|
|
|
success_message = _("Successfully updated Token")
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs: Any) -> Dict[str, Any]:
|
|
|
|
kwargs = super().get_context_data(**kwargs)
|
|
|
|
kwargs["container_template"] = "user/base.html"
|
|
|
|
return kwargs
|
|
|
|
|
|
|
|
def get_object(self) -> Token:
|
|
|
|
identifier = self.kwargs.get("identifier")
|
|
|
|
return get_objects_for_user(
|
|
|
|
self.request.user, "passbook_core.update_token", self.model
|
|
|
|
).filter(intent=TokenIntents.INTENT_API, identifier=identifier)
|
|
|
|
|
|
|
|
|
|
|
|
class TokenDeleteView(LoginRequiredMixin, PermissionRequiredMixin, DeleteMessageView):
|
|
|
|
"""Delete token"""
|
|
|
|
|
|
|
|
model = Token
|
|
|
|
permission_required = "passbook_core.delete_token"
|
|
|
|
template_name = "generic/delete.html"
|
|
|
|
success_url = reverse_lazy("passbook_core:user-tokens")
|
|
|
|
success_message = _("Successfully deleted Token")
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs: Any) -> Dict[str, Any]:
|
|
|
|
kwargs = super().get_context_data(**kwargs)
|
|
|
|
kwargs["container_template"] = "user/base.html"
|
|
|
|
return kwargs
|