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

Commit

Permalink
Merge pull request #2 from theonion/admin-config
Browse files Browse the repository at this point in the history
Adds BETTY_ADMIN_URL to settings
  • Loading branch information
vforgione committed Aug 1, 2015
2 parents f08db98 + b106848 commit 4d84f38
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
1 change: 1 addition & 0 deletions djbetty/conf/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.conf import settings as _settings


BETTY_ADMIN_URL = urljoin(_settings.MEDIA_URL, "images/")
BETTY_IMAGE_URL = urljoin(_settings.MEDIA_URL, "images/")
BETTY_PUBLIC_TOKEN = None
BETTY_PRIVATE_TOKEN = None
Expand Down
17 changes: 14 additions & 3 deletions djbetty/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

class BettyCropperStorage(Storage):

def __init__(self, base_url=None, public_token=None, private_token=None):
def __init__(self, base_url=None, admin_url=None, public_token=None, private_token=None):
self._admin_url = admin_url
self._base_url = base_url
self._public_token = public_token
self._private_token = private_token
Expand All @@ -16,6 +17,16 @@ def __init__(self, base_url=None, public_token=None, private_token=None):
def auth_headers(self):
return {"X-Betty-Api-Key": self.public_token}

@property
def admin_url(self):
base_url = self._admin_url
if not base_url:
base_url = settings.BETTY_ADMIN_URL
if base_url.endswith("/"):
base_url = base_url[:-1]

return base_url

@property
def base_url(self):
base_url = self._base_url
Expand All @@ -42,7 +53,7 @@ def delete(self, image_id):
raise NotImplementedError()

def exists(self, image_id):
detail_url = "{base_url}/api/{id}".format(base_url=self.base_url, id=image_id)
detail_url = "{base_url}/api/{id}".format(base_url=self.admin_url, id=image_id)
r = requests.get(detail_url, headers=self.auth_headers)
return r.status_code == 200

Expand All @@ -56,7 +67,7 @@ def get_available_name(self, image_id):
return image_id

def _save(self, name, content):
endpoint = "{base_url}/api/new".format(base_url=self.base_url)
endpoint = "{base_url}/api/new".format(base_url=self.admin_url)

data = {"name": name}
files = {"image": content}
Expand Down
11 changes: 6 additions & 5 deletions tests/test_image_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import requests # noqa


TEST_DATA_PATH = os.path.join(os.path.dirname(__file__), 'images')
TEST_DATA_PATH = os.path.join(os.path.dirname(__file__), "images")

TEST_IMAGE_DATA = json.dumps({
"id": 12345,
Expand All @@ -26,7 +26,7 @@
}).encode("UTF-8")


@urlmatch(path=r'.*api/new$')
@urlmatch(path=r".*api/new$")
def betty_new_mock(url, request):
return {
"status_code": 200,
Expand All @@ -51,6 +51,7 @@ def setUp(self):
self.base_url = self.base_url[:-1]
self._old_betty_image_url = settings.BETTY_IMAGE_URL
settings.BETTY_IMAGE_URL = self.base_url
settings.BETTY_ADMIN_URL = settings.BETTY_IMAGE_URL

def test_fileless_save(self):
test_object = TestModel()
Expand Down Expand Up @@ -84,7 +85,7 @@ def test_charfield_save(self):
self.assertEqual(test.image, None)

def test_save(self):
lenna_path = os.path.join(TEST_DATA_PATH, 'Lenna.png')
lenna_path = os.path.join(TEST_DATA_PATH, "Lenna.png")
with open(lenna_path, "rb") as lenna:
test = TestModel()
with HTTMock(betty_new_mock):
Expand All @@ -98,7 +99,7 @@ def test_save(self):
self.assertEqual(test.image.name, "Lenna.png")

def test_listing_image(self):
lenna_path = os.path.join(TEST_DATA_PATH, 'Lenna.png')
lenna_path = os.path.join(TEST_DATA_PATH, "Lenna.png")
with open(lenna_path, "rb") as lenna:
test = TestModel()
with HTTMock(betty_new_mock):
Expand All @@ -109,7 +110,7 @@ def test_listing_image(self):
self.assertEqual(test.listing_image.caption, None)

def test_alt_and_caption(self):
lenna_path = os.path.join(TEST_DATA_PATH, 'Lenna.png')
lenna_path = os.path.join(TEST_DATA_PATH, "Lenna.png")
with open(lenna_path, "rb") as lenna:
test = TestModel()
with HTTMock(betty_new_mock):
Expand Down

0 comments on commit 4d84f38

Please sign in to comment.