<commit_before>import collections
import itertools
import os
import re
from os.path import join

from django.conf import settings
from django.contrib.auth.models import AbstractBaseUser, UserManager
from django.db import models
from django.shortcuts import get_object_or_404
from django.utils import timezone

from catalog.models import Category, Course


class CustomUserManager(UserManager):
    PATTERN = re.compile(r'[\W_]+')

    def _create_user(self, netid, email, password, **extra_fields):
        """
        Creates and saves a User with the given username, email and password.
        """
        now = timezone.now()
        if not netid:
            raise ValueError('The given netid must be set')
        email = self.normalize_email(email)
        user = self.model(netid=netid, email=email, last_login=now, **extra_fields)

        user.set_password(password)
        user.save(using=self._db)
        return user

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

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


class User(AbstractBaseUser):

    USERNAME_FIELD = 'netid'
    REQUIRED_FIELDS = ['email', 'first_name', 'last_name']
    DEFAULT_PHOTO = join(settings.STATIC_URL, "images/default.jpg")
    objects = CustomUserManager()

    netid = models.CharField(max_length=20, unique=True)
    created = models.DateTimeField(auto_now_add=True)
    edited = models.DateTimeField(auto_now=True)
    first_name = models.CharField(max_length=127)
    last_name = models.CharField(max_length=127)
    email = models.CharField(max_length=255, unique=True)
    registration = models.CharField(max_length=80, blank=True)
    welcome = models.BooleanField(default=True)
    comment = models.TextField(blank=True, default='')

    register_method = models.CharField(max_length=32)
    last_login_method = models.CharField(max_length=32)

    inferred_faculty = models.TextField(blank=True)
    inscription_faculty = models.TextField(blank=True)

    is_staff = models.BooleanField(default=False)
    is_academic = models.BooleanField(default=False)
    is_representative = models.BooleanField(default=False)

    moderated_courses = models.ManyToManyField('catalog.Course', blank=True)

    notify_on_response = models.BooleanField(default=True)
    notify_on_new_doc = models.BooleanField(default=True)
    notify_on_new_thread = models.BooleanField(default=True)
    notify_on_upload = True

    def __init__(self, *args, **kwargs):
        self._moderated_courses = None
        super().__init__(*args, **kwargs)

    @property
    def name(self):
        return "{0.first_name} {0.last_name}".format(self)

    def getPrograms(self):
        """Returns a QS of the programs in which a course is followed by the user"""<commit_msg>[Add] api_token property on user<commit_after>import collections
import itertools
import os
import re
from os.path import join

from django.conf import settings
from django.contrib.auth.models import AbstractBaseUser, UserManager
from django.db import models
from django.shortcuts import get_object_or_404
from django.utils import timezone

from rest_framework.authtoken.models import Token

from catalog.models import Category, Course


class CustomUserManager(UserManager):
    PATTERN = re.compile(r'[\W_]+')

    def _create_user(self, netid, email, password, **extra_fields):
        """
        Creates and saves a User with the given username, email and password.
        """
        now = timezone.now()
        if not netid:
            raise ValueError('The given netid must be set')
        email = self.normalize_email(email)
        user = self.model(netid=netid, email=email, last_login=now, **extra_fields)

        user.set_password(password)
        user.save(using=self._db)
        return user

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

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


class User(AbstractBaseUser):

    USERNAME_FIELD = 'netid'
    REQUIRED_FIELDS = ['email', 'first_name', 'last_name']
    DEFAULT_PHOTO = join(settings.STATIC_URL, "images/default.jpg")
    objects = CustomUserManager()

    netid = models.CharField(max_length=20, unique=True)
    created = models.DateTimeField(auto_now_add=True)
    edited = models.DateTimeField(auto_now=True)
    first_name = models.CharField(max_length=127)
    last_name = models.CharField(max_length=127)
    email = models.CharField(max_length=255, unique=True)
    registration = models.CharField(max_length=80, blank=True)
    welcome = models.BooleanField(default=True)
    comment = models.TextField(blank=True, default='')

    register_method = models.CharField(max_length=32)
    last_login_method = models.CharField(max_length=32)

    inferred_faculty = models.TextField(blank=True)
    inscription_faculty = models.TextField(blank=True)

    is_staff = models.BooleanField(default=False)
    is_academic = models.BooleanField(default=False)
    is_representative = models.BooleanField(default=False)

    moderated_courses = models.ManyToManyField('catalog.Course', blank=True)

    notify_on_response = models.BooleanField(default=True)
    notify_on_new_doc = models.BooleanField(default=True)
    notify_on_new_thread = models.BooleanField(default=True)
    notify_on_upload = True

    def __init__(self, *args, **kwargs):
        self._moderated_courses = None
        super().__init__(*args, **kwargs)

    @property
    def name(self):
        return "{0.first_name} {0.last_name}".format(self)

    @property
    def api_token(self):
        token, _created = Token.objects.get_or_create(user=self)

        return token

    def getPrograms(self):
        """Returns a QS of the programs in which a course is followed by the user"""
{'fraction_non_alphanumeric': 0.10148717110638994, 'fraction_numerical': 0.007190717437489786, 'mean_word_length': 4.155855096882898, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5347261', 'n_tokens_mistral': 1899, 'n_tokens_neox': 1829, 'n_words': 409}