Commits

Sam Hart committed 5d2e4c9

further work on setup

Comments (0)

Files changed (6)

src/noink/dataModels.py

     actual user that must be in the database. If they do *not* exist in Noink,
     then we add it.
     '''
-    __tablename__ = 'users'
+    __tablename__ = 'user'
 
     id = mainDB.Column(mainDB.Integer, primary_key=True)
     name = mainDB.Column(mainDB.String(60))
     fullname = mainDB.Column(mainDB.String(80))
     bio = mainDB.Column(mainDB.String(4000))
 
-    def __init__(self, name, fullname, passwordHash, bio):
+    def __init__(self, name, fullname, bio):
         self.name = name
         self.fullname = fullname
         self.bio = bio
     id = mainDB.Column(mainDB.Integer, primary_key=True)
     group_id = mainDB.Column(mainDB.Integer, mainDB.ForeignKey("group.id"))
     group = mainDB.relationship('Group')
-    user_id = mainDB.Column(mainDB.Integer, mainDB.ForeignKey("users.id"))
-    user = mainDB.relationship("Users")
+    user_id = mainDB.Column(mainDB.Integer, mainDB.ForeignKey("user.id"))
+    user = mainDB.relationship("User")
 
     def __init__(self, group, user):
         self.group = group
     date = mainDB.Column(mainDB.DateTime())
     title = mainDB.Column(mainDB.String(256))
     entry = mainDB.Column(mainDB.String(40000))
-    author_id = mainDB.Column(mainDB.Integer, mainDB.ForeignKey("users.id"))
+    author_id = mainDB.Column(mainDB.Integer, mainDB.ForeignKey("user.id"))
     author = mainDB.relationship("User")
 
     def __init__(self, title, author, date, entry):

src/noink/defaultConfig.py

 # user in the database!
 ADMIN_USER = "admin"
 
+# The administrative user's fullname
+ADMIN_FULLNAME = "Administrator"
+
 # The name of the administrative group - Note that this field SHOULD NOT be
 # changed after the database has been created, as it will not update the admin
 # group in the database!

src/noink/eventLog.py

 ##BOILERPLATE_COPYRIGHT_END
 '''
 
+import datetime
+
 from noink import mainDB
-from noink.dataModuls import Event
+from noink.dataModels import Event
 from noink.eventsTable import eventTable
 
 class EventLog:
     def __init__(self):
         self.__dict__ = self.__borg_state
 
-    def add(self, name, user, processed=False):
+    def add(self, name, user, processed=False, *args):
         '''
         Adds an event to the log.
 
         '''
 
         if eventTable.has_key(name):
-            e = Event(name, eventTable[name],
+            now = datetime.datetime.now()
+            if len(args) > 0:
+                e = Event(name, eventTable[name] % args, now, user)
+            else:
+                e = Event(name, eventTable[name], now, user)
+
+            e.processed = processed
+            if processed:
+                e.processedDate = now
+
+            mainDB.session.add(e)
+            mainDB.session.commit()
+        else:
+            raise KeyError('%s not in eventTable!' % name)

src/noink/eventsTable.py

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

src/noink/userDB.py

+'''
+##BOILERPLATE_COPYRIGHT
+##BOILERPLATE_COPYRIGHT_END
+'''
+
+import datetime
+
+from noink import mainDB
+from noink.dataModels import User, Group, GroupMapping
+from noink.eventLog import EventLog
+
+class DuplicateUser(Exception):
+
+    def __init__(self, value):
+        self.value = value
+
+    def __str__(self):
+        return repr(self.value)
+
+class UserDB:
+    __borg_state = {}
+
+    def __init__(self):
+        self.__dict__ = self.__borg_state
+
+        self.eventLog = EventLog()
+
+    def add(self, username, fullname, bio=""):
+        '''
+        Adds a user to the database.
+
+        @param username: The username to add, must be unique.
+        @param fullname: The user's full name
+        @param bio: The user's bio (optional)
+        '''
+        exists = User.query.filter_by(name=username).first()
+
+        if exists:
+            raise DuplicateUser("%s already exists in database with id '%d'" % (username, exists.id))
+        else:
+            u = User(username, fullname, bio)
+            mainDB.session.add(u)
+            mainDB.session.commit()
+            self.eventLog.add('add_user', u.id, True, username)
+
+
 setup_db.py
 -----------
 Initializes the database, setting any default values
+
+##BOILERPLATE_COPYRIGHT
+##BOILERPLATE_COPYRIGHT_END
+
 '''
 
-from noink import mainDB
+from noink import mainApp, mainDB
+from noink.eventLog import EventLog
+from noink.userDB import UserDB
+
+eventLog = EventLog()
+userDB = UserDB()
 mainDB.create_all()
+eventLog.add('db_setup', -1, True)
+userDB.add(mainApp.config['ADMIN_USER'], mainApp.config['ADMIN_FULLNAME'])
 
+eventLog.add('db_finish', -1, True)
+