From 1dd19a8f5e03d8826155ef595d3550a9d7ebf43c Mon Sep 17 00:00:00 2001 From: Roland Date: Mon, 27 Mar 2023 16:17:33 -0600 Subject: [PATCH] Reworking tag endpoints in a more restful way. --- alembic/versions/3b6918aeca4b_file_group.py | 9 +- app/src/Dashboard.js | 4 +- app/src/Tags.js | 233 +++++++++++--------- app/src/api.js | 28 ++- wrolpi/errors.py | 9 + wrolpi/root_api.py | 36 +-- wrolpi/schema.py | 2 +- wrolpi/tags.py | 86 +++++--- wrolpi/test/test_tags.py | 53 +++++ 9 files changed, 292 insertions(+), 168 deletions(-) diff --git a/alembic/versions/3b6918aeca4b_file_group.py b/alembic/versions/3b6918aeca4b_file_group.py index d932da3c..90e1834a 100644 --- a/alembic/versions/3b6918aeca4b_file_group.py +++ b/alembic/versions/3b6918aeca4b_file_group.py @@ -98,16 +98,19 @@ def upgrade(): session.execute(''' INSERT INTO tag (name, color) VALUES - ('Automotive','#000000'), + ('Automotive','#353535'), + ('Child Care','#9858C5'), ('Clothing','#9858C5'), ('Comms','#ECD71C'), ('Cooking','#00A04A'), - ('Electronics','#000000'), + ('Electronics','#1b731f'), + ('Entertainment','#000000'), ('Favorite','#000000'), ('Firearms','#000000'), ('Fire Safety','#000000'), ('First Aid','#C40405'), ('Food','#2F8055'), + ('Food Preservation','#2F8055'), ('Fuel','#000000'), ('Garden','#3E8B22'), ('Husbandry','#000000'), @@ -120,8 +123,10 @@ def upgrade(): ('Nuclear','#000000'), ('Power','#D837FF'), ('Radio','#000000'), + ('Schooling','#000000'), ('Security','#333333'), ('Shelter','#A18A62'), + ('Social','#000000'), ('Survival','#A18A62'), ('Tools','#333333'), ('Water','#291BC2') diff --git a/app/src/Dashboard.js b/app/src/Dashboard.js index 16665359..20b582cd 100644 --- a/app/src/Dashboard.js +++ b/app/src/Dashboard.js @@ -12,7 +12,7 @@ import {Divider, Icon, Message} from "semantic-ui-react"; import Grid from "semantic-ui-react/dist/commonjs/collections/Grid"; import {refreshFiles} from "./api"; import _ from "lodash"; -import {TagsSegment} from "./Tags"; +import {TagsDashboard} from "./Tags"; function FlagsMessages({flags}) { if (!flags) { @@ -82,7 +82,7 @@ export function Dashboard() { {!wrol_mode && downloads} {/* Hide Status when user is starting a download */} {!downloadOpen && } - + ; } diff --git a/app/src/Tags.js b/app/src/Tags.js index 593291ba..28cb7c68 100644 --- a/app/src/Tags.js +++ b/app/src/Tags.js @@ -1,19 +1,25 @@ import React, {useEffect, useState} from "react"; -import {addTag, deleteTag, getTags, postTag, removeTag} from "./api"; +import {addTag, deleteTag, getTags, removeTag, saveTag} from "./api"; import { Button as SButton, Confirm, Dimmer, Divider, Form, - FormField, FormInput, Grid, Header, Label, + LabelGroup, Loader, Loader as SLoader, Modal, + Table as STable, + TableBody, + TableCell, + TableHeader, + TableHeaderCell, + TableRow, } from "semantic-ui-react"; import {contrastingColor, HelpPopup} from "./components/Common"; import {Button, Segment} from "./components/Theme"; @@ -21,7 +27,8 @@ import _ from "lodash"; import {Link} from "react-router-dom"; import {HexColorPicker} from "react-colorful"; -export const TagsContext = React.createContext({tags: [], fetchTags: null}); +const DEFAULT_TAG_COLOR = '#000000'; +export const TagsContext = React.createContext({tags: [], fetchTags: null, findTagByName: null}); export function useTags() { const [tags, setTags] = React.useState(null); @@ -39,27 +46,37 @@ export function useTags() { } } - const NameToTagLabel = ({name, color, to, ...props}) => { - if (tags && tags.length) { - for (let i = 0; i < tags.length; i++) { - const tag = tags[i]; - if (name === tag['name']) { - const textColor = contrastingColor(tag['color']); - const label = ; - return label; - } + const findTagByName = (name) => { + if (!tags || tags.length === 0) { + return; + } + for (let i = 0; i < tags.length; i++) { + const tag = tags[i]; + if (name === tag['name']) { + return tag; } } + } - // Could not find tag by name, or no tags have been fetched. - return ; + const NameToTagLabel = ({name, to, ...props}) => { + const defaultTag = ; + const tag = findTagByName(name); + if (tag !== null && tag !== undefined) { + const tagColor = tag['color'] || DEFAULT_TAG_COLOR; + const textColor = contrastingColor(tagColor); + const style = {...props['style'], backgroundColor: tagColor, color: textColor}; + return ; + } + + // No tags have been fetched. + return defaultTag; } const TagsGroup = ({tagNames, onClick}) => { @@ -71,14 +88,14 @@ export function useTags() { } - const TagsLinkGroup = ({tagNames}) => { + const TagsLinkGroup = ({tagNames, ...props}) => { if (!tagNames || tagNames.length === 0) { return ; } return {tagNames.map(i => - - + + )} @@ -89,75 +106,73 @@ export function useTags() { fetchTags(); }, []); - return {tags, tagNames, NameToTagLabel, TagsGroup, TagsLinkGroup, fetchTags} + return {tags, tagNames, NameToTagLabel, TagsGroup, TagsLinkGroup, fetchTags, findTagByName} } -function DeleteTagLabel({name, onConfirm}) { +function EditTagLabel({tag, onDelete, onEdit}) { const {NameToTagLabel} = React.useContext(TagsContext); - const [confirmOpen, setConfirmOpen] = useState(false); + const [confirmDeleteOpen, setConfirmDeleteOpen] = useState(false); + const {name, color, id} = tag; - return
- setConfirmOpen(true)} icon='close'/> - setConfirmOpen(false)} - onConfirm={async () => onConfirm(name)} - /> - -
-
+ return + + setConfirmDeleteOpen(true)} icon='close'/> + setConfirmDeleteOpen(false)} + onConfirm={async () => onDelete(id, name)} + /> + + + onEdit(name, color, id)} icon='edit'/> + + + + + + + } -function DeleteTagModal() { - const {fetchTags, tagNames} = React.useContext(TagsContext); +function EditTagsModal() { + const {fetchTags, tags} = React.useContext(TagsContext); const [open, setOpen] = useState(false); + const [tagId, setTagId] = useState(null); + const [tagName, setTagName] = useState(''); + const [tagColor, setTagColor] = useState(DEFAULT_TAG_COLOR); + const textColor = contrastingColor(tagColor); - const localDeleteTag = async (name) => { - await deleteTag(name); + const localDeleteTag = async (id, name) => { + await deleteTag(id, name); if (fetchTags) { await fetchTags(); } } - const content = tagNames && tagNames.length ? - tagNames.map(i => ) - : ; + const localEditTag = async (name, color, id) => { + setTagName(name); + setTagColor(color || DEFAULT_TAG_COLOR); + setTagId(id); + } - return <> - setOpen(true)} - onClose={() => setOpen(false)} - > - Delete Tags - - {content} - - - setOpen(true)}> - Delete - - -} - -function CreateTagModal() { - const {fetchTags} = React.useContext(TagsContext); - - const [open, setOpen] = useState(false); - const [tagName, setTagName] = useState(''); - const [tagColor, setTagColor] = useState('#000000'); - const textColor = contrastingColor(tagColor); - - const saveTag = async () => { - await postTag(tagName, tagColor); + const localSaveTag = async () => { + await saveTag(tagName, tagColor, tagId); if (fetchTags) { await fetchTags(); } setTagName(''); + setTagId(null); + setTagColor(DEFAULT_TAG_COLOR); + } + + let content = ; + if (tags && tags.length) { + content = tags.map(i => ); } return <> @@ -166,7 +181,7 @@ function CreateTagModal() { onOpen={() => setOpen(true)} onClose={() => setOpen(false)} > - Create New Tag + Edit Tags - -
- - setTagName(value)} - /> - - - - - - Save - + setTagName(value)} + /> + + + + Save + + + + + + + Delete + Edit + Tag + + + + {content} + +
- - setOpen(true)}>New + setOpen(true)}> + Edit + } @@ -265,8 +289,7 @@ export function TagsModal({fileGroup, onClick}) { {availableTagsGroup} - - + @@ -296,20 +319,20 @@ export const TagsDisplay = ({fileGroup, onClick}) => { } -export const TagsSegment = () => { +export const TagsDashboard = () => { const {tagNames, TagsLinkGroup} = useTags(); let availableTagsGroup = ; if (tagNames && tagNames.length) { - availableTagsGroup = ; + availableTagsGroup = ; } + return
Tags
{availableTagsGroup} - - +
} diff --git a/app/src/api.js b/app/src/api.js index 2121cc51..e59dc9b8 100644 --- a/app/src/api.js +++ b/app/src/api.js @@ -698,26 +698,32 @@ export async function removeTag(fileGroup, name) { } } -export async function postTag(name, color) { +export async function saveTag(name, color, id) { const body = {name: name, color: color}; - const uri = `${API_URI}/tag/new`; + let uri = `${API_URI}/tag`; + if (id) { + uri = `${uri}/${id}`; + } let response = await apiPost(uri, body); - if (response.status !== 201) { + if (id && response.status === 200) { + toast({ + type: 'info', title: 'Saved tag', description: `Saved tag: ${name}`, time: 2000, + }); + } else if (response.status === 201) { + toast({ + type: 'info', title: 'Created new tag', description: `Created new tag: ${name}`, time: 2000, + }); + } else { console.error('Failed to create new tag'); toast({ type: 'error', title: 'Error!', description: 'Unable to save tag', time: 5000, }) - } else { - toast({ - type: 'info', title: 'Created new tag', description: `Created new tag: ${name}`, time: 2000, - }); } } -export async function deleteTag(name) { - const body = {name: name}; - const uri = `${API_URI}/tag/delete`; - let response = await apiPost(uri, body); +export async function deleteTag(id, name) { + const uri = `${API_URI}/tag/${id}`; + let response = await apiDelete(uri); if (response.status === 400) { const content = await response.json(); if (content['code'] === 38) { diff --git a/wrolpi/errors.py b/wrolpi/errors.py index f5277473..a314ce3b 100644 --- a/wrolpi/errors.py +++ b/wrolpi/errors.py @@ -160,6 +160,10 @@ class UsedTag(APIError): pass +class InvalidTag(APIError): + pass + + error_codes = iter(range(1, 1000)) API_ERRORS = { @@ -353,4 +357,9 @@ API_ERRORS = { 'message': 'Tag is being used', 'status': HTTPStatus.BAD_REQUEST, }, + InvalidTag: { + 'code': next(error_codes), + 'message': 'Tag is not allowed', + 'status': HTTPStatus.BAD_REQUEST, + }, } diff --git a/wrolpi/root_api.py b/wrolpi/root_api.py index 596db4fe..f7ac205c 100644 --- a/wrolpi/root_api.py +++ b/wrolpi/root_api.py @@ -25,7 +25,7 @@ from wrolpi.errors import WROLModeEnabled, API_ERRORS, APIError, ValidationError from wrolpi.events import get_events from wrolpi.files.lib import get_file_statistics from wrolpi.schema import RegexRequest, RegexResponse, SettingsRequest, SettingsResponse, DownloadRequest, EchoResponse, \ - EventsRequest, NewTagRequest, DeleteTagRequest + EventsRequest, TagRequest from wrolpi.vars import API_HOST, API_PORT, DOCKERIZED, API_DEBUG, API_ACCESS_LOG, API_WORKERS, API_AUTO_RELOAD, \ truthy_arg from wrolpi.version import __version__ @@ -385,17 +385,23 @@ async def get_tags_request(_: Request): return json_response(dict(tags=tags_)) -@api_bp.post('/tag/new') -@validate(NewTagRequest) -async def post_new_tag(_: Request, body: NewTagRequest): - tags.new_tag(body.name, body.color) - return response.empty(HTTPStatus.CREATED) +@api_bp.post('/tag') +@api_bp.post('/tag/') +@validate(TagRequest) +@openapi.definition( + summary='Create or update a Tag', +) +async def post_new_tag(_: Request, body: TagRequest, tag_id: int = None): + tags.upsert_tag(body.name, body.color, tag_id) + if tag_id: + return response.empty(HTTPStatus.OK) + else: + return response.empty(HTTPStatus.CREATED) -@api_bp.post('/tag/delete') -@validate(DeleteTagRequest) -async def delete_tag_request(_: Request, body: DeleteTagRequest): - tags.delete_tag(body.name) +@api_bp.delete('/tag/') +async def delete_tag_request(_: Request, tag_id: int): + tags.delete_tag(tag_id) return response.empty(HTTPStatus.NO_CONTENT) @@ -449,15 +455,19 @@ def json_response(*a, **kwargs) -> HTTPResponse: return resp -def json_error_handler(request, exception: Exception): +def json_error_handler(request: Request, exception: Exception): error = API_ERRORS[type(exception)] if isinstance(exception, ValidationError): body = dict(error='Could not validate the contents of the request', code=error['code']) else: body = dict(message=str(exception), api_error=error['message'], code=error['code']) if cause := exception.__cause__: - cause = API_ERRORS[type(cause)] - body['cause'] = dict(error=cause['message'], code=cause['code']) + try: + cause = API_ERRORS[type(cause)] + body['cause'] = dict(error=cause['message'], code=cause['code']) + except KeyError: + # Cause was not an APIError. + logger.error(f'Could not find cause {cause}') return json_response(body, error['status']) diff --git a/wrolpi/schema.py b/wrolpi/schema.py index 553050ac..190cb029 100644 --- a/wrolpi/schema.py +++ b/wrolpi/schema.py @@ -67,7 +67,7 @@ class EventsRequest: @dataclass -class NewTagRequest: +class TagRequest: name: str color: str diff --git a/wrolpi/tags.py b/wrolpi/tags.py index 070306ee..81fd7aff 100644 --- a/wrolpi/tags.py +++ b/wrolpi/tags.py @@ -1,13 +1,13 @@ import contextlib from typing import List -from cryptography.exceptions import InvalidTag from sqlalchemy import Column, Integer, String, ForeignKey, BigInteger +from sqlalchemy.exc import IntegrityError from sqlalchemy.orm import relationship, Session -from wrolpi.common import ModelHelper, Base, logger, ConfigFile, get_media_directory, background_task +from wrolpi.common import ModelHelper, Base, logger, ConfigFile, get_media_directory, background_task, run_after from wrolpi.db import optional_session -from wrolpi.errors import UnknownTag, UsedTag +from wrolpi.errors import UnknownTag, UsedTag, InvalidTag from wrolpi.vars import PYTEST logger = logger.getChild(__name__) @@ -154,37 +154,7 @@ def test_tags_config(): @optional_session -def get_tags(session: Session) -> List[Tag]: - tags = list(session.query(Tag)) - return tags - - -@optional_session -def new_tag(name: str, color: str, session: Session) -> Tag: - if ',' in name: - raise InvalidTag('Tag name cannot have comma') - - tag = Tag(name=name, color=color) - session.add(tag) - session.flush([tag]) - session.commit() - return tag - - -@optional_session -def delete_tag(name: str, session: Session = None): - tag: Tag = Tag.find_by_name(name, session) - if not tag: - raise UnknownTag(f'Cannot find tag {name}') - if tag.tag_files: - count = len(tag.tag_files) - raise UsedTag(f'Cannot delete {name} it is used by {count} files!') - - session.delete(tag) - session.commit() - - -def schedule_save(session: Session): +def schedule_save(session: Session = None): """Schedule a background task to save all TagFiles to the config file. If testing, save synchronously.""" if PYTEST: get_tags_config().save_tags(session) @@ -193,3 +163,51 @@ def schedule_save(session: Session): get_tags_config().save_tags(session) background_task(_()) + + +@optional_session +def get_tags(session: Session) -> List[Tag]: + tags = list(session.query(Tag).order_by(Tag.name)) + return tags + + +@optional_session +@run_after(schedule_save) +def upsert_tag(name: str, color: str, tag_id: int = None, session: Session = None) -> Tag: + if ',' in name: + raise InvalidTag('Tag name cannot have comma') + + if tag_id: + tag = session.query(Tag).filter_by(id=tag_id).one_or_none() + if not tag: + raise UnknownTag(f'Cannot find tag with id={tag_id}') + tag.name = name + tag.color = color + else: + tag = Tag(name=name, color=color) + session.add(tag) + + try: + session.flush([tag]) + session.commit() + except IntegrityError as e: + # Conflicting name + session.rollback() + raise InvalidTag(f'Name already taken') from e + + return tag + + +@optional_session +def delete_tag(tag_id: int, session: Session = None): + tag: Tag = session.query(Tag).filter_by(id=tag_id).one_or_none() + + if not tag: + raise UnknownTag(f'Cannot find tag {tag_id}') + + if tag.tag_files: + count = len(tag.tag_files) + raise UsedTag(f'Cannot delete {tag.name} it is used by {count} files!') + + session.delete(tag) + session.commit() diff --git a/wrolpi/test/test_tags.py b/wrolpi/test/test_tags.py index aa16b413..54198ea5 100644 --- a/wrolpi/test/test_tags.py +++ b/wrolpi/test/test_tags.py @@ -1,3 +1,6 @@ +import json +from http import HTTPStatus + import pytest from wrolpi import tags @@ -100,3 +103,53 @@ async def test_tags_config_(test_session, test_directory, tag_factory, example_p # Removing non-existent tag does not error. video.remove_tag(tag1) + + +def test_tags_crud(test_session, test_client, example_pdf): + """Test API Create/Retrieve/Update/Delete of Tags.""" + pdf = FileGroup.from_paths(test_session, example_pdf) + test_session.add(pdf) + test_session.commit() + + # Can get empty tags. + request, response = test_client.get('/api/tag') + assert response.status_code == HTTPStatus.OK + assert response.json['tags'] == list() + + # Tags can be created. + content = dict(name='foo', color='#123456') + request, response = test_client.post('/api/tag', content=json.dumps(content)) + assert response.status_code == HTTPStatus.CREATED + + # The tag can be retrieved. + request, response = test_client.get('/api/tag') + assert response.status_code == HTTPStatus.OK + assert response.json['tags'] == [dict(name='foo', color='#123456', id=1)] + + # Apply the tag to the PDF. + tag = test_session.query(tags.Tag).one() + pdf.add_tag(tag, test_session) + test_session.commit() + + # The tag can be updated. + content = dict(name='bar', color='#000000') + request, response = test_client.post('/api/tag/1', content=json.dumps(content)) + assert response.status_code == HTTPStatus.OK + request, response = test_client.get('/api/tag') + assert response.json['tags'] == [dict(name='bar', color='#000000', id=1)] + + # Conflicting names return an error. + content = dict(name='bar', color='#111111') + request, response = test_client.post('/api/tag', content=json.dumps(content)) + assert response.status_code == HTTPStatus.BAD_REQUEST + + # Cannot delete Tag that is used. + request, response = test_client.delete('/api/tag/1') + assert response.status_code == HTTPStatus.BAD_REQUEST + + pdf.remove_tag(tag, test_session) + test_session.commit() + + # Can delete unused Tag. + request, response = test_client.delete('/api/tag/1') + assert response.status_code == HTTPStatus.NO_CONTENT