Add basic CSV export for other distro comparisons

Add the ability to export the comparison search results to CSV format in
order to allow importing the data into external tools.

Note: I implemented this in a different way than the earlier recipe CSV
export, i.e. it uses a template to render the CSV instead of a
function-based view with the Python csv module - the reason for this is
we can reuse the same view as we use for producing the search, with all
of the flexibility that gives us.

Signed-off-by: Paul Eggleton <paul.eggleton@linux.intel.com>
This commit is contained in:
Paul Eggleton 2018-02-14 14:28:43 +13:00
parent 09ef043f3f
commit fb2907a5d8
5 changed files with 24 additions and 0 deletions

View File

@ -0,0 +1,12 @@
from django import template
from .. import utils
register = template.Library()
@register.filter
def replace_commas(string):
return string.replace(',', '_')
@register.filter
def squashspaces(strval):
return utils.squashspaces(strval)

View File

@ -142,6 +142,12 @@ urlpatterns = [
ClassicRecipeSearchView.as_view( ClassicRecipeSearchView.as_view(
template_name='layerindex/classicrecipes.html'), template_name='layerindex/classicrecipes.html'),
name='comparison_recipe_search'), name='comparison_recipe_search'),
url(r'^comparison/search-csv/(?P<branch>[-\w]+)/$',
ClassicRecipeSearchView.as_view(
template_name='layerindex/classicrecipes_csv.txt',
paginate_by=0,
content_type='text/csv'),
name='comparison_recipe_search_csv'),
url(r'^comparison/stats/(?P<branch>[-\w]+)/$', url(r'^comparison/stats/(?P<branch>[-\w]+)/$',
ClassicRecipeStatsView.as_view( ClassicRecipeStatsView.as_view(
template_name='layerindex/classicstats.html'), template_name='layerindex/classicstats.html'),

View File

@ -14,6 +14,7 @@ import time
import fcntl import fcntl
import signal import signal
import codecs import codecs
import re
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
def get_branch(branchname): def get_branch(branchname):
@ -430,3 +431,5 @@ def sanitise_html(html):
return soup.renderContents() return soup.renderContents()
def squashspaces(string):
return re.sub("\s+", " ", string).strip()

View File

@ -123,6 +123,7 @@
</tbody> </tbody>
</table> </table>
<button class="btn" type="submit">Search</button> <button class="btn" type="submit">Search</button>
<button class="btn" type="submit" formaction="{% url 'comparison_recipe_search_csv' branch.name %}">Export CSV</button>
</form> </form>
</div> </div>

View File

@ -0,0 +1,2 @@
{% load extrafilters %}{% for recipe in recipe_list %}{{ recipe.name }},{{ recipe.layerbranch.layer.name }},{{ recipe.short_desc|replace_commas|squashspaces }},{{ recipe.section }},{% if recipe.cover_recipe %}{{ recipe.cover_recipe.get_cover_status_display }}{% endif %},{% if recipe.cover_recipe %}{{ recipe.cover_recipe.pn }}{% endif %}
{% endfor %}