Skip to content
This repository has been archived by the owner on Oct 26, 2022. It is now read-only.

Preview changed images in unchanged articles #183

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions sfdoc/publish/migrations/0031_auto_20180320_0009.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.10 on 2018-03-20 00:09
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('publish', '0030_auto_20180313_1948'),
]

operations = [
migrations.AlterField(
model_name='article',
name='status',
field=models.CharField(choices=[('N', 'New'), ('C', 'Changed'), ('U', 'Unchanged'), ('D', 'Deleted')], max_length=1),
),
]
2 changes: 2 additions & 0 deletions sfdoc/publish/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ class Article(models.Model):
"""Tracks created/updated articles per bundle."""
STATUS_NEW = 'N'
STATUS_CHANGED = 'C'
STATUS_UNCHANGED = 'U'
STATUS_DELETED = 'D'
status = models.CharField(
max_length=1,
choices=(
(STATUS_NEW, 'New'),
(STATUS_CHANGED, 'Changed'),
(STATUS_UNCHANGED, 'Unchanged'),
(STATUS_DELETED, 'Deleted'),
),
)
Expand Down
8 changes: 4 additions & 4 deletions sfdoc/publish/salesforce.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def get_preview_url(self, ka_id, online=False):
preview_url += '&pubstatus=o'
return preview_url

def process_article(self, html, bundle):
def process_article(self, html, bundle, unchanged=False):
"""Create a draft KnowledgeArticleVersion."""

# update links to draft versions
Expand All @@ -188,7 +188,7 @@ def process_article(self, html, bundle):
self.update_draft(kav_id, html)
if result_online['totalSize'] == 1:
# published version exists
status = Article.STATUS_CHANGED
status = Article.STATUS_UNCHANGED if unchanged else Article.STATUS_CHANGED
else:
# not published
status = Article.STATUS_NEW
Expand All @@ -200,13 +200,13 @@ def process_article(self, html, bundle):
# new draft of existing article
record = result_online['records'][0]
# check for changes in article fields
if html.same_as_record(record):
if not unchanged and html.same_as_record(record):
# no update
return
# create draft copy of published article
kav_id = self.create_draft(record['KnowledgeArticleId'])
self.update_draft(kav_id, html)
status = Article.STATUS_CHANGED
status = Article.STATUS_UNCHANGED if unchanged else Article.STATUS_CHANGED

self.save_article(kav_id, html, bundle, status)

Expand Down
32 changes: 31 additions & 1 deletion sfdoc/publish/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def _process_bundle(bundle, path):
url_map = {}
images = set([])
article_image_map = {}
image_article_map = {}
logger.info('Scrubbing all HTML files in %s', bundle)
for n, html_file in enumerate(html_files, start=1):
logger.info('Scrubbing HTML file %d of %d: %s',
Expand All @@ -64,6 +65,10 @@ def _process_bundle(bundle, path):
html.scrub()
article_image_map[html.url_name] = set([])
for image_path in html.get_image_paths():
basename = os.path.basename(image_path)
if basename not in image_article_map:
image_article_map[basename] = set([])
image_article_map[basename].add(html_file)
image_path_full = os.path.abspath(os.path.join(
os.path.dirname(html_file),
image_path,
Expand Down Expand Up @@ -148,12 +153,37 @@ def _process_bundle(bundle, path):
image.replace(path + os.sep, ''),
)
s3.process_image(image, bundle)
# upload unchanged draft articles for changed images
logger.info('Checking for unchanged articles that use changed images')
unchanged_html_files = set([])
for image in bundle.images.filter(status=Image.STATUS_CHANGED):
for html_file in image_article_map[image.filename]:
with open(html_file) as f:
html_raw = f.read()
html = HTML(html_raw)
if not Article.objects.filter(status__in=(
Article.STATUS_NEW,
Article.STATUS_CHANGED,
)).filter(url_name=html.url_name):
# article url name not in list of new/changed articles
unchanged_html_files.add(html_file)
for n, html_file in enumerate(unchanged_html_files, start=1):
logger.info('Uploading unchanged draft article %d of %d: %s',
n,
len(unchanged_html_files),
html.title,
)
with open(html_file) as f:
html_raw = f.read()
html = HTML(html_raw)
salesforce.process_article(html, bundle, unchanged=True)
# upload unchanged images for article previews
logger.info('Checking for unchanged images used in draft articles')
unchanged_images = set([])
for article in bundle.articles.filter(status__in=(
Article.STATUS_NEW,
Article.STATUS_CHANGED,
Article.STATUS_UNCHANGED,
)):
for image in article_image_map[article.url_name]:
if not bundle.images.filter(filename=os.path.basename(image)):
Expand All @@ -170,7 +200,7 @@ def _process_bundle(bundle, path):
if not bundle.articles.count() and not bundle.images.count():
raise SfdocError('No articles or images changed')
# finish
bundle.status = bundle.STATUS_DRAFT
bundle.status = Bundle.STATUS_DRAFT
bundle.save()


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ <h5>Changed Articles</h5>
</p>
{% endif %}

{% if articles_unchanged %}
<br>
<h5>Unchanged Articles with Changed Images</h5>
<p>
<ul>
{% for article in articles_unchanged %}
<li><a href="{{ article.preview_url }}" target="_blank">{{ article.url_name }}</a></li>
{% endfor %}
</ul>
</p>
{% endif %}

{% if articles_deleted %}
<br>
<h5>Deleted Articles</h5>
Expand Down
5 changes: 4 additions & 1 deletion sfdoc/publish/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ def review(request, pk):
'articles_changed': bundle.articles.filter(
status=Article.STATUS_CHANGED
).order_by('url_name'),
'articles_unchanged': bundle.articles.filter(
status=Article.STATUS_UNCHANGED
).order_by('url_name'),
'articles_deleted': bundle.articles.filter(
status=Article.STATUS_DELETED
).order_by('url_name'),
Expand All @@ -162,7 +165,7 @@ def review(request, pk):
status=Image.STATUS_DELETED
).order_by('filename'),
}
return render(request, 'publish.html', context=context)
return render(request, 'review.html', context=context)


@never_cache
Expand Down