예제 #1
0
def get_storefront(request, pre_fetch=False, section=None):
    """
    Returns data for /storefront api invocation including:
        * recommended listings (max=10)
        * featured listings (no limit)
        * recent (new) listings (max=24)
        * most popular listings (max=36)

    NOTE: think about adding Bookmark status to this later on

    Args:
        username
        pre_fetch
        section(str): recommended, featured, recent, most_popular, all

    Returns:
        {
            'recommended': [Listing],
            'featured': [Listing],
            'recent': [Listing],
            'most_popular': [Listing]
        }
    """
    try:
        request_profile = models.Profile.objects.get(user__username=request.user)

        randomize_recommended = str_to_bool(request.query_params.get('randomize', True))

        section = section or 'all'

        data = {}
        extra_data = {}

        if section == 'all' or section == 'recommended':
            recommended_listings, extra_data = get_storefront_recommended(request_profile,
                                                                          pre_fetch,
                                                                          randomize_recommended)
            data['recommended'] = recommended_listings
        else:
            data['recommended'] = []

        if section == 'all' or section == 'featured':
            data['featured'] = get_storefront_featured(request_profile, pre_fetch)
        else:
            data['featured'] = []

        if section == 'all' or section == 'recent':
            data['recent'] = get_storefront_recent(request_profile, pre_fetch)
        else:
            data['recent'] = []

        if section == 'all' or section == 'most_popular':
            data['most_popular'] = get_storefront_most_popular(request_profile, pre_fetch)
        else:
            data['most_popular'] = []

    except Exception:
        # raise Exception({'error': True, 'msg': 'Error getting storefront: {0!s}'.format(str(e))})
        raise  # Should be catch in the django framwork
    return data, extra_data
예제 #2
0
    def list(self, request):
        """
        Get All Bookmarks for request profile

        Acceptance Criteria:
            The bookmark endpoint allows a user to filter the bookmarks seen by
            * shared / not-shared bookmark folders
            * allow sorting by title and created_date

        API:
            Order bookmark by title
            ```
            GET /api/bookmark/?order=title
            ```

            Reverse Order bookmark by title
            ```
            GET /api/bookmark/?order=-title
            ```

            Secondary Sort
            ```
            GET /api/bookmark/?order=-created_date&order=-title
            ```

            Show Shared Bookmarks
            ```
            GET /api/bookmark/?is_shared=true
            ```

            Show Non-Shared Bookmarks
            ```
            GET /api/bookmark/?is_shared=false
            ```

            Show Shared and Non-Shared Bookmarks
            ```
            GET /api/bookmark/?is_shared=none
            GET /api/bookmark/
            ```
        """
        current_request_profile = request.user.profile

        is_shared = str_to_bool(request.query_params.get('is_shared', None))
        ordering_fields = [
            str(record)
            for record in request.query_params.getlist('order', [])
        ]

        data = model_access.get_bookmark_tree(
            current_request_profile,
            serializer_class=serializers.BookmarkSerializer,
            request=request,
            ordering_fields=ordering_fields,
            is_shared=is_shared)
        # data = BookmarkFolder.parse_endpoint(data).shorten_data()
        return Response(data)
예제 #3
0
    def retrieve(self, request, pk=None):
        debug_recommendations = str_to_bool(request.query_params.get('debug_recommendations', False))
        ordering = request.query_params.get('ordering', None)

        data, extra_data = model_access.get_storefront(request, True, pk, ordering=ordering)
        serializer = serializers.StorefrontSerializer(data, context={'request': request})

        serialized_data = serializer.data
        self._add_recommended_scores(serialized_data, extra_data, debug_recommendations)
        return Response(serialized_data)
