# Path: account/models.py
# class SignupCode(models.Model):
# 
#     class AlreadyExists(Exception):
#         pass
# 
#     class InvalidCode(Exception):
#         pass
# 
#     code = models.CharField(_("code"), max_length=64, unique=True)
#     max_uses = models.PositiveIntegerField(_("max uses"), default=0)
#     expiry = models.DateTimeField(_("expiry"), null=True, blank=True)
#     inviter = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.CASCADE)
#     email = models.EmailField(max_length=254, blank=True)
#     notes = models.TextField(_("notes"), blank=True)
#     sent = models.DateTimeField(_("sent"), null=True, blank=True)
#     created = models.DateTimeField(_("created"), default=timezone.now, editable=False)
#     use_count = models.PositiveIntegerField(_("use count"), editable=False, default=0)
# 
#     class Meta:
#         verbose_name = _("signup code")
#         verbose_name_plural = _("signup codes")
# 
#     def __str__(self):
#         if self.email:
#             return "{0} [{1}]".format(self.email, self.code)
#         else:
#             return self.code
# 
#     @classmethod
#     def exists(cls, code=None, email=None):
#         checks = []
#         if code:
#             checks.append(Q(code=code))
#         if email:
#             checks.append(Q(email=email))
#         if not checks:
#             return False
#         return cls._default_manager.filter(functools.reduce(operator.or_, checks)).exists()
# 
#     @classmethod
#     def create(cls, **kwargs):
#         email, code = kwargs.get("email"), kwargs.get("code")
#         if kwargs.get("check_exists", True) and cls.exists(code=code, email=email):
#             raise cls.AlreadyExists()
#         expiry = timezone.now() + datetime.timedelta(hours=kwargs.get("expiry", 24))
#         if not code:
#             code = hookset.generate_signup_code_token(email)
#         params = {
#             "code": code,
#             "max_uses": kwargs.get("max_uses", 0),
#             "expiry": expiry,
#             "inviter": kwargs.get("inviter"),
#             "notes": kwargs.get("notes", "")
#         }
#         if email:
#             params["email"] = email
#         return cls(**params)
# 
#     @classmethod
#     def check_code(cls, code):
#         try:
#             signup_code = cls._default_manager.get(code=code)
#         except cls.DoesNotExist:
#             raise cls.InvalidCode()
#         else:
#             if signup_code.max_uses and signup_code.max_uses <= signup_code.use_count:
#                 raise cls.InvalidCode()
#             else:
#                 if signup_code.expiry and timezone.now() > signup_code.expiry:
#                     raise cls.InvalidCode()
#                 else:
#                     return signup_code
# 
#     def calculate_use_count(self):
#         self.use_count = self.signupcoderesult_set.count()
#         self.save()
# 
#     def use(self, user):
#         """
#         Add a SignupCode result attached to the given user.
#         """
#         result = SignupCodeResult()
#         result.signup_code = self
#         result.user = user
#         result.save()
#         signup_code_used.send(sender=result.__class__, signup_code_result=result)
# 
#     def send(self, **kwargs):
#         protocol = settings.ACCOUNT_DEFAULT_HTTP_PROTOCOL
#         current_site = kwargs["site"] if "site" in kwargs else Site.objects.get_current()
#         if "signup_url" not in kwargs:
#             signup_url = "{0}://{1}{2}?{3}".format(
#                 protocol,
#                 current_site.domain,
#                 reverse("account_signup"),
#                 urlencode({"code": self.code})
#             )
#         else:
#             signup_url = kwargs["signup_url"]
#         ctx = {
#             "signup_code": self,
#             "current_site": current_site,
#             "signup_url": signup_url,
#         }
#         ctx.update(kwargs.get("extra_ctx", {}))
#         hookset.send_invitation_email([self.email], ctx)
#         self.sent = timezone.now()
#         self.save()
#         signup_code_sent.send(sender=SignupCode, signup_code=self)


from django.test import TestCase
from account.models import SignupCode



class SignupCodeModelTestCase(TestCase):
    def test_exists_no_match(self):
        code = SignupCode(email="foobar@example.com", code="FOOFOO")
        code.save()

        self.assertFalse(SignupCode.exists(code="BARBAR"))
        self.assertFalse(SignupCode.exists(email="bar@example.com"))
        self.assertFalse(SignupCode.exists(email="bar@example.com", code="BARBAR"))
        self.assertFalse(SignupCode.exists())

    def test_exists_email_only_match(self):
        code = SignupCode(email="foobar@example.com", code="FOOFOO")
        code.save()

        self.assertTrue(SignupCode.exists(email="foobar@example.com"))

    def test_exists_code_only_match(self):
        code = SignupCode(email="foobar@example.com", code="FOOFOO")
        code.save()


