diff --git a/fyle_accounting_mappings/mixins.py b/fyle_accounting_mappings/mixins.py new file mode 100644 index 0000000..11bd794 --- /dev/null +++ b/fyle_accounting_mappings/mixins.py @@ -0,0 +1,58 @@ +from django.db import models + + +class AutoAddCreateUpdateInfoManager(models.Manager): + def update_or_create(self, defaults=None, **kwargs): + """ + Overrides the default update_or_create to handle 'user' keyword argument. + """ + user = kwargs.pop("user", None) + defaults = defaults or {} + + if user and hasattr(user, "email"): + defaults["updated_by"] = user.email + + instance, created = super().update_or_create(defaults=defaults, **kwargs) + + if created and user and hasattr(user, "email"): + instance.created_by = user.email + instance.save(user=user) + + return instance, created + + +class AutoAddCreateUpdateInfoMixin(models.Model): + """ + Mixin to automatically set created_by and updated_by fields. + Stores only the user's email. + """ + + created_by = models.CharField( + max_length=255, + null=True, + blank=True, + help_text="Email of the user who created this record", + ) + updated_by = models.CharField( + max_length=255, + null=True, + blank=True, + help_text="Email of the user who last updated this record", + ) + + objects = AutoAddCreateUpdateInfoManager() + + class Meta: + abstract = True + + def save(self, *args, **kwargs): + """ + Override the save method to set created_by and updated_by fields. + Expects a 'user' keyword argument containing the user instance. + """ + user = kwargs.pop('user', None) + if user and hasattr(user, 'email'): + if not self.pk: + self.created_by = user.email + self.updated_by = user.email + super().save(*args, **kwargs) diff --git a/fyle_accounting_mappings/models.py b/fyle_accounting_mappings/models.py index 5ee74bf..da5c23a 100644 --- a/fyle_accounting_mappings/models.py +++ b/fyle_accounting_mappings/models.py @@ -971,60 +971,3 @@ def bulk_create_ccc_category_mappings(workspace_id: int): CategoryMapping.objects.bulk_update( mapping_updation_batch, fields=['destination_account'], batch_size=50 ) - - -class AutoAddCreateUpdateInfoManager(models.Manager): - def update_or_create(self, defaults=None, **kwargs): - """ - Overrides the default update_or_create to handle 'user' keyword argument. - """ - user = kwargs.pop("user", None) - defaults = defaults or {} - - if user and hasattr(user, "email"): - defaults["updated_by"] = user.email - - instance, created = super().update_or_create(defaults=defaults, **kwargs) - - if created and user and hasattr(user, "email"): - instance.created_by = user.email - instance.save(user=user) - - return instance, created - - -class AutoAddCreateUpdateInfoMixin(models.Model): - """ - Mixin to automatically set created_by and updated_by fields. - Stores only the user's email. - """ - - created_by = models.CharField( - max_length=255, - null=True, - blank=True, - help_text="Email of the user who created this record", - ) - updated_by = models.CharField( - max_length=255, - null=True, - blank=True, - help_text="Email of the user who last updated this record", - ) - - objects = AutoAddCreateUpdateInfoManager() - - class Meta: - abstract = True - - def save(self, *args, **kwargs): - """ - Override the save method to set created_by and updated_by fields. - Expects a 'user' keyword argument containing the user instance. - """ - user = kwargs.pop('user', None) - if user and hasattr(user, 'email'): - if not self.pk: - self.created_by = user.email - self.updated_by = user.email - super().save(*args, **kwargs) diff --git a/setup.py b/setup.py index 03509bd..eb94159 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ setuptools.setup( name='fyle-accounting-mappings', - version='1.36.0', + version='1.36.1', author='Shwetabh Kumar', author_email='shwetabh.kumar@fyle.in', description='Django application to store the fyle accounting mappings in a generic manner',