Пример #1
0
    def authenticate(self, request):
        try:
            token = request.META.get('HTTP_TOKEN')
            if not token:
                raise CondenastException(
                    message=FailureMessage.SESSION_INVALID.value,
                    status_code=StatusCodes.UNAUTHORIZED.value)
            user = db.UserSession.aggregate([{
                "$match": {
                    "token": token
                }
            }, {
                "$lookup": {
                    "from": "Users",
                    "localField": "userId",
                    "foreignField": "userId",
                    "as": "userData"
                }
            }, {
                "$project": {
                    "user_id": 1,
                    "userData": 1,
                    "_id": 0
                }
            }])
            if not user.alive:
                raise CondenastException(
                    message=FailureMessage.SESSION_INVALID.value,
                    status_code=StatusCodes.UNAUTHORIZED.value)

            return user, None
        except Exception as exception:
            return ErrorHandler.handle_exception(exception)
Пример #2
0
    def get(self, request):

        try:

            contributor_id = request.query_params.get(
                RequestKeys.CONTRIBUTOR_ID.value)
            limit = request.query_params.get(RequestKeys.LIMIT.value)
            page_num = request.query_params.get(RequestKeys.PAGE_NUM.value)

            if limit is None:
                limit = PaginationConstants.DEFAULT_LIMIT.value

            if page_num is None:
                page_num = PaginationConstants.DEFAULT_PAGE_NUM.value

            if contributor_id is None:
                raise CondenastException(
                    message=FailureMessage.INVALID_REQUEST.value)

            response_data = ContributorDetails.contributor_details(
                contributor_id, int(limit), int(page_num))
            return SuccessResponse.get_response_obj(data=response_data)

        except Exception as exception:
            return ErrorHandler.handle_exception(exception)
Пример #3
0
    def get_place_details(place_id, limit, page_num):

        place = db.Places.find({'place_id': place_id})
        if not place.count() > 0:
            raise CondenastException(
                message=FailureMessage.INVALID_PLACE.value)
        else:
            place = place[0]
            place_details = {}
            place_data = {}
            place_data[ResponseKeys.TITLE.value] = place.get('title')
            place_data[ResponseKeys.DESCRIPTION.value] = place.get(
                'description')
            place_data[ResponseKeys.LATITUDE.value] = place.get('location')[1]
            place_data[ResponseKeys.LONGITUDE.value] = place.get('location')[0]
            place_data[ResponseKeys.WORKING.value] = place.get('working_hours')
            place_data[ResponseKeys.CATEGORY.value] = place.get('category')
            place_data[ResponseKeys.PHONE.value] = place.get('phone')
            place_data[ResponseKeys.PLACE_ID.value] = place.get('place_id')
            place_data[ResponseKeys.COST.value] = place.get('cost')
            place_data[ResponseKeys.PLACE_IMAGE.value] = place.get('image')
            place_details[ResponseKeys.PLACE_DETAILS.value] = place_data

            feeds = db.FeedTips.find({
                'place': place_id
            }).skip(limit * page_num).limit(limit)
            place_feeds = []
            if feeds.count() > 0:
                for feed in feeds:
                    feed_details = {}
                    feed_details[ResponseKeys.FEED_DESCRIPTION.
                                 value] = feed.get('description')
                    feed_details[ResponseKeys.FEED_ID.value] = feed.get(
                        'feed_id')
                    feed_details[ResponseKeys.FEED_LIKES.value] = feed.get(
                        'number_of_likes')
                    feed_details[ResponseKeys.FEED_IMAGES.value] = feed.get(
                        'image')
                    place_feeds.append(feed_details)
                place_details[ResponseKeys.TOTAL_FEEDS.value] = feeds.count()
            place_details[ResponseKeys.PLACE_FEEDS.value] = place_feeds

            contributor = db.Contributor.find(
                {'contributor_id': place.get('contributor')})
            contributor_details = {}
            if contributor.count() > 0:
                contributor = contributor[0]
                contributor_details[ResponseKeys.CONTRIBUTOR_ID.
                                    value] = contributor.get('contributor_id')
                contributor_details[ResponseKeys.CONTRIBUTOR_NAME.
                                    value] = contributor.get('name')
                contributor_details[ResponseKeys.CONTRIBUTOR_DESCRIPTION.
                                    value] = contributor.get('description')
                contributor_details[ResponseKeys.CONTRIBUTOR_IMAGE.
                                    value] = contributor.get('profile_image')

            place_details[ResponseKeys.CONTRIBUTOR.value] = contributor_details
            return place_details
