def get(self): """ Поиск по: имени (name) """ employees = [] if 'name' in self.request.params: employees = EmployeeCtrl.find_by_like_name( self.request.params['name']) else: employees = EmployeeCtrl.find_by_like_name('') return self.serializator.dump(employees, many=True).data
def generate_question(employee_name): """ Получение след.вопроса в зависимости от плана и уже отвеченных вопросов """ null_question = Question(n=0) today = date.today() year = today.year month = today.month employee = EmployeeCtrl.find_by_name(employee_name) plan = PlanCtrl.get_current_plan(employee_name, year, month) qty_answered = ResultCtrl.count_answered(employee, year, month) # уже отвечено if plan.qty_question > qty_answered: qty_day = plan.qty_question // plan.qty_work + 1 # к-во вопросов в день # print('qty_day = %s' % (qty_day,)) # print(plan) params = {'employee_n': employee.n, 'date': today} results = ResultCtrl.find(params) # отвечено сегодня # print('results = %s' % (results,)) if len(results) < qty_day: # если план на день еще не выполнен subquery = Session.query( Result.question_n).filter(Result.employee_n == employee.n) q = Session.query(Question).filter( not_(Question.n.in_(subquery))) questions = q.all() # print(questions) if len(questions) == 0 or questions is None: return QuestionCtrl.get_repeat(employee) return questions[0] else: return null_question else: # План по вопросам сделан return null_question
def wrapper(self, *args, **kwargs): # Добавляю в params имя пользователя if get_employee_name(self.request) != '': js = json.loads(text_(self.request.body, self.request.charset)) js['employee_name'] = get_employee_name(self.request) js['employee_n'] = EmployeeCtrl.find_by_name(js['employee_name']).n self.request.body = str.encode(json.dumps(js)) return func(self, *args, **kwargs)
def update(self): params = self.request.json_body employee = EmployeeCtrl.update( self.context.n, params['name'], '', 'password' in params and params['password'] or '', 'employee_group' in params and params['employee_group']['n'] or '1', 'department' in params and params['department']['n'] or '1') return self.serializator.dump(employee).data
def groupfinder_in_db(name, request): """Для реального применения""" # print('----name=%s' % name) # logging.getLogger(__name__).warning('----name=%s' % (name,)) employee = EmployeeCtrl.find_by_name(name) rules = [] for r in employee.employee_group.rights: rules.append(r.rule) # print(rules) return rules
def test_create(self): employee = EmployeeCtrl.create(name='NAME1', password='******', info='INFO1', employee_group_n=1, department_n=1) self.assertEqual(employee.n, 3) self.assertEqual(employee.name, 'NAME1') self.assertEqual(employee.password, 'PASSWORD1') self.assertEqual(employee.info, 'INFO1')
def test_add_right_to_employee_group(self): employee_group = EmployeeCtrl.add_right_to_employee_group( 1, 'doc', 'edit') self.assertEqual(employee_group.n, 1) self.assertEqual(len(employee_group.rights), 2) self.assertEqual(employee_group.rights[0].section, 'doc') self.assertEqual(employee_group.rights[0].access, 'edit') self.assertEqual(employee_group.rights[1].section, 'question') self.assertEqual(employee_group.rights[1].access, 'edit')
def create_on_month(year, month): """ Создание планов на месяц для всех работающих сотрудников """ ret = [] employees = EmployeeCtrl.find_all_worked() # print(len(employees)) for employee in employees: plan = PlanCtrl.create(employee.n, year, month) ret.append(plan) return ret
def exam(question_n, answer_n, employee_name, time_begin, time_end): """ Проверка ответа на вопрос и сохранение результата тестирования """ result = Result() employee = EmployeeCtrl.find_by_name(employee_name) result.employee_n = employee.n result.question_n = question_n result.answer_n = answer_n result.time_begin=time_begin result.time_end=time_end question = Question.get(question_n) result.is_correct = (question.answer_n == int(answer_n)) Session.add(result) Session.flush() return result
def get_next(employee_name): """ Получение следующего вопроса для сотрудника """ null_question = Question(n=0) employee = EmployeeCtrl.find_by_name(employee_name) today_results = Session.query(Result).filter( Result.ddate == date.today()).all() if len(today_results) > 0: return null_question subquery = Session.query( Result.question_n).filter(Result.employee_n == employee.n) q = Session.query(Question).filter(not_(Question.n.in_(subquery))) questions = q.all() if len(questions) == 0: return null_question return questions[0]
def post(self): name = self.request.json_body.get('name') password = self.request.json_body.get('password') # print('----name=%s' % name) # print('----password=%s' % password) try: employee = EmployeeCtrl.find_by_name(name) # print('----login=%s' % login) # print('----name=%s' % name) # print('----password=%s' % employee.password) # if employee.password == crypt(password, 'secret service'): if employee.password == password: policy = self.request.registry.queryUtility(IAuthenticationPolicy) token = policy.encode_jwt( self.request, claims={'sub': name, 'name': employee.name}) return { 'token': 'JWT token="' + token.decode('utf-8') + '"', 'employee': EmployeeJSON().dump(employee).data } else: return exc.HTTPForbidden() except Exception: return exc.HTTPForbidden()
def test_find_by_name(self): employee = EmployeeCtrl.find_by_name('NAME_1') self.assertEqual(employee.n, 1) self.assertEqual(employee.name, 'NAME_1') self.assertEqual(employee.employee_group.name, 'admins') self.assertEqual(len(employee.employee_group.rights), 1)
def test_find_all_worked(self): employees = EmployeeCtrl.find_all_worked() assert len(employees) == 2
def test_delete_right(self): employee_group = EmployeeCtrl.delete_right(1) self.assertEqual(employee_group.n, 1) self.assertEqual(employee_group.name, 'admins') self.assertEqual(len(employee_group.rights), 0)
def test_find_by_like_name(self): employees = EmployeeCtrl.find_by_like_name('N') assert len(employees) > 0
def test_create_employee_group(self): employee_group = EmployeeCtrl.create_employee_group(name='NAME1') self.assertEqual(employee_group.n, 2) self.assertEqual(employee_group.name, 'NAME1')
def test_find_by_name_exception(self): with pytest.raises(Exception) as e: EmployeeCtrl.find_by_name('NAME1') assert 'Employee not found' in str(e.value)