Commit 3f929377 authored by Vladimir Mencl's avatar Vladimir Mencl
Browse files

AllServicePoints: fix MultipleObjectsReturned err

The get_all_services() breaks with unhandled exception MultipleObjectsReturned
when there are multiple names (for either Institution or Service Location) with
the selected language.

And, this is permitted in the model, and we have users who create such DB state.

Fix this by using filter(lang=...) instead of get(lang=...)

And wrap the handling this for both org_name and loc_name in a single generic
function - taking a
django.contrib.contenttypes.fields.GenericRelatedObjectManager reference,
language, default_language and default_value.

This also removes the exception handling clutter from get_all_services().
parent 462aec17
...@@ -1525,22 +1525,8 @@ def get_all_services(request): ...@@ -1525,22 +1525,8 @@ def get_all_services(request):
else: else:
response_location['enc'] = u"-" response_location['enc'] = u"-"
response_location['AP_no'] = u"%s" % (sl.AP_no) response_location['AP_no'] = u"%s" % (sl.AP_no)
try: response_location['inst'] = get_i18n_name(sl.institutionid.org_name, lang, 'en', 'unknown')
response_location['inst'] = sl.institutionid.org_name.get( response_location['name'] = get_i18n_name(sl.loc_name, lang, 'en', 'uknown')
except Name_i18n.DoesNotExist:
response_location['inst'] = sl.institutionid.org_name.get(lang='en').name
except Name_i18n.DoesNotExist:
response_location['inst'] = 'unknown'
response_location['name'] = sl.loc_name.get(lang=lang).name
except Name_i18n.DoesNotExist:
response_location['name'] = sl.loc_name.get(lang='en').name
except Name_i18n.DoesNotExist:
response_location['name'] = 'unknown'
response_location['port_restrict'] = u"%s" % (sl.port_restrict) response_location['port_restrict'] = u"%s" % (sl.port_restrict)
response_location['transp_proxy'] = u"%s" % (sl.transp_proxy) response_location['transp_proxy'] = u"%s" % (sl.transp_proxy)
response_location['IPv6'] = u"%s" % (sl.IPv6) response_location['IPv6'] = u"%s" % (sl.IPv6)
...@@ -2359,3 +2345,13 @@ def lookupShibAttr(attrmap, requestMeta): ...@@ -2359,3 +2345,13 @@ def lookupShibAttr(attrmap, requestMeta):
if len(requestMeta[attr]) > 0: if len(requestMeta[attr]) > 0:
return requestMeta[attr] return requestMeta[attr]
return '' return ''
def get_i18n_name(i18n_name, lang, default_lang='en', default_name='unknown'):
names = i18n_name.filter(lang=lang)
if names.count()==0:
names = i18n_name.filter(lang=default_lang)
if names.count()==0:
return default_name
# follow ServiceLoc.get_name()
return ', '.join([ for i in names])
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment