Exemplo n.º 1
0
    def test_get_rating(self):
        """
        utils.get_rating(user, channel) should return the rating
        the user gave the channel, or none if it hasn't been rated.
        """
        self.assertEquals(utils.get_rating(self.owner, self.channels[0]), None)
        rating, created = Rating.objects.get_or_create(
            channel=self.channels[0], user=self.owner)
        rating.rating = 5
        rating.save()

        self.assertEquals(utils.get_rating(self.owner, self.channels[0]), 5)
Exemplo n.º 2
0
    def test_get_rating(self):
        """
        utils.get_rating(user, channel) should return the rating
        the user gave the channel, or none if it hasn't been rated.
        """
        self.assertEquals(utils.get_rating(self.owner,
                                         self.channels[0]), None)
        rating, created = Rating.objects.get_or_create(
            channel=self.channels[0], user=self.owner)
        rating.rating = 5
        rating.save()

        self.assertEquals(utils.get_rating(self.owner,
                                         self.channels[0]), 5)
Exemplo n.º 3
0
def get_channel(request):
    if not ('id' in request.GET or 'url' in request.GET):
        return error_response(request, 'MISSING_ARGUMENT',
                              "get_channel requires either an id or a URL")
    channels = []
    for value in request.GET.getlist('id'):
        try:
            channels.append(api_utils.get_channel(
                                                  int(value)))
        except (LookupError, ValueError):
            return error_response(request, 'CHANNEL_NOT_FOUND',
                                  'Channel %s not found' % value)
    for value in request.GET.getlist('url'):
        try:
            channels.append(api_utils.get_channel_by_url(
                                                         value))
        except LookupError:
            return error_response(request, 'CHANNEL_NOT_FOUND',
                                  'Channel %s not found' % value)
    if request.user.is_authenticated():
        for channel in channels:
            channel.score = api_utils.get_rating(
                                                 request.user,
                                                 channel)
    if len(channels) == 1:
        data = data_for_channel(channels[0])
    else:
        data = map(data_for_channel, channels)
    return response_for_data(request, data)
Exemplo n.º 4
0
def get_item(request):
    if not ('id' in request.GET or 'url' in request.GET):
        return error_response(request, 'MISSING_ARGUMENT',
                              "get_item requires either an id or a URL")
    items = []
    for value in request.GET.getlist('id'):
        try:
            items.append(api_utils.get_item(int(value)))
        except (LookupError, ValueError):
            return error_response(request, 'ITEM_NOT_FOUND',
                                  'Item %s not found' % value)
    for value in request.GET.getlist('url'):
        try:
            items.append(api_utils.get_item_by_url(value))
        except LookupError:
            return error_response(request, 'ITEM_NOT_FOUND',
                                  'Item %s not found' % value)
    if request.user.is_authenticated():
        for item in items:
            item.score = api_utils.get_rating(request.user, item)
    if len(items) == 1:
        data = data_for_item(items[0])
    else:
        data = map(data_for_item, items)
    return response_for_data(request, data)
Exemplo n.º 5
0
def get_channel(request):
    if not ('id' in request.GET or 'url' in request.GET):
        return error_response(request, 'MISSING_ARGUMENT',
                              "get_channel requires either an id or a URL")
    channels = []
    for value in request.GET.getlist('id'):
        try:
            channels.append(api_utils.get_channel(int(value)))
        except (LookupError, ValueError):
            return error_response(request, 'CHANNEL_NOT_FOUND',
                                  'Channel %s not found' % value)
    for value in request.GET.getlist('url'):
        try:
            channels.append(api_utils.get_channel_by_url(value))
        except LookupError:
            return error_response(request, 'CHANNEL_NOT_FOUND',
                                  'Channel %s not found' % value)
    if request.user.is_authenticated():
        for channel in channels:
            channel.score = api_utils.get_rating(request.user, channel)
    if len(channels) == 1:
        data = data_for_channel(channels[0])
    else:
        data = map(data_for_channel, channels)
    return response_for_data(request, data)
Exemplo n.º 6
0
def get_item(request):
    if not ('id' in request.GET or 'url' in request.GET):
        return error_response(request, 'MISSING_ARGUMENT',
                              "get_item requires either an id or a URL")
    items = []
    for value in request.GET.getlist('id'):
        try:
            items.append(api_utils.get_item(int(value)))
        except (LookupError, ValueError):
            return error_response(request, 'ITEM_NOT_FOUND',
                                  'Item %s not found' % value)
    for value in request.GET.getlist('url'):
        try:
            items.append(api_utils.get_item_by_url(value))
        except LookupError:
            return error_response(request, 'ITEM_NOT_FOUND',
                                  'Item %s not found' % value)
    if request.user.is_authenticated():
        for item in items:
            item.score = api_utils.get_rating(request.user, item)
    if len(items) == 1:
        data = data_for_item(items[0])
    else:
        data = map(data_for_item, items)
    return response_for_data(request, data)
Exemplo n.º 7
0
def rate(request):
    try:
        channel = api_utils.get_channel(int(request.GET.get('id')))
    except (LookupError, ValueError):
        return error_response(request, 'CHANNEL_NOT_FOUND',
                              'Channel %s not found' % request.GET.get('id'))
    if 'rating' in request.GET:
        api_utils.rate(request.user, channel, request.GET['rating'])
    return response_for_data(
        request, {'rating': api_utils.get_rating(request.user, channel)})
Exemplo n.º 8
0
def rate(request):
    try:
        channel = api_utils.get_channel(
                                        int(request.GET.get('id')))
    except (LookupError, ValueError):
        return error_response(request, 'CHANNEL_NOT_FOUND',
                              'Channel %s not found' % request.GET.get('id'))
    if 'rating' in request.GET:
        api_utils.rate(request.user, channel, request.GET['rating'])
    return response_for_data(request,
                             {'rating':
                                  api_utils.get_rating(
                request.user, channel)})