Пример #4
0
    def add_edit_tips(cls, input_request, document_id):

        feed_tip = db.FeedTips.find({'feed_id': document_id})
        if feed_tip.count() > 0:
            feed_tip = feed_tip[0]
            for key, value in input_request.iteritems():

                if key == 'place':
                    place_id = value.get('en-US').get('sys').get('id')
                    feed_tip['place'] = place_id
                elif key == 'image':
                    image_urls = []
                    url = "https:" + (client.asset(
                        value.get('en-US').get('sys').get('id')).url())
                    image_urls.append(url)
                    feed_tip['image'] = image_urls

                else:
                    feed_tip[key] = value.get('en-US')
            feed_tip['number_of_likes'] = feed_tip.get('number_of_likes')
            feed_tip['feed_id'] = feed_tip.get('feed_id')
            feed_tip['created_time'] = feed_tip.get('created_time')
            feed_tip['last_updated_time'] = datetime.now()
            db.FeedTips.update({"feed_id": document_id}, feed_tip)
            return SuccesMessages.UPDATE_SUCCESSFUL.value

        else:

            request_dictionary = {}
            for key, value in input_request.iteritems():
                if key == 'place':
                    place_id = value.get('en-US').get('sys').get('id')
                    request_dictionary['place'] = place_id

                elif key == 'image':
                    image_urls = []
                    url = "https:" + (client.asset(
                        value.get('en-US').get('sys').get('id')).url())
                    image_urls.append(url)
                    request_dictionary['image'] = image_urls
                else:
                    request_dictionary[key] = value.get('en-US')

            serializer = cls.tip_serializer(data=request_dictionary)
            if serializer.is_valid():
                tips = serializer.data
                tips['feed_id'] = document_id
                tips['number_of_likes'] = 0
                tips['created_time'] = datetime.now()
                tips['last_updated_time'] = datetime.now()
                db.FeedTips.insert_one(tips)

                return SuccesMessages.TIP_ADDED_SUCCESSFULLY.value
            else:
                raise CondenastException(
                    message=FailureMessages.SERIALIZER_ERRORS.value,
                    data=serializer.errors)
Пример #5
0
    def post(self, request):

        try:
            if not request.data:
                raise CondenastException(
                    message=FailureMessage.EMPTY_REQUEST.value)

            lat = request.data.get(RequestKeys.LATITUDE.value)
            lon = request.data.get(RequestKeys.LONGITUDE.value)
            radius = request.data.get(RequestKeys.RADIUS.value)

            places_in_range = FindPlacesFromLocation.find_near_by_places(
                lat, lon, radius)
            return SuccessResponse.get_response_obj(data=places_in_range)

        except Exception as exception:
            return ErrorHandler.handle_exception(exception)
Пример #6
0
    def add_edit_contributor(cls, input_request, document_id):

        contributor = db.Contributor.find({'contributor_id': document_id})
        if contributor.count() > 0:
            contributor = contributor[0]

            for key, value in input_request.iteritems():
                if key == 'profile_image':
                    image_id = value.get('en-US').get('sys').get('id')
                    url = client.asset(image_id).url()
                    contributor['profile_image'] = "https:" + url
                else:
                    contributor[key] = value.get('en-US')
            contributor['contributor_id'] = contributor.get('contributor_id')
            contributor['created_time'] = contributor.get('created_time')
            contributor['last_updated_time'] = datetime.now()
            db.Contributor.update({"contributor_id": document_id}, contributor)

            return SuccesMessages.UPDATE_SUCCESSFUL.value

        else:

            request_dictionary = {}
            for key, value in input_request.iteritems():

                if key == 'profile_image':
                    image_id = value.get('en-US').get('sys').get('id')
                    url = client.asset(image_id).url()
                    request_dictionary['profile_image'] = "https:" + url
                else:
                    request_dictionary[key] = value.get('en-US')

            serializer = cls.contributor_serializer(data=request_dictionary)
            if serializer.is_valid():
                contributor = serializer.data
                contributor['contributor_id'] = document_id
                contributor['created_time'] = datetime.now()
                contributor['last_updated_time'] = datetime.now()
                db.Contributor.insert_one(contributor)
                return SuccesMessages.CONTRIBUTOR_ADDED_SUCCESSFULLY.value
            raise CondenastException(
                message=FailureMessages.SERIALIZER_ERRORS.value,
                data=serializer.errors)
