def run(self): #Return All the users in the User table company_id = self.company_id log = Logger('RolesPostHandler') tag = 'run' request_body = json.loads(self.request.request.body) self.user_role = request_body.get('name') if self.user_role is None or self.user_role == '': log.e(tag, 'sent Role is either None or blank') opJson = json.dumps({'pass': False, 'error': 'sent Role is either None or blank'}) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback(self.callback) role = RoleDBHelper() insert_dict = { c.ROLE_TABLE_NAME: self.user_role, c.ROLE_TABLE_COMPANY: company_id, c.ROLE_TABLE_DELETED: False } self.user_role_id = role.add_role(insert_dict) if self.user_role_id is None: log.e(tag,'Role not inserted in table') opJson = json.dumps({'pass': False, 'error': 'Role not inserted in table'}) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback(self.callback) else: opJson = json.dumps({'pass': True, 'role_id': self.user_role_id}) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback(self.callback)
def run(self): # Return All the users in the User table company_id = self.company_id log = Logger('RolesPostHandler') tag = 'run' request_body = json.loads(self.request.request.body) self.user_role = request_body.get('name') if self.user_role is None or self.user_role == '': log.e(tag, 'sent Role is either None or blank') opJson = json.dumps({'pass': False, 'error': 'sent Role is either None or blank'}) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback(self.callback) role = RoleDBHelper() insert_dict = { c.ROLE_TABLE_NAME: self.user_role, c.ROLE_TABLE_COMPANY: company_id, c.ROLE_TABLE_DELETED: False } self.user_role_id = role.add_role(insert_dict) if self.user_role_id is None: log.e(tag, 'Role not inserted in table') opJson = json.dumps({'pass': False, 'error': 'Role not inserted in table'}) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback(self.callback) else: opJson = json.dumps({'pass': True, 'role_id': self.user_role_id}) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback(self.callback)
def parse(self, excel_path, company, company_name=None, callback=None, user_added_callback=None): COMPANY = company company_name = company_name book = open_workbook(filename=excel_path) sheet = book.sheet_by_index(0) users = list() for row_index in range(sheet.nrows): user = dict() for col_index in range(sheet.ncols): cell = sheet.cell(row_index, col_index).value if col_index == 0: user[C.USER_TABLE_EMAIL] = cell elif col_index == 1: user[C.USER_TABLE_NAME] = cell elif col_index == 2: user[C.USER_TABLE_TEAM] = cell elif col_index == 3: user[C.USER_TABLE_ROLE] = cell users.append(user) roles_list = list() teams_list = list() for user in users: if C.USER_TABLE_ROLE in user: roles_list.append(user[C.USER_TABLE_ROLE]) if C.USER_TABLE_TEAM in user: teams_list.append(user[C.USER_TABLE_TEAM]) # get a set to get the unique elements. roles = dict() teams = dict() roles_helper = RoleDBHelper() teams_helper = TeamDBHelper() users_helper = UserDBHelper() enrollment_helper = EnrollmentDBHelper() for role_name in set(roles_list): role = dict() role[C.ROLE_TABLE_COMPANY] = COMPANY role[C.ROLE_TABLE_NAME] = role_name roles[role_name] = roles_helper.add_role(role) for team_name in set(teams_list): team = dict() team[C.TEAM_TABLE_COMPANY] = COMPANY team[C.TEAM_TABLE_NAME] = team_name team[C.TEAM_TABLE_DELETED] = False teams[team_name] = teams_helper.add_team(team) # Now we have id for all teams and roles... Insert the users now. for user in users: user_obj = dict() user_obj[C.USER_TABLE_COMPANY] = COMPANY user_obj[C.USER_TABLE_EMAIL] = user[C.USER_TABLE_EMAIL] user_obj[C.USER_TABLE_NAME] = user[C.USER_TABLE_NAME] user_obj[C.USER_TABLE_ROLE] = roles[user[C.USER_TABLE_ROLE]] user_obj[C.USER_TABLE_TEAM] = teams[user[C.USER_TABLE_TEAM]] user['id'] = users_helper.add_user_if_not_exists(user_obj)[0] if user_added_callback is not None: user_added_callback(user_obj) for user in users: enrollment = dict() enrollment[C.ENROLLMENT_TABLE_USER] = user['id'] enrollment[C.ENROLLMENT_TABLE_PASSWORD] = generate_password() enrollment[C.ENROLLMENT_TABLE_IS_ENROLLED] = False user['passcode'] = enrollment[C.ENROLLMENT_TABLE_PASSWORD] print 'adding enrollment ', enrollment user['enrollment_id'] = enrollment_helper.add_enrollment( enrollment) user['company_name'] = company_name if callback is not None: callback(users) return users