<commit_before># MIT License
# Copyright (c) 2017 MassChallenge, Inc.

import uuid

from django.db import models
from django.conf import settings
from django.contrib.auth.models import AbstractUser
from django.contrib.auth.models import BaseUserManager
from django.utils import timezone
from django.utils.encoding import python_2_unicode_compatible

from accelerator_abstract.models import BaseUserRole


MAX_USERNAME_LENGTH = 30


class UserManager(BaseUserManager):
    use_in_migrations = True

    def _create_user(self, email, password,
                     is_staff, is_superuser, **extra_fields):
        """
        Creates and saves an User with the given email and password.
        """
        now = timezone.now()
        if not email:
            raise ValueError('An email address must be provided.')
        email = self.normalize_email(email)
        if "is_active" not in extra_fields:
            extra_fields["is_active"] = True
        if "username" not in extra_fields:
            # For now we need to have a unique id that is at
            # most 30 characters long.  Using uuid and truncating.
            # Ideally username goes away entirely at some point
            # since we're really using email.  If we have to keep
            # username for some reason then we could switch over
            # to a string version of the pk which is guaranteed
            # be unique.
            extra_fields["username"] = str(uuid.uuid4())[:MAX_USERNAME_LENGTH]
        user = self.model(email=email,
                          is_staff=is_staff, is_superuser=is_superuser,
                          last_login=now, date_joined=now, **extra_fields)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_user(self, email=None, password=None, **extra_fields):
        return self._create_user(email, password, False, False,
                                 **extra_fields)

    def create_superuser(self, email, password, **extra_fields):
        return self._create_user(email, password, True, True,
                                 **extra_fields)


@python_2_unicode_compatible
class User(AbstractUser):
    # Override the parent email field to add uniqueness constraint
    email = models.EmailField(blank=True, unique=True)

    objects = UserManager()

    class Meta:
        db_table = 'auth_user'
        managed = settings.ACCELERATOR_MODELS_ARE_MANAGED

    def __init__(self, *args, **kwargs):
        super(User, self).__init__(*args, **kwargs)
        self.startup = None
        self.team_member = None
        self.profile = None
        self.user_finalist_roles = None

    class AuthenticationException(Exception):
        pass

    def __str__(self):
        return self.email

    def full_name(self):
        fn = self.first_name
        ln = self.last_name
        if fn and ln:
            name = u"%s %s" % (fn, ln)
        else:
            name = str(self.email)
        return name

    def user_phone(self):
        return self._get_profile().phone

    def image_url(self):
        return self._get_profile().image_url()

    def team_member_id(self):
        return self.team_member.id if self._get_member() else ''

    def user_title(self):
        return self.team_member.title if self._get_member() else ''

    def user_twitter_handle(self):
        return self._get_profile().twitter_handle

    def user_linked_in_url(self):
        return self._get_profile().linked_in_url

    def user_facebook_url(self):
        return self._get_profile().facebook_url

    def user_personal_website_url(self):
        return self._get_profile().personal_website_url

    def type(self):
        return self._get_profile().user_type

    def startup_name(self):
        return self.startup.name if self._get_startup() else None

    def startup_industry(self):
        return self.startup.primary_industry if self._get_startup() else None

    def top_level_startup_industry(self):
        industry = (
            self.startup.primary_industry if self._get_startup() else None)
        return industry.parent if industry and industry.parent else industry

    def startup_status_names(self):
        if self._get_startup():
            return [startup_status.program_startup_status.startup_status
                    for startup_status in self.startup.startupstatus_set.all()]

    def finalist_user_roles(self):
        if not self.user_finalist_roles:
            finalist_roles = BaseUserRole.FINALIST_USER_ROLES
            roles = self.programrolegrant_set.filter(
                program_role__user_role__name__in=finalist_roles
            ).values_list('program_role__name', flat=True).distinct()
            self.user_finalist_roles = [role for role in roles]
        return self.user_finalist_roles

    def program(self):
        return self.startup.current_program() if self._get_startup() else None

    def location(self):<commit_msg>stop list comperhension on list<commit_after># MIT License
# Copyright (c) 2017 MassChallenge, Inc.

import uuid

