예제 #1
0
class Attachment(db.Model):
    __tablename__ = 'attachment'

    attachment_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    paste_id = db.Column(db.Integer, index=True)
    file_name = db.Column(db.Text)
    hash_name = db.Column(db.Text)
    file_size = db.Column(db.Integer)
    mime_type = db.Column(db.Text)

    def __init__(
        self,
        paste_id,
        file_name,
        file_size,
        mime_type,
    ):
        self.paste_id = paste_id
        self.file_name = file_name
        self.hash_name = util.cryptography.secure_hash(file_name)
        self.file_size = file_size
        self.mime_type = mime_type

    def as_dict(self):
        """
        Represent this attachment as an easily JSON-serializable dictionary.

        :return: Dictionary of attachment properties
        """
        return {
            'paste_id_repr': util.cryptography.get_id_repr(self.paste_id),
            'file_name': self.file_name,
            'file_size': self.file_size,
            'mime_type': self.mime_type,
        }
예제 #2
0
class User(db.Model):
    __tablename__ = 'user'

    user_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    is_active = db.Column(db.Boolean)
    signup_time = db.Column(db.Integer)
    signup_ip = db.Column(db.Text)
    username = db.Column(db.String(64), index=True)
    password_hash = db.Column(db.Text)
    name = db.Column(db.Text, default=None)
    email = db.Column(db.Text, default=None)
    api_key = db.Column(db.String(64), index=True)

    def __init__(
        self,
        signup_ip,
        username,
        password_hash,
        name,
        email,
    ):
        self.is_active = True
        self.signup_time = time.time()
        self.signup_ip = signup_ip
        self.username = username
        self.password_hash = password_hash
        self.name = name
        self.email = email
        self.api_key = util.testing.random_alphanumeric_string(length=64)

    @staticmethod
    def is_authenticated():
        return True

    @staticmethod
    def is_anonymous():
        return False

    def get_id(self):
        return self.user_id
예제 #3
0
class Paste(db.Model):
    __tablename__ = 'paste'
    __table_args__ = {'mysql_collate': 'utf8mb4_general_ci'}

    paste_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    is_active = db.Column(db.Boolean)
    user_id = db.Column(db.Integer, default=None, index=True)
    post_time = db.Column(db.Integer)
    expiry_time = db.Column(db.Integer, default=None)
    title = db.Column(db.Text, default=None)
    language = db.Column(db.Text)
    password_hash = db.Column(db.Text, default=None)
    contents = db.Column(db.Text(length=4294967295))
    deactivation_token = db.Column(db.Text)
    views = db.Column(db.Integer)
    is_api_post = db.Column(db.Boolean)

    def __init__(
        self,
        user_id,
        password_hash,
        contents,
        expiry_time,
        title,
        language,
        is_api_post,
    ):
        self.is_active = True
        self.user_id = user_id
        self.post_time = int(time.time())
        self.expiry_time = expiry_time
        self.title = title
        self.language = language
        self.password_hash = password_hash
        self.contents = contents
        self.deactivation_token = util.string.random_alphanumeric_string()
        self.views = 0
        self.is_api_post = is_api_post

    def as_dict(self):
        """
        Represent this paste as an easily JSON-serializable dictionary. This method is intended to present the paste
        for consumption at the highest level of the stack, so it should exclude all sensitive information.

        :return: Dictionary of paste properties
        """
        return {
            'paste_id_repr':
            util.cryptography.get_id_repr(self.paste_id),
            'is_active':
            self.is_active,
            'post_time':
            self.post_time,
            'expiry_time':
            self.expiry_time,
            'contents':
            self.contents,
            'title':
            self.title,
            'language':
            self.language,
            'views':
            self.views,
            'is_password_protected':
            self.password_hash is not None,
            'url':
            PasteViewInterfaceURI.full_uri(
                paste_id=util.cryptography.get_id_repr(self.paste_id)),
        }