<commit_before>#  This file is part of Stencila Hub.
#  
#  Copyright (C) 2015-2016 Stencila Ltd.
#  
#  Stencila Hub is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Affero General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#  
#  Stencila Hub is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Affero General Public License for more details.
#  
#  You should have received a copy of the GNU Affero General Public License
#  along with Stencila Hub.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import unicode_literals

from collections import OrderedDict

import time
import datetime
import pytz
import base64

import Crypto.Cipher
import Crypto.Random

from django.db import models
from django.contrib.auth.models import User, AnonymousUser
from django.conf import settings
from django.core.exceptions import PermissionDenied
from django.http import Http404
from django.core.mail import mail_admins
from django.contrib.auth import authenticate, login

import jsonfield

from general.errors import Error

# User "any" is used for creating keys that apply to any user
# Always loaded so available in various authorisation functions
try:
    user_any = User.objects.get(username='any')
except User.DoesNotExist:
    user_any = None


class UnauthenticatedError(Error):
    '''
    Used when user needs to be authenticated
    '''

    code = 401

    def serialize(self):
        return dict(
            error='user:unauthenticated',
            message='User is not authenticated'
        )


class UserDetails(models.Model):
    '''
    Additional details for a user not available on contrib.auth.models.User.

    Following this https://docs.djangoproject.com/en/1.9/topics/auth/customizing/#specifying-a-custom-user-model
    advice not deriving from a AbstractBaseUser but instead using<commit_msg>Add another exception type so does not fail in initial migrations<commit_after>#  This file is part of Stencila Hub.
#  
#  Copyright (C) 2015-2016 Stencila Ltd.
#  
#  Stencila Hub is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Affero General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#  
#  Stencila Hub is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Affero General Public License for more details.
#  
#  You should have received a copy of the GNU Affero General Public License
#  along with Stencila Hub.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import unicode_literals

from collections import OrderedDict

import time
import datetime
import pytz
import base64

import Crypto.Cipher
import Crypto.Random

import django
from django.db import models
from django.contrib.auth.models import User, AnonymousUser
from django.conf import settings
from django.core.exceptions import PermissionDenied
from django.http import Http404
from django.core.mail import mail_admins
from django.contrib.auth import authenticate, login

import jsonfield

from general.errors import Error

# User "any" is used for creating keys that apply to any user
# Always loaded so available in various authorisation functions
try:
    user_any = User.objects.get(username='any')
except (django.db.utils.OperationalError, User.DoesNotExist):
    user_any = None


class UnauthenticatedError(Error):
    '''
    Used when user needs to be authenticated
    '''

    code = 401

    def serialize(self):
        return dict(
            error='user:unauthenticated',
            message='User is not authenticated'
        )


class UserDetails(models.Model):
    '''
    Additional details for a user not available on contrib.auth.models.User.

    Following this https://docs.djangoproject.com/en/1.9/topics/auth/customizing/#specifying-a-custom-user-model
    advice not deriving from a AbstractBaseUser but instead using
{'fraction_non_alphanumeric': 0.0624126688402422, 'fraction_numerical': 0.008849557522123894, 'mean_word_length': 3.8096304591265397, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5596120', 'n_tokens_mistral': 1215, 'n_tokens_neox': 1153, 'n_words': 494}