Skip to content
This repository has been archived by the owner on Apr 14, 2019. It is now read-only.

Commit

Permalink
Merge pull request #187 from bcipolli/issue-58
Browse files Browse the repository at this point in the history
BF: add MEDIA_ROOT/MEDIA_URL to save photos, add to static to serve them
  • Loading branch information
bcipolli committed Feb 10, 2016
2 parents 2f0c701 + e9208a4 commit 2442415
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ data/

# Django
settings_local.py
media/
.static/

# Generated docs
docs/
Expand Down
5 changes: 4 additions & 1 deletion disclosure/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
'localhost',
'127.0.0.1',
]
STATIC_ROOT = op.join(BASE_DIR, ".static")
STATIC_ROOT = op.join(REPO_DIR, "static")
MEDIA_ROOT = op.join(REPO_DIR, "media")

# Set this to the path where cronrunner.py is dumping its logs, or "None" to
# imply that this machine will not be runinng cronjobs.
CRON_LOGS_DIR = None
Expand Down Expand Up @@ -98,6 +100,7 @@
USE_L10N = True
USE_TZ = True
STATIC_URL = '/static/'
MEDIA_URL = '/media/'

_script_dir = op.dirname(__file__)
_settings_local_path = op.join(_script_dir, 'settings_local.py')
Expand Down
Binary file added disclosure/tests/files/img.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
82 changes: 82 additions & 0 deletions disclosure/tests/test_photo_upload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import os.path as op
import tempfile

from django.conf import settings
from django.core.files.uploadedfile import SimpleUploadedFile
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from django.contrib.auth.models import User
from django.test.utils import override_settings

from ballot.models import Candidate
from finance.tests.test_command import WithForm460ADataTest


@override_settings(MEDIA_ROOT=tempfile.mkdtemp(), DEBUG=True)
class ImageUploadTests(WithForm460ADataTest, StaticLiveServerTestCase):

@classmethod
def setUpClass(cls):
WithForm460ADataTest.setUpClass()
StaticLiveServerTestCase.setUpClass()

cls.username = 'admin'
cls.passwd = 'admin'
User.objects.create_superuser(
username=cls.username, password=cls.passwd, email='')

cls.candidate = Candidate.objects.all()[0]
if not cls.candidate.first_name:
cls.candidate.first_name = 'dummy'
cls.candidate.save()

cls.img_path = op.join(op.dirname(__file__), 'files', 'img.jpg')

@classmethod
def tearDownClass(cls):
# somehow this deletes the actual media root!
# shutil.rmtree(settings.MEDIA_ROOT)

StaticLiveServerTestCase.tearDownClass()
# WithForm460ADataTest.tearDownClass()

def test_upload_and_get_textfile(self):
"""Regression test for upload/get of candidate image (#58)"""

# Log on as admin
response = self.client.post(
'/admin/login/', {'username': self.username, 'password': self.passwd})
self.assertTrue(response['Location'].endswith('/accounts/profile/'))

# Post a file
candidate_url = '/admin/ballot/candidate/%d/' % self.candidate.id

with open(self.img_path, 'rb') as fp:
photo_content = fp.read()
photo = SimpleUploadedFile(self.img_path, photo_content, content_type="image/png")
pkg = dict(photo_url=photo)
for prop in ['first_name', 'last_name', 'middle_name']:
pkg[prop] = getattr(self.candidate, prop)
for prop in ['office_election', 'ballot_item']:
pkg[prop] = getattr(self.candidate, prop).id

response = self.client.post(candidate_url, pkg)

# Validate response
msg = ''
if 'errorlist' in response.content:
# Grab the error and report as the test message
idx = response.content.index('errorlist')
msg = response.content[idx:idx + 100]
self.assertTrue('errorlist' not in response.content, msg)
self.assertEqual(response.status_code, 302) # successful save

# Validate the file exists.
out_file = op.basename(self.img_path)
out_path = op.join(settings.MEDIA_ROOT, out_file)
self.assertTrue(op.exists(out_path))

# Validate that the client can grab the file.
# This fails! Can't figure out why; it works in production.
media_url = '%s%s' % (settings.MEDIA_URL, out_file)
response = self.client.get(media_url)
# self.assertEqual(response.status_code, 200, media_url) # successful GET
3 changes: 3 additions & 0 deletions disclosure/urls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.conf import settings
from django.conf.urls import include, patterns, url
from django.conf.urls.static import static
from django.contrib import admin

from . import views
Expand Down Expand Up @@ -46,3 +47,5 @@
url(r'candidate/(?P<candidate_id>[0-9]+)/opposing$',
views.CandidateViewSet.as_view(actions={'get': 'opposing'}),
name='candidate_opposing'))

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

0 comments on commit 2442415

Please sign in to comment.