Skip to content

Commit

Permalink
feat(models): add modified_{by,at} fields
Browse files Browse the repository at this point in the history
  • Loading branch information
open-dynaMIX committed Jul 2, 2020
1 parent 470b36b commit 74b9ee1
Show file tree
Hide file tree
Showing 5 changed files with 274 additions and 125 deletions.
14 changes: 14 additions & 0 deletions alexandria/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from pytest_factoryboy import register
from rest_framework.test import APIClient

from alexandria.oidc_auth.models import OIDCUser


def register_module(module):
for name, obj in inspect.getmembers(module):
Expand All @@ -20,6 +22,18 @@ def register_module(module):
register_module(importlib.import_module(".core.factories", "alexandria"))


@pytest.fixture
def admin_groups():
return ["admin"]


@pytest.fixture
def admin_user(settings, admin_groups):
return OIDCUser(
"sometoken", {"sub": "admin", settings.OIDC_GROUPS_CLAIM: admin_groups}
)


@pytest.fixture
def admin_client(db, admin_user):
client = APIClient()
Expand Down
65 changes: 61 additions & 4 deletions alexandria/core/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 2.2.13 on 2020-07-02 10:02
# Generated by Django 2.2.13 on 2020-07-02 12:03

import django.contrib.postgres.fields.jsonb
import django.db.models.deletion
Expand All @@ -19,7 +19,7 @@ class Migration(migrations.Migration):
migrations.CreateModel(
name="Category",
fields=[
("created_at", models.DateTimeField(auto_now=True, db_index=True)),
("created_at", models.DateTimeField(auto_now_add=True, db_index=True)),
(
"created_by_user",
models.CharField(
Expand All @@ -38,6 +38,25 @@ class Migration(migrations.Migration):
verbose_name="created by group",
),
),
("modified_at", models.DateTimeField(auto_now=True, db_index=True)),
(
"modified_by_user",
models.CharField(
blank=True,
max_length=150,
null=True,
verbose_name="created by user",
),
),
(
"modified_by_group",
models.CharField(
blank=True,
max_length=255,
null=True,
verbose_name="created by group",
),
),
(
"meta",
django.contrib.postgres.fields.jsonb.JSONField(
Expand Down Expand Up @@ -69,7 +88,7 @@ class Migration(migrations.Migration):
migrations.CreateModel(
name="Tag",
fields=[
("created_at", models.DateTimeField(auto_now=True, db_index=True)),
("created_at", models.DateTimeField(auto_now_add=True, db_index=True)),
(
"created_by_user",
models.CharField(
Expand All @@ -88,6 +107,25 @@ class Migration(migrations.Migration):
verbose_name="created by group",
),
),
("modified_at", models.DateTimeField(auto_now=True, db_index=True)),
(
"modified_by_user",
models.CharField(
blank=True,
max_length=150,
null=True,
verbose_name="created by user",
),
),
(
"modified_by_group",
models.CharField(
blank=True,
max_length=255,
null=True,
verbose_name="created by group",
),
),
(
"meta",
django.contrib.postgres.fields.jsonb.JSONField(
Expand Down Expand Up @@ -119,7 +157,7 @@ class Migration(migrations.Migration):
migrations.CreateModel(
name="Document",
fields=[
("created_at", models.DateTimeField(auto_now=True, db_index=True)),
("created_at", models.DateTimeField(auto_now_add=True, db_index=True)),
(
"created_by_user",
models.CharField(
Expand All @@ -138,6 +176,25 @@ class Migration(migrations.Migration):
verbose_name="created by group",
),
),
("modified_at", models.DateTimeField(auto_now=True, db_index=True)),
(
"modified_by_user",
models.CharField(
blank=True,
max_length=150,
null=True,
verbose_name="created by user",
),
),
(
"modified_by_group",
models.CharField(
blank=True,
max_length=255,
null=True,
verbose_name="created by group",
),
),
(
"meta",
django.contrib.postgres.fields.jsonb.JSONField(
Expand Down
9 changes: 8 additions & 1 deletion alexandria/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,20 @@ def make_uuid():


class BaseModel(models.Model):
created_at = models.DateTimeField(auto_now=True, db_index=True)
created_at = models.DateTimeField(auto_now_add=True, db_index=True)
created_by_user = models.CharField(
_("created by user"), max_length=150, blank=True, null=True
)
created_by_group = models.CharField(
_("created by group"), max_length=255, blank=True, null=True
)
modified_at = models.DateTimeField(auto_now=True, db_index=True)
modified_by_user = models.CharField(
_("created by user"), max_length=150, blank=True, null=True
)
modified_by_group = models.CharField(
_("created by group"), max_length=255, blank=True, null=True
)
meta = JSONField(_("meta"), default=dict)

class Meta:
Expand Down
17 changes: 14 additions & 3 deletions alexandria/core/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,23 @@ class BaseSerializer(serializers.ModelSerializer):
def validate(self, *args, **kwargs):
validated_data = super().validate(*args, **kwargs)
user = self.context["request"].user
validated_data["created_by_user"] = user.username
# validated_data["created_by_group"] = user.group
validated_data["modified_by_user"] = user.username
validated_data["modified_by_group"] = user.group
if self.instance is not None:
validated_data["created_by_user"] = user.username
validated_data["created_by_group"] = user.group
return validated_data

class Meta:
fields = ("created_at", "created_by_user", "created_by_group", "meta")
fields = (
"created_at",
"created_by_user",
"created_by_group",
"modified_at",
"modified_by_user",
"modified_by_group",
"meta",
)


class CategorySerializer(BaseSerializer):
Expand Down
Loading

0 comments on commit 74b9ee1

Please sign in to comment.