# 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
# (at your option) any later version.
#
# xmppllist is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with django-xmpp-server-list.  If not, see <http://www.gnu.org/licenses/>.




class ConfirmationView(RedirectView, SingleObjectMixin):
    # from SingleObjectMixin
    pk_url_kwarg = 'key'
    permanent = True

    def get_object(self, queryset=None):
        if queryset is None:
            queryset = self.get_queryset()
        try:
            return queryset.get(key=self.kwargs['key'])
        except self.model.DoesNotExist:
            raise Http404

    def get_redirect_url(self, *args, **kwargs):
        queryset = self.get_queryset().for_user(user=self.request.user)

        # get confirmation key:

# 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

# (at your option) any later version.
#
# xmppllist is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with django-xmpp-server-list.  If not, see <http://www.gnu.org/licenses/>.




class ConfirmationView(RedirectView, SingleObjectMixin):
    # from SingleObjectMixin
    pk_url_kwarg = 'key'
    permanent = True

    def get_object(self, queryset=None):
        if queryset is None:
            queryset = self.get_queryset()
        try:
            return queryset.get(key=self.kwargs['key'])
        except self.model.DoesNotExist:
            raise Http404

    def get_redirect_url(self, *args, **kwargs):
        queryset = self.get_queryset().for_user(user=self.request.user)

        # get confirmation key:

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