Skip to content

Commit

Permalink
timestamp in export filename, and require auth
Browse files Browse the repository at this point in the history
  • Loading branch information
rloomans committed Mar 29, 2016
1 parent b5e4555 commit b6c4503
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion measure_mate/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,4 @@
CSP_FONT_SRC = ("'self'",)
CSP_EXCLUDE_URL_PREFIXES = ("/admin",)


LOGIN_URL = "/admin/login/"
23 changes: 21 additions & 2 deletions measure_mate/tests/view/test_export_view.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
from django.test import TestCase
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from rest_framework import status
import datetime


class ExportViewTestCases(TestCase):
def test_export_view(self):

@classmethod
def setUpTestData(cls):
cls.superuser = User.objects.create(
id=100,
password='sha1$995a3$6011485ea3834267d719b4c801409b8b1ddd0158',
last_login=datetime.datetime(2007, 5, 30, 13, 20, 10),
is_superuser=True, username='super', first_name='Super',
last_name='User', email='super@example.com', is_staff=True,
is_active=True, date_joined=datetime.datetime(2007, 5, 30, 13, 20, 10)
)

def test_export_view(self):
self.client.force_login(self.superuser)
response = self.client.get(reverse('export'))
self.assertTemplateNotUsed(response, 'index.html')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertRegexpMatches(response['Content-Disposition'], r'^attachment;')
self.assertRegexpMatches(response['Content-Type'], r'spreadsheet')
self.assertRegexpMatches(response['Content-Disposition'], r'filename=.*\.xlsx?')
self.assertRegexpMatches(response['Content-Type'], r'spreadsheet|excel')

def test_export_view_noauth(self):
response = self.client.get(reverse('export'))
self.assertEqual(response.status_code, status.HTTP_302_FOUND)
7 changes: 6 additions & 1 deletion measure_mate/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticatedOrReadOnly
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from serializers import *
from models import *
from headers import x_ua_compatible
from datetime import datetime
import django_excel as excel
import pyexcel.ext.xls
import pyexcel.ext.xlsx
Expand All @@ -15,10 +17,13 @@ def home(request):
return render(request, 'index.html')


@login_required
def export_data(request):
now = datetime.utcnow()
timestamp = now.strftime('%Y-%m-%d_%H-%M-%SZ')
return excel.make_response_from_tables(
[Team, Assessment, Measurement, Tag, Template, Attribute, Rating],
'xlsx', file_name='data_export')
'xls', file_name=('measure_mate_export_%s' % timestamp))


class TemplateViewSet(viewsets.ModelViewSet):
Expand Down

0 comments on commit b6c4503

Please sign in to comment.