-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/archive team validation #69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 +1,83 @@ | ||
| # Register your models here. | ||
| import typing | ||
| from datetime import datetime | ||
|
|
||
| from django.contrib import admin | ||
| from django.db import models | ||
| from django.http import HttpRequest | ||
|
|
||
| from apps.common.models import UserResource | ||
|
|
||
| DjangoModel = typing.TypeVar("DjangoModel", bound=models.Model) | ||
|
|
||
|
|
||
| class UserResourceAdmin(admin.ModelAdmin): | ||
| @typing.override | ||
| def get_readonly_fields(self, *args, **kwargs): | ||
| readonly_fields = super().get_readonly_fields(*args, **kwargs) # type: ignore[reportAttributeAccessIssue] | ||
| return [ | ||
| # To maintain order | ||
| *dict.fromkeys( | ||
| [ | ||
| *readonly_fields, | ||
| "created_at", | ||
| "created_by", | ||
| "modified_at", | ||
| "modified_by", | ||
| ], | ||
| ), | ||
| ] | ||
|
|
||
| @typing.override | ||
| def save_model(self, request, obj, form, change): | ||
| if not change: | ||
| obj.created_by = request.user | ||
| obj.modified_by = request.user | ||
| super().save_model(request, obj, form, change) # type: ignore[reportAttributeAccessIssue] | ||
|
|
||
| @typing.override | ||
| def save_formset(self, request, form, formset, change) -> None: | ||
| if not issubclass(formset.model, UserResource): | ||
| return super().save_formset(request, form, formset, change) | ||
| # https://docs.djangoproject.com/en/4.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_formset | ||
| instances = formset.save(commit=False) | ||
| for obj in formset.deleted_objects: | ||
| obj.delete() | ||
| for instance in instances: | ||
| # UserResource changes | ||
| if instance.pk is None: | ||
| instance.created_by = request.user | ||
| instance.modified_by = request.user | ||
| instance.save() | ||
| return None | ||
|
|
||
| @typing.override | ||
| def get_queryset(self, request: HttpRequest) -> models.QuerySet[DjangoModel]: | ||
| return super().get_queryset(request).select_related("created_by", "modified_by") | ||
|
|
||
|
|
||
| class ArchivableResourceAdmin(UserResourceAdmin, admin.ModelAdmin): | ||
| @typing.override | ||
| def get_readonly_fields(self, *args, **kwargs): | ||
| readonly_fields = super().get_readonly_fields(*args, **kwargs) # type: ignore[reportAttributeAccessIssue] | ||
| return [ | ||
| *dict.fromkeys( | ||
| [ | ||
| *readonly_fields, | ||
| "archived_by", | ||
| "archived_at", | ||
| ], | ||
| ), | ||
| ] | ||
|
|
||
| @typing.override | ||
| def save_model(self, request, obj, form, change): | ||
| if not change: | ||
| obj.created_by = request.user | ||
| obj.modified_by = request.user | ||
| if obj.is_archived: | ||
| obj.archived_by = request.user | ||
| obj.archived_at = datetime.now() | ||
| else: | ||
| obj.archived_by = None | ||
| obj.archived_at = None | ||
| super().save_model(request, obj, form, change) # type: ignore[reportAttributeAccessIssue] |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import typing | ||
|
|
||
| import pytest # type: ignore[reportMissingImports] | ||
| from django.contrib.admin.sites import AdminSite | ||
| from django.core.exceptions import ValidationError | ||
|
|
||
| from apps.contributor.admin import ContributorTeamAdmin | ||
| from apps.contributor.factories import ContributorTeamFactory, ContributorUserFactory | ||
| from apps.contributor.models import ContributorTeam | ||
| from apps.user.factories import UserFactory | ||
| from apps.user.models import User | ||
| from main.tests import TestCase | ||
|
|
||
|
|
||
| class MockRequest: | ||
| def __init__(self, user: User): | ||
| self.user = user | ||
|
|
||
|
|
||
| class TestContributorTeam(TestCase): | ||
| @typing.override | ||
| @classmethod | ||
| def setUpClass(cls): | ||
| super().setUpClass() | ||
| cls.user = UserFactory.create() | ||
| cls.user_resource_kwargs = dict( | ||
| created_by=cls.user, | ||
| modified_by=cls.user, | ||
| ) | ||
| cls.site = AdminSite() | ||
| cls.admin = ContributorTeamAdmin(ContributorTeam, cls.site) | ||
| cls.contributor_team = ContributorTeamFactory.create(**cls.user_resource_kwargs) | ||
| cls.contributor_user = ContributorUserFactory.create( | ||
| user_id="test_id", | ||
| team=cls.contributor_team, | ||
| ) | ||
|
|
||
| def test_cannot_archive_team_with_members(self): | ||
| self.contributor_team.is_archived = True | ||
| with pytest.raises(ValidationError): | ||
| self.contributor_team.clean() | ||
|
|
||
| def test_archive_team(self): | ||
| request = MockRequest(user=self.user) | ||
| self.force_login(request.user) | ||
| self.contributor_user.delete() | ||
| self.contributor_team.is_archived = True | ||
| self.admin.save_model(request, self.contributor_team, form=None, change=True) # type: ignore[reportArgumentType] | ||
| assert self.contributor_team.is_archived is True | ||
| assert self.contributor_team.archived_by == self.user | ||
Submodule firebase
updated
3 files
| +58 −4 | .github/workflows/ci.yml | |
| +2 −0 | functions/definition/team.yaml | |
| +1 −0 | functions/generated/pyfirebase/pyfirebase_mapswipe/models.py |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.