def check_tasks_csv(csv_file, organizer): oid = organizer.org.oid task_list = [] input_fields = [ 'title', 'duration', 'address', 'manager_id', 'image_required', 'collect_demand', 'other_fields' ] flag, csv_dict = check_csv_fields(csv_file, input_fields, ';') for row in csv_dict: if row is None: break try: title = row['title'] duration = int(row['duration']) address = row['address'] manager = clean_whs(row['manager_id']) if len(manager) < 1: err_msg = 'Manager not provided!' raise NotAcceptable(detail=err_msg) manager_qs = User.objects.filter( Q(username=create_username(oid, manager))) if not manager_qs.exists(): err_msg = 'Manager: ' + manager + ' not found!' raise NotAcceptable(detail=err_msg) manager = manager_qs[0] image_req = bool(int(row['image_required'])) demand = bool(int(row['collect_demand'])) other_fields = str(row['other_fields']).split(',') except Exception as e: err_msg = str(e) raise NotAcceptable(detail=err_msg) task_list.append({ 'title': title, 'duration': duration, 'address': address, 'manager': manager, 'agents': [], 'image_req': image_req, 'demand': demand, 'other_fields': other_fields, }) # print(task_list) new_task = len(task_list) task_limit = organizer.org.subscription.task_limit consumed_tasks = organizer.org.subscription.current_usage.consumed_tasks if new_task + consumed_tasks > task_limit: diff = (new_task + consumed_tasks) - task_limit err_msg = 'Task limit exceeded! Please upgrade for another ' + str( diff) + ' tasks!' raise NotAcceptable(detail=err_msg) return task_list
def create_teams_sync(user_list, organizer): oid = organizer.org.oid new_agents = 0 for user_dict in user_list: cur_dict = user_dict.copy() cur_dict.pop('parent') if not check_username_exists(oid, cur_dict['username']): cur_dict['username'] = create_username(oid, cur_dict['username']) user = User(**cur_dict) user.org = organizer.org user.is_active = True user.save() userstate = UserState.objects.create(user=user) new_agents += 1 else: username = create_username(oid, cur_dict['username']) cur_dict.pop('username') User.objects.filter(Q(username=username)).update(**cur_dict) for user_dict in user_list: # print(user_dict) if len(user_dict['parent']) > 0: manager_qs = User.objects.filter( username=create_username(oid, user_dict['parent'])) if manager_qs.exists(): parent = manager_qs[0] user = User.objects.get( username=create_username(oid, user_dict['username'])) try: user.parent = parent user.save() except Exception as e: raise NotAcceptable(detail=str(e)) else: err_msg = 'Manager : ' + user_dict['parent'] + ' not found!' raise NotAcceptable(detail=err_msg) organizer.org.subscription.added_agents += new_agents organizer.org.subscription.save() return True
def create_organizer(username, oid='org1'): org = create_org(oid) organizer = User( username=create_username(oid, username), is_active=True, role=ROLE_DICT['Organizer'], org=org, full_name='Organizer', ) organizer.set_unusable_password() organizer.save() state = UserState.objects.create(user=organizer) return organizer
def create_agent(username, parent, is_present=True): oid = parent.org.oid org = parent.org agent = User( username=create_username(oid, username), is_active=True, org=org, role=ROLE_DICT['Employee'], parent=parent, is_present=is_present, full_name='Agent', ) agent.set_unusable_password() agent.save() org.subscription.added_agents += 1 org.subscription.save() state = UserState.objects.create(user=agent) return agent
def create_manager(username, parent, org): manager = User( username=create_username(org.oid, username), is_active=True, role=ROLE_DICT['Manager'], org=org, parent=parent, full_name='Manager', ) manager.set_unusable_password() manager.save() manager.org.subscription.added_agents += 1 manager.org.subscription.save() state = UserState.objects.create(user=manager) return manager
def test_edit_account_success(self): manager = create_manager(self.username1, self.organizer1, self.organizer1.org) agent = create_agent(self.username3, manager) client.force_authenticate(self.organizer1) response = client.patch( user_prefix + USER_ULRS['account_id'].replace('<int:pk>', str(agent.id)), { 'username': self.username4, 'full_name': 'agent1', 'parent': manager.id, 'role': ROLE_DICT['Employee'], }, format='json', ) # print(response.data) self.assertEqual(response.status_code, status.HTTP_200_OK) agent = User.objects.get(id=agent.id) self.assertEqual(agent.username, create_username(agent.org.oid, self.username4))
def test_edit_profile_success(self): manager1 = create_manager(self.username1, self.organizer1, self.organizer1.org) client.force_authenticate(user=manager1) response = client.patch( user_prefix + USER_ULRS['profile_id'].replace('<int:pk>', str(manager1.id)), { 'full_name': 'Manager1', 'phone': '+8801788000000', 'email': '*****@*****.**', 'designation': 'AGM Dhaka Zone', }, format='json', ) # print(response.data) self.assertEqual(response.status_code, status.HTTP_200_OK) response = client.patch( user_prefix + USER_ULRS['profile_id'].replace('<int:pk>', str(manager1.id)), { 'full_name': 'Manager1', 'username': self.username10, 'phone': '+8801788000000', 'email': '*****@*****.**', 'designation': 'AGM Dhaka Zone', }, format='json', ) # print(response.data) self.assertEqual(response.status_code, status.HTTP_200_OK) manager1.refresh_from_db() self.assertEqual(manager1.username, create_username(manager1.org.oid, self.username10))
def check_tasks_excel(excel_file, user): oid = user.org.oid task_list = [] worksheet = openpyxl.load_workbook(excel_file)['Sheet1'] for row in worksheet.iter_rows(min_row=2): if row is None: break try: title = strip_whitespace(row[0]) duration = int(row[1].value) address = strip_whitespace(row[2]) manager_id = clean_whitespace(row[3]) if len(manager_id) < 1: err_msg = 'Manager not provided!' raise NotAcceptable(detail=err_msg) manager_qs = User.objects.filter( Q(username=create_username(oid, manager_id))) if not manager_qs.exists(): err_msg = 'Manager: ' + manager_id + ' not found!' raise NotAcceptable(detail=err_msg) manager_obj = manager_qs[0] agents_str = clean_whitespace(row[4]).split(',') agent_usernames = list(filter(None, agents_str)) agent_objs = [] for username in agent_usernames: agent_qs = User.objects.filter( Q(username=create_username(oid, username))) if not agent_qs.exists(): err_msg = 'Agent: ' + username + ' not found!' raise NotAcceptable(detail=err_msg) agent = agent_qs[0] if agent.parent != manager_obj: err_msg = 'Agent: ' + username + ' is not related to manager: ' + manager_id + '!' raise NotAcceptable(detail=err_msg) agent_objs.append(agent_qs[0]) image_req = bool(int(row[5].value)) demand = bool(int(row[6].value)) other_fields = str(row[7].value).split(';') except Exception as e: err_msg = str(e) raise NotAcceptable(detail=err_msg) task_list.append({ 'title': title, 'duration': duration, 'address': address, 'manager': manager_obj, 'agents': agent_objs, 'image_req': image_req, 'demand': demand, 'other_fields': other_fields, }) # print(task_list) new_task = len(task_list) task_limit = user.org.subscription.task_limit consumed_tasks = user.org.subscription.current_usage.consumed_tasks if new_task + consumed_tasks > task_limit: diff = (new_task + consumed_tasks) - task_limit err_msg = 'Task limit exceeded! Please upgrade for another ' + str( diff) + ' tasks!' raise NotAcceptable(detail=err_msg) return task_list
def check_team_hierarchy_csv(csv_file, organizer): oid = organizer.org.oid roles = ['Manager', 'Employee'] user_graph = Graph() user_list = [] new_users = [] input_fields = [ 'employee_id', 'name', 'phone', 'email', 'designation', 'role', 'manager_id' ] flag, csv_dict = check_csv_fields(csv_file, input_fields, ';') if not flag: err_msg = 'Invalid csv file!' raise NotAcceptable(detail=err_msg) for row in csv_dict: user_handle = clean_whs(row['employee_id']) if len(user_handle) < 1: err_msg = 'Empty employee id!' raise NotAcceptable(detail=err_msg) if not user_handle.isalnum(): err_msg = 'Invalid employee id: <' + user_handle + '>' raise NotAcceptable(detail=err_msg) name = row['name'] phone = row['phone'] email = row['email'] designation = row['designation'] role = row['role'] if role not in roles: err_msg = 'Invalid user role: ' + role raise NotAcceptable(detail=err_msg) manager = clean_whs(row['manager_id']) if len(manager) > 0: if not manager.isalnum(): err_msg = 'Invalid manager id: <' + manager + '>' raise NotAcceptable(detail=err_msg) user_graph.addEdge(manager, user_handle) full_username = create_username(oid, user_handle) user_qs = User.objects.filter(Q(username=full_username)) if not user_qs.exists(): new_users.append(full_username) user_list.append({ 'username': user_handle, 'full_name': name, 'phone': phone, 'email': email, 'designation': designation, 'role': ROLE_DICT[role], 'parent': manager, }) if user_graph.isCyclic(): # print(user_graph.graph) err_msg = 'Invalid user hierarchy!' raise NotAcceptable(detail=err_msg) added = organizer.org.subscription.added_agents new_agent = len(new_users) limit = organizer.org.subscription.agent_limit if new_agent + added > limit: diff = (new_agent + added) - limit err_msg = 'Employee limit exceeded! Please purchase another ' + str( diff) + ' accounts!' raise NotAcceptable(detail=err_msg) # print(user_list) if len(user_list) < 1: err_msg = 'Invalid excel file!' raise NotAcceptable(detail=err_msg) return user_list
def check_team_hierarchy_excel(excel_file, organizer): oid = organizer.org.oid roles = ['Manager', 'Employee'] user_graph = Graph() user_list = [] new_users = [] worksheet = openpyxl.load_workbook(excel_file)['Sheet1'] for row in worksheet.iter_rows(min_row=2): if row is None: break # print(row) user_handle = clean_whitespace(row[0]) if len(user_handle) < 1: err_msg = 'Empty employee id!' raise NotAcceptable(detail=err_msg) if not user_handle.isalnum(): err_msg = 'Invalid employee id: <' + user_handle + '>' raise NotAcceptable(detail=err_msg) name = row[1].value phone = row[2].value email = row[3].value designation = row[4].value role = row[5].value if role not in roles: err_msg = 'Invalid user role: ' + role raise NotAcceptable(detail=err_msg) manager = clean_whitespace(row[6]) if len(manager) > 0: if not manager.isalnum(): err_msg = 'Invalid manager id: <' + manager + '>' raise NotAcceptable(detail=err_msg) user_graph.addEdge(manager, user_handle) full_username = create_username(oid, user_handle) user_qs = User.objects.filter(Q(username=full_username)) if not user_qs.exists(): new_users.append(full_username) user_list.append({ 'username': user_handle, 'full_name': name, 'phone': phone, 'email': email, 'designation': designation, 'role': ROLE_DICT[role], 'parent': manager, }) if user_graph.isCyclic(): # print(user_graph.graph) err_msg = 'Invalid user hierarchy!' raise NotAcceptable(detail=err_msg) added = organizer.org.subscription.added_agents new_agent = len(new_users) limit = organizer.org.subscription.agent_limit if new_agent + added > limit: diff = (new_agent + added) - limit err_msg = 'Employee limit exceeded! Please purchase another ' + str( diff) + ' accounts!' raise NotAcceptable(detail=err_msg) # print(user_list) if len(user_list) < 1: err_msg = 'Invalid excel file!' raise NotAcceptable(detail=err_msg) return user_list