From ec0d11b35a65e77a4f40ec8f6497c04d4c2def2e Mon Sep 17 00:00:00 2001 From: Roland Date: Thu, 10 Oct 2019 11:32:43 -0600 Subject: [PATCH] Adding and testing Channel.GET/DELETE. --- README.md | 6 +++--- docker/web/Dockerfile | 2 +- main.py | 12 +++++------- wrolpi/cmd.py | 17 +++++++++++------ wrolpi/plugins/videos/api.py | 18 ++++++++++++++++++ wrolpi/plugins/videos/common.py | 2 +- wrolpi/plugins/videos/test/test_api.py | 13 ++++++++++--- 7 files changed, 49 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index a9f76d91..78dcc672 100644 --- a/README.md +++ b/README.md @@ -7,14 +7,14 @@ network so that any person with a laptop/tablet/phone can connect and use the da # Install WROLPi runs in a couple Docker containers. Lets get the prerequisites installed... -1. [Install docker-ce](https://docs.docker.com/install/linux/docker-ce/ubuntu/) -1. [Install docker-compose](https://docs.docker.com/compose/install/) +1. [Install docker-ce](https://docs.docker.com/install/linux/docker-ce/ubuntu/) and + [docker-compose](https://docs.docker.com/compose/install/) 1. Copy the latest WROLPi code * `git clone git@github.com:lrnselfreliance/wrolpi.git` 1. Change directory into the code base * `cd wrolpi` 1. Build the docker containers - * `docker-compose build` + * `docker-compose build --parallel` 1. Start the docker containers * `docker-compose up` 1. Navigate to WROLPi diff --git a/docker/web/Dockerfile b/docker/web/Dockerfile index 1e96664e..222f9aee 100644 --- a/docker/web/Dockerfile +++ b/docker/web/Dockerfile @@ -12,4 +12,4 @@ RUN pip install -r requirements.txt # Copy the main script as well as the WROLPi directory WORKDIR /opt/wrolpi -CMD [ "python3", "/opt/wrolpi/main.py", "web", "--host", "0.0.0.0" ] +CMD [ "python3", "/opt/wrolpi/main.py", "-v", "web", "--host", "0.0.0.0" ] diff --git a/main.py b/main.py index cdcfc1a0..1f6a7026 100755 --- a/main.py +++ b/main.py @@ -34,8 +34,6 @@ def main(): help='Initialize the database (all data will be lost).') parser.add_argument('-s', '--save-config', action='store_const', const=CONFIG_PATH, help=f'Save the settings to this config file. DEFAULT: {CONFIG_PATH}') - parser.add_argument('-i', '--import-config', action='store_const', const=CONFIG_PATH, - help=f'Import this config, updating existing settings. DEFAULT: {CONFIG_PATH}') sub_commands = parser.add_subparsers(title='sub-commands', dest='sub_commands') @@ -59,18 +57,18 @@ def main(): logger.warning('Setting verbosity to DEBUG') logger.setLevel(logging.DEBUG) + # Always update the DB from the configs + import_settings_configs(PLUGINS) + if args.sub_commands: - sub_main = choices_to_mains[args.sub_commands] - return sub_main(args) + plugin_main = choices_to_mains[args.sub_commands] + return plugin_main(args) elif args.db: return_code = update_db(PLUGINS) logger.info('DB updated.') elif args.save_config: return_code = save_settings_configs(PLUGINS) logger.info('Config written.') - elif args.import_config: - return_code = import_settings_configs(PLUGINS) - logger.info('Config imported.') else: parser.print_help() return_code = 1 diff --git a/wrolpi/cmd.py b/wrolpi/cmd.py index 3c5c5508..00f953cb 100644 --- a/wrolpi/cmd.py +++ b/wrolpi/cmd.py @@ -1,17 +1,22 @@ -from wrolpi.common import get_db_context +from wrolpi.common import get_db_context, logger + + +def update_db_with_conn(plugins, db_conn, db): + for plugin in plugins.values(): + sql = plugin.get_update_sql(db_conn, db) + curs = db.get_cursor() + curs.execute(sql) + db_conn.commit() def update_db(plugins): with get_db_context() as (db_conn, db): - for plugin in plugins.values(): - sql = plugin.get_update_sql(db_conn, db) - curs = db.get_cursor() - curs.execute(sql) - db_conn.commit() + update_db_with_conn(plugins, db_conn, db) return 0 def import_settings_configs(plugins): + logger.info('Importing configs') for plugin in plugins.values(): plugin.import_settings_config() diff --git a/wrolpi/plugins/videos/api.py b/wrolpi/plugins/videos/api.py index 8306aac7..2400e22c 100644 --- a/wrolpi/plugins/videos/api.py +++ b/wrolpi/plugins/videos/api.py @@ -46,6 +46,14 @@ class Refresh(object): @cherrypy.expose class ChannelAPI(object): + @cherrypy.tools.db() + def GET(self, link, db: DictDB): + Channel = db['channel'] + channel = Channel.get_one(link=link) + if not channel: + return json.dumps({'error': 'Unknown channel'}) + return json.dumps({'channel': channel}) + @cherrypy.tools.db() def POST(self, db: DictDB, **form_data): """Create a new channel""" @@ -129,6 +137,16 @@ class ChannelAPI(object): return json.dumps({'success': 'The channel was updated successfully.'}) + @cherrypy.tools.db() + def DELETE(self, link, db: DictDB): + Channel = db['channel'] + channel = Channel.get_one(link=link) + if not channel: + return json.dumps({'error': 'Unknown channel'}) + with db.transaction(commit=True): + channel.delete() + return json.dumps({'success': 'Channel deleted'}) + def get_channel_form(form_data: dict): channel = dict( diff --git a/wrolpi/plugins/videos/common.py b/wrolpi/plugins/videos/common.py index 6900d10c..c28a2a4d 100644 --- a/wrolpi/plugins/videos/common.py +++ b/wrolpi/plugins/videos/common.py @@ -18,7 +18,7 @@ REQUIRED_OPTIONS = ['name', 'directory'] def get_update_sql(db_conn, db): - with open(MY_DIR / 'sql/v1.sql', 'rt') as fh: + with open(MY_DIR / 'v1.sql', 'rt') as fh: contents = fh.read() return contents diff --git a/wrolpi/plugins/videos/test/test_api.py b/wrolpi/plugins/videos/test/test_api.py index 18924f41..401c1f35 100644 --- a/wrolpi/plugins/videos/test/test_api.py +++ b/wrolpi/plugins/videos/test/test_api.py @@ -1,3 +1,4 @@ +import json import tempfile import unittest from shutil import copyfile @@ -63,14 +64,16 @@ class TestAPI(unittest.TestCase): with get_db_context() as (db_conn, db): Channel = db['channel'] + # Channel doesn't exist - existing = Channel.get_one(name='Example Channel 1') - self.assertIsNone(existing) + existing = api.channel.GET('examplechannel1', db) + self.assertIn('error', existing) # Create it result = api.channel.POST(db, **new_channel) self.assertIn('success', result) - created = Channel.get_one(link='examplechannel1') + response = api.channel.GET('examplechannel1', db) + created = json.loads(response)['channel'] self.assertIsNotNone(created) self.assertIsNotNone(created['id']) @@ -90,3 +93,7 @@ class TestAPI(unittest.TestCase): # Can't update channel that doesn't exist result = api.channel.PUT('DoesntExist', db, **new_channel) self.assertIn('error', result) + + # Delete the new channel + result = api.channel.DELETE('examplechannel1', db) + self.assertIn('success', result)