Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Resolve Import Error #129

Merged
merged 17 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions fyle_accounting_mappings/mixins.py
Original file line number Diff line number Diff line change
@@ -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)
57 changes: 0 additions & 57 deletions fyle_accounting_mappings/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setuptools.setup(
name='fyle-accounting-mappings',
version='1.36.0',
version='1.36.1',
author='Shwetabh Kumar',
author_email='[email protected]',
description='Django application to store the fyle accounting mappings in a generic manner',
Expand Down
Loading