def test_remove_user_from_company(self): company_id = self.create_company() user_id = self.create_user(nickname="test_user") administrator = self.api.get_user(user_id=1) user = self.api.get_user(user_id=user_id) self.api.add_user_to_company(administrator.nickname, user.nickname, company_id) self.assertEqual(user.role, Role(1)) self.api.administrator(user.nickname, user_id) self.assertEqual(user.role, Role(1)) self.api.administrator(administrator.nickname, user_id) self.assertEqual(user.role, Role(2)) self.api.remove_user_from_company(administrator.nickname, user_id, user_id) user_count = len(self.api.get_users(company_id=company_id)) self.assertNotEqual(user.company_id, company_id) self.assertEqual(administrator.company_id, company_id) self.assertEqual(user_count, 1) self.assertEqual(administrator.role, Role(2)) self.assertEqual(user.role, Role(1)) log = len(self.api.get_logs(company_id)) self.assertEqual(log, 3)
def change_permission(self, admin, user_id, role): if admin.role == Role(4): user = self.get_user(user_id) if role == 1: company = self.get_company(nickname=user.nickname) user.role = Role(2) if company else Role(1) else: user.role = Role(role) self._session.commit()
def test_administrator(self): company_id = self.create_company() user_id = self.create_user(nickname="test_user") administrator = self.api.get_user(user_id=1) user = self.api.get_user(user_id=user_id) self.api.add_user_to_company(administrator.nickname, user.nickname, company_id) self.assertEqual(user.role, Role(1)) self.api.administrator(administrator.nickname, user_id) self.assertEqual(user.role, Role(2)) self.assertEqual(user.company_id, company_id)
def contacts(request, company_id): api = get_api() owner = request.user.username user = api.get_user(nickname=owner) company = api.get_company(company_id=company_id) all_administrators = api.get_users(company_id=company_id, role=Role(4)) all_administrators.extend( api.get_users(company_id=company_id, role=Role(3))) all_administrators.extend( api.get_users(company_id=company_id, role=Role(2))) administrators = [] for administrator in all_administrators: administrators.append( (administrator, _get_avatar(administrator.avatar))) all_employees = api.get_users(company_id=company_id, role=Role(1)) employees = [] for employee in all_employees: employees.append((employee, _get_avatar(employee.avatar))) is_administrator = api.is_administrator(user.id, company_id) is_employee = api.is_employee(user.id, company_id) check = request.GET.get("login") == owner show = not check or company != None is_my_company = True if user.company_id == company.id else False return render( request, 'trans/contacts.html', { 'company': company, 'is_my_company': is_my_company, 'administrators': administrators, 'is_administrator': is_administrator, 'is_employee': is_employee, 'employees': employees, 'show': show, 'nickname': user.nickname, 'my_company': is_add_car(user) })
def administrator(self, administrator_nickname, user_id): administrator = self.get_user(nickname=administrator_nickname) user = self.get_user(user_id=user_id) if administrator.role.value > 1 and administrator.company_id == user.company_id: if user.role == Role(1): user.role = Role(2) text_log = RAISING_TO_ADMINISTRATOR % user.nickname self.create_log(administrator.company_id, administrator.nickname, text_log) else: user.role = Role(1) text_log = LOWERING_FROM_ADMINISTRATOR % user.nickname self.create_log(administrator.company_id, administrator.nickname, text_log) self._add(user)
def create_user(self, nickname, name, surname, email, phone, avatar_path="../sources/default.txt"): user = User(nickname, name, surname, email, phone, None, Role(1), avatar_path) self._add(user) return user.id
def create_company(self, nickname, UNP, name, primary_occupation, license, country, town, address, phone, description): company = Company(nickname, UNP, name, primary_occupation, license, country, town, address, phone, description) self._add(company) user = self.get_user(nickname=nickname) user.company_id = company.id user.role = Role(2) self._session.commit() return company.id
def test_edit_user(self): user_id = self.create_user() user = self.api.get_user(user_id=user_id) self.assertEqual(user.nickname, TEST_USER_NICKNAME) self.api.edit_user(TEST_USER_NICKNAME, name="test", surname="test", email="*****@*****.**", phone="+375292123214") self.assertEqual(user.nickname, TEST_USER_NICKNAME) self.assertEqual(user.name, "test") self.assertEqual(user.surname, "test") self.assertEqual(user.email, "*****@*****.**") self.assertEqual(user.phone, "+375292123214") self.assertEqual(user.company_id, None) self.assertEqual(user.role, Role(1))
def test_create_user(self): user_id = self.create_user() user_count = len(self.api.get_users()) self.assertEqual(user_count, 1) test_user = [ self.api.get_user(user_id=user_id), self.api.get_user(nickname=TEST_USER_NICKNAME) ] for user in test_user: self.assertIsInstance(user, User) self.assertEqual(user.nickname, TEST_USER_NICKNAME) self.assertEqual(user.name, TEST_USER_NAME) self.assertEqual(user.surname, TEST_USER_SURNAME) self.assertEqual(user.email, TEST_USER_EMAIL) self.assertEqual(user.phone, TEST_PHONE) self.assertEqual(user.company_id, None) self.assertEqual(user.role, Role(1))
def car_info(request, company_id, car_id): api = get_api() owner = request.user.username user = api.get_user(nickname=owner) company = api.get_company(company_id=company_id) all_administrators = api.get_users(company_id=company_id, role=Role(2)) administrators = [] is_administrator = api.is_administrator(user.id, company_id) is_employee = api.is_employee(user.id, company_id) check = request.GET.get("login") == owner show = not check or company != None is_my_company = True if user.company_id == company.id else False car = api.get_car(car_id) date = car.get_date() body_type = _get_body_type(car.body_type) download_type = DOWNLOAD_TYPE[int(car.download_type)] car_info = [ body_type + ", " + download_type, str(car.carrying_capacity) + " т., " + str(car.volume) + " м³" ] price = [ str(car.rate) + " " + PRICES[int(car.price)], FORM_PRICES[int(car.form_price)] ] requests = [] all_request = api.get_requests(car_id=car.id) for _request in all_request: user_request = api.get_user(_request.user_id) avatar = _get_avatar(user_request.avatar) date_create = _request.date_create.strftime("%d.%m.%Y, %H:%M") request_date = None if _request.date: request_date = _request.date.strftime("%d.%m.%Y, %H:%M") requests.append( (_request, user_request, avatar, date_create, request_date)) return render( request, 'trans/car_info.html', { 'company': company, 'is_my_company': is_my_company, 'requests': requests, 'car': car, 'date': date, 'car_info': car_info, 'prices': price, 'is_administrator': is_administrator, 'is_employee': is_employee, 'show': show, 'nickname': user.nickname, 'my_company': is_add_car(user) })
def is_moder(self, user): return True if user.role == Role(3) else False
def is_admin(self, user): return True if user.role == Role(4) else False
def is_administrator(self, user_id, company_id): user = self.get_user(user_id) return True if ( user.company_id == int(company_id) and user.role == Role(2) ) or user.role == Role(3) or user.role == Role(4) else False