Skip to content

Commit

Permalink
Add “modified_date” & “updated_by” to the User table ohcnetwork#2085
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushchauhan12 committed Apr 21, 2024
1 parent 2c023e1 commit c59c884
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 2 deletions.
2 changes: 2 additions & 0 deletions care/users/api/serializers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 13 additions & 0 deletions care/users/current_user.py
Original file line number Diff line number Diff line change
@@ -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)
18 changes: 18 additions & 0 deletions care/users/migrations/0016_user_modified_date.py
Original file line number Diff line number Diff line change
@@ -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),
),
]
17 changes: 17 additions & 0 deletions care/users/migrations/0017_remove_user_modified_date.py
Original file line number Diff line number Diff line change
@@ -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",
),
]
23 changes: 23 additions & 0 deletions care/users/migrations/0018_user_modified_date_user_updated_by.py
Original file line number Diff line number Diff line change
@@ -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),
),
]
18 changes: 18 additions & 0 deletions care/users/migrations/0019_alter_user_updated_by.py
Original file line number Diff line number Diff line change
@@ -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),
),
]
18 changes: 18 additions & 0 deletions care/users/migrations/0020_alter_user_updated_by.py
Original file line number Diff line number Diff line change
@@ -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"),
),
]
9 changes: 7 additions & 2 deletions care/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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]
)
Expand Down Expand Up @@ -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)


Expand Down
3 changes: 3 additions & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@
"simple_history.middleware.HistoryRequestMiddleware",
"maintenance_mode.middleware.MaintenanceModeMiddleware",
"care.audit_log.middleware.AuditLogMiddleware",
"care.users.current_user.CurrentUserMiddleware",


]

# STATIC
Expand Down

0 comments on commit c59c884

Please sign in to comment.