<commit_before>from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.gis.db import models
from django.core.urlresolvers import reverse

from utils import prettify_lat, prettify_long

from base64 import b64encode
import pickle
import random
import re


def make_token():
    return b64encode(random.getrandbits(256).to_bytes(32, "big"))


class UserProfile(models.Model):
    """
    Extension to the built-in User class that adds a token for allowing
    password-less login from email links.

    For now, we can get away with this, because we won't be handling any
    sensitive data.
    """
    user = models.OneToOneField(settings.AUTH_USER_MODEL,
                                on_delete=models.CASCADE,
                                related_name="profile")
    token = models.CharField(max_length=64, default=make_token)
    # The user should not be able to log in if this is false:
    nickname = models.CharField(max_length=128,
                                help_text="What do you prefer to be called?")

    def activate(self):
        self.is_active = True
        self.token = make_token()
        self.save()

    def deactivate(self):
        self.is_active = False
        self.token = make_token()
        self.save()

    def generate_token(self):
        "Creates a new verification token for the user and saves it."
        self.token = make_token()
        self.save()

        return self.token

    def confirmation_link(self):
        return reverse("confirm-user",
                       kwargs={"token": self.token,
                               "pk": self.pk})


class Subscription(models.Model):
    # The subscription belong to a registered user:
    user = models.ForeignKey(User, null=True,
                             related_name="subscriptions")
    # Stores the pickle serialization of a dictionary describing the query
    query = models.BinaryField()
    last_notified = models.DateTimeField(auto_now=True)

    @property
    def query_dict(self):
        return pickle.loads(self.query)

    @query_dict.setter
    def query_dict(self, new_dict):
        self.query = pickle.dumps(new_dict)

    def set_validated_query(self, q):
        self.query_dict = self.validate_query(q)

    valid_keys = {
        "projects": lambda v: v.lower() in {"all", "null"},
        "text": lambda _: True,
        "box": lambda v: (len(v.split(",")) == 4 and<commit_msg>Correctly reference User object<commit_after>from django.conf import settings
from django.contrib.gis.db import models
from django.core.urlresolvers import reverse

from utils import prettify_lat, prettify_long

from base64 import b64encode
import pickle
import random
import re


def make_token():
    return b64encode(random.getrandbits(256).to_bytes(32, "big"))


class UserProfile(models.Model):
    """
    Extension to the built-in User class that adds a token for allowing
    password-less login from email links.

    For now, we can get away with this, because we won't be handling any
    sensitive data.
    """
    user = models.OneToOneField(settings.AUTH_USER_MODEL,
                                on_delete=models.CASCADE,
                                related_name="profile")
    token = models.CharField(max_length=64, default=make_token)
    # The user should not be able to log in if this is false:
    nickname = models.CharField(max_length=128,
                                help_text="What do you prefer to be called?")

    def activate(self):
        self.is_active = True
        self.token = make_token()
        self.save()

    def deactivate(self):
        self.is_active = False
        self.token = make_token()
        self.save()

    def generate_token(self):
        "Creates a new verification token for the user and saves it."
        self.token = make_token()
        self.save()

        return self.token

    def confirmation_link(self):
        return reverse("confirm-user",
                       kwargs={"token": self.token,
                               "pk": self.pk})


class Subscription(models.Model):
    # The subscription belong to a registered user:
    user = models.ForeignKey(settings.AUTH_USER_MODEL,
                             null=True,
                             related_name="subscriptions")
    # Stores the pickle serialization of a dictionary describing the query
    query = models.BinaryField()
    last_notified = models.DateTimeField(auto_now=True)

    @property
    def query_dict(self):
        return pickle.loads(self.query)

    @query_dict.setter
    def query_dict(self, new_dict):
        self.query = pickle.dumps(new_dict)

    def set_validated_query(self, q):
        self.query_dict = self.validate_query(q)

    valid_keys = {
        "projects": lambda v: v.lower() in {"all", "null"},
        "text": lambda _: True,
        "box": lambda v: (len(v.split(",")) == 4 and
{'fraction_non_alphanumeric': 0.09126094927683846, 'fraction_numerical': 0.006926054186188633, 'mean_word_length': 2.416840640222686, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 3, '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': '5134833', 'n_tokens_mistral': 1425, 'n_tokens_neox': 1369, 'n_words': 398}