def main(request): uds = UserDataStore('lightning', request.user) return render(request, self.template_path("index.html"), { 'title': 'Lightning Network', 'api_key': uds.get_string("api_key") })
def test_plugin_datastore(self): enable_plugin("test") test_plugin = get_plugin_by_name("test") user = User.objects.get(username='******') other_user = User.objects.get(username='******') uds = test_plugin.get_user_data_store(user) other_uds = test_plugin.get_user_data_store(other_user) gds = test_plugin.get_global_data_store() # No key self.assertFalse(uds.has_key('mykey')) # Default value works self.assertTrue(uds.get_string('mykey', 'default') == 'default') # Still no key should have been added self.assertFalse(uds.has_key('mykey')) # Add key (object, created) = uds.set_string('mykey', 'value') self.assertTrue(object.string_value == 'value') self.assertTrue(created) self.assertTrue(uds.has_key('mykey')) # Key is not visible in global datastore self.assertFalse(gds.has_key('mykey')) # Key is not visible in another user's data store self.assertFalse(other_uds.has_key('mykey')) # Key is not visible in another's plugin data store # for the same user other_puds = UserDataStore('test2', user) self.assertFalse(other_puds.has_key('mykey')) # Deleting a non-existing key return False self.assertFalse(uds.del_key('nonexistant')) # Deleting a valid key returns True self.assertTrue(uds.del_key('mykey')) self.assertFalse(uds.has_key('mykey')) # Various data types setter/getter work uds.set_int('myint', 5) self.assertTrue(uds.get_int('myint') == 5) uds.set_float('myfloat', 10.0) self.assertTrue(uds.get_float('myfloat', 50.0) == 10.0) uds.set_bool('mybool', True) self.assertTrue(uds.get_bool('mybool')) uds.set_json('myjson', {'test': 123}) self.assertTrue('test' in uds.get_json('myjson')) # Invalid types self.assertRaises(InvalidDataStoreValue, uds.set_bool, 'invalidbool', 5)
def save_api_key(request): api_key = request.POST.get('api_key') if api_key is None: return JsonResponse({'error': 'api_key is required'}) uds = UserDataStore('lightning', request.user) uds.set_string('api_key', api_key) return JsonResponse({'success': True})
def test_plugin_datastore(self): test_plugin = get_plugin_by_name("test") user = User.objects.get(username='******') other_user = User.objects.get(username='******') uds = test_plugin.get_user_data_store(user) other_uds = test_plugin.get_user_data_store(other_user) gds = test_plugin.get_global_data_store() # No key self.assertFalse(uds.has_key('mykey')) # Default value works self.assertTrue(uds.get_string('mykey', 'default') == 'default') # Still no key should have been added self.assertFalse(uds.has_key('mykey')) # Add key (object, created) = uds.set_string('mykey', 'value') self.assertTrue(object.string_value == 'value') self.assertTrue(created) self.assertTrue(uds.has_key('mykey')) # Key is not visible in global datastore self.assertFalse(gds.has_key('mykey')) # Key is not visible in another user's data store self.assertFalse(other_uds.has_key('mykey')) # Key is not visible in another's plugin data store # for the same user other_puds = UserDataStore('test2', user) self.assertFalse(other_puds.has_key('mykey')) # Deleting a non-existing key return False self.assertFalse(uds.del_key('nonexistant')) # Deleting a valid key returns True self.assertTrue(uds.del_key('mykey')) self.assertFalse(uds.has_key('mykey')) # Various data types setter/getter work uds.set_int('myint', 5) self.assertTrue(uds.get_int('myint') == 5) uds.set_float('myfloat', 10.0) self.assertTrue(uds.get_float('myfloat', 50.0) == 10.0) uds.set_bool('mybool', True) self.assertTrue(uds.get_bool('mybool')) uds.set_json('myjson', {'test': 123}) self.assertTrue('test' in uds.get_json('myjson')) # Invalid types self.assertRaises(InvalidDataStoreValue, uds.set_bool, 'invalidbool', 5)
def get_user_data_store(self, user): """ Helper function to instantiate a user data store associated with this plugin :return: UserDataStore """ return UserDataStore(self.get_name(), user)