Commits

Sam Hart committed 6026111

add group

Comments (0)

Files changed (3)

src/noink/eventsTable.py

 eventTable = {
     'db_setup' : 'Initial database creation',
     'db_finish' : 'Database initialization complete',
-    'add_user' : "User '%s' added"
+    'add_user' : "User '%s' added",
+    'add_group' : "Group '%s' added"
 }

src/noink/userDB.py

     def __str__(self):
         return repr(self.value)
 
+class DuplicateGroup(Exception):
+
+    def __init__(self, value):
+        self.value = value
+
+    def __str__(self):
+        `return repr(self.value)
+
+class UserNotFound(Exception):
+
+    def __init__(self, value):
+        self.value = value
+
+    def __str__(self):
+        return repr(self.value)
+
 class UserDB:
     __borg_state = {}
 
         @param username: The username to add, must be unique.
         @param fullname: The user's full name
         @param bio: The user's bio (optional)
+
+        @return The user id for the user crated.
         '''
         exists = User.query.filter_by(name=username).first()
 
             mainDB.session.add(u)
             mainDB.session.commit()
             self.eventLog.add('add_user', u.id, True, username)
+            return u.id
 
+    def addGroup(self, groupName, userId=None):
+        '''
+        Adds a new grou to the database.
 
+        @param groupName: The group name to add, must be unique.
+        @param userId: (Optional) Single or multiple user IDs to associate with this group.
+        '''
+
+        exists = Group.query.filter_by(name=groupName).first()
+
+        if exists:
+            raise DuplicateGroup("%s already exists in database with id '%s'" % (groupName, exists.id))
+        else:
+            g = Group(groupName)
+            mainDB.session.add(g)
+            if userId:
+                exists = User.query.filter_by(id=userId).first()
+                if exists:
+                    gm = GroupMapping(g.id, userId)
+                    mainDB.session.add(gm)
+                else:
+                    raise UserNotFound("%s not found in database to match with new group" % userId)
+
+            mainDB.session.commit()
+            self.eventLog.add('add_group', (groupName))
 userDB = UserDB()
 mainDB.create_all()
 eventLog.add('db_setup', -1, True)
-userDB.add(mainApp.config['ADMIN_USER'], mainApp.config['ADMIN_FULLNAME'])
+id = userDB.add(mainApp.config['ADMIN_USER'], mainApp.config['ADMIN_FULLNAME'])
+userDB.addGroup(mainApp.config['ADMIN_GROUP'], id)
 
 eventLog.add('db_finish', -1, True)