from django.db import models
from django.conf import settings
from django.contrib.auth.models import AbstractUser
from django.contrib.auth.models import BaseUserManager
from django.utils import timezone
from django.utils.encoding import python_2_unicode_compatible

from accelerator_abstract.models import BaseUserRole


MAX_USERNAME_LENGTH = 30


class UserManager(BaseUserManager):
    use_in_migrations = True

    def _create_user(self, email, password,
                     is_staff, is_superuser, **extra_fields):
        """
        Creates and saves an User with the given email and password.
        """
        now = timezone.now()
        if not email:
            raise ValueError('An email address must be provided.')
        email = self.normalize_email(email)
        if "is_active" not in extra_fields:
            extra_fields["is_active"] = True
        if "username" not in extra_fields:
            # For now we need to have a unique id that is at
            # most 30 characters long.  Using uuid and truncating.
            # Ideally username goes away entirely at some point
            # since we're really using email.  If we have to keep
            # username for some reason then we could switch over
            # to a string version of the pk which is guaranteed
            # be unique.
            extra_fields["username"] = str(uuid.uuid4())[:MAX_USERNAME_LENGTH]
        user = self.model(email=email,
                          is_staff=is_staff, is_superuser=is_superuser,
                          last_login=now, date_joined=now, **extra_fields)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_user(self, email=None, password=<PASSWORD>, **extra_fields):
        return self._create_user(email, password, False, False,
                                 **extra_fields)

    def create_superuser(self, email, password, **extra_fields):
        return self._create_user(email, password, True, True,
                                 **extra_fields)


@python_2_unicode_compatible
class User(AbstractUser):
    # Override the parent email field to add uniqueness constraint
    email = models.EmailField(blank=True, unique=True)

    objects = UserManager()

    class Meta:
        db_table = 'auth_user'
        managed = settings.ACCELERATOR_MODELS_ARE_MANAGED

    def __init__(self, *args, **kwargs):
        super(User, self).__init__(*args, **kwargs)
        self.startup = None
        self.team_member = None
        self.profile = None
        self.user_finalist_roles = None

    class AuthenticationException(Exception):
        pass

    def __str__(self):
        return self.email

    def full_name(self):
        fn = self.first_name
        ln = self.last_name
        if fn and ln:
            name = u"%s %s" % (fn, ln)
        else:
            name = str(self.email)
        return name

    def user_phone(self):
        return self._get_profile().phone

    def image_url(self):
        return self._get_profile().image_url()

    def team_member_id(self):
        return self.team_member.id if self._get_member() else ''

    def user_title(self):
        return self.team_member.title if self._get_member() else ''

    def user_twitter_handle(self):
        return self._get_profile().twitter_handle

    def user_linked_in_url(self):
        return self._get_profile().linked_in_url

    def user_facebook_url(self):
        return self._get_profile().facebook_url

    def user_personal_website_url(self):
        return self._get_profile().personal_website_url

    def type(self):
        return self._get_profile().user_type

    def startup_name(self):
        return self.startup.name if self._get_startup() else None

    def startup_industry(self):
        return self.startup.primary_industry if self._get_startup() else None

    def top_level_startup_industry(self):
        industry = (
            self.startup.primary_industry if self._get_startup() else None)
        return industry.parent if industry and industry.parent else industry

    def startup_status_names(self):
        if self._get_startup():
            return [startup_status.program_startup_status.startup_status
                    for startup_status in self.startup.startupstatus_set.all()]

    def finalist_user_roles(self):
        if not self.user_finalist_roles:
            finalist_roles = BaseUserRole.FINALIST_USER_ROLES
            self.user_finalist_roles = self.programrolegrant_set.filter(
                program_role__user_role__name__in=finalist_roles
            ).values_list('program_role__name', flat=True).distinct()
        return list(self.user_finalist_roles)

    def program(self):
        return self.startup.current_program() if self._get_startup() else None

    def location(self):
{'fraction_non_alphanumeric': 0.07832607153713649, 'fraction_numerical': 0.0022292025534501976, 'mean_word_length': 2.5838779956427014, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1030423', 'n_tokens_mistral': 2826, 'n_tokens_neox': 2740, 'n_words': 719}