Skip to content

Instantly share code, notes, and snippets.

@muhammadbahtiars
Created November 27, 2023 11:05
Show Gist options
  • Save muhammadbahtiars/b9d49a94925d7ff17f174be4862bb6ac to your computer and use it in GitHub Desktop.
Save muhammadbahtiars/b9d49a94925d7ff17f174be4862bb6ac to your computer and use it in GitHub Desktop.
NOTIFICATION MANAGER PYTHON
@shared_task
def addNotification(type_notif, activity, code, role_target, user_target, message):
data = {
"type": type_notif,
"activity": activity,
"code": code,
"role_target": role_target,
"user_target": user_target,
"sender_status": False,
"message": message,
}
try:
record = NotificationManager.objects.get(type=data["type"], code=data["code"])
record.__dict__.update(**data)
record.save()
except:
NotificationManager.objects.create(**data)
# sendMailNotificationUser.delay(mail=user_target)
# sendMailNotificationRole.delay(role=role_target)
return "success"
class NotificationView(APIView):
authentication_classes = [CustomJWTAuthentication]
@csrf_exempt
def get(self, request):
email = request.user.email
role = request.user.role_code
data = NotificationManager.objects.filter(
Q(role_target__icontains=role) |
Q(user_target__icontains=email)
).values(
'type','activity','code','message'
)
response_data = StandardReturn(
message="Data listed successfully", data=data, meta=[]
)
return Response(response_data.__dict__, status=status.HTTP_200_OK)
class NotificationManager(models.Model):
"""Model Notification Manager"""
oid = models.UUIDField(
primary_key=True,
default=uuid.uuid4,
editable=False,
)
type = models.CharField(
db_column="TYPE", max_length=100, blank=True, null=True, unique=True
)
activity = models.CharField(
db_column="ACTVT", max_length=50, blank=True, null=True, unique=True
)
code = models.CharField(db_column="CDNOT", max_length=50, blank=True, null=True)
role_target = models.CharField(
db_column="RLTRG", max_length=50, blank=True, null=True
)
user_target = models.CharField(
db_column="UTRGT", max_length=50, blank=True, null=True
)
sender_status = models.BooleanField(
db_column="SNDST", default=False
)
message = models.CharField(
db_column="ACMSG", default=False
)
begin_date = models.DateField(
db_column="BEGDA", blank=True, null=True, auto_now_add=True
)
end_date = models.DateField(
db_column="ENDDA", blank=True, null=True, default="2099-11-01"
)
change_date = models.DateTimeField(
db_column="CHGDA", blank=True, null=True, auto_now=True
)
change_by = models.CharField(
db_column="CHGBY", max_length=50, blank=True, null=True
)
class Meta:
db_table = "NOTIFICATION_MANAGER"
"type": Checklist / Initial Asset / Movement or etc,
"activity": Reuquest / Review / Approve or etc,
"code": checklist code or etc,
"role_target": role code ( If target notif = bulk by role ),
"user_target": user_target ( If target notif = personal by email),
"sender_status": False, ( Used as email sending flag )
"message": message,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment