o
    ѷ6i                     @   s,  d dl mZ d dl mZ d dl mZ d dl mZ d dlmZ d dlmZ d dl	m
Z d dlZd	d
lmZ d	dlmZ 				ddededededededeej fddZ		ddejdedeeef deeef dejf
ddZ				ddededejdeeef deeef deeef fddZdS )    )Any)List)Tuple)Union)ContentType)Model)gettextN   )models)app_settingsmessagekeytarget_objecturlfilter_excluderecipient_usersreturnc           	      C   sj   t jrg S |rt|tsttd|j}nd}tjj	||| |||d}t
jr3ddl m} ||| |S )a  
    Notify subscribing users of a new event. Key can be any kind of string,
    just make sure to reuse it where applicable.

    Here is the most basic example: Everyone subscribing to the `"new_comments"` key
    are sent a notification with the message "New comment posted"::

        notify("New comment posted", "new_comments")

    Here is an example that will create a Notification object for everyone who
    has a subscription for the key `"comment/response"` and the model instance `comment_instance`.
    The idea would be that the poster of `comment_instance` will receive notifications when someone responds to that comment.

    .. code-block:: python

        notify(
            "there was a response to your comment",
            "comment/response",
            target_object=comment_instance,
            url=reverse('comments:view', args=(comment_instance.id,))
        )

    :param message: A string containing the message that should be sent to all subscribed users.
    :param key: A key object which is matched to ``NotificationType.key``.
                Users with a Subscription for that NotificationType will have a Notification object created.
    :param url: A URL pointing to your notification.
                If the URL is pointing to your Django project's unique website,
                then add an ``/absolute/url``. However, if the URL should take the user to a different website,
                use a full HTTP scheme, i.e. ``https://example.org/url/``.
    :param recipient_users: A possible iterable of users that should be notified
                            instead of notifying all subscribers of the event.
                            Notice that users still have to be actually subscribed
                            to the event key!
    :param target_object: Any Django model instance that this notification
                          relates to. Uses Django content types.
                          Subscriptions with a matching content_type and object_id will be notified.
    :param filter_exclude: Keyword arguments passed to filter out Subscriptions.
                           Will be handed to ``Subscription.objects.exclude(**filter_exclude)``.
    zFYou supplied a target_object that's not an instance of a django Model.N)	object_idr   r   r   r   r   )subscribers)
django_nyt_disable_notifications
isinstancer   	TypeError_idr
   Notificationcreate_notificationsr   NYT_ENABLE_CHANNELSr   notify_subscribers)	r   r   r   r   r   r   r   notificationsr    r    U/var/www/hoanhtaovolam_webdjango/env/lib/python3.10/site-packages/django_nyt/utils.pynotify   s.   0

r"   settingscontent_typer   c                 K   s0   t jj||d}t jjjd| ||d|d S )am  
    Creates a new subscription to a given key. If the key does not exist
    as a NotificationType, it will be created

    Uses `get_or_create <https://docs.djangoproject.com/en/dev/ref/models/querysets/#get-or-create>`__
    to avoid double creation.

    :param settings: A models.Settings instance
    :param key: The unique key that the Settings should subscribe to
    :param content_type: If notifications are regarding a specific ContentType, it should be set
    :param object_id: If the notifications should only regard a specific object_id
    :param **kwargs: Additional models.Subscription field values
    )r$   )r#   notification_typer   r   Nr    )r
   NotificationType
get_by_keySubscriptionobjectsget_or_create)r#   r   r$   r   kwargsr%   r    r    r!   	subscribe_   s   
r,   userc                 C   s   |r|rJ d|s|sJ dt |t |ksJ dtjjj| d}|r2|r2tjjj||d}n	tjjjddd}|rC|j|d}|rK|j|d}| S )	aw  
    Shortcut function to remove all subscriptions related to a notification key and either a user or a settings object.

    Unsubscribing does NOT delete old notifications, however the subscription relation is nullified.
    This means that any objects accessed through that relation will become inaccessible.
    This is a particular feature chosen to avoid accidentally allowing access to data that may be otherwise have credential-based access.

    :param key: The notification key to unsubscribe a user/user settings.
    :param user: User to unsubscribe
    :param settings: ...or a UserSettings object
    :param content_type: Further narrow down subscriptions to only this content_type
    :param object_id: Further narrow down subscriptions to only this object_id (provided a content_type)
    :return: (int, dict) - the return value of Django's queryset.delete() method.
    z/Cannot apply both User and UserSettings object.zLNeed at least a User and a UserSettings object, refusing to unsubscribe all.zEYou have to supply both a content_type and object_id or none of them.)notification_type__key)notification_type__content_typer   N)settings__user)r#   )boolr
   r(   r)   filterdelete)r   r-   r#   r$   r   subscriptionsr    r    r!   unsubscribe   sB   r5   )NNNN)NN)typingr   r   r   r   "django.contrib.contenttypes.modelsr   django.db.modelsr   django.utils.translationr   r   r    r
   confr   strdictlistr   r"   Settingsintr(   r,   r5   r    r    r    r!   <module>   sv    
S


"


