Skip to content

Commit

Permalink
feat: automatically set updated_by and created_by
Browse files Browse the repository at this point in the history
  • Loading branch information
ngurenyaga committed Jul 14, 2021
1 parent 3746cce commit 15f6eb2
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 1 addition & 3 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 15 additions & 3 deletions pepfar_mle/common/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
21 changes: 21 additions & 0 deletions pepfar_mle/common/forms.py
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__"
27 changes: 27 additions & 0 deletions pepfar_mle/common/migrations/0003_auto_20210714_1232.py
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),
),
]
23 changes: 23 additions & 0 deletions pepfar_mle/common/migrations/0004_auto_20210714_1339.py
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),
),
]
7 changes: 5 additions & 2 deletions pepfar_mle/common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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."""
Expand Down
50 changes: 50 additions & 0 deletions pepfar_mle/common/tests/test_admin.py
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
14 changes: 14 additions & 0 deletions pepfar_mle/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@
{% compress js %}
<script defer src="{% static 'js/project.js' %}"></script>
{% endcompress %} {% endblock javascript %}
<!-- Global site tag (gtag.js) - Google Analytics -->
<script
async
src="https://www.googletagmanager.com/gtag/js?id=G-WW2W29ZMTZ"
></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag("js", new Date());

gtag("config", "G-WW2W29ZMTZ");
</script>
</head>

<body>
Expand Down

0 comments on commit 15f6eb2

Please sign in to comment.