<commit_before>import time

import config.options
import util.cryptography
from linkr import db


class Link(db.Model):
    __tablename__ = 'link'

    link_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    user_id = db.Column(db.Integer, index=True, default=None)
    submit_time = db.Column(db.Integer)
    password_hash = db.Column(db.Text, default=None)
    alias = db.Column(db.String(32), index=True, unique=True)
    outgoing_url = db.Column(db.Text)

    def __init__(
        self,
        alias,
        outgoing_url,
        password=None,
        user_id=None,
    ):
        self.submit_time = int(time.time())
        self.alias = alias
        self.outgoing_url = outgoing_url
        self.password_hash = util.cryptography.secure_hash(password) if password else None
        self.user_id = user_id

    def validate_password(self, password):
        return not self.password_hash or \
            util.cryptography.secure_hash(password) == self.password_hash

    def as_dict(self):
        return {
            'link_id': self.link_id,
            'user_id': self.user_id,
            'submit_time': self.submit_time,
            'alias': self.alias,
            'full_alias': '{base}/{alias}'.format(base=config.options.LINKR_URL, alias=self.alias),
            'outgoing_url': self.outgoing_url,
            'is_password_protected': bool(self.password_hash),
        }
<commit_msg>Documentation on models.Link and add is_password_protected method<commit_after>import time

import config.options
import util.cryptography
from linkr import db


class Link(db.Model):
    """
    Model representing a link entry.
    """

    __tablename__ = 'link'

    link_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    user_id = db.Column(db.Integer, index=True, default=None)
    submit_time = db.Column(db.Integer)
    password_hash = db.Column(db.Text, default=None)
    alias = db.Column(db.String(32), index=True, unique=True)
    outgoing_url = db.Column(db.Text)

    def __init__(
        self,
        alias,
        outgoing_url,
        password=None,
        user_id=None,
    ):
        """
        Create a new link entry.

        :param alias:
        :param outgoing_url:
        :param password:
        :param user_id:
        """
        self.submit_time = int(time.time())
        self.alias = alias
        self.outgoing_url = outgoing_url
        self.password_hash = util.cryptography.secure_hash(password) if password else None
        self.user_id = user_id

    def validate_password(self, password):
        """
        Validate that the supplied link password is correct for this link. This method will
        consider any password to be "correct" if there is no password associated with this link.

        :param password: The <PASSWORD>.
        :return: True if the supplied password matches the link's password or if the link is not
                 password protected; False otherwise.
        """
        return not self.password_hash or \
            util.cryptography.secure_hash(password) == self.password_hash

    def as_dict(self):
        """
        Represent this link entry as a API-friendly, JSON-formatted dictionary.

        :return: A representation of this link's data as a dictionary.
        """
        return {
            'link_id': self.link_id,
            'user_id': self.user_id,
            'submit_time': self.submit_time,
            'alias': self.alias,
            'full_alias': '{base}/{alias}'.format(base=config.options.LINKR_URL, alias=self.alias),
            'outgoing_url': self.outgoing_url,
            'is_password_protected': self.is_password_protected(),
        }

    def is_password_protected(self):
        """
        Return whether this link is password protected.

        :return: True if the link is password protected; False otherwise.
        """
        return bool(self.password_hash)

{'fraction_non_alphanumeric': 0.09895434838051517, 'fraction_numerical': 0.00102014792144861, 'mean_word_length': 2.4616063548102383, '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': '5646363', 'n_tokens_mistral': 1158, 'n_tokens_neox': 1131, 'n_words': 279}