<commit_before>from cachable import Cachable, DBPropertyFactory, DBFKPropertyFactory, CachingMeta
import toolkit, permissions, project

_users = None
def users(db):
    global _users
    if _users is None:
        _users = Users(db)
    return _users
        
class Users(object):
    def __init__(self, db):
        self.db = db
        self.byid, self.byname = {}, {}
        SQL = "select userid from users"
        for uid, in db.doQuery(SQL):
            u = User(db, uid)
            self.byid[uid] = u
            self.byname[u.username] = u
    def getUser(self, userid):
        return self.byid[userid]
    def getByName(self, username):
        return self.byname[username]
    def __iter__(self):
        for user in self.byid.values():
            yield user

class User(Cachable):
    __metaclass__ = CachingMeta
    __table__ = 'users'
    __idcolumn__ = 'userid'
    __labelprop__ = 'username'
    

    __dbproperties__ = ["username", "fullname", "affiliation", "active", "email"]
    permissionLevel = DBPropertyFactory("permissionid", table="permissions_users", func=permissions.UserPermission.get)
    projects = DBFKPropertyFactory("permissions_projects_users", "projectid", dbfunc=lambda db, id : project.Project(db, id))

    def __init__(self, db, id):
        print "Creating user %i" % id
        Cachable.__init__(self, db, id)
    
    @property
    def permissionLevelName(self):
        return self.permissionLevel.label
    
    def getProjects(self, own):
        return project.projects(self.db, self.id, own=own)
        
    @property
    def canCreateNewProject(self):
        return self.permissionLevel.value > permissions.UserPermission.CODER.value
    
    @property
    def canViewAllProjects(self):
        return self.permissionLevel.value > permissions.UserPermission.CODER.value
    
    @property
    def canViewAffiliationUserList(self):
        return self.permissionLevel.value > permissions.UserPermission.CODER.value
        
    @property
    def canViewUserList(self):
        return self.permissionLevel.value >= permissions.UserPermission.SUPER_ADMIN.value

    @property
    def canAddNewUserToAffiliation(self):
        return self.permissionLevel.value >= permissions.UserPermission.ADMIN.value
    
    @property
    def isSuperAdmin(self):
        return self.permissionLevel.value >= permissions.UserPermission.SUPER_ADMIN.value
        
        
    
        
        
if __name__ == '__main__':
    import dbtoolkit
    db = dbtoolkit.amcatDB()
    p = dbtoolkit.ProfilingAfterQueryListener()
    db.afterQueryListeners.append(p)
    for i in range(4):
        u = User(db, 2)
        print u.permissionLevel
        print u.fullname
        print u.label
    p.printreport()
<commit_msg>[svn r2021] * Added support for users lacking global permissions (currently only the case for the generic account for draft). * Fixed the test (listeners is a set, not a sequence).<commit_after>from cachable import Cachable, DBPropertyFactory, DBFKPropertyFactory, CachingMeta
import toolkit, permissions, project

_users = None
def users(db):
    global _users
    if _users is None:
        _users = Users(db)
    return _users
        
class Users(object):
    def __init__(self, db):
        self.db = db
        self.byid, self.byname = {}, {}
        SQL = "select userid from users"
        for uid, in db.doQuery(SQL):
            u = User(db, uid)
            self.byid[uid] = u
            self.byname[u.username] = u
    def getUser(self, userid):
        return self.byid[userid]
    def getByName(self, username):
        return self.byname[username]
    def __iter__(self):
        for user in self.byid.values():
            yield user

class User(Cachable):
    __metaclass__ = CachingMeta
    __table__ = 'users'
    __idcolumn__ = 'userid'
    __labelprop__ = 'username'
    

    __dbproperties__ = ["username", "fullname", "affiliation", "active", "email"]
    permissionLevel = DBPropertyFactory("permissionid", table="permissions_users", func=permissions.UserPermission.get)
    projects = DBFKPropertyFactory("permissions_projects_users", "projectid", dbfunc=lambda db, id : project.Project(db, id))

    def __init__(self, db, id):
        print "Creating user %i" % id
        Cachable.__init__(self, db, id)
    
    @property
    def permissionLevelName(self):
        if self.permissionLevel is None:
            return "None"
        else:
            return self.permissionLevel.label
    
    def getProjects(self, own):
        return project.projects(self.db, self.id, own=own)
        
    @property
    def canCreateNewProject(self):
        return self.permissionLevel.value > permissions.UserPermission.CODER.value
    
    @property
    def canViewAllProjects(self):
        return self.permissionLevel.value > permissions.UserPermission.CODER.value
    
    @property
    def canViewAffiliationUserList(self):
        return self.permissionLevel.value > permissions.UserPermission.CODER.value
        
    @property
    def canViewUserList(self):
        return self.permissionLevel.value >= permissions.UserPermission.SUPER_ADMIN.value

    @property
    def canAddNewUserToAffiliation(self):
        return self.permissionLevel.value >= permissions.UserPermission.ADMIN.value
    
    @property
    def isSuperAdmin(self):
        return self.permissionLevel.value >= permissions.UserPermission.SUPER_ADMIN.value
        
        
    
        
        
if __name__ == '__main__':
    import dbtoolkit
    db = dbtoolkit.amcatDB()
    p = dbtoolkit.ProfilingAfterQueryListener()
    db.afterQueryListeners.add(p)
    for i in range(4):
        u = User(db, 2)
        print u.permissionLevel
        print u.fullname
        print u.label
    p.printreport()

{'fraction_non_alphanumeric': 0.0935489480090419, 'fraction_numerical': 0.0013910624239262736, 'mean_word_length': 2.7133634602969656, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '3444491', 'n_tokens_mistral': 1691, 'n_tokens_neox': 1684, 'n_words': 395}