def ParseHydrusNetworkGETArgs(requests_args): args = ParseTwistedRequestGETArgs(requests_args, INT_PARAMS, BYTE_PARAMS, STRING_PARAMS, JSON_PARAMS, JSON_BYTE_LIST_PARAMS) if 'subject_account_key' in args: args['subject_identifier'] = HydrusNetwork.AccountIdentifier( account_key=args['subject_account_key']) elif 'subject_hash' in args: hash = args['subject_hash'] if 'subject_tag' in args: tag = args['subject_tag'] content = HydrusNetwork.Content(HC.CONTENT_TYPE_MAPPING, (tag, hash)) else: content = HydrusNetwork.Content(HC.CONTENT_TYPE_FILES, [hash]) args['subject_identifier'] = HydrusNetwork.AccountIdentifier( content=content) return args
def work_callable(): account_errors = set() account_keys_to_accounts = {} account_keys_to_account_info = {} for account_identifier in account_identifiers: try: result = service.Request( HC.GET, 'other_account', {'subject_identifier': account_identifier}) except Exception as e: account_errors.add(str(e)) continue if 'account' in result: account = result['account'] account_key = account.GetAccountKey() if account_key in account_keys_to_accounts: continue account_keys_to_accounts[account_key] = account try: response = self._service.Request( HC.GET, 'account_info', { 'subject_identifier': HydrusNetwork.AccountIdentifier( account_key=account_key) }) except Exception as e: HydrusData.PrintException(e) continue account_string = str(response['account_info']) account_keys_to_account_info[account_key] = account_string return (account_keys_to_accounts, account_keys_to_account_info, account_errors)
def work_callable(): for subject_account_key in subject_account_keys: service.Request( HC.POST, 'modify_account_unban', { 'subject_identifier': HydrusNetwork.AccountIdentifier( account_key=subject_account_key) }) return 1
def _ModifyAccounts( self ): accounts = self._account_list.GetData( only_selected = True ) if len( accounts ) > 0: subject_account_identifiers = [ HydrusNetwork.AccountIdentifier( account_key = account.GetAccountKey() ) for account in accounts ] frame = ClientGUITopLevelWindowsPanels.FrameThatTakesScrollablePanel( self.window().parentWidget(), 'manage accounts' ) panel = ModifyAccountsPanel( frame, self._service_key, subject_account_identifiers ) frame.SetPanel( panel )
def work_callable(): for (subject_account_key, new_expires) in subject_account_keys_and_new_expires: service.Request( HC.POST, 'modify_account_expires', { 'subject_identifier': HydrusNetwork.AccountIdentifier( account_key=subject_account_key), 'expires': new_expires }) return 1
def work_callable(): for subject_account_key in subject_account_keys: service.Request( HC.POST, 'modify_account_ban', { 'subject_identifier': HydrusNetwork.AccountIdentifier( account_key=subject_account_key), 'reason': reason, 'expires': expires }) return 1
def _test_tag_repo(self, service): # account from tag test_tag = 'character:samus aran' test_hash = HydrusData.GenerateKey() subject_content = HydrusNetwork.Content( content_type=HC.CONTENT_TYPE_MAPPING, content_data=(test_tag, test_hash)) subject_account_identifier = HydrusNetwork.AccountIdentifier( content=subject_content) HG.test_controller.SetRead('account', self._account) response = service.Request( HC.GET, 'other_account', {'subject_identifier': subject_account_identifier}) self.assertEqual(repr(response['account']), repr(self._account))
def _test_restricted(self, service): # access_key registration_key = HydrusData.GenerateKey() HG.test_controller.SetRead('access_key', self._access_key) response = service.Request(HC.GET, 'access_key', {'registration_key': registration_key}) self.assertEqual(response['access_key'], self._access_key) # set up session last_error = 0 account = self._account HG.test_controller.SetRead('service', service) HG.test_controller.SetRead('account_key_from_access_key', HydrusData.GenerateKey()) HG.test_controller.SetRead('account', self._account) # account response = service.Request(HC.GET, 'account') self.assertEqual(repr(response['account']), repr(self._account)) # account from access key HG.test_controller.SetRead('account', self._account) subject_account_identifier = HydrusNetwork.AccountIdentifier( account_key=self._account.GetAccountKey()) response = service.Request( HC.GET, 'other_account', {'subject_identifier': subject_account_identifier}) self.assertEqual(repr(response['account']), repr(self._account)) # account from file HG.test_controller.SetRead('account_from_content', self._account) content = HydrusNetwork.Content( content_type=HC.CONTENT_TYPE_FILES, content_data=(HydrusData.GenerateKey(), )) subject_account_identifier = HydrusNetwork.AccountIdentifier( content=content) response = service.Request( HC.GET, 'other_account', {'subject_identifier': subject_account_identifier}) self.assertEqual(repr(response['account']), repr(self._account)) # account from mapping HG.test_controller.SetRead('account_from_content', self._account) content = HydrusNetwork.Content( content_type=HC.CONTENT_TYPE_MAPPING, content_data=('hello', HydrusData.GenerateKey())) subject_account_identifier = HydrusNetwork.AccountIdentifier( content=content) response = service.Request( HC.GET, 'other_account', {'subject_identifier': subject_account_identifier}) self.assertEqual(repr(response['account']), repr(self._account)) # account_info account_info = {'message': 'hello'} HG.test_controller.SetRead('account_info', account_info) subject_account_identifier = HydrusNetwork.AccountIdentifier( account_key=HydrusData.GenerateKey()) response = service.Request( HC.GET, 'account_info', {'subject_identifier': subject_account_identifier}) self.assertEqual(response['account_info'], account_info) # content = HydrusNetwork.Content( content_type=HC.CONTENT_TYPE_FILES, content_data=(HydrusData.GenerateKey(), )) subject_account_identifier = HydrusNetwork.AccountIdentifier( content=content) with self.assertRaises(HydrusExceptions.BadRequestException): # can only do it with an account id response = service.Request( HC.GET, 'account_info', {'subject_identifier': subject_account_identifier}) content = HydrusNetwork.Content( content_type=HC.CONTENT_TYPE_MAPPING, content_data=('hello', HydrusData.GenerateKey())) subject_account_identifier = HydrusNetwork.AccountIdentifier( content=content) with self.assertRaises(HydrusExceptions.BadRequestException): # can only do it with an account id response = service.Request( HC.GET, 'account_info', {'subject_identifier': subject_account_identifier}) # account_types account_types = [ HydrusNetwork.AccountType.GenerateAdminAccountType( service.GetServiceType()) ] HG.test_controller.SetRead('account_types', account_types) HG.test_controller.ClearWrites('account_types') response = service.Request(HC.GET, 'account_types') self.assertEqual(response['account_types'][0].GetAccountTypeKey(), account_types[0].GetAccountTypeKey()) empty_account_type = HydrusNetwork.AccountType.GenerateNewAccountType( 'empty account', {}, HydrusNetworking.BandwidthRules()) account_types.append(empty_account_type) service.Request( HC.POST, 'account_types', { 'account_types': account_types, 'deletee_account_type_keys_to_new_account_type_keys': {} }) written = HG.test_controller.GetWrite('account_types') [(args, kwargs)] = written (written_service_key, written_account, written_account_types, written_deletee_account_type_keys_to_new_account_type_keys) = args self.assertEqual( {wat.GetAccountTypeKey() for wat in written_account_types}, {at.GetAccountTypeKey() for at in account_types}) self.assertEqual( written_deletee_account_type_keys_to_new_account_type_keys, {}) # registration_keys registration_key = HydrusData.GenerateKey() HG.test_controller.SetRead('registration_keys', [registration_key]) response = service.Request( HC.GET, 'registration_keys', { 'num': 1, 'account_type_key': os.urandom(32), 'expires': HydrusData.GetNow() + 1200 }) self.assertEqual(response['registration_keys'], [registration_key])
def _test_file_repo(self, service): # file path = ServerFiles.GetExpectedFilePath(self._file_hash) HydrusPaths.MakeSureDirectoryExists(os.path.dirname(path)) with open(path, 'wb') as f: f.write(EXAMPLE_FILE) response = service.Request(HC.GET, 'file', {'hash': self._file_hash}) self.assertEqual(response, EXAMPLE_FILE) # try: os.remove(path) except: pass path = os.path.join(HC.STATIC_DIR, 'hydrus.png') with open(path, 'rb') as f: file_bytes = f.read() HG.test_controller.ClearWrites('file') service.Request(HC.POST, 'file', {'file': file_bytes}) written = HG.test_controller.GetWrite('file') [(args, kwargs)] = written (written_service_key, written_account, written_file_dict) = args hash = b'\xadm5\x99\xa6\xc4\x89\xa5u\xeb\x19\xc0&\xfa\xce\x97\xa9\xcdey\xe7G(\xb0\xce\x94\xa6\x01\xd22\xf3\xc3' self.assertEqual(written_file_dict['hash'], hash) self.assertEqual(written_file_dict['ip'], '127.0.0.1') self.assertEqual(written_file_dict['height'], 200) self.assertEqual(written_file_dict['width'], 200) self.assertEqual(written_file_dict['mime'], 2) self.assertEqual(written_file_dict['size'], 5270) # ip (ip, timestamp) = ('94.45.87.123', HydrusData.GetNow() - 100000) HG.test_controller.SetRead('ip', (ip, timestamp)) response = service.Request(HC.GET, 'ip', {'hash': self._file_hash}) self.assertEqual(response['ip'], ip) self.assertEqual(response['timestamp'], timestamp) # account from hash subject_content = HydrusNetwork.Content( content_type=HC.CONTENT_TYPE_FILES, content_data=hash) subject_account_identifier = HydrusNetwork.AccountIdentifier( content=subject_content) HG.test_controller.SetRead('account', self._account) response = service.Request( HC.GET, 'other_account', {'subject_identifier': subject_account_identifier}) self.assertEqual(repr(response['account']), repr(self._account)) # thumbnail path = ServerFiles.GetExpectedThumbnailPath(self._file_hash) HydrusPaths.MakeSureDirectoryExists(os.path.dirname(path)) with open(path, 'wb') as f: f.write(EXAMPLE_THUMBNAIL) response = service.Request(HC.GET, 'thumbnail', {'hash': self._file_hash}) self.assertEqual(response, EXAMPLE_THUMBNAIL) try: os.remove(path) except: pass
def publish_callable( result ): ( self._account_keys_to_accounts, self._account_keys_to_account_info, account_errors ) = result if len( account_errors ) > 0: account_errors = sorted( account_errors ) QW.QMessageBox.information( self, 'Information', 'Errors were encountered during account fetch:{}{}'.format( os.linesep * 2, os.linesep.join( account_errors ) ) ) if not self._done_first_fetch: # if we launched with CPU-expensive mapping identifiers, let's move to nice account keys for future refreshes self._account_identifiers = [ HydrusNetwork.AccountIdentifier( account_key = account_key ) for account_key in self._account_keys_to_accounts.keys() ] # account_keys_sorted = sorted( list( self._account_keys_to_accounts.keys() ), key = lambda sak: ( self._account_keys_to_accounts[ sak ].GetAccountType().GetTitle(), sak.hex() ) ) my_admin_account = self._service.GetAccount() my_admin_account_key = my_admin_account.GetAccountKey() for account_key in account_keys_sorted: item = QW.QListWidgetItem() item.setFlags( item.flags() | QC.Qt.ItemIsUserCheckable ) account = self._account_keys_to_accounts[ account_key ] text = account.GetSingleLineTitle() if account_key == my_admin_account_key: text = 'THIS IS YOU: {}'.format( text ) item.setText( text ) if not self._done_first_fetch or account_key in selected_account_keys: item.setCheckState( QC.Qt.Checked ) else: item.setCheckState( QC.Qt.Unchecked ) item.setData( QC.Qt.UserRole, account_key ) self._account_list.addItem( item ) # self._status_st.setVisible( False ) self._status_st.setText( '' ) if self._account_list.count() > 0: self._account_list.item( 0 ).setSelected( True ) self._AccountClicked() self._accounts_loaded = True self._done_first_fetch = True self.accountsFetchFinished.emit()