notification system deployment - v2
This commit is contained in:
@@ -38,11 +38,11 @@ class BaseModel(models.Model):
|
|||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
user = get_current_user() # get user object
|
user = get_current_user() # get user object
|
||||||
if not isinstance(user, AnonymousUser):
|
if not isinstance(user, AnonymousUser):
|
||||||
self.modified_by = user
|
self.modified_by = user if user else None
|
||||||
if not self.creator_info:
|
if not self.creator_info:
|
||||||
self.created_by = user
|
self.created_by = user if user else None
|
||||||
self.creator_info = user.first_name + ' ' + user.last_name
|
self.creator_info = user.first_name + ' ' + user.last_name if user else None
|
||||||
self.modifier_info = user.first_name + ' ' + user.last_name
|
self.modifier_info = user.first_name + ' ' + user.last_name if user else None
|
||||||
super(BaseModel, self).save(*args, **kwargs)
|
super(BaseModel, self).save(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ class Notification(BaseModel):
|
|||||||
delivered = models.BooleanField(default=False)
|
delivered = models.BooleanField(default=False)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f'{self.organization.name} - {self.title}'
|
return f'{self.organization.name if self.organization else self.id} - {self.title}'
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
return super(Notification, self).save(*args, **kwargs)
|
return super(Notification, self).save(*args, **kwargs)
|
||||||
|
|||||||
@@ -1,9 +1,54 @@
|
|||||||
|
from apps.notification.pos.api.v1.serializers import NotificationSerializer
|
||||||
|
from apps.pos_device.mixins.pos_device_mixin import POSDeviceMixin
|
||||||
|
from apps.core.mixins.soft_delete_mixin import SoftDeleteMixin
|
||||||
|
from apps.core.mixins.search_mixin import DynamicSearchMixin
|
||||||
from apps.notification.models import Notification
|
from apps.notification.models import Notification
|
||||||
|
from rest_framework.permissions import AllowAny
|
||||||
|
from rest_framework.decorators import action
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework import viewsets
|
from rest_framework import viewsets
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
|
|
||||||
|
|
||||||
class NotificationViewSet(viewsets.ModelViewSet):
|
class NotificationViewSet(SoftDeleteMixin, POSDeviceMixin, DynamicSearchMixin, viewsets.ModelViewSet):
|
||||||
pass
|
queryset = Notification.objects.all().select_related('organization')
|
||||||
|
serializer_class = NotificationSerializer
|
||||||
|
permission_classes = [AllowAny]
|
||||||
|
|
||||||
|
def list(self, request, *args, **kwargs):
|
||||||
|
""" show all notification of device """
|
||||||
|
|
||||||
|
organization = self.get_device_organization()
|
||||||
|
queryset = self.queryset.filter(
|
||||||
|
organization=organization,
|
||||||
|
delivered=False,
|
||||||
|
is_read=False
|
||||||
|
)
|
||||||
|
|
||||||
|
# set notifications delivered status to true
|
||||||
|
# queryset.update(delivered=True)
|
||||||
|
|
||||||
|
# paginate & response
|
||||||
|
page = self.paginate_queryset(queryset)
|
||||||
|
if page is not None:
|
||||||
|
serializer = self.get_serializer(page, many=True)
|
||||||
|
return self.get_paginated_response(serializer.data)
|
||||||
|
|
||||||
|
@action(
|
||||||
|
methods=['put'],
|
||||||
|
detail=True,
|
||||||
|
url_path='read_notification',
|
||||||
|
url_name='read_notification',
|
||||||
|
name='read_notification',
|
||||||
|
)
|
||||||
|
@transaction.atomic
|
||||||
|
def read_notification(self, request, pk=None):
|
||||||
|
""" read notification by pos organization """
|
||||||
|
|
||||||
|
query = self.get_object()
|
||||||
|
|
||||||
|
query.is_read = True
|
||||||
|
query.save()
|
||||||
|
|
||||||
|
return Response(status=status.HTTP_200_OK)
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
from rest_framework import serializers
|
||||||
|
from apps.notification.models import Notification
|
||||||
|
|
||||||
|
|
||||||
|
class NotificationSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = Notification
|
||||||
|
fields = '__all__'
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
from django.urls import path, include
|
from django.urls import path, include
|
||||||
from rest_framework.routers import DefaultRouter
|
from rest_framework.routers import DefaultRouter
|
||||||
|
from .api import NotificationViewSet
|
||||||
|
|
||||||
router = DefaultRouter()
|
router = DefaultRouter()
|
||||||
router.register(r'')
|
router.register(r'notification', NotificationViewSet, basename='notification')
|
||||||
|
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
|
|||||||
@@ -2,5 +2,5 @@ from django.urls import path, include
|
|||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
# path('web/', include('apps.notification.web.api.v1.urls')),
|
# path('web/', include('apps.notification.web.api.v1.urls')),
|
||||||
# path('pos/', include('apps.notification.pos.api.v1.urls'))
|
path('pos/', include('apps.notification.pos.api.v1.urls'))
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
from apps.notification.models import Notification
|
||||||
from apps.product.web.api.v1.serializers import quota_distribution_serializers as distribution_serializers
|
from apps.product.web.api.v1.serializers import quota_distribution_serializers as distribution_serializers
|
||||||
from apps.core.mixins.soft_delete_mixin import SoftDeleteMixin
|
from apps.core.mixins.soft_delete_mixin import SoftDeleteMixin
|
||||||
from apps.core.mixins.search_mixin import DynamicSearchMixin
|
from apps.core.mixins.search_mixin import DynamicSearchMixin
|
||||||
@@ -49,6 +50,7 @@ class QuotaDistributionViewSet(SoftDeleteMixin, viewsets.ModelViewSet, DynamicSe
|
|||||||
def create(self, request, *args, **kwargs):
|
def create(self, request, *args, **kwargs):
|
||||||
""" create distribution for organizations users """
|
""" create distribution for organizations users """
|
||||||
|
|
||||||
|
organization = get_organization_by_user(request.user)
|
||||||
# get distributed assigner user
|
# get distributed assigner user
|
||||||
try:
|
try:
|
||||||
assigner_user = product_models.UserRelations.objects.filter(
|
assigner_user = product_models.UserRelations.objects.filter(
|
||||||
@@ -60,8 +62,16 @@ class QuotaDistributionViewSet(SoftDeleteMixin, viewsets.ModelViewSet, DynamicSe
|
|||||||
request.data.update({'assigner_organization': assigner_user.organization.id})
|
request.data.update({'assigner_organization': assigner_user.organization.id})
|
||||||
serializer = self.serializer_class(data=request.data)
|
serializer = self.serializer_class(data=request.data)
|
||||||
if serializer.is_valid():
|
if serializer.is_valid():
|
||||||
serializer.save()
|
distribution = serializer.save()
|
||||||
|
|
||||||
|
# create notification for organization on pos device
|
||||||
|
Notification.objects.create(
|
||||||
|
organization=organization,
|
||||||
|
title=f" {distribution.quota.quota_id} توزیع جدید به شما از سهمیه با کد ", # noqa
|
||||||
|
message=f' مقدار {distribution.weight} کیلوگرم' # noqa
|
||||||
|
f' از سهمیه با کد {distribution.quota.quota_id} به شما توزیع شده است ', # noqa
|
||||||
|
type='inventory',
|
||||||
|
)
|
||||||
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
||||||
return Response(serializer.errors, status=status.HTTP_403_FORBIDDEN)
|
return Response(serializer.errors, status=status.HTTP_403_FORBIDDEN)
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ from apps.core.mixins.soft_delete_mixin import SoftDeleteMixin
|
|||||||
from apps.core.mixins.search_mixin import DynamicSearchMixin
|
from apps.core.mixins.search_mixin import DynamicSearchMixin
|
||||||
from apps.warehouse import models as warehouse_models
|
from apps.warehouse import models as warehouse_models
|
||||||
from common.helpers import get_organization_by_user
|
from common.helpers import get_organization_by_user
|
||||||
|
from apps.notification.models import Notification
|
||||||
from common.generics import base64_to_image_file
|
from common.generics import base64_to_image_file
|
||||||
from common.liara_tools import upload_to_liara
|
from common.liara_tools import upload_to_liara
|
||||||
from rest_framework.decorators import action
|
from rest_framework.decorators import action
|
||||||
@@ -56,8 +57,9 @@ class InventoryEntryViewSet(SoftDeleteMixin, viewsets.ModelViewSet, DynamicSearc
|
|||||||
# create inventory entry
|
# create inventory entry
|
||||||
inventory_balance = request.data['weight']
|
inventory_balance = request.data['weight']
|
||||||
|
|
||||||
|
organization = get_organization_by_user(request.user)
|
||||||
request.data.update({
|
request.data.update({
|
||||||
'organization': (get_organization_by_user(request.user)).id,
|
'organization': organization.id,
|
||||||
'balance': inventory_balance
|
'balance': inventory_balance
|
||||||
})
|
})
|
||||||
serializer = self.serializer_class(data=request.data)
|
serializer = self.serializer_class(data=request.data)
|
||||||
@@ -68,6 +70,15 @@ class InventoryEntryViewSet(SoftDeleteMixin, viewsets.ModelViewSet, DynamicSearc
|
|||||||
if 'document' in request.data.keys():
|
if 'document' in request.data.keys():
|
||||||
self.upload_confirmation_document(request, inventory=inventory_entry.id)
|
self.upload_confirmation_document(request, inventory=inventory_entry.id)
|
||||||
|
|
||||||
|
# create notification for organization on pos device
|
||||||
|
Notification.objects.create(
|
||||||
|
organization=organization,
|
||||||
|
title=f" {inventory_entry.distribution.distribution_id} ورودی جدید به انبار از توزیع با کد ", # noqa
|
||||||
|
message=f' مقدار {inventory_entry.distribution.weight} کیلوگرم' # noqa
|
||||||
|
f' از توزیع با کد {inventory_entry.distribution.distribution_id} به انبار ورود خورده است ', # noqa
|
||||||
|
type='inventory',
|
||||||
|
)
|
||||||
|
|
||||||
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
||||||
return Response(serializer.errors, status=status.HTTP_403_FORBIDDEN)
|
return Response(serializer.errors, status=status.HTTP_403_FORBIDDEN)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user