Skip to content

Commit

Permalink
Merge pull request #2 from ImaginaryLandscape/feature/csv_download
Browse files Browse the repository at this point in the history
add csv download url
  • Loading branch information
imagescape authored Apr 28, 2017
2 parents 547fef0 + d8ceae7 commit ece3a5e
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 95 deletions.
15 changes: 15 additions & 0 deletions django_version_viewer/pip_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@


def list_package_versions():
"""
Returns a dict of installed pip packages
{'package_name': "django", 'package_version': "1.8.18"}
"""
installed_packages = pip.get_installed_distributions()
results = [{"package_name": i.key, "package_version": i.version} for i in installed_packages]
return sorted(results, key=itemgetter('package_name'))


def get_pip_packages_csv(writer):
"""
Takes a csv writer and writes installed pip packages to it.
"""
installed_packages = pip.get_installed_distributions()
for i in installed_packages:
writer.writerow([i.key, i.version])
return writer
3 changes: 2 additions & 1 deletion django_version_viewer/templates/version_viewer.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;</button>
<h4 class="modal-title">Versions</h4>
<a href='{% url "django_version_viewer_csv" %}' download>Download CSV</a>
</div>
<div class="modal-body">
<div class="box" id="packages_box">
Expand All @@ -38,7 +39,7 @@ <h4 class="modal-title">Versions</h4>
var packages;
$.ajax({
type: "GET",
url: {% url "django_version_viewer" %},
url: "{% url 'django_version_viewer' %}",
csrfmiddlewaretoken: '{{ csrf_token }}',
success: function(data){
packages = data;
Expand Down
1 change: 1 addition & 0 deletions django_version_viewer/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@

urlpatterns = [
url(r'^$', views.DjangoVersionViewer.as_view(), name='django_version_viewer'),
url(r'^csv/$', views.DjangoVersionViewerCSV.as_view(), name='django_version_viewer_csv')
]
23 changes: 21 additions & 2 deletions django_version_viewer/views.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import json
import csv

from pydoc import locate

from django.http import HttpResponse
from django.conf import settings
from django.core.exceptions import PermissionDenied
from pip_viewer import list_package_versions
from django.views.generic import View
import json

from pip_viewer import list_package_versions, get_pip_packages_csv

accessor_class = locate(
getattr(settings, 'ACCESSOR_CLASS_PATH', 'django_version_viewer.mixins.Accessor'))
Expand All @@ -19,3 +22,19 @@ def get(self, request, *args, **kwargs):
raise PermissionDenied
packages = list_package_versions()
return HttpResponse(json.dumps(packages), status=200, content_type="application/json")


class DjangoVersionViewerCSV(View):

def get(self, request, *args, **kwargs):
if not accessor.allow_access(request):
raise PermissionDenied
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="pip_package_versions.csv"'

fieldnames = ['Package Name', 'Package Version']
writer = csv.writer(response)

writer.writerow(fieldnames)
get_pip_packages_csv(writer)
return response
92 changes: 0 additions & 92 deletions example18/templates/admin/base.html

This file was deleted.

11 changes: 11 additions & 0 deletions example18/templates/admin/base_site.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% extends "admin/base.html" %}
{% load i18n pip_version_viewer_tags %}

{% block extrahead %}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
{% endblock %}
{% block pretitle %}
{% show_pip_package_versions %}
{% endblock %}

0 comments on commit ece3a5e

Please sign in to comment.