import device in notification structure
This commit is contained in:
20
apps/notification/migrations/0002_notification_device.py
Normal file
20
apps/notification/migrations/0002_notification_device.py
Normal file
@@ -0,0 +1,20 @@
|
||||
# Generated by Django 5.0 on 2025-10-07 05:35
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('notification', '0001_initial'),
|
||||
('pos_device', '0075_posfreeproducts_company_fee'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='notification',
|
||||
name='device',
|
||||
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='notifications', to='pos_device.device'),
|
||||
),
|
||||
]
|
||||
@@ -1,5 +1,6 @@
|
||||
from apps.core.models import BaseModel
|
||||
from apps.authentication.models import Organization
|
||||
from apps.pos_device.models import Device
|
||||
from django.db import models
|
||||
|
||||
|
||||
@@ -10,7 +11,12 @@ class Notification(BaseModel):
|
||||
('transaction', 'Transaction'),
|
||||
('system', 'System'),
|
||||
)
|
||||
|
||||
device = models.ForeignKey(
|
||||
Device,
|
||||
on_delete=models.CASCADE,
|
||||
related_name='notifications',
|
||||
null=True
|
||||
)
|
||||
organization = models.ForeignKey(
|
||||
Organization,
|
||||
on_delete=models.CASCADE,
|
||||
|
||||
@@ -20,7 +20,10 @@ class NotificationViewSet(SoftDeleteMixin, POSDeviceMixin, DynamicSearchMixin, v
|
||||
""" show all notification of device """
|
||||
|
||||
organization = self.get_device_organization()
|
||||
device = self.get_pos_device()
|
||||
|
||||
queryset = self.queryset.filter(
|
||||
device=device,
|
||||
organization=organization,
|
||||
delivered=False,
|
||||
is_read=False
|
||||
|
||||
@@ -2,6 +2,7 @@ from django.db.models.signals import post_save
|
||||
from django.dispatch import receiver
|
||||
from apps.warehouse.models import InventoryEntry
|
||||
from apps.warehouse.models import product_models
|
||||
from apps.pos_device.models import Device
|
||||
from .models import Notification
|
||||
from django.db import transaction
|
||||
|
||||
@@ -15,29 +16,37 @@ def create_inventory_entry_notification(sender, instance, created, **kwargs):
|
||||
|
||||
def _create_notification():
|
||||
# create notification for organization on pos device
|
||||
Notification.objects.create(
|
||||
organization=instance.organization,
|
||||
title=f" {instance.distribution.distribution_id} ورودی جدید به انبار از توزیع با کد ", # noqa
|
||||
message=f' مقدار {instance.distribution.weight} کیلوگرم' # noqa
|
||||
f' از توزیع با کد {instance.distribution.distribution_id} به انبار ورود خورده است ', # noqa
|
||||
type='inventory',
|
||||
)
|
||||
devices = Device.objects.filter(assignment__client__organization=instance.organization)
|
||||
for device in devices:
|
||||
Notification.objects.create(
|
||||
device=device,
|
||||
organization=instance.organization,
|
||||
title=f" {instance.distribution.distribution_id} ورودی جدید به انبار از توزیع با کد ", # noqa
|
||||
message=f' مقدار {instance.distribution.weight} کیلوگرم' # noqa
|
||||
f' از توزیع با کد {instance.distribution.distribution_id} به انبار ورود خورده است ', # noqa
|
||||
type='inventory',
|
||||
)
|
||||
|
||||
transaction.on_commit(_create_notification)
|
||||
|
||||
@receiver(post_save, sender=product_models.QuotaDistribution) # noqa
|
||||
def create_quota_distribution_notification(sender, instance, created, **kwargs):
|
||||
""" Create notification for organization for quota distribution creation """
|
||||
|
||||
if not created:
|
||||
return
|
||||
|
||||
def _create_notification():
|
||||
# create notification for organization on pos device
|
||||
Notification.objects.create(
|
||||
organization=instance.assigned_organization,
|
||||
title=f" {instance.quota.quota_id} توزیع جدید به شما از سهمیه با کد ", # noqa
|
||||
message=f' مقدار {instance.weight} کیلوگرم' # noqa
|
||||
f' از سهمیه با کد {instance.quota.quota_id} به شما توزیع شده است ', # noqa
|
||||
type='inventory',
|
||||
)
|
||||
devices = Device.objects.filter(assignment__client__organization=instance.organization)
|
||||
for device in devices:
|
||||
Notification.objects.create(
|
||||
device=device,
|
||||
organization=instance.assigned_organization,
|
||||
title=f" {instance.quota.quota_id} توزیع جدید به شما از سهمیه با کد ", # noqa
|
||||
message=f' مقدار {instance.weight} کیلوگرم' # noqa
|
||||
f' از سهمیه با کد {instance.quota.quota_id} به شما توزیع شده است ', # noqa
|
||||
type='inventory',
|
||||
)
|
||||
|
||||
transaction.on_commit(_create_notification)
|
||||
|
||||
Reference in New Issue
Block a user