예제 #4
0
    def list(self, request):
        """
        Get All Bookmarks for request profile

        Acceptance Criteria:
            The bookmark endpoint allows a user to filter the bookmarks seen by
            * shared / not-shared bookmark folders
            * allow sorting by title and created_date

        API:
            Order bookmark by title
            ```
            GET /api/bookmark/?order=title
            ```

            Reverse Order bookmark by title
            ```
            GET /api/bookmark/?order=-title
            ```

            Secondary Sort
            ```
            GET /api/bookmark/?order=-created_date&order=-title
            ```

            Show Shared Bookmarks
            ```
            GET /api/bookmark/?is_shared=true
            ```

            Show Non-Shared Bookmarks
            ```
            GET /api/bookmark/?is_shared=false
            ```

            Show Shared and Non-Shared Bookmarks
            ```
            GET /api/bookmark/?is_shared=none
            GET /api/bookmark/
            ```
        """
        current_request_profile = request.user.profile

        is_shared = str_to_bool(request.query_params.get('is_shared', None))
        ordering_fields = [str(record) for record in request.query_params.getlist('order', [])]

        data = model_access.get_bookmark_tree(
            current_request_profile,
            serializer_class=serializers.BookmarkSerializer,
            request=request,
            ordering_fields=ordering_fields,
            is_shared=is_shared
        )
        # data = BookmarkFolder.parse_endpoint(data).shorten_data()
        return Response(data)
예제 #5
0
    def list(self, request):
        """
        Recommended, Featured, recent, and most popular listings
        ---
        serializer: ozpcenter.api.storefront.serializers.StorefrontSerializer
        """
        debug_recommendations = str_to_bool(request.query_params.get('debug_recommendations', False))
        ordering = request.query_params.get('ordering', None)

        data, extra_data = model_access.get_storefront(request, True, ordering=ordering)
        serializer = serializers.StorefrontSerializer(data, context={'request': request})

        serialized_data = serializer.data
        self._add_recommended_scores(serialized_data, extra_data, debug_recommendations)

        return Response(serialized_data)
예제 #6
0
def get_storefront(request, pre_fetch=False, section=None, ordering=None):
    """
    Returns data for /storefront api invocation including:
        * recommended listings (max=10)
        * featured listings (no limit)
        * recent (new) listings (max=24)
        * most popular listings (max=36)

    NOTE: think about adding Bookmark status to this later on

    Args:
        username
        pre_fetch
        section(str): recommended, featured, recent, most_popular, all

    Returns:
        {
            'recommended': [Listing],
            'featured': [Listing],
            'recent': [Listing],
            'most_popular': [Listing]
        }
    """
    try:
        request_profile = models.Profile.objects.get(user__username=request.user)

        randomize_recommended = str_to_bool(request.query_params.get('randomize', True))

        section = section or 'all'

        data = {}
        extra_data = {}

        if section == 'all' or section == 'recommended':
            recommended_listings, extra_data = get_storefront_recommended(request_profile,
                                                                          pre_fetch,
                                                                          randomize_recommended,
                                                                          ordering)
            data['recommended'] = recommended_listings
        else:
            data['recommended'] = []

        if section == 'all' or section == 'featured':
            data['featured'] = get_storefront_featured(request_profile,
                                                       pre_fetch,
                                                       ordering)
        else:
            data['featured'] = []

        if section == 'all' or section == 'recent':
            data['recent'] = get_storefront_recent(request_profile,
                                                   pre_fetch,
                                                   ordering)
        else:
            data['recent'] = []

        if section == 'all' or section == 'most_popular':
            data['most_popular'] = get_storefront_most_popular(request_profile,
                                                               pre_fetch,
                                                               ordering)
        else:
            data['most_popular'] = []

    except Exception:
        # raise Exception({'error': True, 'msg': 'Error getting storefront: {0!s}'.format(str(e))})
        raise  # Should be catch in the django framwork
    return data, extra_data
예제 #7
0
# CELERY_RESULT_BACKEND = 'rpc://'
# CELERY_RESULT_BACKEND= 'elasticsearch://example.com:9200/index_name/doc_type'  # encoding issue

# http://docs.celeryproject.org/en/latest/userguide/calling.html#calling-serializers
# CELERY_TASK_SERIALIZER = 'json'  # 'msgpack'
# CELERY_RESULT_SERIALIZER = 'json'  # 'msgpack'
# CELERY_ACCEPT_CONTENT = ['json', 'msgpack']
# CELERY_timezone = 'Europe/Oslo'
# CELERY_enable_utc = True

# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/

# CELERY REDIS CONFIGURATION
# docker-compose exec ozp_api celery -A ozp.celery worker -B -l info --scheduler django_celery_beat.schedulers:DatabaseScheduler
CELERY_ENABLED = str_to_bool(os.getenv('CELERY_ENABLED', "true"))

if CELERY_ENABLED:

    from celery.schedules import crontab

    INSTALLED_APPS += (
        'django_celery_beat',
    )

    CELERY_BROKER_URL = os.getenv('REDIS_HOST', 'redis://localhost:6379')
    CELERY_enable_utc = True
    CELERY_RESULT_BACKEND = CELERY_BROKER_URL
    CELERY_ACCEPT_CONTENT = ['application/json']
    CELERY_TASK_SERIALIZER = 'json'
    CELERY_RESULT_SERIALIZER = 'json'
    def __init__(self, request):
        self.base_url = '{scheme}://{host}'.format(scheme=request.scheme, host=request.get_host())

        self.search_string = request.query_params.get('search', None)

        if self.search_string:
            try:
                self.search_string = str(self.search_string).strip()
            except:
                self.search_string = None

        if request.query_params.get('limit', False):
            self.limit_set = True
        else:
            self.limit_set = False

        try:
            self.offset = int(request.query_params.get('offset', 0))
        except:
            self.offset = 0

        try:
            self.limit = int(request.query_params.get('limit', 100))
        except:
            self.limit = 100

        # Filtering
        self.tags = [str(record) for record in request.query_params.getlist('tag', [])]
        self.categories = [str(record) for record in request.query_params.getlist('category', [])]
        self.agencies = [str(record) for record in request.query_params.getlist('agency', [])]
        self.listing_types = [str(record) for record in request.query_params.getlist('type', [])]

        self.is_508_compliant = None

        # Override is_508_compliant  if self.request.user.profile.only_508_search_flag is true
        if request.user.profile.only_508_search_flag is True:
            self.is_508_compliant = True

        # Override is_508_compliant via params
        self.is_508_compliant_params = request.query_params.get('is_508_compliant', '').strip()  # Can be None, False, True
        if self.is_508_compliant_params:
            self.is_508_compliant = utils.str_to_bool(self.is_508_compliant_params)

        # Ordering Example: api/listings/essearch/?search=&limit=24&offset=24&ordering=-title
        self.ordering = [str(record) for record in request.query_params.getlist('ordering', [])]

        # Minscore
        try:
            self.min_score = float(request.query_params.get('minscore', constants.ES_MIN_SCORE))
        except:
            self.min_score = constants.ES_MIN_SCORE

        # Boost - Title
        try:
            self.boost_title = float(request.query_params.get('bti', constants.ES_BOOST_TITLE))
        except:
            self.boost_title = constants.ES_BOOST_TITLE

        # Boost - Description
        try:
            self.boost_description = float(request.query_params.get('bde', constants.ES_BOOST_DESCRIPTION))
        except:
            self.boost_description = constants.ES_BOOST_DESCRIPTION

        # Boost - Short Description
        try:
            self.boost_description_short = float(request.query_params.get('bds', constants.ES_BOOST_DESCRIPTION_SHORT))
        except:
            self.boost_description_short = constants.ES_BOOST_DESCRIPTION_SHORT

        # Boost - Tags
        try:
            self.boost_tags = float(request.query_params.get('btg', constants.ES_BOOST_TAGS))
        except:
            self.boost_tags = constants.ES_BOOST_TAGS
