From 15f6eb277d2d50b3817bcc08c00f4d8df1017634 Mon Sep 17 00:00:00 2001 From: Ngure Nyaga Date: Wed, 14 Jul 2021 13:53:35 +0300 Subject: [PATCH] feat: automatically set updated_by and created_by --- cloudbuild.yaml | 2 +- config/settings/base.py | 4 +- pepfar_mle/common/admin.py | 18 +++++-- pepfar_mle/common/forms.py | 21 ++++++++ .../migrations/0003_auto_20210714_1232.py | 27 ++++++++++ .../migrations/0004_auto_20210714_1339.py | 23 +++++++++ pepfar_mle/common/models.py | 7 ++- pepfar_mle/common/tests/test_admin.py | 50 +++++++++++++++++++ pepfar_mle/templates/base.html | 14 ++++++ 9 files changed, 157 insertions(+), 9 deletions(-) create mode 100644 pepfar_mle/common/forms.py create mode 100644 pepfar_mle/common/migrations/0003_auto_20210714_1232.py create mode 100644 pepfar_mle/common/migrations/0004_auto_20210714_1339.py create mode 100644 pepfar_mle/common/tests/test_admin.py diff --git a/cloudbuild.yaml b/cloudbuild.yaml index ef203c62..d7fb84b6 100644 --- a/cloudbuild.yaml +++ b/cloudbuild.yaml @@ -31,7 +31,7 @@ steps: '--max-instances', '4', '--memory', '256M', '--cpu', '1', - '--update-secrets', '/app/envs/.env=mle_django_settings:latest' + '--update-secrets', '/tmp/secrets/.env=mle_django_settings:latest' ] images: diff --git a/config/settings/base.py b/config/settings/base.py index 15060cec..31c66910 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -9,10 +9,8 @@ from google.cloud import secretmanager ROOT_DIR = Path(__file__).resolve(strict=True).parent.parent.parent -# pepfar_mle/ APPS_DIR = ROOT_DIR / "pepfar_mle" -ENVS_DIR = ROOT_DIR / "envs" -ENV_PATH = os.path.join(ENVS_DIR, ".env") +ENV_PATH = "/tmp/secrets/.env" env = environ.Env() env.read_env(ENV_PATH) diff --git a/pepfar_mle/common/admin.py b/pepfar_mle/common/admin.py index 6ef98b02..3d50f82a 100644 --- a/pepfar_mle/common/admin.py +++ b/pepfar_mle/common/admin.py @@ -3,15 +3,27 @@ from .models import Facility, FacilityAttachment, Organisation -class FacilityAdmin(admin.ModelAdmin): +class BaseAdmin(admin.ModelAdmin): + def save_model(self, request, obj, form, change): + if not change: + obj.created_by = request.user.pk + obj.updated_by = request.user.pk + + if change: + obj.updated_by = request.user.pk + + obj.save() + + +class FacilityAdmin(BaseAdmin): pass -class FacilityAttachmentAdmin(admin.ModelAdmin): +class FacilityAttachmentAdmin(BaseAdmin): pass -class OrganisationAdmin(admin.ModelAdmin): +class OrganisationAdmin(BaseAdmin): pass diff --git a/pepfar_mle/common/forms.py b/pepfar_mle/common/forms.py new file mode 100644 index 00000000..268374cd --- /dev/null +++ b/pepfar_mle/common/forms.py @@ -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__" diff --git a/pepfar_mle/common/migrations/0003_auto_20210714_1232.py b/pepfar_mle/common/migrations/0003_auto_20210714_1232.py new file mode 100644 index 00000000..f554868c --- /dev/null +++ b/pepfar_mle/common/migrations/0003_auto_20210714_1232.py @@ -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), + ), + ] diff --git a/pepfar_mle/common/migrations/0004_auto_20210714_1339.py b/pepfar_mle/common/migrations/0004_auto_20210714_1339.py new file mode 100644 index 00000000..0f6b4cde --- /dev/null +++ b/pepfar_mle/common/migrations/0004_auto_20210714_1339.py @@ -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), + ), + ] diff --git a/pepfar_mle/common/models.py b/pepfar_mle/common/models.py index e528a2a5..11edb4fc 100644 --- a/pepfar_mle/common/models.py +++ b/pepfar_mle/common/models.py @@ -168,8 +168,8 @@ class OrganisationAbstractBase(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) created = models.DateTimeField(default=timezone.now) updated = models.DateTimeField(default=timezone.now) - created_by = models.UUIDField() - updated_by = models.UUIDField() + created_by = models.UUIDField(blank=True, null=True) + updated_by = models.UUIDField(blank=True, null=True) def preserve_created_and_created_by(self): """Ensure that created and created_by values are not overwritten.""" @@ -394,6 +394,9 @@ def facility_name_longer_than_three_characters(self): def __str__(self): return f"{self.name} - {self.mfl_code} ({self.county})" + class Meta(AbstractBase.Meta): + verbose_name_plural = "facilities" + class FacilityAttachment(Attachment): """Any document attached to a facility.""" diff --git a/pepfar_mle/common/tests/test_admin.py b/pepfar_mle/common/tests/test_admin.py new file mode 100644 index 00000000..0e957361 --- /dev/null +++ b/pepfar_mle/common/tests/test_admin.py @@ -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 diff --git a/pepfar_mle/templates/base.html b/pepfar_mle/templates/base.html index cc2d6334..51a91c97 100644 --- a/pepfar_mle/templates/base.html +++ b/pepfar_mle/templates/base.html @@ -87,6 +87,20 @@ {% compress js %} {% endcompress %} {% endblock javascript %} + + +