def show_browse_page(): server = ShorthandServer(current_app.config['config_path']) toc = server.get_toc() return render_template('browse.j2', toc=json.dumps(toc), static_content=static_content)
def get_current_todos(): server = ShorthandServer(current_app.config['config_path']) status = get_request_argument(request.args, name='status', default='incomplete') directory_filter = get_request_argument(request.args, name='directory_filter') query_string = get_request_argument(request.args, name='query_string') sort_by = get_request_argument(request.args, name='sort_by') tag = get_request_argument(request.args, name='tag') suppress_future = get_request_argument(request.args, name='suppress_future', arg_type='bool', default=True) case_sensitive = get_request_argument(request.args, name='case_sensitive', arg_type='bool', default=False) if directory_filter == 'ALL': directory_filter = None if tag == 'ALL': tag = None todos = server.get_todos(todo_status=status, directory_filter=directory_filter, query_string=query_string, case_sensitive=case_sensitive, sort_by=sort_by, suppress_future=suppress_future, tag=tag) current_app.logger.info(f'Returning {len(todos)} todo results') wrapped_response = wrap_response_data(todos) wrapped_response['meta'] = analyze_todos(todos) return json.dumps(wrapped_response)
def record_file_view_api(): server = ShorthandServer(current_app.config['config_path']) note_path = get_request_argument(request.args, name='note_path', required=True) server.record_file_view(note_path=note_path) return 'ack'
def get_gps_locations(): server = ShorthandServer(current_app.config['config_path']) directory_filter = get_request_argument(request.args, name='directory_filter') locations = server.get_locations(directory_filter=directory_filter) wrapped_response = wrap_response_data(locations) return json.dumps(wrapped_response)
def mark_todo_status(): server = ShorthandServer(current_app.config['config_path']) filename = get_request_argument(request.args, name='filename') line_number = get_request_argument(request.args, name='line_number', arg_type='int') status = get_request_argument(request.args, name='status') return server.mark_todo(filename, line_number, status)
def write_updated_note(): server = ShorthandServer(current_app.config['config_path']) path = get_request_argument(request.args, name='path', required=True) request.get_data() content = request.data.decode('utf-8') server.update_note(path, content) return 'Note Updated'
def send_processed_markdown(): server = ShorthandServer(current_app.config['config_path']) note_path = request.args.get('path') file_content = server.get_note(note_path) file_content, toc_content = get_rendered_markdown(file_content, note_path) return json.dumps({ 'file_content': file_content, 'toc_content': toc_content })
def show_calendar(): server = ShorthandServer(current_app.config['config_path']) all_directories = ['ALL'] + server.get_subdirs() default_directory = server.get_config().get('default_directory') return render_template('calendar.j2', all_directories=all_directories, default_directory=default_directory, static_content=static_content)
def fetch_calendar(): server = ShorthandServer(current_app.config['config_path']) directory_filter = get_request_argument(request.args, name='directory_filter') if directory_filter == 'ALL': directory_filter = None calendar = server.get_calendar(directory_filter=directory_filter) return json.dumps(calendar)
def fetch_record_sets(): server = ShorthandServer(current_app.config['config_path']) directory_filter = get_request_argument(request.args, name='directory_filter') if directory_filter == 'ALL': directory_filter = None record_sets = server.get_record_sets(directory_filter=None) return json.dumps(wrap_response_data(record_sets))
def show_glossary(): server = ShorthandServer(current_app.config['config_path']) all_directories = ['ALL'] + server.get_subdirs() default_directory = server.get_config().get('default_directory') tags = server.get_tags() current_app.logger.info('Showing the Definitions search page') return render_template('glossary.j2', all_directories=all_directories, default_directory=default_directory, tags=tags, static_content=static_content)
def get_files(): server = ShorthandServer(current_app.config['config_path']) query_string = get_request_argument(request.args, name='query_string') prefer_recent = get_request_argument(request.args, name='prefer_recent', arg_type='bool', default=True) case_sensitive = get_request_argument(request.args, name='case_sensitive', arg_type='bool', default=False) files = server.search_filenames( prefer_recent=prefer_recent, query_string=query_string, case_sensitive=case_sensitive) return json.dumps(files)
def get_search_results(): server = ShorthandServer(current_app.config['config_path']) query_string = get_request_argument(request.args, name='query_string') case_sensitive = get_request_argument(request.args, name='case_sensitive', arg_type='bool', default=False) aggregate_by_file = get_request_argument(request.args, name='aggregate_by_file', arg_type='bool', default=False) search_results = server.search_full_text( query_string=query_string, case_sensitive=case_sensitive, aggregate_by_file=aggregate_by_file) return json.dumps(search_results)
def fetch_questions(): server = ShorthandServer(current_app.config['config_path']) status = get_request_argument(request.args, name='status', default='all') directory_filter = get_request_argument(request.args, name='directory_filter') if directory_filter == 'ALL': directory_filter = None current_app.logger.info(f'Getting {status} questions in ' f'directory {directory_filter}') questions = server.get_questions( question_status=status, directory_filter=directory_filter) current_app.logger.info(f'Returning {len(questions)} question results') return json.dumps(wrap_response_data(questions))
def get_note_links(): server = ShorthandServer(current_app.config['config_path']) source = get_request_argument(request.args, name='source') target = get_request_argument(request.args, name='target') note = get_request_argument(request.args, name='note') include_external = get_request_argument(request.args, name='include_external', arg_type='bool', default=False) include_invalid = get_request_argument(request.args, name='include_invalid', arg_type='bool', default=False) return json.dumps(server.get_links(source=source, target=target, note=note, include_external=include_external, include_invalid=include_invalid))
def test_update_config(self): write_config(ORIGINAL_CONFIG) server = ShorthandServer(server_config_path) # Test a valid update update = { 'log_level': 'WARNING', 'frontend': { 'view_history_limit': 500, 'map_tileserver_url': 'https://{s}.site.com/{z}/{x}/{y}.png' } } server.update_config(update) updated_config = server.get_config() assert updated_config['log_level'] == update['log_level'] assert updated_config['frontend'] == update['frontend'] # Test an invalid update bad_update = { 'log_level': 'INVALID' } with pytest.raises(ValueError) as e: server.update_config(bad_update) assert str(e.value) assert updated_config['log_level'] == update['log_level'] assert updated_config['frontend'] == update['frontend']
def fetch_record_set(): server = ShorthandServer(current_app.config['config_path']) file_path = get_request_argument(request.args, name='file_path') line_number = get_request_argument(request.args, name='line_number', arg_type='int') parse = get_request_argument(request.args, name='parse', arg_type='bool', default=True) include_config = get_request_argument(request.args, name='include_config', arg_type='bool', default=False) parse_format = get_request_argument(request.args, name='parse_format', default='json') return server.get_record_set( file_path=file_path, line_number=line_number, parse=parse, parse_format=parse_format, include_config=include_config)
def test_logging(self): write_config(ORIGINAL_CONFIG) server = ShorthandServer(server_config_path) test_message = 'This is just a test message' server.log.error(test_message) log_file_path = server.config['log_file_path'] assert os.path.exists(log_file_path) with open(log_file_path, 'r') as f: log_content = f.read() assert log_content assert test_message in log_content
def test_reload_config(self): write_config(ORIGINAL_CONFIG) server = ShorthandServer(server_config_path) # Make a small but valid change to the config, # and ensure it is reflected after reloading new_config = copy.deepcopy(ORIGINAL_CONFIG) new_config['log_level'] = 'warning' write_config(new_config) server.reload_config() assert server.get_config() == clean_and_validate_config(new_config) # Make an invalid change to the config bad_config = copy.deepcopy(new_config) bad_config['log_level'] = 'foobar' write_config(bad_config) with pytest.raises(ValueError) as e: server.reload_config() assert 'Invalid log level' in str(e.value) # Ensure the config is not actually updated assert server.get_config() == clean_and_validate_config(new_config)
def test_save_config(self): write_config(ORIGINAL_CONFIG) server = ShorthandServer(server_config_path) # Test a valid update update = { 'log_level': 'WARNING', 'frontend': { 'view_history_limit': 500, 'map_tileserver_url': 'https://{s}.site.com/{z}/{x}/{y}.png' } } server.update_config(update) server.save_config() with open(server_config_path, 'r') as config_file_object: config = json.load(config_file_object) assert config['log_level'] == update['log_level'] assert config['frontend'] == update['frontend']
def show_home_page(): server = ShorthandServer(current_app.config['config_path']) default_directory = server.get_config().get('default_directory') todos = server.get_todos(todo_status='incomplete', directory_filter=default_directory) questions = server.get_questions(question_status='unanswered', directory_filter=default_directory) summary = server.get_calendar() events = [] colors = { 'section': 'black', 'incomplete_todo': 'red', 'completed_todo': 'blue', 'skipped_todo': 'grey', 'question': 'purple', 'answer': 'green', } for year, year_data in summary.items(): for month, month_data in year_data.items(): for day, day_data in month_data.items(): for event in day_data: formatted_event = { 'title': event['event'], 'start': f'{year}-{month}-{day}', 'url': f'/render?path={event["file_path"]}' f'#line-number-{event["line_number"]}', 'type': event['type'] } if formatted_event['type'] in colors.keys(): formatted_event['color'] = colors[ formatted_event['type']] events.append(formatted_event) return render_template('home.j2', num_todos=len(todos), num_questions=len(questions), events=json.dumps(events), static_content=static_content)
def get_toc_data(): server = ShorthandServer(current_app.config['config_path']) return json.dumps(server.get_toc())
def get_full_note(): server = ShorthandServer(current_app.config['config_path']) path = get_request_argument(request.args, name='path', required=True) return server.get_note(path)
def get_server_config(): server = ShorthandServer(current_app.config['config_path']) current_app.logger.info('Returning config') return json.dumps(server.get_config())
def test_get_config(self): write_config(ORIGINAL_CONFIG) server = ShorthandServer(server_config_path) assert server.get_config() == clean_and_validate_config(ORIGINAL_CONFIG)
def get_typeahead(): server = ShorthandServer(current_app.config['config_path']) query_string = get_request_argument(request.args, name='query') return json.dumps(server.get_typeahead_suggestions( query_string=query_string))
def stamp(): server = ShorthandServer(current_app.config['config_path']) return server.stamp_notes(stamp_todos=True, stamp_today=True, stamp_questions=True, stamp_answers=True)
def validate_note_links(): server = ShorthandServer(current_app.config['config_path']) source = get_request_argument(request.args, name='source') return json.dumps(server.validate_internal_links(source=source))
def show_databases(): server = ShorthandServer(current_app.config['config_path']) record_sets = server.get_record_sets() return render_template('record_sets.j2', record_sets=record_sets, static_content=static_content)