# 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 import admin
from .models import ServerConfirmationKey
from .models import UserConfirmationKey




class UserConfirmationKeyAdmin(admin.ModelAdmin):
    actions = ['resend']
    list_display = ['subject', 'created', 'type']
    list_filter = ['type']
    ordering = ['created']

    def resend(self, request, queryset):
        proto = 'https' if request.is_secure() else 'http'
        for obj in queryset:
            obj.send(proto, request.get_host())
    resend.short_description = 'Resend Confirmation'


class ServerConfirmationKeyAdmin(admin.ModelAdmin):
    actions = ['resend']
    list_display = ['subject', 'created', 'type']
    list_filter = ['type']
    ordering = ['created']

    def resend(self, request, queryset):
        proto = 'https' if request.is_secure() else 'http'
        for obj in queryset:
            obj.send(proto, request.get_host())
    resend.short_description = 'Resend Confirmation'

admin.site.register(UserConfirmationKey, UserConfirmationKeyAdmin)

# 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/admin.py
from django.contrib import admin
from .models import ServerConfirmationKey
from .models import UserConfirmationKey





class UserConfirmationKeyAdmin(admin.ModelAdmin):
    actions = ['resend']
    list_display = ['subject', 'created', 'type']
    list_filter = ['type']
    ordering = ['created']

    def resend(self, request, queryset):
        proto = 'https' if request.is_secure() else 'http'
        for obj in queryset:
            obj.send(proto, request.get_host())
    resend.short_description = 'Resend Confirmation'


class ServerConfirmationKeyAdmin(admin.ModelAdmin):
    actions = ['resend']
    list_display = ['subject', 'created', 'type']
    list_filter = ['type']
    ordering = ['created']

    def resend(self, request, queryset):
        proto = 'https' if request.is_secure() else 'http'
        for obj in queryset:
            obj.send(proto, request.get_host())
    resend.short_description = 'Resend Confirmation'

admin.site.register(UserConfirmationKey, UserConfirmationKeyAdmin)

admin.site.register(ServerConfirmationKey, ServerConfirmationKeyAdmin)