예제 #9
0
    def __init__(self, request):
        self.base_url = '{scheme}://{host}'.format(scheme=request.scheme,
                                                   host=request.get_host())

        self.search_string = request.query_params.get('search', None)

        if self.search_string:
            try:
                self.search_string = str(self.search_string).strip()
            except:
                self.search_string = None

        if request.query_params.get('limit', False):
            self.limit_set = True
        else:
            self.limit_set = False

        try:
            self.offset = int(request.query_params.get('offset', 0))
        except:
            self.offset = 0

        try:
            self.limit = int(request.query_params.get('limit', 100))
        except:
            self.limit = 100

        # Filtering
        self.tags = [
            str(record) for record in request.query_params.getlist('tag', [])
        ]
        self.categories = [
            str(record)
            for record in request.query_params.getlist('category', [])
        ]
        self.agencies = [
            str(record)
            for record in request.query_params.getlist('agency', [])
        ]
        self.listing_types = [
            str(record) for record in request.query_params.getlist('type', [])
        ]

        self.is_508_compliant = None

        # Override is_508_compliant  if self.request.user.profile.only_508_search_flag is true
        if request.user.profile.only_508_search_flag is True:
            self.is_508_compliant = True

        # Override is_508_compliant via params
        self.is_508_compliant_params = request.query_params.get(
            'is_508_compliant', '').strip()  # Can be None, False, True
        if self.is_508_compliant_params:
            self.is_508_compliant = utils.str_to_bool(
                self.is_508_compliant_params)

        # Ordering Example: api/listings/essearch/?search=&limit=24&offset=24&ordering=-title
        self.ordering = [
            str(record)
            for record in request.query_params.getlist('ordering', [])
        ]

        # Minscore
        try:
            self.min_score = float(
                request.query_params.get('minscore', constants.ES_MIN_SCORE))
        except:
            self.min_score = constants.ES_MIN_SCORE

        # Boost - Title
        try:
            self.boost_title = float(
                request.query_params.get('bti', constants.ES_BOOST_TITLE))
        except:
            self.boost_title = constants.ES_BOOST_TITLE

        # Boost - Description
        try:
            self.boost_description = float(
                request.query_params.get('bde',
                                         constants.ES_BOOST_DESCRIPTION))
        except:
            self.boost_description = constants.ES_BOOST_DESCRIPTION

        # Boost - Short Description
        try:
            self.boost_description_short = float(
                request.query_params.get('bds',
                                         constants.ES_BOOST_DESCRIPTION_SHORT))
        except:
            self.boost_description_short = constants.ES_BOOST_DESCRIPTION_SHORT

        # Boost - Tags
        try:
            self.boost_tags = float(
                request.query_params.get('btg', constants.ES_BOOST_TAGS))
        except:
            self.boost_tags = constants.ES_BOOST_TAGS
from ozpcenter.utils import str_to_bool
from ozpcenter import models
from ozpcenter.api.notification import model_access as notification_model_access
from ozpcenter.recommend.recommend import RecommenderDirectory
import ozpcenter.api.listing.model_access as listing_model_access
import ozpcenter.api.profile.model_access as profile_model_access
import ozpcenter.api.listing.model_access_es as model_access_es
import ozpcenter.api.bookmark.model_access as bookmark_model_access

TEST_BASE_PATH = os.path.realpath(os.path.join(os.path.dirname(__file__)))
TEST_IMG_PATH = os.path.join(TEST_BASE_PATH, 'test_images') + '/'
TEST_DATA_PATH = os.path.join(TEST_BASE_PATH, 'test_data')
DEMO_APP_ROOT = settings.OZP['DEMO_APP_ROOT']

FAST_MODE = str_to_bool(os.getenv('FAST_MODE', False))

ES_ENABLED = settings.ES_ENABLED

# Create AML 3.0 Bookmarks
AML_BOOKMARK_FLAG = str_to_bool(os.getenv('AML_BOOKMARK_FLAG', True))


def print_settings():
    print('Local Settings')
    print('-' * 10)
    print('TEST_BASE_PATH: {}'.format(TEST_BASE_PATH))
    print('TEST_IMG_PATH: {}'.format(TEST_IMG_PATH))
    print('TEST_DATA_PATH: {}'.format(TEST_BASE_PATH))
    print('DEMO_APP_ROOT: {}'.format(DEMO_APP_ROOT))
예제 #11
0
    }
}

