Make sure bundle auths to partners with collections show in my_library
@@ -2,7 +2,7 @@ import datetime
from django.db.models import Q
-from TWLight.resources.models import Partner
+from TWLight.resources.models import Partner, Stream
from TWLight.users.models import Authorization
@@ -48,6 +48,34 @@ def get_valid_partner_authorizations(partner_pk, stream_pk=None):
return Authorization.objects.none()
+def create_resource_dict(name, authorization, partner, stream):
+ resource_item = {
+ "name": name,
+ "partner": partner,
+ "authorization": authorization,
+ "stream": stream
+ }
+
+ if stream:
+ access_url = stream.get_access_url
+ else:
+ access_url = partner.get_access_url
+ resource_item["access_url"] = access_url
+
+ valid_authorization = authorization.is_valid
+ resource_item["valid_proxy_authorization"] = (
+ partner.authorization_method == partner.PROXY
+ and valid_authorization
+ )
+ resource_item["valid_authorization_with_access_url"] = (
+ access_url
+ and valid_authorization
+ and authorization.user.userprofile.terms_of_use
+ )
+
+ return resource_item
+
+
def sort_authorizations_into_resource_list(authorizations):
"""
Given a queryset of Authorization objects, return a
@@ -59,34 +87,39 @@ def sort_authorizations_into_resource_list(authorizations):
if authorizations:
for authorization in authorizations:
for partner in authorization.partners.all():
+ stream = authorization.stream
+ partner_streams = Stream.objects.filter(
+ partner=partner
+ )
+ if partner_streams and not stream:
+ # If this authorization wasn't to a specific stream, but the
+ # partner has streams, we assume that means all streams
+ # need to be included, with the same auth method.
+ # This is specifically useful for Bundle authorizations to
+ # partners with multiple streams.
+ for stream in partner_streams:
+ resource_list.append(
+ create_resource_dict(
+ stream.name,
+ authorization,
+ partner,
+ stream)
+ )
+ else:
# Name this item according to whether this authorization is
# to a stream
- stream = authorization.stream
if stream:
name_string = stream.name
else:
name_string = partner.company_name
- resource_list.append({"name": name_string})
- this_item = resource_list[-1]
-
- this_item["partner"] = partner
- this_item["authorization"] = authorization
- this_item["stream"] = stream
- if stream:
- access_url = stream.get_access_url
- else:
- access_url = partner.get_access_url
- this_item["access_url"] = access_url
- valid_authorization = authorization.is_valid
- this_item["valid_proxy_authorization"] = (
- partner.authorization_method == partner.PROXY
- and valid_authorization
+ resource_list.append(
+ create_resource_dict(
+ name_string,
+ authorization,
+ partner,
+ stream
)
- this_item["valid_authorization_with_access_url"] = (
- access_url
- and valid_authorization
- and authorization.user.userprofile.terms_of_use
)
# Alphabetise by name
