fix customer view and edit
This commit is contained in:
1 parent
81335b1439
commit
d5c22a39dc
13 files changed
+329
-81
No files matched your search
+17
-1
@@ -1,5 +1,5 @@
|
||||
from django.contrib import admin
|
||||
from .models import ContactGroup, CustomerProfile
|
||||
from .models import ContactEditLog, ContactGroup, CustomerProfile
|
||||
|
||||
|
||||
@admin.register(ContactGroup)
|
||||
@@ -15,3 +15,19 @@ class CustomerProfileAdmin(admin.ModelAdmin):
|
||||
list_filter = ("stakeholder_type", "groups")
|
||||
search_fields = ("user__mobile", "organization_name", "first_name", "last_name")
|
||||
filter_horizontal = ("groups",)
|
||||
|
||||
|
||||
@admin.register(ContactEditLog)
|
||||
class ContactEditLogAdmin(admin.ModelAdmin):
|
||||
list_display = ("contact_public_id", "actor_name", "created_at")
|
||||
search_fields = ("actor_name", "contact_public_id")
|
||||
readonly_fields = ("contact", "contact_public_id", "actor", "actor_name", "changes", "created_at")
|
||||
|
||||
def has_add_permission(self, request):
|
||||
return False
|
||||
|
||||
def has_change_permission(self, request, obj=None):
|
||||
return False
|
||||
|
||||
def has_delete_permission(self, request, obj=None):
|
||||
return False
|
||||
@@ -0,0 +1,33 @@
|
||||
# Generated by Django 5.1.1 on 2026-09-29 06:14
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('customers', '0004_contactgroup_unit_customerprofile_groups_and_more'),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='ContactEditLog',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('contact_public_id', models.UUIDField(db_index=True)),
|
||||
('actor_name', models.CharField(max_length=150)),
|
||||
('changes', models.JSONField()),
|
||||
('created_at', models.DateTimeField(auto_now_add=True, db_index=True)),
|
||||
('actor', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='contact_edits', to=settings.AUTH_USER_MODEL)),
|
||||
('contact', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='edit_logs', to='customers.customerprofile')),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'سابقه ویرایش مخاطب',
|
||||
'verbose_name_plural': 'سوابق ویرایش مخاطبان',
|
||||
'ordering': ['-created_at', '-pk'],
|
||||
},
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,25 @@
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
def backfill_customer_profiles(apps, schema_editor):
|
||||
User = apps.get_model("accounts", "User")
|
||||
CustomerProfile = apps.get_model("customers", "CustomerProfile")
|
||||
database = schema_editor.connection.alias
|
||||
missing_users = (
|
||||
User.objects.using(database)
|
||||
.filter(account_type="CUSTOMER", customer_profile__isnull=True)
|
||||
.values("pk", "first_name", "last_name")
|
||||
)
|
||||
batch = []
|
||||
for user in missing_users.iterator(chunk_size=500):
|
||||
batch.append(CustomerProfile(user_id=user["pk"], first_name=user["first_name"][:100], last_name=user["last_name"][:100]))
|
||||
if len(batch) == 500:
|
||||
CustomerProfile.objects.using(database).bulk_create(batch, ignore_conflicts=True, batch_size=500)
|
||||
batch.clear()
|
||||
if batch:
|
||||
CustomerProfile.objects.using(database).bulk_create(batch, ignore_conflicts=True, batch_size=500)
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [("customers", "0005_contacteditlog")]
|
||||
operations = [migrations.RunPython(backfill_customer_profiles, migrations.RunPython.noop)]
|
||||
@@ -52,3 +52,19 @@ class CustomerProfile(PersianTextNormalizationMixin, TimeStampedModel):
|
||||
province = models.CharField(max_length=100, blank=True)
|
||||
city = models.CharField(max_length=100, blank=True)
|
||||
def __str__(self): return (f"{self.first_name} {self.last_name}".strip() or self.user.mobile or self.user.username)
|
||||
|
||||
|
||||
class ContactEditLog(models.Model):
|
||||
"""Persistent record of changes made through the staff contact editor."""
|
||||
|
||||
contact = models.ForeignKey(CustomerProfile, on_delete=models.SET_NULL, null=True, related_name="edit_logs")
|
||||
contact_public_id = models.UUIDField(db_index=True)
|
||||
actor = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True, related_name="contact_edits")
|
||||
actor_name = models.CharField(max_length=150)
|
||||
changes = models.JSONField()
|
||||
created_at = models.DateTimeField(auto_now_add=True, db_index=True)
|
||||
|
||||
class Meta:
|
||||
ordering = ["-created_at", "-pk"]
|
||||
verbose_name = "سابقه ویرایش مخاطب"
|
||||
verbose_name_plural = "سوابق ویرایش مخاطبان"
|
||||
Reference in new issue
Block a user