Adding and testing Channel.GET/DELETE.

This commit is contained in:
Roland 2019-10-10 11:32:43 -06:00
parent 3c474cb247
commit ec0d11b35a
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
7 changed files with 49 additions and 21 deletions

View file

@ -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

View file

@ -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" ]

12
main.py
View file

@ -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

View file

@ -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()

View file

@ -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(

View file

@ -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

View file

@ -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)