From 429399c5ab974a84ea337ad876639b44daf3d5ac Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Valenta?= <git@imaniti.org>
Date: Thu, 30 Mar 2023 10:39:00 +0200
Subject: [PATCH] created_by, created_on

---
 contracts/admin.py                            |  8 +++++---
 .../migrations/0019_contract_created_on.py    | 19 +++++++++++++++++++
 .../0020_alter_contract_created_on.py         | 19 +++++++++++++++++++
 contracts/models.py                           | 12 ++++++++++++
 4 files changed, 55 insertions(+), 3 deletions(-)
 create mode 100644 contracts/migrations/0019_contract_created_on.py
 create mode 100644 contracts/migrations/0020_alter_contract_created_on.py

diff --git a/contracts/admin.py b/contracts/admin.py
index 40623f0..864fb13 100644
--- a/contracts/admin.py
+++ b/contracts/admin.py
@@ -70,7 +70,7 @@ class OwnPermissionsMixin(
         lambda request, obj: obj.created_by != request.user,
     )
 ):
-    def own_permissions_mixin_save_model(self, request, obj, form, change):
+    def save_model(self, request, obj, form, change):
         if obj.created_by is None:
             obj.created_by = request.user
 
@@ -153,10 +153,10 @@ class ContractAdmin(
 ):
     form = ContractAdminForm
 
-    ordering = ("name",)
+    ordering = ("created_on", "name",)
     search_fields = ("name",)
 
-    readonly_fields = ("created_by",)
+    readonly_fields = ("created_by", "created_on",)
     autocomplete_fields = (
         "primary_contract",
         "types",
@@ -235,6 +235,8 @@ class ContractAdmin(
                     "fields": [
                         "issues",
                         "notes",
+                        "created_by",
+                        "created_on",
                     ]
                 },
             ),
diff --git a/contracts/migrations/0019_contract_created_on.py b/contracts/migrations/0019_contract_created_on.py
new file mode 100644
index 0000000..ae8162d
--- /dev/null
+++ b/contracts/migrations/0019_contract_created_on.py
@@ -0,0 +1,19 @@
+# Generated by Django 4.1.4 on 2023-03-30 08:35
+
+import datetime
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('contracts', '0018_contractee_created_by_signee_created_by'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='contract',
+            name='created_on',
+            field=models.DateTimeField(default=datetime.datetime.now, verbose_name='Čas vytvoření'),
+        ),
+    ]
diff --git a/contracts/migrations/0020_alter_contract_created_on.py b/contracts/migrations/0020_alter_contract_created_on.py
new file mode 100644
index 0000000..960aa5a
--- /dev/null
+++ b/contracts/migrations/0020_alter_contract_created_on.py
@@ -0,0 +1,19 @@
+# Generated by Django 4.1.4 on 2023-03-30 08:36
+
+import contracts.models
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('contracts', '0019_contract_created_on'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='contract',
+            name='created_on',
+            field=models.DateTimeField(default=contracts.models.get_created_on_timestamp, verbose_name='Čas vytvoření'),
+        ),
+    ]
diff --git a/contracts/models.py b/contracts/models.py
index 1e5cb1f..7950626 100644
--- a/contracts/models.py
+++ b/contracts/models.py
@@ -1,3 +1,4 @@
+import datetime
 import typing
 
 from django.conf import settings
@@ -317,6 +318,10 @@ class ContractFilingArea(ContractCountMixin, NameStrMixin, models.Model):
         verbose_name_plural = "Spisovny"
 
 
+def get_created_on_timestamp():
+    return datetime.datetime.now(datetime.timezone.utc)
+
+
 class Contract(NameStrMixin, models.Model):
     # BEGIN Automatically set fields
 
@@ -330,6 +335,13 @@ class Contract(NameStrMixin, models.Model):
         help_text="Informace není veřejně přístupná. Pokud vytváříš novou smlouvu, budeš to ty.",
     )  # WARNING: exclude in admin
 
+    created_on = models.DateTimeField(
+        blank=False,
+        null=False,
+        default=get_created_on_timestamp,
+        verbose_name="Čas vytvoření",
+    )
+
     public_status_set_by = models.ForeignKey(
         User,
         on_delete=models.SET_NULL,
-- 
GitLab