# Path: humans/models.py
# class User(AbstractUser):
#     """
#         Project's base user model
#     """
#     LANGUAGE_CHOICES = (
#         ('en-us', 'English'),
#         ('pt-pt', _('Portuguese')),
#     )
# 
#     organization = models.CharField(
#         null=True, blank=True, max_length=80, verbose_name=_('Organization'))
#     public_key = models.TextField(
#         blank=True, null=True, verbose_name=_('Public key'))
#     fingerprint = models.CharField(
#         null=True, blank=True, max_length=50, verbose_name=_('Fingerprint'))
#     keyserver_url = models.URLField(
#         null=True, blank=True, verbose_name=_('Key server URL'))
#     timezone = TimeZoneField(default='UTC', verbose_name=_('Timezone'))
#     language = models.CharField(
#         default="en-us", max_length=16, choices=LANGUAGE_CHOICES, verbose_name=_('Language'))
# 
#     def __init__(self, *args, **kwargs):
#         super().__init__(*args, **kwargs)
#         self.base_fingerprint = self.fingerprint
# 
#     def save(self, *args, **kwargs):
#         ip = kwargs.pop('ip', None)
#         agent = kwargs.pop('agent', '')
#         with transaction.atomic():
#             super().save(*args, **kwargs)
#             if self.base_fingerprint != self.fingerprint:
#                 self.keychanges.create(user=self,
#                                        prev_fingerprint=self.base_fingerprint,
#                                        to_fingerprint=self.fingerprint,
#                                        ip_address=ip,
#                                        agent=agent)
#                 self.base_fingerprint = self.fingerprint
# 
#     def has_setup_complete(self):
#         if self.public_key and self.fingerprint:
#             return True
#         return False
# 
#     @property
#     def has_github_login(self):
#         return self.socialaccount_set.filter(provider='github').count() >= 1
# 
#     @property
#     def has_public_key(self):
#         return True if self.public_key else False
# 
#     @property
#     def has_keyserver_url(self):
#         return True if self.keyserver_url else False
# 
# Path: humans/utils.py
# @with_gpg_obj
# def key_state(key, gpg):
#     INVALID = (None, "invalid", -1)
#     if not key:
#         return INVALID
#     results = gpg.import_keys(key).results
#     if not results:
#         return INVALID
#     # Key data is present in the last element of the list
#     key_fingerprint = results[-1].get("fingerprint")
#     if not key_fingerprint:
#         return INVALID
# 
#     # Since the keyring is exclusive for this import
#     # only the imported key exists in it.
#     key = gpg.list_keys()[0]
#     exp_timestamp = int(key["expires"]) if key["expires"] else 0
#     expires = datetime.fromtimestamp(exp_timestamp, timezone.utc)
#     to_expire = expires - timezone.now()
#     days_to_expire = to_expire.days
# 
#     if key["trust"] == "r":
#         state = "revoked"
#     elif exp_timestamp and expires < timezone.now():
#         state = "expired"
#     else:
#         state = "valid"
# 
#     return key_fingerprint, state, days_to_expire


from django.forms import ModelForm
from django import forms
from django.contrib.auth.password_validation import validate_password
from django.utils.translation import ugettext_lazy as _
from allauth.account.forms import LoginForm as BaseLoginForm
from allauth.account.forms import SignupForm as BaseSignupForm
from .models import User
from .utils import key_state
import requests
        current_password = self.cleaned_data.get('current_password')
        if len(current_password) > 0:
            if not self.instance.check_password(current_password):
                self.add_error('current_password',
                               _('Your current password was entered incorrectly.'))
        return current_password

    def clean_new_password2(self):
        """
        Validates that both new password entries are equal.
        """
        password1 = self.cleaned_data.get('new_password1')
        password2 = self.cleaned_data.get('new_password2')
        if password1 and password2:
            validate_password(password1, self.instance)
            if password1 != password2:
                self.add_error('new_password2',
                               _("The two password fields didn't match."))
            else:
                self.change_password = True
        return password2

    def clean_public_key(self):
        # Validate the public key
        if self.pub_key:
            pub_key = self.pub_key
        else:
            pub_key = self.cleaned_data.get("public_key", "")

        if pub_key:

