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
def run(self): #Find all the POST arguments required company_id = self.company_id company_name = str(self.company_name) loader = Loader("/opt/toppatch/mv/media/app/") thread1 = {} log = Logger('EnrollDevice') tag = 'POST' request_body = json.loads(self.request.request.body) try: self.user_email = str(request_body.get('user_email', None)) self.user_name = str(request_body.get('user_name', None)) self.team_id = str(request_body.get('team_id', None)) self.role_id = str(request_body.get('role_id', None)) self.company_id = str(company_id) except: self.request.write('Some arguments are not supplied') opJson = json.dumps({'pass':False, 'user_name': None, 'link' : None, 'password':None, 'error':'Some argument not supplied'}) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback(self.callback) log.e(tag, 'Some arguments not sent in POST request for enrollment') return if self.user_email is None or self.user_name is None or\ self.company_id is None: log.e(tag, 'Email or user_name is NULL') opJson = json.dumps({'pass':False, 'user_name': None, 'link' : None, 'password':None, 'error':'user_name or Email or Company is None'}) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback(self.callback) if self.user_name == '' or self.user_email == '': log.e(tag, 'Email or user_name is empty') opJson = json.dumps({'pass':False, 'user_name': None, 'link' : None, 'password':None, 'error':'email or user_name or company is empty'}) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback(self.callback) password = generate_password() user = UserDBHelper() user_dict = { c.USER_TABLE_NAME: str(self.user_name), c.USER_TABLE_TEAM : str(self.team_id), c.USER_TABLE_ROLE: str(self.role_id), c.USER_TABLE_EMAIL: str(self.user_email), c.USER_TABLE_COMPANY: str(self.company_id) } user_id, duplicate = user.add_user_if_not_exists(user_dict) if duplicate: log.e(tag,'No id from primary key ') opJson = json.dumps({'pass':False, 'user_name': self.user_name, 'link' : None, 'password':None, 'duplicate': True, 'error':'DB has problem'}) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback(self.callback) else: enrollment_dict = { c.ENROLLMENT_TABLE_USER: user_id, c.ENROLLMENT_TABLE_PASSWORD: password, c.ENROLLMENT_TABLE_IS_ENROLLED: False } enrollment = EnrollmentDBHelper() enrollment_id = str(enrollment.add_enrollment(enrollment_dict)) enrollment_dict = enrollment.get_enrollment(enrollment_id) if enrollment_id is not None: self.link += enrollment_id try: message = loader.load('user_enroll_mail.html').generate( company_name=self.company_name, user_passwd=password, activation_link=self.link) ses_conn.send_email('*****@*****.**', 'MDM Enrollment verification', message, [self.user_email], format='html') print 'No error found' log.i(tag, 'Enrollment request successful') opJson = json.dumps({'pass':True, 'user_name': self.user_name, 'link' : 'link', 'password':'******', 'error':None}) except Exception, err: print 'Mail Sending error exception is :', repr(err) log.e(tag, 'Incorrect EmailID sent') opJson = json.dumps({'pass':False, 'user_name': self.user_name, 'link' : None, 'password':None, 'error':'Wrong emailID'}) else:
def run(self): # Find all the POST arguments required company_id = self.company_id # company_name = str(self.company_name) loader = Loader("/opt/toppatch/mv/media/app/") # thread1 = {} log = Logger('EnrollDevice') tag = 'POST' request_body = json.loads(self.request.request.body) try: self.user_email = str(request_body.get('user_email', None)) self.user_name = str(request_body.get('user_name', None)) self.team_id = str(request_body.get('team_id', None)) self.role_id = str(request_body.get('role_id', None)) self.company_id = str(company_id) except: self.request.write('Some arguments are not supplied') opJson = json.dumps({ 'pass': False, 'user_name': None, 'link': None, 'password': None, 'error': 'Some argument not supplied' }) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback(self.callback) log.e(tag, 'Some arguments not sent in POST request for enrollment') return if self.user_email is None or self.user_name is None or\ self.company_id is None: log.e(tag, 'Email or user_name is NULL') opJson = json.dumps({ 'pass': False, 'user_name': None, 'link': None, 'password': None, 'error': 'user_name or Email or Company \ is None' }) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback(self.callback) if self.user_name == '' or self.user_email == '': log.e(tag, 'Email or user_name is empty') opJson = json.dumps({ 'pass': False, 'user_name': None, 'link': None, 'password': None, 'error': 'email or user_name or company \ is empty' }) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback(self.callback) password = generate_password() user = UserDBHelper() user_dict = { c.USER_TABLE_NAME: str(self.user_name), c.USER_TABLE_TEAM: str(self.team_id), c.USER_TABLE_ROLE: str(self.role_id), c.USER_TABLE_EMAIL: str(self.user_email), c.USER_TABLE_COMPANY: str(self.company_id) } user_id, duplicate = user.add_user_if_not_exists(user_dict) if duplicate: log.e(tag, 'No id from primary key ') opJson = json.dumps({ 'pass': False, 'user_name': self.user_name, 'link': None, 'password': None, 'duplicate': True, 'error': 'DB has problem' }) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback(self.callback) else: enrollment_dict = { c.ENROLLMENT_TABLE_USER: user_id, c.ENROLLMENT_TABLE_PASSWORD: password, c.ENROLLMENT_TABLE_IS_ENROLLED: False } enrollment = EnrollmentDBHelper() enrollment_id = str(enrollment.add_enrollment(enrollment_dict)) enrollment_dict = enrollment.get_enrollment(enrollment_id) if enrollment_id is not None: self.link += enrollment_id try: message = loader.load('user_enroll_mail.html').generate( company_name=self.company_name, user_passwd=password, activation_link=self.link) ses_conn.send_email('*****@*****.**', 'MDM Enrollment verification', message, [self.user_email], format='html') print 'No error found' log.i(tag, 'Enrollment request successful') opJson = json.dumps({ 'pass': True, 'user_name': self.user_name, 'link': 'link', 'password': '******', 'error': None }) except Exception as err: print 'Mail Sending error exception is :', repr(err) log.e(tag, 'Incorrect EmailID sent') opJson = json.dumps({ 'pass': False, 'user_name': self.user_name, 'link': None, 'password': None, 'error': 'Wrong emailID' }) else: log.e( tag, 'Entry is not done in Enrollment table for UserID = ' + str(user_id)) opJson = json.dumps({ 'pass': False, 'user_name': self.user_name, 'link': None, 'password': None, 'error': 'Wrong emailID' }) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback(self.callback)