Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
 def setUp(self):
     User = get_user_model()
     self.password = '******'
     self.username = '******'
     self.device_uid = 'test-device'
     self.user = User(username=self.username, email='*****@*****.**')
     self.user.set_password(self.password)
     self.user.save()
     self.user.is_active = True
     self.client = Client()
     self.extra = {
         'HTTP_AUTHORIZATION': create_auth_string(self.username,
                                                  self.password)
     }
     self.formats = ['txt', 'json', 'jsonp', 'opml']
     self.subscriptions_urls = dict(
         (fmt, self.get_subscriptions_url(fmt)) for fmt in self.formats)
     self.blank_values = {
         'txt': b'\n',
         'json': b'[]',
         'opml': Exporter('Subscriptions').generate([]),
     }
     self.all_subscriptions_url = reverse(
         'api-all-subscriptions',
         kwargs={
             'format': 'txt',
             'username': self.user.username
         },
     )
     self.toplist_urls = dict(
         (fmt, self.get_toplist_url(fmt)) for fmt in self.formats)
     self.search_urls = dict(
         (fmt, self.get_search_url(fmt)) for fmt in self.formats)
Ejemplo n.º 4
0
 def setUp(self):
     User = get_user_model()
     self.password = "******"
     self.username = "******"
     self.device_uid = "test-device"
     self.user = User(username=self.username, email="*****@*****.**")
     self.user.set_password(self.password)
     self.user.save()
     self.user.is_active = True
     self.client = Client()
     self.extra = {
         "HTTP_AUTHORIZATION": create_auth_string(self.username,
                                                  self.password)
     }
     self.formats = ["txt", "json", "jsonp", "opml"]
     self.subscriptions_urls = dict(
         (fmt, self.get_subscriptions_url(fmt)) for fmt in self.formats)
     self.blank_values = {
         "txt": b"\n",
         "json": b"[]",
         "opml": Exporter("Subscriptions").generate([]),
     }
     self.all_subscriptions_url = reverse(
         "api-all-subscriptions",
         kwargs={
             "format": "txt",
             "username": self.user.username
         },
     )
     self.toplist_urls = dict(
         (fmt, self.get_toplist_url(fmt)) for fmt in self.formats)
     self.search_urls = dict(
         (fmt, self.get_search_url(fmt)) for fmt in self.formats)
Ejemplo n.º 5
0
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')
Ejemplo n.º 6
0
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')
Ejemplo n.º 7
0
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')
Ejemplo n.º 8
0
 def test_post_subscription_valid(self):
     sample_url = 'http://example.com/directory-podcast.xml'
     podcast = Podcast.objects.get_or_create_for_url(sample_url,
                                                     defaults={
                                                         'title':
                                                         'My Podcast'
                                                     }).object
     payloads = {
         'txt': sample_url,
         'json': json.dumps([sample_url]),
         #'opml': Exporter('Subscriptions').generate([sample_url]),
         'opml': Exporter('Subscriptions').generate([podcast]),
     }
     payloads = dict(
         (fmt, format_podcast_list([podcast], fmt, 'test title').content)
         for fmt in self.formats)
     for fmt in self.formats:
         url = self.subscriptions_urls[fmt]
         payload = payloads[fmt]
         response = self.client.generic('POST', url, payload, **self.extra)
         self.assertEqual(response.status_code, 200, response.content)
Ejemplo n.º 9
0
 def test_post_subscription_valid(self):
     sample_url = "http://example.com/directory-podcast.xml"
     podcast = Podcast.objects.get_or_create_for_url(sample_url,
                                                     defaults={
                                                         "title":
                                                         "My Podcast"
                                                     }).object
     payloads = {
         "txt": sample_url,
         "json": json.dumps([sample_url]),
         #'opml': Exporter('Subscriptions').generate([sample_url]),
         "opml": Exporter("Subscriptions").generate([podcast]),
     }
     payloads = dict(
         (fmt, format_podcast_list([podcast], fmt, "test title").content)
         for fmt in self.formats)
     for fmt in self.formats:
         url = self.subscriptions_urls[fmt]
         payload = payloads[fmt]
         response = self.client.generic("POST", url, payload, **self.extra)
         self.assertEqual(response.status_code, 200, response.content)