# Path: confirm/models.py
# class ServerConfirmationKey(ConfirmationKey):
#     subject = models.ForeignKey(Server, on_delete=models.CASCADE, related_name='confirmations')
#     objects = ServerConfirmationKeyQuerySet.as_manager()
# 
#     message_template = 'confirm/server_contact.txt'
#     message_subject = 'Confirm contact details for %(serverdomain)s on '
#     '%(protocol)s://%(domain)s'
# 
#     def add_context(self):
#         return {'serverdomain': self.subject.domain}
# 
#     @property
#     def recipient(self):
#         return self.subject.contact
# 
#     def __init__(self, *args, **kwargs):
#         super(ServerConfirmationKey, self).__init__(*args, **kwargs)
#         # TODO: WHAT?
#         if self.id:
#             self.type = self.subject.contact_type
# 
#     def confirm(self):
#         server = self.subject
#         owner = server.user
#         server.contact_verified = True
# 
#         # If this confirms a JID and the owners JID is the same, it's confirmed too
#         # Note: We also check if the JID is actually unconfirmed so we call save() only when the user is
#         # really changed.
#         if server.contact_type == Server.CONTACT_TYPE_JID and server.contact == owner.jid \
#                 and not owner.jid_confirmed:
#             owner.jid_confirmed = True
#             owner.save()
# 
#         # same but for email address
#         elif server.contact_type == Server.CONTACT_TYPE_EMAIL and server.contact == owner.email \
#                 and not owner.email_confirmed:
#             owner.email_confirmed = True
#             owner.save()
# 
#         self.subject.save()
# 
#     def __str__(self):
#         return self.subject.domain
# 
#     @property
#     def user(self):
#         return self.subject.user
# 
#     def get_absolute_url(self):
#         return reverse('confirm:server', kwargs={'key': self.key})
# 
# Path: confirm/models.py
# class UserConfirmationKey(ConfirmationKey, UserConfirmationMixin):
#     subject = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE,
#                                 related_name='confirmations')
# 
#     message_template = 'confirm/user_contact.txt'
#     message_subject = 'Confirm your %(addr_type)s on %(protocol)s://%(domain)s'
# 
#     def confirm(self):
#         if self.type == ConfirmationKey.TYPE_EMAIL:
#             self.subject.email_confirmed = True
# 
#             # confirm any servers that have the same contact data
#             self.subject.servers.email(self.subject.email).update(contact_verified=True)
#         elif self.type == ConfirmationKey.TYPE_JID:
#             self.subject.jid_confirmed = True
# 
#             # confirm any servers that have the same contact data
#             self.subject.servers.jid(self.subject.jid).update(contact_verified=True)
# 
#         self.subject.save()
# 
#     def get_absolute_url(self):
#         return reverse('confirm:user_contact', kwargs={'key': self.key})


from django.contrib.auth import login
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import Http404
from django.urls import reverse
from django.views.generic.base import RedirectView
from django.views.generic.detail import SingleObjectMixin
from confirm.models import ServerConfirmationKey
from confirm.models import UserConfirmationKey
        key = self.get_object(queryset=queryset.valid())

        if not self.request.user.is_authenticated:
            key.user.backend = 'django.contrib.auth.backends.ModelBackend'
            login(self.request, key.user)

        # log user in if not authenticated so far:
        if not self.request.user.is_authenticated:
            key.user.backend = 'django.contrib.auth.backends.ModelBackend'
            login(self.request, key.user)

        # take any confirmation action
        key.confirm()

        # invalidate old keys/unused keys:
        queryset.invalidate(subject=key.subject, typ=key.type)  # delete all old ones for this user
        self.get_queryset().invalidate_outdated()  # delete expired keys

        return self.get_success_url(key)

    def get_success_url(self, key):
        return reverse(self.url)


class UserConfirmationView(ConfirmationView):
    model = UserConfirmationKey
    url = 'account:index'


class ConfirmServerContactView(LoginRequiredMixin, ConfirmationView):

