Skip to content

Commit

Permalink
Add adapter_extra_parameters and store adapter_used after send
Browse files Browse the repository at this point in the history
  • Loading branch information
hugobessa committed Dec 21, 2024
1 parent 68689e8 commit b46d68a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 53 deletions.
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions vintasend_django/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,18 @@ class Migration(migrations.Migration):
),
),
(
"metadata",
"adapter_extra_parameters",
models.JSONField(
null=True,
verbose_name="Metadata for the notification adapter",
verbose_name="Extra parameters for the notification adapter",
),
),
(
"adapter_used",
models.CharField(
max_length=255,
null=True,
verbose_name="Adapter used to send the notification",
),
),
],
Expand Down
3 changes: 2 additions & 1 deletion vintasend_django/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ class Notification(models.Model):
created = AutoCreatedField(_("created"), db_index=True)
modified = AutoLastModifiedField(_("modified"), db_index=True)

metadata = models.JSONField(_("metadata"), default=dict, blank=True)
adapter_extra_parameters = models.JSONField(_("adapter_extra_parameters"), null=True)

context_used = models.JSONField(_("context used when notification was sent"), null=True)
adapter_used = models.CharField(_("adapter used to send the notification"), max_length=255, null=True)

objects: models.Manager["Notification"]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,30 @@ def _get_all_future_notifications_queryset(self) -> QuerySet["NotificationModel"
Q(send_after__gte=datetime.datetime.now()) | Q(send_after__isnull=False),
status=NotificationStatus.PENDING_SEND.value,
).order_by("created")

def _get_all_in_app_unread_notifications_queryset(self, user_id: int | str | uuid.UUID) -> QuerySet["NotificationModel"]:

def _get_all_in_app_unread_notifications_queryset(
self, user_id: int | str | uuid.UUID
) -> QuerySet["NotificationModel"]:
return NotificationModel.objects.filter(
user_id=str(user_id),
status=NotificationStatus.SENT.value,
notification_type=NotificationTypes.IN_APP,
).order_by("created")

def _get_all_pending_notifications_queryset(self) -> QuerySet["NotificationModel"]:
return NotificationModel.objects.filter(
Q(send_after__lte=datetime.datetime.now()) | Q(send_after__isnull=True),
status=NotificationStatus.PENDING_SEND.value,
).order_by("created")

def _paginate_queryset(self, queryset: "QuerySet[NotificationModel]", page: int, page_size: int) -> QuerySet["NotificationModel"]:

def _paginate_queryset(
self, queryset: "QuerySet[NotificationModel]", page: int, page_size: int
) -> QuerySet["NotificationModel"]:
return queryset[((page - 1) * page_size) : ((page - 1) * page_size) + page_size]

def _serialize_notification_queryset(self, queryset: "QuerySet[NotificationModel]") -> Iterable[Notification]:

def _serialize_notification_queryset(
self, queryset: "QuerySet[NotificationModel]"
) -> Iterable[Notification]:
return (self.serialize_notification(n) for n in queryset.iterator())

def serialize_notification(self, notification: NotificationModel) -> Notification:
Expand Down Expand Up @@ -72,7 +78,7 @@ def persist_notification(
send_after: datetime.datetime | None,
subject_template: str | None = None,
preheader_template: str | None = None,
metadata: dict | None = None,
adapter_extra_parameters: dict | None = None,
) -> Notification:
notification_instance = NotificationModel.objects.create(
user_id=str(user_id),
Expand All @@ -84,6 +90,7 @@ def persist_notification(
send_after=send_after,
subject_template=subject_template or "",
preheader_template=preheader_template or "",
adapter_extra_parameters=adapter_extra_parameters,
)
return self.serialize_notification(notification_instance)

Expand Down Expand Up @@ -144,33 +151,25 @@ def get_notification(
except NotificationModel.DoesNotExist as e:
raise NotificationNotFoundError("Notification not found") from e
return self.serialize_notification(notification_instance)

def get_all_pending_notifications(self) -> Iterable[Notification]:
return (
self._serialize_notification_queryset(
self._get_all_pending_notifications_queryset()
)
)
return self._serialize_notification_queryset(self._get_all_pending_notifications_queryset())

def get_pending_notifications(self, page: int, page_size: int) -> Iterable[Notification]:
return (
self._serialize_notification_queryset(
self._paginate_queryset(
self._get_all_pending_notifications_queryset(),
page,
page_size,
)
return self._serialize_notification_queryset(
self._paginate_queryset(
self._get_all_pending_notifications_queryset(),
page,
page_size,
)
)

def filter_all_in_app_unread_notifications(
self,
user_id: int | str | uuid.UUID,
) -> Iterable[Notification]:
return (
self._serialize_notification_queryset(
self._get_all_in_app_unread_notifications_queryset(user_id),
)
return self._serialize_notification_queryset(
self._get_all_in_app_unread_notifications_queryset(user_id),
)

def filter_in_app_unread_notifications(
Expand All @@ -179,49 +178,54 @@ def filter_in_app_unread_notifications(
page: int = 1,
page_size: int = 10,
) -> Iterable[Notification]:
return (
self._serialize_notification_queryset(
self._paginate_queryset(
self._get_all_in_app_unread_notifications_queryset(user_id),
page,
page_size,
)
return self._serialize_notification_queryset(
self._paginate_queryset(
self._get_all_in_app_unread_notifications_queryset(user_id),
page,
page_size,
)
)

def get_all_future_notifications(self) -> Iterable["Notification"]:
return self._serialize_notification_queryset(
self._get_all_future_notifications_queryset()
)

return self._serialize_notification_queryset(self._get_all_future_notifications_queryset())

def get_future_notifications(self, page: int, page_size: int) -> Iterable["Notification"]:
return self._serialize_notification_queryset(
self._paginate_queryset(
self._get_all_future_notifications_queryset(),
page,
page_size
)
self._paginate_queryset(self._get_all_future_notifications_queryset(), page, page_size)
)

def get_all_future_notifications_from_user(self, user_id: int | str | uuid.UUID) -> Iterable["Notification"]:

def get_all_future_notifications_from_user(
self, user_id: int | str | uuid.UUID
) -> Iterable["Notification"]:
return self._serialize_notification_queryset(
self._get_all_future_notifications_queryset().filter(user_id=str(user_id))
)

def get_future_notifications_from_user(self, user_id: int | str | uuid.UUID, page: int, page_size: int) -> Iterable["Notification"]:

def get_future_notifications_from_user(
self, user_id: int | str | uuid.UUID, page: int, page_size: int
) -> Iterable["Notification"]:
return self._serialize_notification_queryset(
self._paginate_queryset(
self._get_all_future_notifications_queryset().filter(user_id=str(user_id)),
page,
page_size
page_size,
)
)

def get_user_email_from_notification(self, notification_id: int | str | uuid.UUID) -> str:
notification_user = NotificationModel.objects.select_related("user").get(id=str(notification_id)).user
notification_user = (
NotificationModel.objects.select_related("user").get(id=str(notification_id)).user
)
if not notification_user or not notification_user.is_active:
raise NotificationUserNotFoundError("User not found")
return notification_user.email

def store_context_used(self, notification_id: int | str | uuid.UUID, context: dict) -> None:
NotificationModel.objects.filter(id=str(notification_id)).update(context_used=context)
def store_context_used(
self,
notification_id: int | str | uuid.UUID,
context: dict,
adapter_import_str: str,
) -> None:
NotificationModel.objects.filter(id=str(notification_id)).update(
context_used=context, adapter_used=adapter_import_str
)

0 comments on commit b46d68a

Please sign in to comment.