# 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
        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()
            new_user.token = token
            new_user.email = email
            new_user.save()
            return new_user, token


class SettingsForm(forms.Form):
    password = CharField(required=False, max_length=100)
    enable_emails = BooleanField(required=False)
    enable_pushbullets = BooleanField(required=False)
    pushbullet_api_key = CharField(required=False, max_length=64)

    def save(self, user):
        cleaned_data = super().clean()
        enable_emails = cleaned_data.get('enable_emails')
        enable_pushbullets = cleaned_data.get('enable_pushbullets')
        p_api_key = cleaned_data.get('pushbullet_api_key')
        password = cleaned_data.get('password')


# 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

        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()
            new_user.token = token
            new_user.email = email
            new_user.save()
            return new_user, token


class SettingsForm(forms.Form):
    password = CharField(required=False, max_length=100)
    enable_emails = BooleanField(required=False)
    enable_pushbullets = BooleanField(required=False)
    pushbullet_api_key = CharField(required=False, max_length=64)

    def save(self, user):
        cleaned_data = super().clean()
        enable_emails = cleaned_data.get('enable_emails')
        enable_pushbullets = cleaned_data.get('enable_pushbullets')
        p_api_key = cleaned_data.get('pushbullet_api_key')
        password = cleaned_data.get('password')


        if not len(p_api_key):