Пример #7
0
    def get(self, request):

        try:

            place_id = request.query_params.get(RequestKeys.PLACE_ID.value)
            limit = request.query_params.get(RequestKeys.LIMIT.value)
            page_num = request.query_params.get(RequestKeys.PAGE_NUM.value)

            if limit is None:
                limit = PaginationConstants.DEFAULT_LIMIT.value

            if page_num is None:
                page_num = PaginationConstants.DEFAULT_PAGE_NUM.value

            if not place_id:
                raise CondenastException(
                    message=FailureMessage.INVALID_REQUEST.value)

            place_details = GetPlaceDetailsFromPlaceId.get_place_details(
                place_id, int(limit), int(page_num))
            return SuccessResponse.get_response_obj(data=place_details)

        except Exception as exception:
            return ErrorHandler.handle_exception(exception)
Пример #8
0
    def add_edit_place(cls, input_request, place_id):

        # check if object exist, if yes update
        place = db.Places.find({'place_id': place_id})
        if place.count() > 0:
            place = place[0]
            for key, value in input_request.iteritems():
                if key == 'location':
                    location = value.get('en-US')
                    place_location = []
                    place_location.insert(0, location.get('lon'))
                    place_location.insert(1, location.get('lat'))
                    place['location'] = place_location

                elif key == 'contributor':
                    place['contributor'] = value.get('en-US').get('sys').get(
                        'id')

                elif key == 'feeds':
                    feeds = value.get('en-US')
                    feeds_tips = []
                    for feed in feeds:
                        feed_id = feed.get('sys').get('id')
                        feeds_tips.append(feed_id)
                        place['feeds'] = feeds_tips

                elif key == 'image':
                    image_id = value.get('en-US').get('sys').get('id')
                    url = client.asset(image_id).url()
                    url = "https:" + url
                    place['image'] = url

                else:
                    place[key] = value.get('en-US')

            place['place_id'] = place.get('place_id')
            place['created_time'] = place.get('created_time')
            place['last_updated_time'] = datetime.now()
            db.Places.update({"place_id": place_id}, place)
            return SuccesMessages.UPDATE_SUCCESSFUL.value

        # If object does not exist create
        else:
            request_dictionary = {}
            for key, value in input_request.iteritems():
                if key == 'location':
                    location = value.get('en-US')
                    place_location = []
                    place_location.insert(0, location.get('lon'))
                    place_location.insert(1, location.get('lat'))
                    request_dictionary['location'] = place_location

                elif key == 'contributor':
                    request_dictionary['contributor'] = value.get('en-US').get(
                        'sys').get('id')

                elif key == 'feeds':
                    feeds = value.get('en-US')
                    feeds_tips = []
                    for feed in feeds:
                        feed_id = feed.get('sys').get('id')
                        feeds_tips.append(feed_id)
                        request_dictionary['feeds'] = feeds_tips

                elif key == 'image':
                    image_id = value.get('en-US').get('sys').get('id')
                    url = client.asset(image_id).url()
                    url = "https:" + url
                    request_dictionary['image'] = url

                else:
                    request_dictionary[key] = value.get('en-US')

            serializer_data = cls.place_serializer(data=request_dictionary)
            if serializer_data.is_valid():
                place = serializer_data.data
                place['place_id'] = place_id
                place['created_time'] = datetime.now()
                place['last_updated_time'] = datetime.now()
                db.Places.insert_one(place)
                return SuccesMessages.PLACE_ADDED_SUCCESSFULLY.value
            else:
                raise CondenastException(
                    message=FailureMessages.SERIALIZER_ERRORS.value,
                    data=serializer_data.errors)