-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: limit users based on alloted facilities
- Loading branch information
1 parent
5aa4901
commit 52813da
Showing
11 changed files
with
221 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,53 @@ | ||
from typing import Any, Dict | ||
from typing import Any, Dict, Optional, Union | ||
|
||
from django.forms import Form | ||
from django.contrib.auth import get_user_model | ||
from django.db.models import Model | ||
from django.forms import ModelForm | ||
|
||
from ..models import Facility, UserFacilityAllotment | ||
from ..models.common_models import FacilityManager, FacilityQuerySet | ||
|
||
class FormContextMixin(Form): | ||
def __init__(self, context: Dict[str, Any] = None, *args, **kwargs): | ||
User = get_user_model() | ||
|
||
|
||
class FormContextMixin(ModelForm): | ||
"""Add context to a form.""" | ||
|
||
def __init__(self, *args, **kwargs): | ||
self._context: Dict[str, Any] = kwargs.pop("context", {}) | ||
super().__init__(*args, **kwargs) | ||
self._context: Dict[str, Any] = context or {} | ||
|
||
@property | ||
def context(self): | ||
def context(self) -> Dict[str, Any]: | ||
"""Return the context as passed on the form during initialization.""" | ||
|
||
return self._context | ||
|
||
|
||
class GetUserFromContextMixin(FormContextMixin): | ||
"""Add a method to retrieve the logged in user from a form's context.""" | ||
|
||
def get_logged_in_user(self) -> Optional[Model]: | ||
"""Determine and return the logged in user from the context.""" | ||
|
||
request = self.context.get("request", None) | ||
user = getattr(request, "user", None) if request else None | ||
return user | ||
|
||
|
||
class GetAllottedFacilitiesMixin(GetUserFromContextMixin): | ||
"""Add a method to retrieve the allotted facilities for the logged-in user. | ||
The logged-in user is determined from a form's context data. | ||
""" | ||
|
||
def get_allotted_facilities(self) -> Union[FacilityManager, FacilityQuerySet]: | ||
"""Return a queryset consisting of all the allotted facilities of the logged in user. | ||
If the logged in user cannot be determined, then an empty queryset is returned instead. | ||
""" | ||
|
||
user = self.get_logged_in_user() | ||
if not user: | ||
return Facility.objects.none() | ||
return UserFacilityAllotment.get_facilities_for_user(user) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.