This commit is contained in:
Viner Abubakirov
2025-12-10 14:18:04 +05:00
parent 9a16f9c9ca
commit 36fbd46909
28 changed files with 819 additions and 0 deletions

View File

View File

@@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

View File

@@ -0,0 +1,5 @@
from django.apps import AppConfig
class CoreConfig(AppConfig):
name = 'core'

View File

@@ -0,0 +1,34 @@
import threading
from django.utils.deprecation import MiddlewareMixin
_thread_local = threading.local()
def get_current_request():
"""Retrieve the current request stored in thread-local storage."""
return getattr(_thread_local, "request", None)
def get_current_user():
"""Retrieve the user from the current request."""
request = get_current_request()
if request:
return getattr(request, "user", None)
return None
class CurrentRequestMiddleware(MiddlewareMixin):
"""Middleware to store the current request in thread-local storage.
Args:
MiddlewareMixin : Base class for Django middleware.
"""
def process_request(self, request):
_thread_local.request = request
def process_response(self, request, response):
if hasattr(_thread_local, "request"):
del _thread_local.request
return response

View File

@@ -0,0 +1,42 @@
from django.db import models
from django.contrib.auth import get_user_model
from core.middleware.current_request import get_current_user
UserModel = get_user_model()
class BaseModel(models.Model):
"""Abstract base model that includes created_by and updated_by fields.
The fields are automatically populated with the current user from the request.
"""
created_by = models.ForeignKey(
UserModel,
on_delete=models.SET_NULL,
null=True,
blank=True,
related_name="%(class)s_created",
editable=False,
)
updated_by = models.ForeignKey(
UserModel,
on_delete=models.SET_NULL,
null=True,
blank=True,
related_name="%(class)s_updated",
editable=False,
)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
def save(self, *args, **kwargs):
current_user = get_current_user()
if not self.pk and not self.created_by:
self.created_by = current_user
self.updated_by = current_user
super().save(*args, **kwargs)

View File

@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

View File

@@ -0,0 +1,3 @@
from django.shortcuts import render
# Create your views here.