# Path: boardinghouse/schema.py
# def get_active_schema():
#     """
#     Get the (internal) name of the currently active schema.
#     """
#     return _get_schema(get_active_schema_name())
# 
# Path: boardinghouse/contrib/invite/models.py
# class Invitation(SharedSchemaModel):
#     email = models.EmailField(verbose_name=_('Email address'))
#     sender = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='sent_invitations')
#     message = models.TextField()
#     schema = models.ForeignKey(settings.BOARDINGHOUSE_SCHEMA_MODEL, related_name='invitations')
#     redemption_code = models.UUIDField(null=True, blank=True)
# 
#     created_at = models.DateTimeField(auto_now_add=True)
#     # Can we ensure that at most one of these two is not null?
#     accepted_at = models.DateTimeField(null=True, blank=True)
#     declined_at = models.DateTimeField(null=True, blank=True)
#     accepted_by = models.ForeignKey(settings.AUTH_USER_MODEL,
#                                     related_name='accepted_invitations',
#                                     null=True, blank=True)
# 
#     objects = InvitationQuerySet.as_manager()
# 
#     class Meta:
#         ordering = ('created_at',)
#         app_label = 'invite'
# 
#     def __str__(self):
#         return u'[{0!s}] Invitation to {1!s} from {2!s} to join {3!s}'.format(
#             six.text_type(self.status), self.email, self.sender, self.schema.name
#         )
# 
#     def save(self, *args, **kwargs):
#         if not self.redeemed and not self.redemption_code:
#             self.redemption_code = uuid.uuid4()
#         return super(Invitation, self).save(*args, **kwargs)
# 
#     @property
#     def redeemed(self):
#         return self.accepted_at or self.declined_at
# 
#     @property
#     def declined(self):
#         return self.declined_at is not None
# 
#     @property
#     def accepted(self):
#         return self.accepted_at is not None
# 
#     @property
#     def expired(self):
#         return self.created_at < now() - INVITATION_EXPIRY
# 
#     @property
#     def redeemable(self):
#         return not (self.expired or self.redeemed)
# 
#     @property
#     def status(self):
#         if self.declined:
#             return _('DECLINED')
#         if self.accepted:
#             return _('ACCEPTED')
#         if self.expired:
#             return _('EXPIRED')
#         return _('PENDING')
# 
#     @property
#     def expires_at(self):
#         return self.created_at + INVITATION_EXPIRY


import datetime
import uuid
from django import forms
from django.utils.translation import ugettext_lazy as _
from boardinghouse.schema import get_active_schema
from .models import Invitation
    This will only allow you to invite someone to the current schema.

    It will automatically generate a redemption code, that will be a
    part of the url the user needs to click on in order to accept or
    deny the invitation.

    The message will be emailed.
    """
    class Meta:
        model = Invitation
        fields = ('email', 'message',)

    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('user')
        super(InvitePersonForm, self).__init__(*args, **kwargs)

    def save(self, *args, **kwargs):
        self.instance.schema = get_active_schema()
        self.instance.redemption_code = uuid.uuid4()
        self.instance.sender = self.user
        # TODO: email the user.
        return super(InvitePersonForm, self).save(*args, **kwargs)


class AcceptForm(forms.ModelForm):
    """
    A form that can be used to accept an invitation to a schema.
    """

    class Meta:

# Path: boardinghouse/schema.py
# def get_active_schema():
#     """
#     Get the (internal) name of the currently active schema.
#     """
#     return _get_schema(get_active_schema_name())
# 
# Path: boardinghouse/contrib/invite/models.py
# class Invitation(SharedSchemaModel):
#     email = models.EmailField(verbose_name=_('Email address'))
#     sender = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='sent_invitations')
#     message = models.TextField()
#     schema = models.ForeignKey(settings.BOARDINGHOUSE_SCHEMA_MODEL, related_name='invitations')
#     redemption_code = models.UUIDField(null=True, blank=True)
# 
#     created_at = models.DateTimeField(auto_now_add=True)
#     # Can we ensure that at most one of these two is not null?
#     accepted_at = models.DateTimeField(null=True, blank=True)
#     declined_at = models.DateTimeField(null=True, blank=True)
#     accepted_by = models.ForeignKey(settings.AUTH_USER_MODEL,
#                                     related_name='accepted_invitations',
#                                     null=True, blank=True)
# 
#     objects = InvitationQuerySet.as_manager()
# 
#     class Meta:
#         ordering = ('created_at',)
#         app_label = 'invite'
# 
#     def __str__(self):
#         return u'[{0!s}] Invitation to {1!s} from {2!s} to join {3!s}'.format(
#             six.text_type(self.status), self.email, self.sender, self.schema.name
#         )
# 
#     def save(self, *args, **kwargs):
#         if not self.redeemed and not self.redemption_code:
#             self.redemption_code = uuid.uuid4()
#         return super(Invitation, self).save(*args, **kwargs)
# 
#     @property
#     def redeemed(self):
#         return self.accepted_at or self.declined_at
# 
#     @property
#     def declined(self):
#         return self.declined_at is not None
# 
#     @property
#     def accepted(self):
#         return self.accepted_at is not None
# 
#     @property
#     def expired(self):
#         return self.created_at < now() - INVITATION_EXPIRY
# 
#     @property
#     def redeemable(self):
#         return not (self.expired or self.redeemed)
# 
#     @property
#     def status(self):
#         if self.declined:
#             return _('DECLINED')
#         if self.accepted:
#             return _('ACCEPTED')
#         if self.expired:
#             return _('EXPIRED')
#         return _('PENDING')
# 
#     @property
#     def expires_at(self):
#         return self.created_at + INVITATION_EXPIRY


# Path: boardinghouse/contrib/invite/forms.py
import datetime
import uuid
from django import forms
from django.utils.translation import ugettext_lazy as _
from boardinghouse.schema import get_active_schema
from .models import Invitation

    This will only allow you to invite someone to the current schema.

    It will automatically generate a redemption code, that will be a
    part of the url the user needs to click on in order to accept or
    deny the invitation.

    The message will be emailed.
    """
    class Meta:
        model = Invitation
        fields = ('email', 'message',)

    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('user')
        super(InvitePersonForm, self).__init__(*args, **kwargs)

    def save(self, *args, **kwargs):
        self.instance.schema = get_active_schema()
        self.instance.redemption_code = uuid.uuid4()
        self.instance.sender = self.user
        # TODO: email the user.
        return super(InvitePersonForm, self).save(*args, **kwargs)


class AcceptForm(forms.ModelForm):
    """
    A form that can be used to accept an invitation to a schema.
    """

    class Meta:

        model = Invitation