diff --git a/care/users/api/serializers/user.py b/care/users/api/serializers/user.py index 3530d695bd..9d27848186 100644 --- a/care/users/api/serializers/user.py +++ b/care/users/api/serializers/user.py @@ -420,6 +420,8 @@ class Meta: "local_body_object", "district_object", "state_object", + "modified_date", + "updated_by", "user_type", "doctor_qualification", "doctor_experience_commenced_on", diff --git a/care/users/current_user.py b/care/users/current_user.py new file mode 100644 index 0000000000..ffbf8dbf99 --- /dev/null +++ b/care/users/current_user.py @@ -0,0 +1,13 @@ +from threading import current_thread +from django.utils.deprecation import MiddlewareMixin + +from threading import local + +_user = local() + +class CurrentUserMiddleware(MiddlewareMixin): + def process_request(self, request): + _user.value = request.user + +def get_current_user(): + return str(_user.value) diff --git a/care/users/migrations/0016_user_modified_date.py b/care/users/migrations/0016_user_modified_date.py new file mode 100644 index 0000000000..d956daa435 --- /dev/null +++ b/care/users/migrations/0016_user_modified_date.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.10 on 2024-04-21 07:11 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("users", "0015_age_to_dateofbirth"), + ] + + operations = [ + migrations.AddField( + model_name="user", + name="modified_date", + field=models.DateTimeField(auto_now=True), + ), + ] diff --git a/care/users/migrations/0017_remove_user_modified_date.py b/care/users/migrations/0017_remove_user_modified_date.py new file mode 100644 index 0000000000..057940c86f --- /dev/null +++ b/care/users/migrations/0017_remove_user_modified_date.py @@ -0,0 +1,17 @@ +# Generated by Django 4.2.10 on 2024-04-21 07:28 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ("users", "0016_user_modified_date"), + ] + + operations = [ + migrations.RemoveField( + model_name="user", + name="modified_date", + ), + ] diff --git a/care/users/migrations/0018_user_modified_date_user_updated_by.py b/care/users/migrations/0018_user_modified_date_user_updated_by.py new file mode 100644 index 0000000000..e3c7ddaad6 --- /dev/null +++ b/care/users/migrations/0018_user_modified_date_user_updated_by.py @@ -0,0 +1,23 @@ +# Generated by Django 4.2.10 on 2024-04-21 07:32 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("users", "0017_remove_user_modified_date"), + ] + + operations = [ + migrations.AddField( + model_name="user", + name="modified_date", + field=models.DateTimeField(auto_now=True), + ), + migrations.AddField( + model_name="user", + name="updated_by", + field=models.CharField(default="", max_length=150), + ), + ] diff --git a/care/users/migrations/0019_alter_user_updated_by.py b/care/users/migrations/0019_alter_user_updated_by.py new file mode 100644 index 0000000000..b5c62ab291 --- /dev/null +++ b/care/users/migrations/0019_alter_user_updated_by.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.10 on 2024-04-21 12:41 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("users", "0018_user_modified_date_user_updated_by"), + ] + + operations = [ + migrations.AlterField( + model_name="user", + name="updated_by", + field=models.CharField(default="username", max_length=150), + ), + ] diff --git a/care/users/migrations/0020_alter_user_updated_by.py b/care/users/migrations/0020_alter_user_updated_by.py new file mode 100644 index 0000000000..99105eb9ed --- /dev/null +++ b/care/users/migrations/0020_alter_user_updated_by.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.10 on 2024-04-21 12:53 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("users", "0019_alter_user_updated_by"), + ] + + operations = [ + migrations.AlterField( + model_name="user", + name="updated_by", + field=models.CharField(max_length=150, verbose_name="username"), + ), + ] diff --git a/care/users/models.py b/care/users/models.py index c06ae3e43c..7f642c0bcf 100644 --- a/care/users/models.py +++ b/care/users/models.py @@ -7,7 +7,7 @@ from django.urls import reverse from django.utils.timezone import now from django.utils.translation import gettext_lazy as _ - +from .current_user import get_current_user from care.utils.models.base import BaseModel from care.utils.models.validators import ( UsernameValidator, @@ -232,7 +232,10 @@ class User(AbstractUser): District, on_delete=models.PROTECT, null=True, blank=True ) state = models.ForeignKey(State, on_delete=models.PROTECT, null=True, blank=True) - + modified_date = models.DateTimeField(auto_now=True) + updated_by=models.CharField(_("username"), + max_length=150 + ) phone_number = models.CharField( max_length=14, validators=[mobile_or_landline_number_validator] ) @@ -373,6 +376,8 @@ def save(self, *args, **kwargs) -> None: self.district = self.local_body.district if self.district is not None: self.state = self.district.state + self.modified_date + self.updated_by=get_current_user() super().save(*args, **kwargs) diff --git a/config/settings/base.py b/config/settings/base.py index 9a6df3249b..e0256790d6 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -185,6 +185,9 @@ "simple_history.middleware.HistoryRequestMiddleware", "maintenance_mode.middleware.MaintenanceModeMiddleware", "care.audit_log.middleware.AuditLogMiddleware", + "care.users.current_user.CurrentUserMiddleware", + + ] # STATIC