# Path: confirm/models.py
# class ServerConfirmationKey(ConfirmationKey):
#     subject = models.ForeignKey(Server, on_delete=models.CASCADE, related_name='confirmations')
#     objects = ServerConfirmationKeyQuerySet.as_manager()
# 
#     message_template = 'confirm/server_contact.txt'
#     message_subject = 'Confirm contact details for %(serverdomain)s on '
#     '%(protocol)s://%(domain)s'
# 
#     def add_context(self):
#         return {'serverdomain': self.subject.domain}
# 
#     @property
#     def recipient(self):
#         return self.subject.contact
# 
#     def __init__(self, *args, **kwargs):
#         super(ServerConfirmationKey, self).__init__(*args, **kwargs)
#         # TODO: WHAT?
#         if self.id:
#             self.type = self.subject.contact_type
# 
#     def confirm(self):
#         server = self.subject
#         owner = server.user
#         server.contact_verified = True
# 
#         # If this confirms a JID and the owners JID is the same, it's confirmed too
#         # Note: We also check if the JID is actually unconfirmed so we call save() only when the user is
#         # really changed.
#         if server.contact_type == Server.CONTACT_TYPE_JID and server.contact == owner.jid \
#                 and not owner.jid_confirmed:
#             owner.jid_confirmed = True
#             owner.save()
# 
#         # same but for email address
#         elif server.contact_type == Server.CONTACT_TYPE_EMAIL and server.contact == owner.email \
#                 and not owner.email_confirmed:
#             owner.email_confirmed = True
#             owner.save()
# 
#         self.subject.save()
# 
#     def __str__(self):
#         return self.subject.domain
# 
#     @property
#     def user(self):
#         return self.subject.user
# 
#     def get_absolute_url(self):
#         return reverse('confirm:server', kwargs={'key': self.key})
# 
# Path: confirm/models.py
# class UserConfirmationKey(ConfirmationKey, UserConfirmationMixin):
#     subject = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE,
#                                 related_name='confirmations')
# 
#     message_template = 'confirm/user_contact.txt'
#     message_subject = 'Confirm your %(addr_type)s on %(protocol)s://%(domain)s'
# 
#     def confirm(self):
#         if self.type == ConfirmationKey.TYPE_EMAIL:
#             self.subject.email_confirmed = True
# 
#             # confirm any servers that have the same contact data
#             self.subject.servers.email(self.subject.email).update(contact_verified=True)
#         elif self.type == ConfirmationKey.TYPE_JID:
#             self.subject.jid_confirmed = True
# 
#             # confirm any servers that have the same contact data
#             self.subject.servers.jid(self.subject.jid).update(contact_verified=True)
# 
#         self.subject.save()
# 
#     def get_absolute_url(self):
#         return reverse('confirm:user_contact', kwargs={'key': self.key})


# Path: confirm/views.py
from django.contrib.auth import login
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import Http404
from django.urls import reverse
from django.views.generic.base import RedirectView
from django.views.generic.detail import SingleObjectMixin
from confirm.models import ServerConfirmationKey
from confirm.models import UserConfirmationKey

        key = self.get_object(queryset=queryset.valid())

        if not self.request.user.is_authenticated:
            key.user.backend = 'django.contrib.auth.backends.ModelBackend'
            login(self.request, key.user)

        # log user in if not authenticated so far:
        if not self.request.user.is_authenticated:
            key.user.backend = 'django.contrib.auth.backends.ModelBackend'
            login(self.request, key.user)

        # take any confirmation action
        key.confirm()

        # invalidate old keys/unused keys:
        queryset.invalidate(subject=key.subject, typ=key.type)  # delete all old ones for this user
        self.get_queryset().invalidate_outdated()  # delete expired keys

        return self.get_success_url(key)

    def get_success_url(self, key):
        return reverse(self.url)


class UserConfirmationView(ConfirmationView):
    model = UserConfirmationKey
    url = 'account:index'


class ConfirmServerContactView(LoginRequiredMixin, ConfirmationView):

    model = ServerConfirmationKey