Skip to content

Commit 019139e

Browse files
authored
Merge pull request #5404 from benjaoming/unregistered-device
Simplify am_i_online which broke because of a 301 on kalite.learninequality.org
2 parents f5518d3 + 0744172 commit 019139e

File tree

6 files changed

+25
-21
lines changed

6 files changed

+25
-21
lines changed

docs/installguide/release_notes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ Bug fixes
7676
* Installation works with latest ``pip 9``. :url-issue:`5319`
7777
* ``kalite manage contentpackchecker all --update`` wrongly retrieved all available content packs. Now only updates *installed* content packs.
7878
* No content pack files are placed in ``STATIC_ROOT``, ensuring that ``kalite manage collectstatic`` will not remove any files from content packs (subtitles!). :url-issue:`5386` :url-issue:`5073`
79+
* Online availability incorrectly detected, bypassing registration progress on Video and Language downloads :url-issue:`5401`
7980
* **Windows**: Logging works again: Writing to ``server.log`` was disabled on Windows :url-issue:`5057`
8081
* **Dev** Loading subtitles now works in ``bin/kalite manage runserver --settings=kalite.project.settings.dev``
8182
* **Dev** Auto-discovery of content-packs in well-known location ``/usr/share/kalite/preseed/contentpack-<version>.<lang>.zip``, example: ``/usr/share/kalite/preseed/contentpack-0.17.en.zip``. Happens during ``kalite.distributed.management.commands.setup``.

kalite/packages/bundled/fle_utils/internet/functions.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,44 @@
22
For functions mucking with internet access
33
"""
44
import ifcfg
5+
import logging
56
import os
67
import platform
78
import re
89
import requests
9-
import socket
1010

11-
from urlparse import parse_qs, urlsplit, urlunsplit
11+
12+
from django.conf import settings
13+
from django.core.urlresolvers import reverse
14+
from urlparse import parse_qs, urlsplit, urlunsplit, urljoin
1215
from urllib import urlencode
1316

1417

15-
def am_i_online(url, expected_val=None, search_string=None, timeout=5, allow_redirects=True):
18+
logger = logging.getLogger(__name__)
19+
20+
21+
def am_i_online():
1622
"""Test whether we are online or not.
1723
returns True or False.
18-
Eats all exceptions!
24+
Eats all exceptions! <- great :( /benjaoming
1925
"""
20-
assert not (search_string and expected_val is not None), "Search string and expected value cannot both be set"
21-
2226
from kalite.version import user_agent
2327

28+
url = urljoin(settings.CENTRAL_SERVER_URL, reverse("get_server_info"))
29+
2430
try:
25-
if not search_string and expected_val is None:
26-
response = requests.head(url, headers={"user-agent": user_agent()})
27-
else:
28-
response = requests.get(url, timeout=timeout, allow_redirects=allow_redirects, headers={"user-agent": user_agent()})
31+
response = requests.get(url, timeout=5, allow_redirects=False, headers={"user-agent": user_agent()})
2932

3033
# Validate that response came from the requested url
3134
if response.status_code != 200:
35+
36+
logger.warning("Unexpected response detecting online status: {}".format(response))
3237
return False
33-
elif not allow_redirects and response.url != url:
34-
return False
35-
36-
# Check the output, if expected values are specified
37-
if expected_val is not None:
38-
return expected_val == response.text
39-
elif search_string:
40-
return search_string in response.text
4138

4239
return True
4340

4441
except Exception as e:
42+
logger.warning("Unhandled exception when detecting if online: {}".format(e))
4543
return False
4644

4745

kalite/packages/bundled/securesync/devices/api_views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ def get_server_info(request):
209209
if settings.CENTRAL_SERVER:
210210
device_info[field] = True
211211
else:
212-
device_info[field] = am_i_online(url="%s://%s%s" % (settings.SECURESYNC_PROTOCOL, settings.CENTRAL_SERVER_HOST, reverse("get_server_info")))
212+
device_info[field] = am_i_online()
213213

214214
elif field:
215215
# the field isn't one we know about, so add it to the list of invalid fields

kalite/packages/bundled/securesync/devices/decorators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def require_registration(resource_name):
1414
"""
1515
def real_decorator_wrapper(handler):
1616
def real_decorator_wrapper_fn(request, *args, **kwargs):
17-
if Device.get_own_device().is_registered() or not am_i_online(settings.CENTRAL_SERVER_URL):
17+
if Device.get_own_device().is_registered() or not am_i_online():
1818
return handler(request, *args, **kwargs)
1919
else:
2020
messages.warning(

kalite/settings/base.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@
7272
'level': LOGGING_LEVEL,
7373
'propagate': False,
7474
},
75+
'fle_utils': {
76+
'handlers': ['console'],
77+
'level': LOGGING_LEVEL,
78+
'propagate': False,
79+
},
7580
'cherrypy.console': {
7681
'handlers': ['console'],
7782
'level': LOGGING_LEVEL,

kalite/updates/tests/regression_tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def setUp(self):
2424
@override_settings(CENTRAL_SERVER_URL="http://127.0.0.1:8997") # We hope this is unreachable
2525
def test_not_redirected_when_offline(self):
2626
self.assertFalse(Device.get_own_device().is_registered(), "The device should be unregistered!")
27-
self.assertFalse(am_i_online(url=settings.CENTRAL_SERVER_URL), "Central server should be unreachable!")
27+
self.assertFalse(am_i_online(), "Central server should be unreachable!")
2828
updated_videos_url = self.reverse("update_videos")
2929
response = self.client.get(updated_videos_url, follow=True)
3030
redirect_chain = response.redirect_chain # Will be the empty list if there are no redirects

0 commit comments

Comments
 (0)