# Path: humans/models.py
# class User(AbstractUser):
#     """
#         Project's base user model
#     """
#     LANGUAGE_CHOICES = (
#         ('en-us', 'English'),
#         ('pt-pt', _('Portuguese')),
#     )
# 
#     organization = models.CharField(
#         null=True, blank=True, max_length=80, verbose_name=_('Organization'))
#     public_key = models.TextField(
#         blank=True, null=True, verbose_name=_('Public key'))
#     fingerprint = models.CharField(
#         null=True, blank=True, max_length=50, verbose_name=_('Fingerprint'))
#     keyserver_url = models.URLField(
#         null=True, blank=True, verbose_name=_('Key server URL'))
#     timezone = TimeZoneField(default='UTC', verbose_name=_('Timezone'))
#     language = models.CharField(
#         default="en-us", max_length=16, choices=LANGUAGE_CHOICES, verbose_name=_('Language'))
# 
#     def __init__(self, *args, **kwargs):
#         super().__init__(*args, **kwargs)
#         self.base_fingerprint = self.fingerprint
# 
#     def save(self, *args, **kwargs):
#         ip = kwargs.pop('ip', None)
#         agent = kwargs.pop('agent', '')
#         with transaction.atomic():
#             super().save(*args, **kwargs)
#             if self.base_fingerprint != self.fingerprint:
#                 self.keychanges.create(user=self,
#                                        prev_fingerprint=self.base_fingerprint,
#                                        to_fingerprint=self.fingerprint,
#                                        ip_address=ip,
#                                        agent=agent)
#                 self.base_fingerprint = self.fingerprint
# 
#     def has_setup_complete(self):
#         if self.public_key and self.fingerprint:
#             return True
#         return False
# 
#     @property
#     def has_github_login(self):
#         return self.socialaccount_set.filter(provider='github').count() >= 1
# 
#     @property
#     def has_public_key(self):
#         return True if self.public_key else False
# 
#     @property
#     def has_keyserver_url(self):
#         return True if self.keyserver_url else False
# 
# Path: humans/utils.py
# @with_gpg_obj
# def key_state(key, gpg):
#     INVALID = (None, "invalid", -1)
#     if not key:
#         return INVALID
#     results = gpg.import_keys(key).results
#     if not results:
#         return INVALID
#     # Key data is present in the last element of the list
#     key_fingerprint = results[-1].get("fingerprint")
#     if not key_fingerprint:
#         return INVALID
# 
#     # Since the keyring is exclusive for this import
#     # only the imported key exists in it.
#     key = gpg.list_keys()[0]
#     exp_timestamp = int(key["expires"]) if key["expires"] else 0
#     expires = datetime.fromtimestamp(exp_timestamp, timezone.utc)
#     to_expire = expires - timezone.now()
#     days_to_expire = to_expire.days
# 
#     if key["trust"] == "r":
#         state = "revoked"
#     elif exp_timestamp and expires < timezone.now():
#         state = "expired"
#     else:
#         state = "valid"
# 
#     return key_fingerprint, state, days_to_expire


# Path: humans/forms.py
from django.forms import ModelForm
from django import forms
from django.contrib.auth.password_validation import validate_password
from django.utils.translation import ugettext_lazy as _
from allauth.account.forms import LoginForm as BaseLoginForm
from allauth.account.forms import SignupForm as BaseSignupForm
from .models import User
from .utils import key_state
import requests

        current_password = self.cleaned_data.get('current_password')
        if len(current_password) > 0:
            if not self.instance.check_password(current_password):
                self.add_error('current_password',
                               _('Your current password was entered incorrectly.'))
        return current_password

    def clean_new_password2(self):
        """
        Validates that both new password entries are equal.
        """
        password1 = self.cleaned_data.get('new_password1')
        password2 = self.cleaned_data.get('new_password2')
        if password1 and password2:
            validate_password(password1, self.instance)
            if password1 != password2:
                self.add_error('new_password2',
                               _("The two password fields didn't match."))
            else:
                self.change_password = True
        return password2

    def clean_public_key(self):
        # Validate the public key
        if self.pub_key:
            pub_key = self.pub_key
        else:
            pub_key = self.cleaned_data.get("public_key", "")

        if pub_key:

            fingerprint, *state = key_state(pub_key)