Open imincik opened 9 years ago
Momentalne to vyzera tak, ze objekt triedy GISLab by bol bezstavovy, a preto by som navrhol pouzit dekorator @staticmethod. Vsetky administratorske funkcie by tymto boli definovane v jednom logickom celku a nemusela by sa vytvarat instancia objektu.
>>> import GISLab
>>> GISLab.useradd(...)
>>> GISLab.usermod(...)
class GISLab(object):
@staticmethod
def useradd(username, *kwargs):
return GISLabUser.create(username, *kwargs)
@staticmethod
def userdel(username):
GISLabUser.get(username).delete()
@staticmethod
def usermod(username, **kwargs):
GISLabUser.get(username).modify(**kwargs)
@staticmethod
def userget(username):
return GISLabUser.get(username)
@staticmethod
def userslist():
return GISLabUser.users(query)
V triede GISLabUser by som pouzival jej konstruktor iba interne, na vytvaranie novych a ziskavanie uz existujucich pouzivatelov by som vytvoril metody triedy (dekorator @classmethod), ktore by vzdy vracali uz inicializovane objekty pouzivatelov (atribut GISLabUser._initialized by uz nebol potrebny).
class GISLabUser(object):
@classmethod
def create(cls, username, firstname, lastname, email, password, description, superuser):
user = cls(username, firstname=firstname, lastname=lastname, email=email, password=password, description=description, superuser=superuser)
user.uid = nextuid()
user.gid =
user.home =
user.published_data =
...
user._create_ldap()
user._create_postgres()
user._create_dirs(uid, gid, home, published_data)
return user
@classmethod
def users(cls, query=None):
if not query:
# all gislab users
gid = grp.getgrnam('gislabusers').gr_gid
query = "(&(objectClass=inetOrgPerson)(gidNumber={}))".format(gid)
users = []
for ldap_item in ... :
username = ldap_item[1]['uid'][0]
data = ldap_item[0][1]
firstname = data['givenName'][0]
...
user = cls(username, firstname=firstname, ...)
users.append(user)
return users
@classmethod
def get(cls, username):
query = "(uid={})".format(username)
users = GISLabUser.users(query)
if users is None or len(users) == 0:
raise GISLabError("GIS.lab user '{0}' doesn't exists".format(username))
return users[0]
def __init__(self, username, **kwargs):
self.username = self._validate_username(username)
self._log = "GISLabUser(%s): " % self.username
for name, value in kwargs.iteritems():
setattr(self, name, value)
def _create_ldap(self):
...
Development of Python library which will allow to list and manage GIS.lab user's accounts and will replace Bash code in gislab-adduser|deluser|password .
NOTES: