def format_podcast_list(obj_list, format, title, get_podcast=None, json_map=lambda x: x.url, jsonp_padding=None, xml_template=None, request=None, template_args={}): """ Formats a list of podcasts for use in a API response obj_list is a list of podcasts or objects that contain podcasts format is one if txt, opml or json title is a label of the list if obj_list is a list of objects containing podcasts, get_podcast is the function used to get the podcast out of the each of these objects json_map is a function returning the contents of an object (from obj_list) that should be contained in the result (only used for format='json') """ def default_get_podcast(p): return p get_podcast = get_podcast or default_get_podcast if format == 'txt': podcasts = map(get_podcast, obj_list) s = '\n'.join([p.url for p in podcasts] + ['']) return HttpResponse(s, content_type='text/plain') elif format == 'opml': podcasts = map(get_podcast, obj_list) exporter = Exporter(title) opml = exporter.generate(podcasts) return HttpResponse(opml, content_type='text/xml') elif format == 'json': objs = list(map(json_map, obj_list)) return JsonResponse(objs) elif format == 'jsonp': ALLOWED_FUNCNAME = string.ascii_letters + string.digits + '_' if not jsonp_padding: return HttpResponseBadRequest('For a JSONP response, specify the name of the callback function in the jsonp parameter') if any(x not in ALLOWED_FUNCNAME for x in jsonp_padding): return HttpResponseBadRequest('JSONP padding can only contain the characters %(char)s' % {'char': ALLOWED_FUNCNAME}) objs = map(json_map, obj_list) return JsonResponse(objs, jsonp_padding=jsonp_padding) elif format == 'xml': if None in (xml_template, request): return HttpResponseBadRequest('XML is not a valid format for this request') podcasts = map(json_map, obj_list) template_args.update({'podcasts': podcasts}) return render(request, xml_template, template_args, content_type='application/xml') else: return None
def format_podcast_list(obj_list, format, title, get_podcast=None, json_map=lambda x: x.url, jsonp_padding=None, xml_template=None, request=None, template_args={}): """ Formats a list of podcasts for use in a API response obj_list is a list of podcasts or objects that contain podcasts format is one if txt, opml or json title is a label of the list if obj_list is a list of objects containing podcasts, get_podcast is the function used to get the podcast out of the each of these objects json_map is a function returning the contents of an object (from obj_list) that should be contained in the result (only used for format='json') """ def default_get_podcast(p): return p get_podcast = get_podcast or default_get_podcast if format == 'txt': podcasts = map(get_podcast, obj_list) s = '\n'.join([p.url for p in podcasts] + ['']) return HttpResponse(s, content_type='text/plain') elif format == 'opml': podcasts = map(get_podcast, obj_list) exporter = Exporter(title) opml = exporter.generate(podcasts) return HttpResponse(opml, content_type='text/xml') elif format == 'json': objs = map(json_map, obj_list) return JsonResponse(objs) elif format == 'jsonp': ALLOWED_FUNCNAME = string.letters + string.digits + '_' if not jsonp_padding: return HttpResponseBadRequest('For a JSONP response, specify the name of the callback function in the jsonp parameter') if any(x not in ALLOWED_FUNCNAME for x in jsonp_padding): return HttpResponseBadRequest('JSONP padding can only contain the characters %(char)s' % {'char': ALLOWED_FUNCNAME}) objs = map(json_map, obj_list) return JsonResponse(objs, jsonp_padding=jsonp_padding) elif format == 'xml': if None in (xml_template, request): return HttpResponseBadRequest('XML is not a valid format for this request') podcasts = map(json_map, obj_list) template_args.update({'podcasts': podcasts}) return render(request, xml_template, template_args, content_type='application/xml') else: return None
def getlist(request): emailaddr = request.GET.get('username', None) password = request.GET.get('password', None) user = auth(emailaddr, password) if user is None: return HttpResponse('@AUTHFAIL', mimetype='text/plain') dev = get_device(user, LEGACY_DEVICE_UID, request.META.get('HTTP_USER_AGENT', ''), undelete=True) podcasts = dev.get_subscribed_podcasts() title = "{username}'s subscriptions".format(username=user.username) exporter = Exporter(title) opml = exporter.generate(podcasts) return HttpResponse(opml, mimetype='text/xml')
def getlist(request): emailaddr = request.GET.get('username', None) password = request.GET.get('password', None) user = auth(emailaddr, password) if user is None: return HttpResponse('@AUTHFAIL', content_type='text/plain') dev = get_device(user, LEGACY_DEVICE_UID, request.META.get('HTTP_USER_AGENT', ''), undelete=True) podcasts = dev.get_subscribed_podcasts() title = "{username}'s subscriptions".format(username=user.username) exporter = Exporter(title) opml = exporter.generate(podcasts) return HttpResponse(opml, content_type='text/xml')
def getlist(request): emailaddr = request.GET.get('username', None) password = request.GET.get('password', None) user = auth(emailaddr, password) if user is None: return HttpResponse('@AUTHFAIL', mimetype='text/plain') dev = get_device(user, LEGACY_DEVICE_UID, request.META.get('HTTP_USER_AGENT', ''), undelete=True) podcasts = dev.get_subscribed_podcasts() # FIXME: Get username and set a proper title (e.g. "thp's subscription list") title = 'Your subscription list' exporter = Exporter(title) opml = exporter.generate(podcasts) return HttpResponse(opml, mimetype='text/xml')