-
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: automatically set updated_by and created_by
- Loading branch information
1 parent
3746cce
commit 15f6eb2
Showing
9 changed files
with
157 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from django.forms import ModelForm | ||
|
||
from .models import Facility, FacilityAttachment, Organisation | ||
|
||
|
||
class OrganisationForm(ModelForm): | ||
class Meta: | ||
model = Organisation | ||
fields = "__all__" | ||
|
||
|
||
class FacilityForm(ModelForm): | ||
class Meta: | ||
model = Facility | ||
fields = "__all__" | ||
|
||
|
||
class FacilityAttachmentForm(ModelForm): | ||
class Meta: | ||
model = FacilityAttachment | ||
fields = "__all__" |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Generated by Django 3.1.12 on 2021-07-14 09:32 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('common', '0002_auto_20210714_0824'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterModelOptions( | ||
name='facility', | ||
options={'ordering': ('-updated', '-created'), 'verbose_name_plural': 'facilities'}, | ||
), | ||
migrations.AlterField( | ||
model_name='organisation', | ||
name='created_by', | ||
field=models.UUIDField(blank=True), | ||
), | ||
migrations.AlterField( | ||
model_name='organisation', | ||
name='updated_by', | ||
field=models.UUIDField(blank=True), | ||
), | ||
] |
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Generated by Django 3.1.12 on 2021-07-14 10:39 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('common', '0003_auto_20210714_1232'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='organisation', | ||
name='created_by', | ||
field=models.UUIDField(blank=True, null=True), | ||
), | ||
migrations.AlterField( | ||
model_name='organisation', | ||
name='updated_by', | ||
field=models.UUIDField(blank=True, null=True), | ||
), | ||
] |
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import pytest | ||
from django.conf import settings | ||
from django.contrib.admin.sites import AdminSite | ||
from model_bakery import baker | ||
|
||
from pepfar_mle.common.admin import OrganisationAdmin | ||
from pepfar_mle.common.forms import OrganisationForm | ||
from pepfar_mle.common.models import Organisation | ||
|
||
pytestmark = pytest.mark.django_db | ||
|
||
|
||
@pytest.fixture | ||
def request_with_user(rf, django_user_model): | ||
url = settings.ADMIN_URL + "/common/organisation/add/" | ||
request = rf.get(url) | ||
user = baker.make(django_user_model) | ||
request.user = user | ||
return request | ||
|
||
|
||
@pytest.fixture | ||
def organisation_admin(): | ||
admin = OrganisationAdmin(model=Organisation, admin_site=AdminSite()) | ||
return admin | ||
|
||
|
||
@pytest.fixture | ||
def organisation_form(): | ||
form = OrganisationForm() | ||
return form | ||
|
||
|
||
def test_base_admin_update_created_by(request_with_user, organisation_admin, organisation_form): | ||
org = baker.prepare(Organisation) | ||
assert org.created_by is None | ||
assert org.updated_by is None | ||
organisation_admin.save_model(request_with_user, org, organisation_form, change=False) | ||
assert org.created_by is not None | ||
assert org.updated_by is not None | ||
|
||
|
||
def test_base_admin_update_updated_by(request_with_user, organisation_admin, organisation_form): | ||
org = baker.make(Organisation) | ||
original_created_by = org.created_by | ||
original_updated_by = org.updated_by | ||
|
||
organisation_admin.save_model(request_with_user, org, organisation_form, change=True) | ||
assert org.created_by == original_created_by | ||
assert org.updated_by != original_updated_by |
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