# Path: ngnotifier/models.py
# class User(AbstractBaseUser):
#     objects = UserManager()
# 
#     USERNAME_FIELD = 'email'
#     REQUIRED_FIELDS = []
# 
#     email = models.EmailField('email address', max_length=254, unique=True)
#     token = models.TextField()
#     pushbullet_api_key = models.TextField(null=True)
#     send_emails = models.BooleanField(default=True)
#     send_pushbullets = models.BooleanField(default=False)
#     date_joined = models.DateTimeField(default=datetime.now)
#     is_active = models.BooleanField(default=False)
#     is_admin = models.BooleanField(default=False)
#     signature = models.TextField(default='')
#     anonymous = models.BooleanField(default=False)
# 
#     def save(self, *args, **kwargs):
#         if not self.pk:
#             super().save(*args, **kwargs)
#             new_user_log = Log()
#             new_user_log.type = 'NU'
#             new_user_log.user = self
#             new_user_log.save()
#         else:
#             super().save(*args, **kwargs)
# 
#     def get_groups_followed(self):
#         return [d['followers_set'] for d in
#                 User.objects.values('followers_set').filter(id=self.id)]
# 
#     def switch_emails(self):
#         self.send_emails = not self.send_emails
#         self.save()
# 
#     def switch_pushbullets(self):
#         if self.pushbullet_api_key:
#             self.send_pushbullets = not self.send_pushbullets
#             self.save()
# 
#     def switch_device(self, registration_id):
#         devices = self.get_devices()
#         for device in devices:
#             if device.registration_id == registration_id:
#                 device.switch_active()
#                 break
# 
#     def add_ng_group(self, ng_group):
#         ng_group.followers.add(self)
#         ng_group.save()
# 
#     def del_ng_group(self, ng_group):
#         ng_group.followers.remove(self)
#         ng_group.save()
# 
#     def create_session(self, service, registration_id, device_name):
#         try:
#             if service == 'android':
#                 DeviceSession.objects.get(gcm_device__registration_id=registration_id).delete()
#             else:
#                 DeviceSession.objects.get(apns_device__registration_id=registration_id).delete()
#         except:
#             pass
#         session = DeviceSession()
#         session.service = 'AN' if service == 'android' else 'IO'
#         if service == 'android':
#             gcm_device = GCMDevice(registration_id=registration_id, user=self, name=device_name)
#             gcm_device.save()
#             session.gcm_device = gcm_device
#         else:
#             apns_device = APNSDevice(registration_id=registration_id, user=self, name=device_name)
#             apns_device.save()
#             session.apns_device = apns_device
#         session.save()
#         return session
# 
#     def get_devices(self):
#         devices = list(DeviceSession.objects.filter(gcm_device__user_id=self.id))
#         devices += list(DeviceSession.objects.filter(apns_device__user_id=self.id))
#         return devices


from _sha256 import sha224
from uuid import uuid4
from django.core.exceptions import ObjectDoesNotExist
from django.forms import EmailField, CharField, BooleanField
from django import forms
from captcha.fields import CaptchaField
from ngnotifier.models import User




class CaptchaForm(forms.Form):
    email = EmailField()
    captcha = CaptchaField()

    def save(self):
        cleaned_data = super().clean()
        email = cleaned_data.get('email')

        token = sha224(uuid4().hex.encode('utf-8')).hexdigest()
        try:
            # If the user already exists, we just update its token
            user = User.objects.get(
                email=email
            )
            user.token = token
            user.save()
            return user, token
        except ObjectDoesNotExist:

# Path: ngnotifier/models.py
# class User(AbstractBaseUser):
#     objects = UserManager()
# 
#     USERNAME_FIELD = 'email'
#     REQUIRED_FIELDS = []
# 
#     email = models.EmailField('email address', max_length=254, unique=True)
#     token = models.TextField()
#     pushbullet_api_key = models.TextField(null=True)
#     send_emails = models.BooleanField(default=True)
#     send_pushbullets = models.BooleanField(default=False)
#     date_joined = models.DateTimeField(default=datetime.now)
#     is_active = models.BooleanField(default=False)
#     is_admin = models.BooleanField(default=False)
#     signature = models.TextField(default='')
#     anonymous = models.BooleanField(default=False)
# 
#     def save(self, *args, **kwargs):
#         if not self.pk:
#             super().save(*args, **kwargs)
#             new_user_log = Log()
#             new_user_log.type = 'NU'
#             new_user_log.user = self
#             new_user_log.save()
#         else:
#             super().save(*args, **kwargs)
# 
#     def get_groups_followed(self):
#         return [d['followers_set'] for d in
#                 User.objects.values('followers_set').filter(id=self.id)]
# 
#     def switch_emails(self):
#         self.send_emails = not self.send_emails
#         self.save()
# 
#     def switch_pushbullets(self):
#         if self.pushbullet_api_key:
#             self.send_pushbullets = not self.send_pushbullets
#             self.save()
# 
#     def switch_device(self, registration_id):
#         devices = self.get_devices()
#         for device in devices:
#             if device.registration_id == registration_id:
#                 device.switch_active()
#                 break
# 
#     def add_ng_group(self, ng_group):
#         ng_group.followers.add(self)
#         ng_group.save()
# 
#     def del_ng_group(self, ng_group):
#         ng_group.followers.remove(self)
#         ng_group.save()
# 
#     def create_session(self, service, registration_id, device_name):
#         try:
#             if service == 'android':
#                 DeviceSession.objects.get(gcm_device__registration_id=registration_id).delete()
#             else:
#                 DeviceSession.objects.get(apns_device__registration_id=registration_id).delete()
#         except:
#             pass
#         session = DeviceSession()
#         session.service = 'AN' if service == 'android' else 'IO'
#         if service == 'android':
#             gcm_device = GCMDevice(registration_id=registration_id, user=self, name=device_name)
#             gcm_device.save()
#             session.gcm_device = gcm_device
#         else:
#             apns_device = APNSDevice(registration_id=registration_id, user=self, name=device_name)
#             apns_device.save()
#             session.apns_device = apns_device
#         session.save()
#         return session
# 
#     def get_devices(self):
#         devices = list(DeviceSession.objects.filter(gcm_device__user_id=self.id))
#         devices += list(DeviceSession.objects.filter(apns_device__user_id=self.id))
#         return devices


# Path: ngnotifier/forms.py
from _sha256 import sha224
from uuid import uuid4
from django.core.exceptions import ObjectDoesNotExist
from django.forms import EmailField, CharField, BooleanField
from django import forms
from captcha.fields import CaptchaField
from ngnotifier.models import User





class CaptchaForm(forms.Form):
    email = EmailField()
    captcha = CaptchaField()

    def save(self):
        cleaned_data = super().clean()
        email = cleaned_data.get('email')

        token = sha224(uuid4().hex.encode('utf-8')).hexdigest()
        try:
            # If the user already exists, we just update its token
            user = User.objects.get(
                email=email
            )
            user.token = token
            user.save()
            return user, token
        except ObjectDoesNotExist:

            new_user = User()