<commit_before># -*- coding: utf-8 -*-
"""User models."""
import datetime as dt
import urllib, hashlib
from time import mktime

from flask_login import UserMixin

from dribdat.extensions import hashing
from dribdat.database import (
    Column,
    db,
    Model,
    reference_col,
    relationship,
    SurrogatePK,
)

from dribdat.user import PROJECT_PROGRESS_PHASE

from sqlalchemy import or_

class Role(SurrogatePK, Model):
    __tablename__ = 'roles'
    name = Column(db.String(80), unique=True, nullable=False)
    user_id = reference_col('users', nullable=True)
    user = relationship('User', backref='roles')

    def __init__(self, name, **kwargs):
        """Create instance."""
        db.Model.__init__(self, name=name, **kwargs)

    def __repr__(self):
        """Represent instance as a unique string."""
        return '<Role({name})>'.format(name=self.name)


class User(UserMixin, SurrogatePK, Model):
    """A user of the app."""

    __tablename__ = 'users'
    username = Column(db.String(80), unique=True, nullable=False)
    email = Column(db.String(80), unique=True, nullable=False)
    webpage_url = Column(db.String(128), nullable=True)
    #: The hashed password
    password = Column(db.String(128), nullable=True)
    created_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
    active = Column(db.Boolean(), default=False)
    is_admin = Column(db.Boolean(), default=False)

    cardtype = Column(db.String(80), nullable=True)
    carddata = Column(db.String(255), nullable=True)

    @property
    def data(self):
        return {
            'id': self.id,
            'username': self.username,
        }

    def socialize(self):
        if 'github.com/' in self.webpage_url:
            self.cardtype = 'github'
            self.carddata = self.webpage_url.strip('/').split('/')[-1]
        elif 'twitter.com/' in self.webpage_url:
            self.cardtype = 'twitter'
            self.carddata = self.webpage_url.strip('/').split('/')[-1]
        else:
            gr_size = 40
            gravatar_url = hashlib.md5(self.email.lower()).hexdigest() + "?"
            gravatar_url += urllib.urlencode({'s':str(gr_size)})
            self.cardtype = 'gravatar'
            self.carddata = gravatar_url
        self.save()

    def __init__(self, username=None, email=None, password=None, **kwargs):
        """Create instance."""
        if username and email:
            db.Model.__init__(self, username=username, email=email, **kwargs)
        if password:
            self.set_password(password)

    def set_password(self, password):
        """Set password."""
        self.password = <PASSWORD>(password)

    def check_password(self, value):
        """Check password."""
        return hashing.check_value(self.password, value)

    def __repr__(self):
        """Represent instance as a unique string."""
        return '<User({username!r})>'.format(username=self.username)


class Event(SurrogatePK, Model):
    __tablename__ = 'events'
    name = Column(db.String(80), unique=True, nullable=False)
    hostname = Column(db.String(80), nullable=True)
    location = Column(db.String(255), nullable=True)
    description = Column(db.UnicodeText(), nullable=True)

    logo_url = Column(db.String(255), nullable=True)
    custom_css = Column(db.UnicodeText(), nullable=True)

    webpage_url = Column(db.String(255), nullable=True)
    community_url = Column(db.String(255), nullable=True)
    community_embed = Column(db.UnicodeText(), nullable=True)

    starts_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
    ends_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
    is_current = Column(db.Boolean(), default=False)

    @property
    def data(self):
        return {
            'id': self.id,
            'name': self.name,
            'hostname': self.hostname,
            'location': self.location,
            'starts_at': self.starts_at,
            'ends_at': self.ends_at,
            'info_url': self.webpage_url,
        }

    @property
    def has_started(self):
        return self.starts_at <= dt.datetime.utcnow() <= self.ends_at

    @property
    def countdown(self):
        if self.starts_at > dt.datetime.utcnow():
            return self.starts_at
        elif self.ends_at > dt.datetime.utcnow():
            return self.ends_at
        else:
            return None

    @property
    def date(self):
        if self.starts_at.month == self.ends_at.month:
            return "{0} {1}-{2}, {3}".format(
                self.starts_at.strftime("%B"),
                self.starts_at.day,
                self.ends_at.day,
                self.ends_at.year,
            )
        else:<commit_msg>API refactored<commit_after># -*- coding: utf-8 -*-
"""User models."""
import datetime as dt
import urllib, hashlib
from time import mktime

