Tuesday, December 30, 2014

Python / Django / Selenium: Set viewport size (window size)

The default viewport size for PhantomJs is like a phone size width. I found plenty of examples of setting the viewport size of the window for Java, C# and Ruby but not much for Python.  It's ridiculously simple.  Below is an example basically for my future reminder, but here for your enjoyment. This sets the viewport 1280px wide by 720px high.
CustomLiveServerTestCase(LiveServerTestCase):
def setUp(self):
self.wd = webdriver.PhantomJS()
self.wd.set_window_size(1280, 720)
super(CustomLiveServerTestCase, self).setUp()

def tearDown(self):
super(CustomLiveServerTestCase, self).tearDown()
self.wd.close()

Python / Django / Selenium: "Webdriver Exception: Unable to startphantomjs with ghostdriver"

You might be using an outdated version of Selenium or possibly running it on Windows. Try the latest version of Selenium for Python first.

However, if that doesn't solve your issues you might have a failing test and not closing the webdriver down in your teardown method in your test if you have a custom test case:
CustomLiveServerTestCase(LiveServerTestCase):
    def setUp(self):
        self.wd = webdriver.PhantomJS()
        super(CustomLiveServerTestCase, self).setUp()

    def tearDown(self):
        super(CustomLiveServerTestCase, self).tearDown()
        self.wd.close()

Monday, December 29, 2014

Show Django-Debug-Toolbar when development IP addresses are dynamic (Vagrant, Landrush, etc.)

In certain circumstances, your IP address in development changes and therefore it's hard to have all the IP address in the list of INTERNAL_IPS that Django Debug Toolbar uses.  In your development.py settings file (you separate them out right?), you can add this to shortcircuit the logic and allow ANY IP address (be careful):
def show_toolbar(request):
return True

DEBUG_TOOLBAR_CONFIG = {
'SHOW_TOOLBAR_CALLBACK': 'YourAppName.settings.development.show_toolbar',
}

Tuesday, December 16, 2014

Django-Stronghold with Django-Debug-Toolbar

Django Stronghold intercepts calls to Django Debug Toolbar panels which cause the panels to show your login page. This can easily be fixed to exclude the Debug Toolbar urls in your settings.py file:
STRONGHOLD_DEFAULTS = True
STRONGHOLD_PUBLIC_URLS = (
r'^/__debug__/.+$',
)

The STRONGHOLD_DEFAULTS tells Stronghold to include regex patterns for Static and Media file urls so your static assets will work too.

Wednesday, December 10, 2014

Correct connection settings for ElasticSearch / Django-Haystack on Bonsai.io

I should have read the documentation about this better, so I'm blogging this because I'm sure other people have had the same issue. Bonsai.io requires the username and password to be sent as an http_auth header.
from urlparse import urlparse

es = urlparse(os.environ.get('BONSAI_URL'))
port = es.port or 80

HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
'URL': es.scheme + '://' + es.hostname + ':' + str(port),
'INDEX_NAME': 'your_index_name',
},
}

if es.username:
HAYSTACK_CONNECTIONS['default']['KWARGS'] = {"http_auth": es.username + ':' + es.password}