def setUp(self): self.other_user = AccountFactory.create() self.other_user_post = PostFactory.create(owner=self.other_user, publisher=self.other_user) self.account = AccountFactory.create() self.user = self.account.user [ PostFactory.create(owner=self.account, publisher=self.account) for _ in range(2) ]
def setUp(self): account0 = AccountFactory.create(user__first_name='Search 0') account1 = AccountFactory.create(user__first_name='Search 1') account2 = AccountFactory.create(user__first_name='Search 2') RelationshipFactory.create(owner=account0, follow=account1) RelationshipFactory.create(owner=account1, follow=account2) [PostFactory.create(owner=account0, publisher=account0, title='Search A', tags='search') for _ in range(2)] [PostFactory.create(owner=account1, publisher=account1, title='Search B', tags='search') for _ in range(2)] [PostFactory.create(owner=account2, publisher=account2, title='Search C', tags='search') for _ in range(2)] [PostFactory.create(title='Search D', tags='search') for _ in range(2)] self.user = account0.user
def setUp(self): new_user = AccountFactory.build().user self.account = AccountFactory.create() self.user = self.account.user self.form_login = { 'identity': self.user.username, 'password': DEFAULT_PASSWORD, } self.form_user = { 'first_name': new_user.first_name, 'last_name': new_user.last_name, 'username': new_user.username, 'email': new_user.email, 'password': DEFAULT_PASSWORD, }
def login(request): # 不接受POST请求 if request.method != 'GET': raise Http404() account = AccountFactory.getAccountObj() return account.login(request)
def test_follow_other_user(self): user2 = AccountFactory.create().user self.client.login(username=self.user.username, password=DEFAULT_PASSWORD) response = self.client.post( reverse('account_follow', args=[user2.username])) self.assertRedirects(response, reverse('profile', args=[user2.username]))
def authenticate(self, ticket=None, request=None): account = AccountFactory.getAccountObj() status, user = account.check_backend_login_status(request) if not status or not user: return None return user
def process_response(self, request, response): """ 统一处理response请求 """ account = AccountFactory.getAccountObj() # 统一处理 response, 包括 cookie、session 等 response = account.handel_response(request, response) return response
def test_unfollow_following_user(self): account2 = AccountFactory.create() RelationshipFactory.create(owner=self.account, follow=account2) self.client.login(username=self.user.username, password=DEFAULT_PASSWORD) response = self.client.post( reverse('account_unfollow', args=[account2.user.username])) self.assertRedirects(response, reverse('profile', args=[account2.user.username]))
def get_login_state(request): """ 获取登录态 """ account = AccountFactory.getAccountObj() # 重定向到首页 response = HttpResponseRedirect(SITE_URL) # 处理登录的cookie response = account.handel_response(request, response) return response
def setUp(self) -> None: self.client = Client() self.user = AccountFactory() self.alarm_statuses = [ AlarmStatusFactory(), AlarmStatusFactory(), ] self.house = HouseFactory() self.client.force_login(self.user)
def http_referer(request): """ 获取 HTTP_REFERER 头 """ if 'HTTP_REFERER' in request.META: http_referer = urlparse.urlparse(request.META['HTTP_REFERER'])[2] else: from account.factories import AccountFactory account = AccountFactory.getAccountObj() http_referer = account._config.LOGIN_REDIRECT_URL return http_referer
def process_view(self, request, view, args, kwargs): """ 校验登录, 如果未登录由跳转登录页 """ # 判断豁免权 if getattr(view, 'login_exempt', False): return None # 判断用户登录状态是否OK account = AccountFactory.getAccountObj() loginStatus = account.check_user_login_status(request) if loginStatus == 0: return None # 判断视图是不是自定义了login_url和redirect_field_name loginUrl = getattr(view, 'login_url', account.getConfig().LOGIN_URL) redirectFieldName = getattr(view, 'redirect_field_name', REDIRECT_FIELD_NAME) # 对于登录态不OK的用户, 引导重新登录 (首页的直接跳登录页, 非首页的跳登录态错误页) return account.redirectReLogin(request, loginUrl, redirectFieldName)
def login_success(request): """ 登录成功页面 """ account = AccountFactory.getAccountObj() return account.login_success(request)
def check_failed(request): """ 权限验证错误页面 """ account = AccountFactory.getAccountObj() return account.check_failed(request)
def logout(request): account = AccountFactory.getAccountObj() return account.logout(request)
def login_page(request): """ 平台统一登录页面 """ account = AccountFactory.getAccountObj() return account.login_page(request)
def setUp(self): account = AccountFactory.create() self.user = account.user
def setUpTestData(cls): cls.account = AccountFactory()