def __init__(self): self.logger = logging.getLogger('session') self.db = Database() self.generateSessions( ) #Run through last session logs and generate state information self.eventThread = threading.Thread(target=self.eventLoop, args=(())) self.eventThread.daemon = True self.eventThread.start()
def __init__(self, parent, controller, prj): Frame.__init__(self, parent) self.prj = prj self.controller = controller self.model = ModelDatabase(Database()) self.model = ModelDatabase(Database()) self.log_pwd_msg = Label(text="", font='Arial 12', fg='red') self.name_text = Label(text="Project name*", font='Arial 12') self.name_input = Entry(font='Arial 12') self.name_input.insert(END, self.prj.name) self.author_text = Label(text="Author*", font='Arial 12') self.author_input = Entry(font='Arial 12') self.author_input.insert(END, self.prj.author) self.desc_text = Label(text="Description", font='Arial 12') self.desc_input = Text(font='Arial 12', width=20, height=6) self.desc_input.insert(END, self.prj.description) self.users_list = Label(text="Members", font='Arial 12', borderwidth=0, highlightthickness=0) self.lb_user = Listbox(font='Arial 12', selectmode="multiple") for numb, user in enumerate(self.model.get_users()): self.lb_user.insert(numb, user.login) self.add_btn = Button(text="Save", font='Arial 12', command=lambda: self.save_project()) self.cancel_btn = Button( text="Cancel", font='Arial 12', command=lambda: controller.create_projects_page()) # dispose element on widget\ self.name_text.grid(row=1, columnspan=2, pady=10) self.name_input.grid(row=2, columnspan=2) self.author_text.grid(row=3, columnspan=2) self.author_input.grid(row=4, columnspan=2, pady=10, padx=10) self.desc_text.grid(row=7, columnspan=2, pady=6) self.desc_input.grid(row=8, columnspan=2, padx=10) self.users_list.grid(row=1, column=2, columnspan=2) self.lb_user.grid(row=1, rowspan=8, column=2, columnspan=2, padx=10) self.add_btn.grid(row=9, column=1, sticky="w", pady=10) self.cancel_btn.grid(row=9, column=1, sticky="e", padx=10)
def __init__(self, parent, controller): Frame.__init__(self, parent) self.controller = controller self.model = ModelDatabase(Database()) self.log_pwd_msg = Label(text="", font='Arial 12', fg='red') # Creating the username & password entry boxes self.login_text = Label(text="Username*", font='Arial 12') self.login_input = Entry(font='Arial 12') self.login_input.bind("<Key>", self.hide_login_password_message) self.password_text = Label(text="Password*", font='Arial 12') self.password_input = Entry(show="*", font='Arial 12') self.password_input.bind("<Key>", self.hide_login_password_message) # attempt to login button self.attempt_login = Button( text="Enter", font='Arial 12', command=lambda: self.login_password_message()) self.register = Button(text=" New register", font='Arial 12', command=lambda: controller.create_reg_page()) self.login_text.grid(row=1, columnspan=2, pady=10) self.login_input.grid(row=2, columnspan=2) self.password_text.grid(row=3, columnspan=2) self.password_input.grid(row=4, columnspan=2, pady=10) self.attempt_login.grid(row=5, column=0, sticky="e", pady=10, padx=15) self.register.grid(row=5, column=1, sticky="w", pady=10, padx=15)
def __init__(self, parent, controller): Frame.__init__(self, parent) self.controller = controller self.model = ModelDatabase(Database()) self.model = ModelDatabase(Database()) self.log_pwd_msg = Label(text="", font='Arial 12', fg='red') self.log_text = Label(text="Username*", font='Arial 12') self.log_input = Entry(font='Arial 12') self.email_text = Label(text="Email*", font='Arial 12') self.email_input = Entry(font='Arial 12') self.pwd_text = Label(text="Password*", font='Arial 12') self.pwd_input = Entry(show="*", font='Arial 12') self.cfm_pwd_text = Label(text="Repeat Password*", font='Arial 12') self.cfm_pwd_input = Entry(show="*", font='Arial 12') self.attempt_login = Button( text="Login", font='Arial 12', command=lambda: self.controller.create_log_page()) self.register = Button(text="Register", font='Arial 12', command=lambda: self.register_validation()) # dispose element on widget self.log_text.grid(row=1, columnspan=2, pady=10) self.log_input.grid(row=2, columnspan=2) self.email_text.grid(row=3, columnspan=2, pady=10) self.email_input.grid(row=4, columnspan=2) self.pwd_text.grid(row=5, columnspan=2) self.pwd_input.grid(row=6, columnspan=2, pady=10) self.cfm_pwd_text.grid(row=7, columnspan=2) self.cfm_pwd_input.grid(row=8, columnspan=2, pady=10) self.attempt_login.grid(row=9, column=0, sticky="e", pady=10, padx=15) self.register.grid(row=9, column=1, sticky="w", pady=10, padx=15)
def __init__(self): self.model = ModelDatabase(Database()) self.command_set = { '1': self.create_user, '2': self.create_project, '3': self.update_user, '4': self.update_project, '5': self.delete_user, '6': self.delete_project, '7': self.show_users, '8': self.show_projects, 'exit': None, 'q': None } self.connect()
def __init__(self, parent, controller): Frame.__init__(self, parent) self.model = ModelDatabase(Database()) self.projects_list = Label(text="Projects", font='Arial 12', borderwidth=0, highlightthickness=0) self.lb_projects = Listbox(font='Arial 12') for numb, project in enumerate(self.model.get_projects()): self.lb_projects.insert(numb, project.name) self.update_project = Button(text="Update project", font='Arial 12') self.create_project = Button( text="New project", font='Arial 12', command=lambda: controller.create_add_project_page()) self.update_project.grid(row=9, column=0, sticky="e", pady=10, padx=15) self.create_project.grid(row=9, column=1, sticky="w", pady=10, padx=15) self.projects_list.grid(row=1, columnspan=2, pady=5) self.lb_projects.grid(row=2, columnspan=2, pady=10)
class SessionTracker: STATE = {} #Stores current state of all signed-in students. def __init__(self): self.logger = logging.getLogger('session') self.db = Database() self.generateSessions( ) #Run through last session logs and generate state information self.eventThread = threading.Thread(target=self.eventLoop, args=(())) self.eventThread.daemon = True self.eventThread.start() def studentScanEvent(self, studentID, eventTime=None): #process student login / logout. returns false if student is not in existing database priorStudent = self.db.isStudentInDatabase( studentID ) #the name "studentPresent" implies "is the student present at the club?" not "is the student included in the database?" changed name to "priorStudent" for clarity if not priorStudent: return False #Exit? if eventTime == None: eventTime = datetime.datetime.now() reTime = self.roundTime(eventTime, TIME_INCREMENTS) if not studentID in self.STATE.keys(): self.rescanStudents() if studentID in self.STATE.keys(): self.db.appendEventLog(studentID, eventTime) self.logger.debug('Scan event %s %s', studentID, eventTime.isoformat()) self.generateSessions() return True def roundTime(self, dt=None, dateDelta=datetime.timedelta(minutes=1)): #Rounds a datetime.datetime object to a datetime.timedelta object roundTo = dateDelta.total_seconds() if dt == None: dt = datetime.datetime.now() seconds = (dt - dt.min).seconds # // is a floor division, not a comment on following line: rounding = (seconds + roundTo / 2) // roundTo * roundTo return dt + datetime.timedelta(0, rounding - seconds, -dt.microsecond) def dateFromISO(self, datestr): #parses a string for an ISO8601 date. returns a datetime object return datetime.datetime.strptime(datestr, "%Y-%m-%dT%H:%M:%S.%f") def getCurrentState(self, studentID): #gets the current state of a student ('signin' | 'signout' | 'timeout') if not studentID in self.STATE.keys(): return None else: return self.STATE[studentID] def rescanStudents(self): #Generates the state of students for the first time for studentID in self.db.listStudents(): if studentID in self.STATE.keys(): continue # Ignore students that already have a state log = self.db.getEventsFor( studentID) # Get the last few scan events for this student sess = self.db.getLatestSessionFor( studentID) # Get the latest Sesssion for this student if len(log ) == 0: #Set STATE timestamp to latest scan event, if any tStamp = None else: tStamp = log[-1]['timestamp'] if sess == None: #Set STATE status to latest session status (scanout | timeout), if any status = None else: status = sess['status'] data = { int(studentID): { 'status': status, #Write the new state to self.STATE 'timestamp': tStamp } } self.STATE.update(data) def generateSessions(self): #Iterates through scan events, and generates Sessions from patterns in the scan events STATE = {} #NOTE: This is different from self.STATE! uuidGroups = [ [s['start_uuid'], s['end_uuid']] for s in self.db.SESSIONS ] # Iterate through existing Sessions, and gather all UUIDs parsedUUIDs = [uuid for sublist in uuidGroups for uuid in sublist] # of already existing Sessions for event in self.db.EVENTS: eventUUID = event['uuid'] if eventUUID in parsedUUIDs: continue #Do not process any events that have already been processed # (UUID exists in another event) registerTimeout = False stID = event['id'] #Get studentID and timestamp of event timestamp = event['timestamp'] sessionLength = 0 if stID in STATE.keys( ): #Student has already been processed (at least second time), we can use previous data sessStatus = STATE[stID]['status'] sessUUID = STATE[stID][ 'uuid'] #get status, UUID, and timestamp of previous event sessTimestamp = STATE[stID]['timestamp'] sessionLength = timestamp - sessTimestamp #Get difference between timestamps (length of session if signout) #NOTE: A Timeout Session is generated Before a scanin event (two generated in one loop) #Student was previously scanned in and session is longer than acceptable: TIMEOUT if sessStatus == 'scanin' and sessionLength.total_seconds( ) >= MAX_SESSION_LENGTH: #TODO: Calculate timestamp of timeout session end in a better fashion. backtimestamp = sessTimestamp.replace( hour=DEF_SESSION_END_HRS, minute=DEF_SESSION_END_MIN, second=DEF_SESSION_END_SEC) registerTimeout = True #event is a timeout (just used for debug purpouses) self.db.createSession( stID, sessTimestamp, backtimestamp, 'timeout', sessUUID, None) #Create a new Session on timeout STATE.update({ stID: { 'status': 'timeout', 'timestamp': backtimestamp } }) #Update state with timeout info #Student was previously scanned in and session is within acceptable size: SCANOUT if sessStatus == 'scanin' and sessionLength.total_seconds( ) <= MAX_SESSION_LENGTH: self.db.createSession( stID, sessTimestamp, timestamp, 'scanout', sessUUID, eventUUID) #Create a new Session on scanout newStatus = 'scanout' #Student was previously scanned out: SCANIN elif sessStatus == 'scanout' or sessStatus == 'timeout': newStatus = 'scanin' else: newStatus = 'scanin' sessionLength = 0 logDebug = not ( stID in self.STATE.keys() and self.STATE[stID]['status'] == newStatus ) #Events are registered but not printed to console (prevents console spam) if registerTimeout: self.logger.info( 'Student %-18s (%s) Forgot to sign out. length: %s', self.db.STUDENTS[stID]['name'], stID, backtimestamp - sessTimestamp) if newStatus == 'scanin' and logDebug: self.logger.info( 'Student %-18s (%s) Scanned In at %s length: %s', self.db.STUDENTS[stID]['name'], stID, timestamp.isoformat(), sessionLength) elif newStatus == 'scanout' and logDebug: self.logger.info( 'Student %-18s (%s) Scanned Out at %s length: %s', self.db.STUDENTS[stID]['name'], stID, timestamp.isoformat(), sessionLength) STATE.update({ stID: { 'status': newStatus, 'timestamp': timestamp, 'uuid': eventUUID } }) #Update state with status and timestamp #Update self.STATE information from newly generate STATE info self.rescanStudents() for sID, data in STATE.iteritems(): if data['status'] == 'scanin': sessTimestamp = data['timestamp'] sessionLength = datetime.datetime.now() - sessTimestamp if sessionLength.total_seconds() >= MAX_SESSION_LENGTH: backtimestamp = sessTimestamp.replace( hour=DEF_SESSION_END_HRS, minute=DEF_SESSION_END_MIN, second=DEF_SESSION_END_SEC) self.db.createSession(sID, sessTimestamp, backtimestamp, 'timeout', data['uuid'], None) STATE.update({ stID: { 'status': 'timeout', 'timestamp': backtimestamp } }) self.logger.debug( 'Student %-18s (%s) Forgot to sign out. length: %s', self.db.STUDENTS[sID]['name'], sID, backtimestamp - sessTimestamp) if sID in self.STATE.keys(): self.STATE[sID].update({ 'status': data['status'], 'timestamp': data['timestamp'] }) def eventLoop(self): while True: time.sleep(60 * 5) self.generateSessions()
if user_name == user.login: self.data['users'].remove(user) self.db.update_data(self.data) @update_user_validation def update_user(self, user_name, property): if property[0] == "login": for project in self.data['projects']: if user_name in project.members: project.remove_member(user_name) project.add_member(property[1]) if user_name == project.author: project.author = property[1] for user in self.data['users']: if user_name == user.login: user.update_property(*property) self.db.update_data(self.data) if __name__ == "__main__": a = ModelDatabase(Database()) user = User('*****@*****.**', 'masha', '789') print(a.get_users()) a.update_user('tom', ('email', '*****@*****.**')) print(a.get_users())