# Path: tidings/models.py
# class Watch(ModelBase):
#     """The registration of a user's interest in a certain event
# 
#     At minimum, specifies an event_type and thereby an
#     :class:`~tidings.events.Event` subclass. May also specify a content type
#     and/or object ID and, indirectly, any number of
#     :class:`WatchFilters <WatchFilter>`.
# 
#     """
#     #: Key used by an Event to find watches it manages:
#     event_type = models.CharField(max_length=30, db_index=True)
# 
#     #: Optional reference to a content type:
#     content_type = models.ForeignKey(ContentType, null=True, blank=True,
#                                      on_delete=models.CASCADE)
#     object_id = models.PositiveIntegerField(db_index=True, null=True)
#     content_object = GenericForeignKey('content_type', 'object_id')
# 
#     user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True,
#                              on_delete=models.CASCADE)
# 
#     #: Email stored only in the case of anonymous users:
#     email = models.EmailField(db_index=True, null=True, blank=True)
# 
#     #: Secret for activating anonymous watch email addresses.
#     secret = models.CharField(max_length=10, null=True, blank=True)
#     #: Active watches receive notifications, inactive watches don't.
#     is_active = models.BooleanField(default=False, db_index=True)
# 
#     def __unicode__(self):
#         # TODO: Trace event_type back to find the Event subclass, and ask it
#         # how to describe me in English.
#         rest = self.content_object or self.content_type or self.object_id
#         return u'id=%s, type=%s, content_object=%s' % (self.pk,
#                                                        self.event_type,
#                                                        text_type(rest))
# 
#     def activate(self):
#         """Enable this watch so it actually fires.
# 
#         Return ``self`` to support method chaining.
# 
#         """
#         self.is_active = True
#         return self
# 
#     def unsubscribe_url(self):
#         """Return the absolute URL to visit to delete me."""
#         server_relative = ('%s?s=%s' % (reverse('tidings.unsubscribe',
#                                                 args=[self.pk]),
#                                         self.secret))
#         return 'https://%s%s' % (Site.objects.get_current().domain,
#                                  server_relative)


from django.conf import settings
from django.shortcuts import render
from tidings.models import Watch



def unsubscribe(request, watch_id):
    """Unsubscribe from (i.e. delete) the watch of ID ``watch_id``.

    Expects an ``s`` querystring parameter matching the watch's secret.

    GET will result in a confirmation page (or a failure page if the secret is
    wrong). POST will actually delete the watch (again, if the secret is
    correct).

    Uses these templates:

    * tidings/unsubscribe.html - Asks user to confirm deleting a watch
    * tidings/unsubscribe_error.html - Shown when a watch is not found
    * tidings/unsubscribe_success.html - Shown when a watch is deleted

    The shipped templates assume a ``head_title`` and a ``content`` block
    in a ``base.html`` template.

    The template extension can be changed from the default ``html`` using
    the setting :data:`~django.conf.settings.TIDINGS_TEMPLATE_EXTENSION`.
    """
    ext = getattr(settings, 'TIDINGS_TEMPLATE_EXTENSION', 'html')

    # Grab the watch and secret; complain if either is wrong:
    try:

# Path: tidings/models.py
# class Watch(ModelBase):
#     """The registration of a user's interest in a certain event
# 
#     At minimum, specifies an event_type and thereby an
#     :class:`~tidings.events.Event` subclass. May also specify a content type
#     and/or object ID and, indirectly, any number of
#     :class:`WatchFilters <WatchFilter>`.
# 
#     """
#     #: Key used by an Event to find watches it manages:
#     event_type = models.CharField(max_length=30, db_index=True)
# 
#     #: Optional reference to a content type:
#     content_type = models.ForeignKey(ContentType, null=True, blank=True,
#                                      on_delete=models.CASCADE)
#     object_id = models.PositiveIntegerField(db_index=True, null=True)
#     content_object = GenericForeignKey('content_type', 'object_id')
# 
#     user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True,
#                              on_delete=models.CASCADE)
# 
#     #: Email stored only in the case of anonymous users:
#     email = models.EmailField(db_index=True, null=True, blank=True)
# 
#     #: Secret for activating anonymous watch email addresses.
#     secret = models.CharField(max_length=10, null=True, blank=True)
#     #: Active watches receive notifications, inactive watches don't.
#     is_active = models.BooleanField(default=False, db_index=True)
# 
#     def __unicode__(self):
#         # TODO: Trace event_type back to find the Event subclass, and ask it
#         # how to describe me in English.
#         rest = self.content_object or self.content_type or self.object_id
#         return u'id=%s, type=%s, content_object=%s' % (self.pk,
#                                                        self.event_type,
#                                                        text_type(rest))
# 
#     def activate(self):
#         """Enable this watch so it actually fires.
# 
#         Return ``self`` to support method chaining.
# 
#         """
#         self.is_active = True
#         return self
# 
#     def unsubscribe_url(self):
#         """Return the absolute URL to visit to delete me."""
#         server_relative = ('%s?s=%s' % (reverse('tidings.unsubscribe',
#                                                 args=[self.pk]),
#                                         self.secret))
#         return 'https://%s%s' % (Site.objects.get_current().domain,
#                                  server_relative)


# Path: tidings/views.py
from django.conf import settings
from django.shortcuts import render
from tidings.models import Watch




def unsubscribe(request, watch_id):
    """Unsubscribe from (i.e. delete) the watch of ID ``watch_id``.

    Expects an ``s`` querystring parameter matching the watch's secret.

    GET will result in a confirmation page (or a failure page if the secret is
    wrong). POST will actually delete the watch (again, if the secret is
    correct).

    Uses these templates:

    * tidings/unsubscribe.html - Asks user to confirm deleting a watch
    * tidings/unsubscribe_error.html - Shown when a watch is not found
    * tidings/unsubscribe_success.html - Shown when a watch is deleted

    The shipped templates assume a ``head_title`` and a ``content`` block
    in a ``base.html`` template.

    The template extension can be changed from the default ``html`` using
    the setting :data:`~django.conf.settings.TIDINGS_TEMPLATE_EXTENSION`.
    """
    ext = getattr(settings, 'TIDINGS_TEMPLATE_EXTENSION', 'html')

    # Grab the watch and secret; complain if either is wrong:
    try:

        watch = Watch.objects.get(pk=watch_id)