# Path: account/models.py
# class SignupCode(models.Model):
# 
#     class AlreadyExists(Exception):
#         pass
# 
#     class InvalidCode(Exception):
#         pass
# 
#     code = models.CharField(_("code"), max_length=64, unique=True)
#     max_uses = models.PositiveIntegerField(_("max uses"), default=0)
#     expiry = models.DateTimeField(_("expiry"), null=True, blank=True)
#     inviter = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.CASCADE)
#     email = models.EmailField(max_length=254, blank=True)
#     notes = models.TextField(_("notes"), blank=True)
#     sent = models.DateTimeField(_("sent"), null=True, blank=True)
#     created = models.DateTimeField(_("created"), default=timezone.now, editable=False)
#     use_count = models.PositiveIntegerField(_("use count"), editable=False, default=0)
# 
#     class Meta:
#         verbose_name = _("signup code")
#         verbose_name_plural = _("signup codes")
# 
#     def __str__(self):
#         if self.email:
#             return "{0} [{1}]".format(self.email, self.code)
#         else:
#             return self.code
# 
#     @classmethod
#     def exists(cls, code=None, email=None):
#         checks = []
#         if code:
#             checks.append(Q(code=code))
#         if email:
#             checks.append(Q(email=email))
#         if not checks:
#             return False
#         return cls._default_manager.filter(functools.reduce(operator.or_, checks)).exists()
# 
#     @classmethod
#     def create(cls, **kwargs):
#         email, code = kwargs.get("email"), kwargs.get("code")
#         if kwargs.get("check_exists", True) and cls.exists(code=code, email=email):
#             raise cls.AlreadyExists()
#         expiry = timezone.now() + datetime.timedelta(hours=kwargs.get("expiry", 24))
#         if not code:
#             code = hookset.generate_signup_code_token(email)
#         params = {
#             "code": code,
#             "max_uses": kwargs.get("max_uses", 0),
#             "expiry": expiry,
#             "inviter": kwargs.get("inviter"),
#             "notes": kwargs.get("notes", "")
#         }
#         if email:
#             params["email"] = email
#         return cls(**params)
# 
#     @classmethod
#     def check_code(cls, code):
#         try:
#             signup_code = cls._default_manager.get(code=code)
#         except cls.DoesNotExist:
#             raise cls.InvalidCode()
#         else:
#             if signup_code.max_uses and signup_code.max_uses <= signup_code.use_count:
#                 raise cls.InvalidCode()
#             else:
#                 if signup_code.expiry and timezone.now() > signup_code.expiry:
#                     raise cls.InvalidCode()
#                 else:
#                     return signup_code
# 
#     def calculate_use_count(self):
#         self.use_count = self.signupcoderesult_set.count()
#         self.save()
# 
#     def use(self, user):
#         """
#         Add a SignupCode result attached to the given user.
#         """
#         result = SignupCodeResult()
#         result.signup_code = self
#         result.user = user
#         result.save()
#         signup_code_used.send(sender=result.__class__, signup_code_result=result)
# 
#     def send(self, **kwargs):
#         protocol = settings.ACCOUNT_DEFAULT_HTTP_PROTOCOL
#         current_site = kwargs["site"] if "site" in kwargs else Site.objects.get_current()
#         if "signup_url" not in kwargs:
#             signup_url = "{0}://{1}{2}?{3}".format(
#                 protocol,
#                 current_site.domain,
#                 reverse("account_signup"),
#                 urlencode({"code": self.code})
#             )
#         else:
#             signup_url = kwargs["signup_url"]
#         ctx = {
#             "signup_code": self,
#             "current_site": current_site,
#             "signup_url": signup_url,
#         }
#         ctx.update(kwargs.get("extra_ctx", {}))
#         hookset.send_invitation_email([self.email], ctx)
#         self.sent = timezone.now()
#         self.save()
#         signup_code_sent.send(sender=SignupCode, signup_code=self)


# Path: account/tests/test_models.py
from django.test import TestCase
from account.models import SignupCode




class SignupCodeModelTestCase(TestCase):
    def test_exists_no_match(self):
        code = SignupCode(email="foobar@example.com", code="FOOFOO")
        code.save()

        self.assertFalse(SignupCode.exists(code="BARBAR"))
        self.assertFalse(SignupCode.exists(email="bar@example.com"))
        self.assertFalse(SignupCode.exists(email="bar@example.com", code="BARBAR"))
        self.assertFalse(SignupCode.exists())

    def test_exists_email_only_match(self):
        code = SignupCode(email="foobar@example.com", code="FOOFOO")
        code.save()

        self.assertTrue(SignupCode.exists(email="foobar@example.com"))

    def test_exists_code_only_match(self):
        code = SignupCode(email="foobar@example.com", code="FOOFOO")
        code.save()


        self.assertTrue(SignupCode.exists(code="FOOFOO"))