diff --git a/app/api/site_videos.py b/app/api/site_videos.py index ad1ab50..41a6703 100644 --- a/app/api/site_videos.py +++ b/app/api/site_videos.py @@ -97,6 +97,14 @@ def update_video(video_id: str, body: VideoUpdate, user=Depends(can('manage'))): with db.connect() as c: previous = c.execute('SELECT upload_id,captions_id,video_key,captions_key FROM dtf_local.site_videos WHERE id=%s FOR UPDATE', (video_id,)).fetchone() or {} + # One floating video per product page; checked before any copy, and + # the refusal names the video that holds the product (DTF-WEB-G). + if body.enabled and body.product: + other = c.execute('SELECT id FROM dtf_local.site_videos WHERE id<>%s AND product=%s AND enabled', + (video_id, body.product)).fetchone() + if other: + raise HTTPException(409, f'Este produto já tem o vídeo "{CATALOG.get(other["id"], other["id"])}" ' + 'publicado. Tire esse do Site antes, ou escolha outro lugar.') identity = operator_identity(user) keys = [] for uid, expected, previous_id, previous_key in ( @@ -113,11 +121,6 @@ def update_video(video_id: str, body: VideoUpdate, user=Depends(can('manage'))): CopySource={'Bucket': storage.bucket, 'Key': upload['object_key']}, Key=key, ContentType=mime, MetadataDirective='REPLACE') keys.append(key) - if body.enabled and body.product: - other = c.execute('SELECT id FROM dtf_local.site_videos WHERE id<>%s AND product=%s AND enabled', - (video_id, body.product)).fetchone() - if other: - raise HTTPException(409, 'Já existe um vídeo flutuante ativo para este produto.') c.execute('''INSERT INTO dtf_local.site_videos(id,upload_id,captions_id,video_key,captions_key,product,enabled,updated_by) VALUES(%s,%s,%s,%s,%s,%s,%s,%s) ON CONFLICT (id) DO UPDATE SET upload_id=EXCLUDED.upload_id,captions_id=EXCLUDED.captions_id, diff --git a/tests/site_video_test.py b/tests/site_video_test.py index 76d7dbe..5a98daa 100644 --- a/tests/site_video_test.py +++ b/tests/site_video_test.py @@ -48,6 +48,13 @@ def run(): entry = next(row for row in published if row['id'] == 'sala') assert entry['product'] == 'file' assert entry['captions'] == '/api/site/videos/sala/captions' + # One floating video per product: a second one is refused before any + # copy, naming the video that holds the product (DTF-WEB-G). + refused = operator.call('/operator/site-videos/png', {'upload_id': video, + 'product': 'file', 'enabled': True}, expected=409) + assert 'A sala às 3 da manhã' in refused['detail'], refused + with db.connect() as c: + assert not c.execute("SELECT 1 FROM dtf_local.site_videos WHERE id='png'").fetchone() with urlopen(entry['src'], timeout=30) as response: assert response.read() == VIDEO with urlopen(Request(BASE + entry['captions'], headers=with_host()), timeout=30) as response: diff --git a/web/kanban-site.js b/web/kanban-site.js index 13320f3..04d05a9 100644 --- a/web/kanban-site.js +++ b/web/kanban-site.js @@ -582,18 +582,37 @@ function videoDialog(video){ captionsField.append(node('span',video.captions_name?'Legenda':'Legenda · opcional'), dropField(captionsInput,{title:'Escolher legenda',hint:'Arquivo VTT.'}), node('small',video.captions_name||'Nenhuma legenda enviada.')); + // A product page shows one floating video: the products another published + // video holds are marked, and closed while this one is published (DTF-WEB-G). + const taken=new Map(siteVideos.filter(other=>other.id!==video.id&&other.enabled&&other.product) + .map(other=>[other.product,other.title])); const product=node('select'); for(const [value,label] of SITE_VIDEO_PRODUCTS){ - const option=node('option',label);option.value=value;product.append(option); + const option=node('option',taken.has(value)?label+' · já tem vídeo':label);option.value=value;product.append(option); } product.value=video.product||''; + const where=field('Onde aparece',product,'Ou flutuando na página de um produto.'); + const whereHint=where.querySelector('small'); + const paintWhere=()=>{ + for(const option of product.options)option.disabled=published&&taken.has(option.value); + if(!taken.has(product.value))where.classList.remove('warn'); + whereHint.textContent=taken.has(product.value) + ?'Este produto já tem o vídeo "'+taken.get(product.value)+'" publicado. Tire esse do Site para publicar este aqui.' + :'Ou flutuando na página de um produto.'; + }; + product.onchange=paintWhere; const sw=node('button',undefined,'sw'+(published?' on':'')); sw.type='button';sw.setAttribute('role','switch');sw.setAttribute('aria-checked',String(published)); sw.append(node('span',undefined,'t'),'Publicar no Site'); - sw.onclick=()=>{published=!published;sw.classList.toggle('on',published);sw.setAttribute('aria-checked',String(published));}; + sw.onclick=()=>{ + if(!published&&taken.has(product.value)){where.classList.add('warn');product.focus();return;} + published=!published;sw.classList.toggle('on',published);sw.setAttribute('aria-checked',String(published)); + paintWhere(); + }; const situation=node('div',undefined,'fld');situation.append(node('span','Situação'),sw); const row=node('div',undefined,'g2'); - row.append(field('Onde aparece',product,'Ou flutuando na página de um produto.'),situation); + row.append(where,situation); + paintWhere(); formDialog(video.title,{width:640,className:'siteDialog', text:video.enabled?'Publicado no Site':'Rascunho · ainda não aparece no Site', fields:[videoField,captionsField,row],label:'Salvar vídeo',onsave:async()=>{