mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-21 15:36:18 -05:00
Reworking tag endpoints in a more restful way.
This commit is contained in:
parent
d222ba4d32
commit
1dd19a8f5e
9 changed files with 292 additions and 168 deletions
|
|
@ -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')
|
||||
|
|
|
|||
|
|
@ -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 && <DashboardStatus/>}
|
||||
<TagsSegment/>
|
||||
<TagsDashboard/>
|
||||
</>;
|
||||
}
|
||||
|
||||
|
|
|
|||
233
app/src/Tags.js
233
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 = <Label
|
||||
size='large'
|
||||
style={{backgroundColor: tag['color'], color: textColor}}
|
||||
className='clickable'
|
||||
{...props} // onClick passed here.
|
||||
>
|
||||
{name}
|
||||
</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 <Label size='large'>{name}</Label>;
|
||||
const NameToTagLabel = ({name, to, ...props}) => {
|
||||
const defaultTag = <Label size='large'>{name}</Label>;
|
||||
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 <Label
|
||||
size='large'
|
||||
{...props} // onClick passed here.
|
||||
style={style}
|
||||
className='clickable'
|
||||
>
|
||||
{name}
|
||||
</Label>;
|
||||
}
|
||||
|
||||
// No tags have been fetched.
|
||||
return defaultTag;
|
||||
}
|
||||
|
||||
const TagsGroup = ({tagNames, onClick}) => {
|
||||
|
|
@ -71,14 +88,14 @@ export function useTags() {
|
|||
</Label.Group>
|
||||
}
|
||||
|
||||
const TagsLinkGroup = ({tagNames}) => {
|
||||
const TagsLinkGroup = ({tagNames, ...props}) => {
|
||||
if (!tagNames || tagNames.length === 0) {
|
||||
return <React.Fragment/>;
|
||||
}
|
||||
return <Label.Group tag>
|
||||
{tagNames.map(i =>
|
||||
<Link key={i} to={`/?tag=${i}`} style={{margin: '0.3em'}}>
|
||||
<NameToTagLabel name={i}/>
|
||||
<Link key={i} to={`/?tag=${i}`} style={{marginLeft: '0.3em', marginRight: '0.3em'}}>
|
||||
<NameToTagLabel name={i} {...props}/>
|
||||
</Link>
|
||||
)}
|
||||
</Label.Group>
|
||||
|
|
@ -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 <div style={{marginTop: '1em'}}>
|
||||
<SButton color='red' onClick={() => setConfirmOpen(true)} icon='close'/>
|
||||
<Confirm
|
||||
id={`confirm${name}`}
|
||||
open={confirmOpen}
|
||||
content={`Are you sure you want to delete: ${name}?`}
|
||||
confirmButton='Delete'
|
||||
onCancel={() => setConfirmOpen(false)}
|
||||
onConfirm={async () => onConfirm(name)}
|
||||
/>
|
||||
<NameToTagLabel name={name}/>
|
||||
<br/>
|
||||
</div>
|
||||
return <TableRow>
|
||||
<TableCell>
|
||||
<SButton color='red' onClick={() => setConfirmDeleteOpen(true)} icon='close'/>
|
||||
<Confirm
|
||||
id={`confirm${name}`}
|
||||
open={confirmDeleteOpen}
|
||||
content={`Are you sure you want to delete: ${name}?`}
|
||||
confirmButton='Delete'
|
||||
onCancel={() => setConfirmDeleteOpen(false)}
|
||||
onConfirm={async () => onDelete(id, name)}
|
||||
/>
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<SButton primary onClick={() => onEdit(name, color, id)} icon='edit'/>
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<LabelGroup tag>
|
||||
<NameToTagLabel name={name}/>
|
||||
</LabelGroup>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
}
|
||||
|
||||
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 => <DeleteTagLabel key={i} name={i} onConfirm={localDeleteTag}/>)
|
||||
: <Dimmer><Loader inline active/></Dimmer>;
|
||||
const localEditTag = async (name, color, id) => {
|
||||
setTagName(name);
|
||||
setTagColor(color || DEFAULT_TAG_COLOR);
|
||||
setTagId(id);
|
||||
}
|
||||
|
||||
return <>
|
||||
<Modal closeIcon
|
||||
open={open}
|
||||
onOpen={() => setOpen(true)}
|
||||
onClose={() => setOpen(false)}
|
||||
>
|
||||
<Modal.Header>Delete Tags</Modal.Header>
|
||||
<Modal.Content>
|
||||
{content}
|
||||
</Modal.Content>
|
||||
</Modal>
|
||||
<SButton color='red' onClick={() => setOpen(true)}>
|
||||
Delete
|
||||
</SButton>
|
||||
</>
|
||||
}
|
||||
|
||||
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 = <Dimmer><Loader inline active/></Dimmer>;
|
||||
if (tags && tags.length) {
|
||||
content = tags.map(i => <EditTagLabel key={i['name']} tag={i} onDelete={localDeleteTag}
|
||||
onEdit={localEditTag}/>);
|
||||
}
|
||||
|
||||
return <>
|
||||
|
|
@ -166,7 +181,7 @@ function CreateTagModal() {
|
|||
onOpen={() => setOpen(true)}
|
||||
onClose={() => setOpen(false)}
|
||||
>
|
||||
<Modal.Header>Create New Tag</Modal.Header>
|
||||
<Modal.Header>Edit Tags</Modal.Header>
|
||||
<Modal.Content>
|
||||
<Label.Group tag>
|
||||
<Label size='large' style={{backgroundColor: tagColor, color: textColor}}>
|
||||
|
|
@ -174,35 +189,44 @@ function CreateTagModal() {
|
|||
</Label>
|
||||
</Label.Group>
|
||||
|
||||
<Divider/>
|
||||
|
||||
<Form autoComplete='off'>
|
||||
<FormField>
|
||||
<FormInput required
|
||||
label='Tag Name'
|
||||
type='text'
|
||||
placeholder='Unique Name'
|
||||
value={tagName}
|
||||
onChange={(e, {value}) => setTagName(value)}
|
||||
/>
|
||||
</FormField>
|
||||
|
||||
<HexColorPicker color={tagColor} onChange={setTagColor}/>
|
||||
|
||||
<SButton primary
|
||||
size='big'
|
||||
onClick={saveTag}
|
||||
style={{marginTop: '2em'}}
|
||||
disabled={!!!tagName}
|
||||
>
|
||||
Save
|
||||
</SButton>
|
||||
<FormInput required
|
||||
label='Tag Name'
|
||||
placeholder='Unique name'
|
||||
value={tagName}
|
||||
onChange={(e, {value}) => setTagName(value)}
|
||||
/>
|
||||
</Form>
|
||||
|
||||
<HexColorPicker color={tagColor} onChange={setTagColor} style={{marginTop: '1em'}}/>
|
||||
|
||||
<SButton primary
|
||||
size='big'
|
||||
onClick={localSaveTag}
|
||||
style={{marginTop: '2em'}}
|
||||
disabled={!!!tagName}
|
||||
>
|
||||
Save
|
||||
</SButton>
|
||||
|
||||
<Divider/>
|
||||
<STable striped basic='very' unstackable>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHeaderCell width={2}>Delete</TableHeaderCell>
|
||||
<TableHeaderCell width={2}>Edit</TableHeaderCell>
|
||||
<TableHeaderCell width={8}>Tag</TableHeaderCell>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{content}
|
||||
</TableBody>
|
||||
</STable>
|
||||
</Modal.Content>
|
||||
</Modal>
|
||||
|
||||
<SButton primary onClick={() => setOpen(true)}>New</SButton>
|
||||
<SButton primary onClick={() => setOpen(true)}>
|
||||
Edit
|
||||
</SButton>
|
||||
</>
|
||||
}
|
||||
|
||||
|
|
@ -265,8 +289,7 @@ export function TagsModal({fileGroup, onClick}) {
|
|||
{availableTagsGroup}
|
||||
</Modal.Content>
|
||||
<Modal.Actions>
|
||||
<DeleteTagModal/>
|
||||
<CreateTagModal/>
|
||||
<EditTagsModal/>
|
||||
</Modal.Actions>
|
||||
</Modal>
|
||||
|
||||
|
|
@ -296,20 +319,20 @@ export const TagsDisplay = ({fileGroup, onClick}) => {
|
|||
</Grid>
|
||||
}
|
||||
|
||||
export const TagsSegment = () => {
|
||||
export const TagsDashboard = () => {
|
||||
const {tagNames, TagsLinkGroup} = useTags();
|
||||
|
||||
let availableTagsGroup = <SLoader active inline/>;
|
||||
if (tagNames && tagNames.length) {
|
||||
availableTagsGroup = <TagsLinkGroup tagNames={tagNames}/>;
|
||||
availableTagsGroup = <TagsLinkGroup tagNames={tagNames} style={{marginTop: '0.5em'}}/>;
|
||||
}
|
||||
|
||||
return <Segment>
|
||||
<Header as='h2'>Tags</Header>
|
||||
{availableTagsGroup}
|
||||
|
||||
<Divider/>
|
||||
|
||||
<DeleteTagModal/>
|
||||
<CreateTagModal/>
|
||||
<EditTagsModal/>
|
||||
</Segment>
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
},
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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/<tag_id:int>')
|
||||
@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/<tag_id:int>')
|
||||
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'])
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -67,7 +67,7 @@ class EventsRequest:
|
|||
|
||||
|
||||
@dataclass
|
||||
class NewTagRequest:
|
||||
class TagRequest:
|
||||
name: str
|
||||
color: str
|
||||
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue