<commit_before>from FlaskWebProject import db
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()


class User(Base):
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), index=True, unique=True)
    email = db.Column(db.String(120), index=True, unique=True)
    password = db.Column(db.String(64))
    sso = db.Column(db.String(64))  # none, google, facebook
    # association proxy of "user_userfiles" collection
    # to "keyword" attribute
    userfiles = association_proxy('user_userfiles', 'userfile')

    def __init__(self, username, email, password, sso):
        self.username = username
        self.email = email
        self.password = password
        self.sso = sso

    def is_authenticated(self):
        return True

    def is_active(self):
        return True

    def is_anonymous(self):
        return False

    def get_id(self):
        try:
            return unicode(self.id)  # python 2
        except NameError:
            return str(self.id)  # python 3

    def __repr__(self):
        return '<User %r>' % (self.username)


class UserUserfile(Base):
    # create with:
    # UserUserfile(Userfile("folder","file"), user, permission=7)
    __tablename__ = 'user_userfile'
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True)
    userfile_id = db.Column(db.Integer, db.ForeignKey('userfile.id'), primary_key=True)
    permission = db.Column(db.String(50))

    # bidirectional attribute/collection of "user"/"user_userfiles"
    user = db.relationship(User,
                backref=db.backref("user_userfiles",
                                cascade="all, delete-orphan")
            )

    # reference to the "Keyword" object
    userfile = db.relationship("Userfile")

    def __init__(self, userfile=None, user=None, permission=None):
        self.user = user
        self.userfile = userfile
        self.permission = permission


class Userfile(Base):
    __tablename__ = 'userfile'
    id = db.Column(db.Integer, primary_key=True)
    folder = db.Column('folder', db.String(64))
    name = db.Column('name', db.String(120))

    def __init__(self, folder, name):
        self.folder = folder
        self.name = name

    def get_id(self):
        try:
            return unicode(self.id)  # python 2
        except NameError:
            return str(self.id)  # python 3

    def __repr__(self):
        return '<Userfile %r>' % (self.name)


"""
class Group(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), index=True, unique=True)
    files = db.relationship('File', secondary=groupToFile,
        backref=db.backref('groups', lazy='dynamic'))

    def __init__(self, name, files):
        self.name = name
        self.files = files

    def get_id(self):
        try:
            return unicode(self.id)  # python 2
        except NameError:
            return str(self.id)  # python 3

    def __repr__(self):
        return '<Group %r>' % (self.name)

userToFile = db.Table('userToFile',
    db.Column('user_id', db.Integer, db.ForeignKey('user.id')),
    db.Column('file_id', db.Integer, db.ForeignKey('file.id')),
    db.Column('permission', db.Integer)
)

groupToFile = db.Table('groupToFile',
    db.Column('group_id', db.Integer, db.ForeignKey('group.id')),
    db.Column('file_id', db.Integer, db.ForeignKey('file.id')),
    db.Column('permission', db.Integer)
)

userToGroup = db.Table('userToGroup',
    db.Column('user_id', db.Integer, db.ForeignKey('user.id')),
    db.Column('group_id', db.Integer, db.ForeignKey('group.id')),
    db.Column('permission', db.Integer)
)
"""<commit_msg>working User - File mapping - prepared implementation of User-Group-File mapping<commit_after>from FlaskWebProject import db
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()


class User(Base):
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), index=True, unique=True)
    email = db.Column(db.String(120), index=True, unique=True)
    password = db.Column(db.String(64))
    sso = db.Column(db.String(64))  # none, google, facebook
    # association proxy of "user_userfiles" collection
    # to "keyword" attribute
    userfiles = association_proxy('user_userfiles', 'userfile')

    def __init__(self, username, email, password, sso):
        self.username = username
        self.email = email
        self.password = password
        self.sso = sso

    def is_authenticated(self):
        return True

    def is_active(self):
        return True

    def is_anonymous(self):
        return False

    def get_id(self):
        try:
            return unicode(self.id)  # python 2
        except NameError:
            return str(self.id)  # python 3

    def __repr__(self):
        return '<User %r>' % (self.username)


class UserUserfile(Base):
    # create with:
    # UserUserfile(Userfile("folder","file"), user, permission=7)
    # check with:
    # session.query(UserUserfile).filter(UserUserfile.user_id==1,UserUserfile.userfile_id==2).first().permission
    __tablename__ = 'user_userfile'
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True)
    userfile_id = db.Column(db.Integer, db.ForeignKey('userfile.id'), primary_key=True)
    permission = db.Column(db.String(50))

    # bidirectional attribute/collection of "user"/"user_userfiles"
    user = db.relationship(User,
                backref=db.backref("user_userfiles",
                                cascade="all, delete-orphan")
            )

    # reference to the "Keyword" object
    userfile = db.relationship("Userfile")

    def __init__(self, userfile=None, user=None, permission=None):
        self.user = user
        self.userfile = userfile
        self.permission = permission


class Userfile(Base):
    __tablename__ = 'userfile'
    id = db.Column(db.Integer, primary_key=True)
    folder = db.Column('folder', db.String(64))
    name = db.Column('name', db.String(120))

    def __init__(self, folder, name):
        self.folder = folder
        self.name = name

    def get_id(self):
        try:
            return unicode(self.id)  # python 2
        except NameError:
            return str(self.id)  # python 3

    def __repr__(self):
        return '<Userfile %r>' % (self.name)

"""
User -> Group -> File Mapping:
additional info: http://docs.sqlalchemy.org/en/latest/orm/extensions/associationproxy.html

# should be class Group(Base):
class Group(db.Model): 
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), index=True, unique=True)
    files = db.relationship('File', secondary=groupToFile,
        backref=db.backref('groups', lazy='dynamic'))

    def __init__(self, name, files):
        self.name = name
        self.files = files

    def get_id(self):
        try:
            return unicode(self.id)  # python 2
        except NameError:
            return str(self.id)  # python 3

    def __repr__(self):
        return '<Group %r>' % (self.name)

# should be class GroupUserfile(Base):
groupToFile = db.Table('groupToFile',
    db.Column('group_id', db.Integer, db.ForeignKey('group.id')),
    db.Column('file_id', db.Integer, db.ForeignKey('file.id')),
    db.Column('permission', db.Integer)
)

# should be class UserGroup(Base):
userToGroup = db.Table('userToGroup',
    db.Column('user_id', db.Integer, db.ForeignKey('user.id')),
    db.Column('group_id', db.Integer, db.ForeignKey('group.id')),
    db.Column('permission', db.Integer)
)
"""
{'fraction_non_alphanumeric': 0.1324306898774984, 'fraction_numerical': 0.00670535138620245, 'mean_word_length': 3.0041300980898296, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '1041576', 'n_tokens_mistral': 2521, 'n_tokens_neox': 2422, 'n_words': 518}