<commit_before># -*- coding: utf-8 -*-
import base64
import random

import flask.ext.login

from enum import Enum
from datetime import datetime, timedelta

from sqlalchemy import (
    Column, Integer, String, Boolean, UniqueConstraint,
    DateTime
)
from database import Base

try:
    from config import TOKEN_BYTES
except (ImportError, NameError):
    TOKEN_BYTES = 15

try:
    from config import TOKEN_LIFETIME
except (ImportError, NameError):
    TOKEN_LIFETIME = timedelta(days=7)

try:
    from config import LOGIN_LIFETIME
except (ImportError, NameError):
    LOGIN_LIFETIME = timedelta(days=2)

_rng = random.SystemRandom()


class InvitationState(Enum):
    VALID = "Valid"
    REDEEMED = "Redeemed"
    EXPIRED = "Expired"


class Invitation(Base):
    __tablename__ = "invitations"
    __table_args__ = (
        UniqueConstraint("token"),
    )

    id = Column(Integer, primary_key=True)
    token = Column(String, nullable=False)
    redeemed = Column(Boolean)
    creator = Column(String, nullable=False)
    created_for_mail = Column(String, nullable=False)
    expires = Column(DateTime, nullable=False)

    def __init__(self, creator, created_for_mail):
        self.token = Invitation.create_token()
        self.redeemed = False
        self.creator = creator
        self.created_for_mail = created_for_mail
        self.expires = datetime.utcnow() + TOKEN_LIFETIME

    def redeem(self):
        self.redeemed = True

    def get_state(self):
        if self.redeemed:
            return InvitationState.REDEEMED
        elif datetime.utcnow() > self.expires:
            return InvitationState.EXPIRED
        else:
            return InvitationState.VALID

    @staticmethod
    def create_token():
        token_bits = TOKEN_BYTES * 8
        token_data = _rng.getrandbits(token_bits).to_bytes(
            TOKEN_BYTES, "little")
        return base64.urlsafe_b64encode(token_data).decode("ascii")


class User(flask.ext.login.UserMixin, Base):
    __tablename__ = "ldap_session_users"<commit_msg>Use lower token size and base32 for better transferability<commit_after># -*- coding: utf-8 -*-
import base64
import random

import flask.ext.login

from enum import Enum
from datetime import datetime, timedelta

from sqlalchemy import (
    Column, Integer, String, Boolean, UniqueConstraint,
    DateTime
)
from database import Base

try:
    from config import TOKEN_BYTES
except (ImportError, NameError):
    TOKEN_BYTES = 5

try:
    from config import TOKEN_LIFETIME
except (ImportError, NameError):
    TOKEN_LIFETIME = timedelta(days=7)

try:
    from config import LOGIN_LIFETIME
except (ImportError, NameError):
    LOGIN_LIFETIME = timedelta(days=2)

_rng = random.SystemRandom()


class InvitationState(Enum):
    VALID = "Valid"
    REDEEMED = "Redeemed"
    EXPIRED = "Expired"


class Invitation(Base):
    __tablename__ = "invitations"
    __table_args__ = (
        UniqueConstraint("token"),
    )

    id = Column(Integer, primary_key=True)
    token = Column(String, nullable=False)
    redeemed = Column(Boolean)
    creator = Column(String, nullable=False)
    created_for_mail = Column(String, nullable=False)
    expires = Column(DateTime, nullable=False)

    def __init__(self, creator, created_for_mail):
        self.token = Invitation.create_token()
        self.redeemed = False
        self.creator = creator
        self.created_for_mail = created_for_mail
        self.expires = datetime.utcnow() + TOKEN_LIFETIME

    def redeem(self):
        self.redeemed = True

    def get_state(self):
        if self.redeemed:
            return InvitationState.REDEEMED
        elif datetime.utcnow() > self.expires:
            return InvitationState.EXPIRED
        else:
            return InvitationState.VALID

    @staticmethod
    def create_token():
        token_bits = TOKEN_BYTES * 8
        token_data = _rng.getrandbits(token_bits).to_bytes(
            TOKEN_BYTES, "little")
        return base64.b32encode(token_data).decode("ascii").lower()


class User(flask.ext.login.UserMixin, Base):
    __tablename__ = "ldap_session_users"
{'fraction_non_alphanumeric': 0.088623046875, 'fraction_numerical': 0.006103515625, 'mean_word_length': 3.2324380165289255, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1281310', 'n_tokens_mistral': 1348, 'n_tokens_neox': 1276, 'n_words': 295}