from flask_login import UserMixin

from dribdat.extensions import hashing
from dribdat.database import (
    Column,
    db,
    Model,
    reference_col,
    relationship,
    SurrogatePK,
)

from dribdat.user import PROJECT_PROGRESS_PHASE

from sqlalchemy import or_

class Role(SurrogatePK, Model):
    __tablename__ = 'roles'
    name = Column(db.String(80), unique=True, nullable=False)
    user_id = reference_col('users', nullable=True)
    user = relationship('User', backref='roles')

    def __init__(self, name, **kwargs):
        """Create instance."""
        db.Model.__init__(self, name=name, **kwargs)

    def __repr__(self):
        """Represent instance as a unique string."""
        return '<Role({name})>'.format(name=self.name)


class User(UserMixin, SurrogatePK, Model):
    """A user of the app."""

    __tablename__ = 'users'
    username = Column(db.String(80), unique=True, nullable=False)
    email = Column(db.String(80), unique=True, nullable=False)
    webpage_url = Column(db.String(128), nullable=True)
    #: The hashed password
    password = Column(db.String(128), nullable=True)
    created_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
    active = Column(db.Boolean(), default=False)
    is_admin = Column(db.Boolean(), default=False)

    cardtype = Column(db.String(80), nullable=True)
    carddata = Column(db.String(255), nullable=True)

    @property
    def data(self):
        return {
            'id': self.id,
            'username': self.username,
        }

    def socialize(self):
        if 'github.com/' in self.webpage_url:
            self.cardtype = 'github'
            self.carddata = self.webpage_url.strip('/').split('/')[-1]
        elif 'twitter.com/' in self.webpage_url:
            self.cardtype = 'twitter'
            self.carddata = self.webpage_url.strip('/').split('/')[-1]
        else:
            gr_size = 40
            gravatar_url = hashlib.md5(self.email.lower()).hexdigest() + "?"
            gravatar_url += urllib.urlencode({'s':str(gr_size)})
            self.cardtype = 'gravatar'
            self.carddata = gravatar_url
        self.save()

    def __init__(self, username=None, email=None, password=None, **kwargs):
        """Create instance."""
        if username and email:
            db.Model.__init__(self, username=username, email=email, **kwargs)
        if password:
            self.set_password(password)

    def set_password(self, password):
        """Set password."""
        self.password = hashing.hash_value(password)

    def check_password(self, value):
        """Check password."""
        return hashing.check_value(self.password, value)

    def __repr__(self):
        """Represent instance as a unique string."""
        return '<User({username!r})>'.format(username=self.username)


class Event(SurrogatePK, Model):
    __tablename__ = 'events'
    name = Column(db.String(80), unique=True, nullable=False)
    hostname = Column(db.String(80), nullable=True)
    location = Column(db.String(255), nullable=True)
    description = Column(db.UnicodeText(), nullable=True)

    logo_url = Column(db.String(255), nullable=True)
    custom_css = Column(db.UnicodeText(), nullable=True)

    webpage_url = Column(db.String(255), nullable=True)
    community_url = Column(db.String(255), nullable=True)
    community_embed = Column(db.UnicodeText(), nullable=True)

    starts_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
    ends_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
    is_current = Column(db.Boolean(), default=False)

    @property
    def data(self):
        return {
            'id': self.id,
            'name': self.name,
            'hostname': self.hostname,
            'location': self.location,
            'starts_at': self.starts_at,
            'has_started': self.has_started,
            'ends_at': self.ends_at,
            'info_url': self.webpage_url,
        }

    @property
    def has_started(self):
        return self.starts_at <= dt.datetime.utcnow() <= self.ends_at

    @property
    def countdown(self):
        if self.starts_at > dt.datetime.utcnow():
            return self.starts_at
        elif self.ends_at > dt.datetime.utcnow():
            return self.ends_at
        else:
            return None

    @property
    def date(self):
        if self.starts_at.month == self.ends_at.month:
            return "{0} {1}-{2}, {3}".format(
                self.starts_at.strftime("%B"),
                self.starts_at.day,
                self.ends_at.day,
                self.ends_at.year,
            )
        else:
{'fraction_non_alphanumeric': 0.13466386554621848, 'fraction_numerical': 0.009033613445378152, 'mean_word_length': 2.6675654853620956, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, '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': '1166640', 'n_tokens_mistral': 3048, 'n_tokens_neox': 2941, 'n_words': 604}