# Plugin Info
ACCESS_CONTROL_PLUGIN = 'default_access_control'
AUTHORIZATION_PLUGIN = 'default_authorization'

# Set to empty string if no default agency exists
# If a default agency exists, set it to the agency's short name
DEFAULT_AGENCY = ''

# Number of seconds to cache data
GLOBAL_SECONDS_TO_CACHE_DATA = 60 * 60 * 24  # 24 Hours

# Boolean to enable/disable the use Elasticsearch use
ES_ENABLED = str_to_bool(os.getenv(
    'ES_ENABLED', False))  # This needs to be false for unit test to pass
ES_INDEX_NAME = 'appsmall'
ES_TYPE_NAME = 'listings'
ES_ID_FIELD = 'id'
ES_RECOMMEND_USER = '******'
ES_RECOMMEND_CONTENT = 'es_recommend_content'
ES_RECOMMEND_TYPE = 'recommend'

ES_NUMBER_OF_SHARDS = 1
ES_NUMBER_OF_REPLICAS = 0

ES_HOST = [{"host": "localhost", "port": 9200}]

ES_BASIC_AUTH = str_to_bool(os.getenv('ES_BASIC_AUTH', False))
ES_AUTH_USERNAME = os.getenv('ES_AUTH_USERNAME', 'user')
ES_AUTH_PASSWORD = os.getenv('ES_AUTH_PASSWORD', 'password')
예제 #12
0
    }
}

# Plugin Info
ACCESS_CONTROL_PLUGIN = 'default_access_control'
AUTHORIZATION_PLUGIN = 'default_authorization'

# Set to empty string if no default agency exists
# If a default agency exists, set it to the agency's short name
DEFAULT_AGENCY = ''

# Number of seconds to cache data
GLOBAL_SECONDS_TO_CACHE_DATA = 60 * 60 * 24  # 24 Hours

# Boolean to enable/disable the use Elasticsearch use
ES_ENABLED = str_to_bool(os.getenv('ES_ENABLED', False))  # This needs to be false for unit test to pass
ES_INDEX_NAME = 'appsmall'
ES_TYPE_NAME = 'listings'
ES_ID_FIELD = 'id'
ES_RECOMMEND_USER = '******'
ES_RECOMMEND_CONTENT = 'es_recommend_content'
ES_RECOMMEND_TYPE = 'recommend'

ES_NUMBER_OF_SHARDS = 1
ES_NUMBER_OF_REPLICAS = 0

ES_HOST = [{
    "host": "localhost",
    "port": 9200
}]
예제 #13
0
from ozpcenter.utils import str_to_bool
from ozpcenter import models
from ozpcenter.api.notification import model_access as notification_model_access
from ozpcenter.recommend.recommend import RecommenderDirectory
import ozpcenter.api.listing.model_access as listing_model_access
import ozpcenter.api.profile.model_access as profile_model_access
import ozpcenter.api.listing.model_access_es as model_access_es
import ozpcenter.api.bookmark.model_access as bookmark_model_access

TEST_BASE_PATH = os.path.realpath(os.path.join(os.path.dirname(__file__)))
TEST_IMG_PATH = os.path.join(TEST_BASE_PATH, 'test_images') + '/'
TEST_DATA_PATH = os.path.join(TEST_BASE_PATH, 'test_data')
DEMO_APP_ROOT = settings.OZP['DEMO_APP_ROOT']

FAST_MODE = str_to_bool(os.getenv('FAST_MODE', False))

ES_ENABLED = settings.ES_ENABLED

# Create AML 3.0 Bookmarks
AML_BOOKMARK_FLAG = str_to_bool(os.getenv('AML_BOOKMARK_FLAG', True))


def print_settings():
    print('Local Settings')
    print('-' * 10)
    print('TEST_BASE_PATH: {}'.format(TEST_BASE_PATH))
    print('TEST_IMG_PATH: {}'.format(TEST_IMG_PATH))
    print('TEST_DATA_PATH: {}'.format(TEST_BASE_PATH))
    print('DEMO_APP_ROOT: {}'.format(DEMO_APP_ROOT))