예제 #1
0
    def read(self, request):
        logger.debug("OfferingCollection.read()")
        try:
            # Read the query string in order to know the filter and the page
            filter_ = request.GET.get('filter', 'published')
            user = request.user
            action = request.GET.get('action', None)
            sort = request.GET.get('sort', None)

            # Check sorting values
            if sort != None:
                if sort != 'date' and sort != 'popularity' and sort != 'name':
                    return build_response(request, 400, 'Invalid sorting')

            pagination = {
                'skip': request.GET.get('start', None),
                'limit': request.GET.get('limit', None)
            }

            if action != 'count':
                if pagination['skip'] and pagination['limit']:
                    if filter_ == 'provided':
                        result = get_offerings(user, request.GET.get('state'), owned=True, pagination=pagination, sort=sort)

                    elif filter_ == 'published':
                        result = get_offerings(user, pagination=pagination, sort=sort)

                    elif filter_ == 'purchased':
                        result = get_offerings(user, 'purchased', owned=True, pagination=pagination, sort=sort)
                else:
                    if filter_ == 'provided':
                        result = get_offerings(user, request.GET.get('state'), owned=True, sort=sort)

                    elif filter_ == 'published':
                        result = get_offerings(user, sort=sort)

                    elif filter_ == 'purchased':
                        result = get_offerings(user, 'purchased', owned=True, sort=sort)

            else:
                if filter_ == 'provided':
                    result = count_offerings(user, request.GET.get('state'), owned=True)

                elif filter_ == 'published':
                    result = count_offerings(user)

                elif filter_ == 'purchased':
                    result = count_offerings(user, 'purchased', owned=True)

        except Exception as e:
            return build_response(request, 400, unicode(e))

        mime_type = 'application/JSON; charset=UTF-8'
        return HttpResponse(json.dumps(result), status=200, mimetype=mime_type)
예제 #2
0
    def read(self, request):
        try:
            # Read the query string in order to know the filter and the page
            filter_ = request.GET.get('filter', 'published')
            user = request.user
            action = request.GET.get('action', None)
            sort = request.GET.get('sort', None)

            state = request.GET.get('state', None)
            start = request.GET.get('start', None)
            limit = request.GET.get('limit', None)

            if state:
                if state == 'ALL':
                    state = ['uploaded', 'published', 'deleted']
                else:
                    state = state.split(',')

            # Check sorting values
            if sort is not None:
                if sort != 'date' and sort != 'popularity' and sort != 'name':
                    return build_response(request, 400, 'Invalid query string: Invalid sorting')

            # Build pagination
            pagination = None
            if start and limit:
                pagination = {
                    'skip': start,
                    'limit': limit
                }

            if action != 'count':
                result = get_offerings(user, filter_, state, pagination=pagination, sort=sort)
            else:
                result = count_offerings(user, filter_, state)

        except Exception as e:
            return build_response(request, 400, unicode(e))

        mime_type = 'application/JSON; charset=UTF-8'
        return HttpResponse(json.dumps(result), status=200, mimetype=mime_type)
예제 #3
0
    def test_count_offerings(self, side_effect, expected_count, states, filter_, err_type=None, err_msg=None):

        if side_effect:
            side_effect(self)

        error = None
        try:
            return_value = offerings_management.count_offerings(self.user, filter_, states)
        except Exception as e:
            error = e

        if not err_type:
            self.assertFalse(error)
            self.assertEquals(return_value['number'], expected_count)

            if filter_ == 'provided':
                offerings_management.Offering.objects.filter.assert_called_once_with(owner_admin_user=self.user, state__in=states, owner_organization=self.org)
            elif filter_ == 'published':
                offerings_management.Offering.objects.filter.assert_called_once_with(state=filter_)
        else:
            self.assertTrue(isinstance(error, err_type))
            self.assertEquals(unicode(error), err_msg)
예제 #4
0
    def test_count_offerings(self, side_effect, expected_count, states, filter_, err_type=None, err_msg=None):

        if side_effect:
            side_effect(self)

        error = None
        try:
            return_value = offerings_management.count_offerings(self.user, filter_, states)
        except Exception as e:
            error = e

        if not err_type:
            self.assertFalse(error)
            self.assertEquals(return_value["number"], expected_count)

            if filter_ == "provided":
                offerings_management.Offering.objects.filter.assert_called_once_with(
                    owner_admin_user=self.user, state__in=states, owner_organization=self.org
                )
            elif filter_ == "published":
                offerings_management.Offering.objects.filter.assert_called_once_with(state=filter_)
        else:
            self.assertTrue(isinstance(error, err_type))
            self.assertEquals(unicode(error), err_msg)