Ejemplo n.º 1
0
    def anno_my_stuff(self, request):
        """
        Exposes an API endpoint to return all my anno list.
        """
        user = auth_user(self.request_state.headers)
        anno_list = Anno.query_anno_by_author(user)
        vote_list = Vote.query_vote_by_author(user)
        for vote in vote_list:
            anno = Anno.get_by_id(vote.anno_key.id())
            if anno is not None:
                anno_list.append(anno)
        flag_list = Flag.query_flag_by_author(user)
        for flag in flag_list:
            anno = Anno.get_by_id(flag.anno_key.id())
            if anno is not None:
                anno_list.append(anno)
        followup_list = FollowUp.query_followup_by_author(user)
        for followup in followup_list:
            anno = Anno.get_by_id(followup.anno_key.id())
            if anno is not None:
                anno_list.append(anno)
        anno_set = list(set(anno_list))

        anno_message_list = []
        for anno in anno_set:
            anno_message_list.append(anno.to_response_message())
        return AnnoListMessage(anno_list=anno_message_list)
Ejemplo n.º 2
0
    def anno_list(self, request):
        """
        Exposes an API endpoint to retrieve a list of anno.
        """
        user = auth_user(self.request_state.headers)
        limit = 10
        if request.limit is not None:
            limit = request.limit

        curs = None
        if request.cursor is not None:
            try:
                curs = Cursor(urlsafe=request.cursor)
            except BadValueError:
                raise endpoints.BadRequestException('Invalid cursor %s.' % request.cursor)

        select_projection = None
        if request.select is not None:
            select_projection = request.select.split(',')

        if request.query_type == 'by_created':
            return Anno.query_by_app_by_created(request.app, limit, select_projection, curs)
        elif request.query_type == 'by_vote_count':
            return Anno.query_by_vote_count(request.app)
        elif request.query_type == 'by_flag_count':
            return Anno.query_by_flag_count(request.app)
        elif request.query_type == 'by_activity_count':
            return Anno.query_by_activity_count(request.app)
        elif request.query_type == 'by_last_activity':
            return Anno.query_by_last_activity(request.app)
        elif request.query_type == 'by_country':
            return Anno.query_by_country(request.app)
        else:
            return Anno.query_by_page(limit, select_projection, curs)
Ejemplo n.º 3
0
    def followup_insert(self, request):
        """
        Exposes and API endpoint to insert a follow up for the current user.
        """
        user = auth_user(self.request_state.headers)

        anno = Anno.get_by_id(request.anno_id)
        if anno is None:
            raise endpoints.NotFoundException('No anno entity with the id "%s" exists.' % request.id)

        followup = FollowUp()
        followup.anno_key = anno.key
        followup.creator = user.key
        followup.comment = request.comment
        if request.created is not None:
            followup.created = request.created
        followup.put()

        anno.followup_count += 1
        anno.last_update_time = datetime.datetime.now()
        anno.last_activity = 'follwup'
        anno.last_update_type = 'create'
        anno.put()
        # update search document
        put_search_document(anno.generate_search_document())
        return followup.to_message()
Ejemplo n.º 4
0
    def followup_list(self, request):
        """
        Exposes an API endpoint to retrieve a list of follow up.
        """
        user = auth_user(self.request_state.headers)
        limit = 10
        if request.limit is not None:
            limit = request.limit

        curs = None
        if request.cursor is not None:
            try:
                curs = Cursor(urlsafe=request.cursor)
            except BadValueError:
                raise endpoints.BadRequestException('Invalid cursor %s.' % request.cursor)

        query = FollowUp.query().order(-FollowUp.created)
        if curs is not None:
            followups, next_curs, more = query.fetch_page(limit, start_cursor=curs)
        else:
            followups, next_curs, more = query.fetch_page(limit)

        items = [entity.to_message() for entity in followups]
        if more:
            return FollowupListMessage(followup_list=items, cursor=next_curs.urlsafe(), has_more=more)
        else:
            return FollowupListMessage(followup_list=items, has_more=more)
Ejemplo n.º 5
0
 def anno_get(self, request):
     """
     Exposes an API endpoint to get an anno detail by the specified id.
     """
     try:
         user = auth_user(self.request_state.headers)
     except Exception:
         user = None
     if request.id is None:
         raise endpoints.BadRequestException('id field is required.')
     anno = Anno.get_by_id(request.id)
     if anno is None:
         raise endpoints.NotFoundException('No anno entity with the id "%s" exists.' % request.id)
         # set anno basic properties
     anno_resp_message = anno.to_response_message()
     # set anno association with followups
     followups = FollowUp.find_by_anno(anno)
     followup_messages = [entity.to_message() for entity in followups]
     anno_resp_message.followup_list = followup_messages
     # set anno association with votes/flags
     # if current user exists, then fetch vote/flag.
     if user is not None:
         anno_resp_message.is_my_vote = Vote.is_belongs_user(anno, user)
         anno_resp_message.is_my_flag = Flag.is_belongs_user(anno, user)
     return anno_resp_message
Ejemplo n.º 6
0
 def update_password(self, request):
     current_user = get_endpoints_current_user(raise_unauthorized=False)
     if current_user is not None:
         raise endpoints.BadRequestException("Google OAuth User can't update password.")
     user = auth_user(self.request_state.headers)
     user.password = md5(request.password)
     user.put()
     return message_types.VoidMessage()
Ejemplo n.º 7
0
    def anno_insert(self, request):
        """
        Exposes an API endpoint to insert an anno for the current user.

        if current user doesn't exist, the user will be created first.
        """
        user = auth_user(self.request_state.headers)
        if Anno.is_anno_exists(user, request):
            raise endpoints.BadRequestException("Duplicate anno already exists.")
        entity = Anno.insert_anno(request, user)
        return entity.to_response_message()
Ejemplo n.º 8
0
 def vote_get(self, request):
     """
     Exposes an API endpoint to get a vote.
     """
     user = auth_user(self.request_state.headers)
     if request.id is None:
         raise endpoints.BadRequestException('id field is required.')
     vote = Vote.get_by_id(request.id)
     if vote is None:
         raise endpoints.NotFoundException('No vote entity with the id "%s" exists.' % request.id)
     return vote.to_message()
Ejemplo n.º 9
0
 def followup_get(self, request):
     """
     Exposes an API endpoint to get a followup.
     """
     user = auth_user(self.request_state.headers)
     if request.id is None:
         raise endpoints.BadRequestException('id field is required.')
     followup = FollowUp.get_by_id(request.id)
     if followup is None:
         raise endpoints.NotFoundException('No follow up entity with the id "%s" exists.' % request.id)
     return followup.to_message()
Ejemplo n.º 10
0
 def anno_delete(self, request):
     """
     Exposes an API endpoint to delete an existing anno.
     """
     user = auth_user(self.request_state.headers)
     if request.id is None:
         raise endpoints.BadRequestException('id field is required.')
     anno = Anno.get_by_id(request.id)
     if anno is None:
         raise endpoints.NotFoundException('No anno entity with the id "%s" exists.' % request.id)
     Anno.delete(anno)
     return message_types.VoidMessage()
Ejemplo n.º 11
0
    def anno_merge(self, request):
        """
        Exposes an API endpoint to merge(update only the specified properties) an anno.
        """
        user = auth_user(self.request_state.headers)
        if request.id is None:
            raise endpoints.BadRequestException('id field is required.')
        anno = Anno.get_by_id(request.id)
        if anno is None:
            raise endpoints.NotFoundException('No anno entity with the id "%s" exists.' % request.id)

        anno.merge_from_message(request)
        anno.put()
        return anno.to_response_message()
Ejemplo n.º 12
0
 def followup_delete(self, request):
     """
     Exposes an API endpoint to delete an existing follow up.
     """
     user = auth_user(self.request_state.headers)
     if request.id is None:
         raise endpoints.BadRequestException('id field is required.')
     followup = FollowUp.get_by_id(request.id)
     if followup is None:
         raise endpoints.NotFoundException('No follow up entity with the id "%s" exists.' % request.id)
     anno = followup.anno_key.get()
     followup.key.delete()
     anno.followup_count -= 1
     anno.put()
     return message_types.VoidMessage()
Ejemplo n.º 13
0
    def anno_insert(self, request):
        """
        Exposes an API endpoint to insert an anno for the current user.

        if current user doesn't exist, the user will be created first.
        """
        user = auth_user(self.request_state.headers)
        exist_anno = Anno.is_anno_exists(user, request)
        if exist_anno is not None:
            raise endpoints.BadRequestException("Duplicate anno(%s) already exists." % exist_anno.key.id())
        entity = Anno.insert_anno(request, user)

        # index this document. strange exception here.
        put_search_document(entity.generate_search_document())

        return entity.to_response_message()
Ejemplo n.º 14
0
    def anno_merge(self, request):
        """
        Exposes an API endpoint to merge(update only the specified properties) an anno.
        """
        user = auth_user(self.request_state.headers)
        if request.id is None:
            raise endpoints.BadRequestException('id field is required.')
        anno = Anno.get_by_id(request.id)
        if anno is None:
            raise endpoints.NotFoundException('No anno entity with the id "%s" exists.' % request.id)

        anno.merge_from_message(request)
        # set last update time & activity
        anno.last_update_time = datetime.datetime.now()
        anno.last_activity = 'anno'
        anno.put()
        # update search document.
        put_search_document(anno.generate_search_document())
        return anno.to_response_message()
Ejemplo n.º 15
0
    def vote_insert(self, request):
        """
        Exposes an API endpoint to insert a vote for the current user.
        """
        user = auth_user(self.request_state.headers)

        anno = Anno.get_by_id(request.anno_id)
        if anno is None:
            raise endpoints.NotFoundException('No anno entity with the id "%s" exists.' % request.id)

        vote = Vote()
        vote.anno_key = anno.key
        vote.creator = user.key
        if request.created is not None:
            vote.created = request.created
        vote.put()

        anno.vote_count += 1
        anno.last_update_time = datetime.datetime.now()
        anno.last_activity = 'vote'
        anno.put()
        return vote.to_message()
Ejemplo n.º 16
0
    def flag_insert(self, request):
        """
        Exposes an API endpoint to insert a flag for the current user.
        """
        user = auth_user(self.request_state.headers)

        anno = Anno.get_by_id(request.anno_id)
        if anno is None:
            raise endpoints.NotFoundException('No anno entity with the id "%s" exists.')

        flag = Flag()
        flag.anno_key = anno.key
        flag.creator = user.key
        if request.created is not None:
            flag.created = request.created
        flag.put()

        anno.flag_count += 1
        anno.last_update_time = datetime.datetime.now()
        anno.last_activity = 'flag'
        anno.put()
        return flag.to_message()
Ejemplo n.º 17
0
 def vote_delete(self, request):
     """
     Exposes an API endpoint to delete an existing vote.
     """
     user = auth_user(self.request_state.headers)
     if request.id is None and request.anno_id is None:
         raise endpoints.BadRequestException('id or anno_id field is required.')
     if request.id is not None:
         vote = Vote.get_by_id(request.id)
         if vote is None:
             raise endpoints.NotFoundException('No vote entity with the id "%s" exists.' % request.id)
         anno = vote.anno_key.get()
         vote.key.delete()
         anno.vote_count -= 1
         anno.put()
     elif request.anno_id is not None:
         anno = Anno.get_by_id(request.anno_id)
         for key in Vote.query(Vote.anno_key == anno.key, Vote.creator == user.key).iter(keys_only=True):
             key.delete()
             anno.vote_count -= 1
             anno.put()
     return message_types.VoidMessage()
Ejemplo n.º 18
0
    def anno_search(self, request):
        """
        Exposes and API endpoint to search anno list.
        """
        user = auth_user(self.request_state.headers)

        if request.order_type is None:
            raise endpoints.BadRequestException('order_type field is required.')
        if request.order_type != 'recent' and request.order_type != 'active' and request.order_type != 'popular':
            raise endpoints.BadRequestException(
                'Invalid order_type field value, valid values are "recent", "active" and "popular"')

        app_set = None
        logging.info("only_my_apps=%s" % request.only_my_apps)
        if request.only_my_apps:
            app_set = set()
            for anno in Anno.query_anno_by_author(user):
                app_set.add(anno.app_name)
            for vote in Vote.query_vote_by_author(user):
                anno = Anno.get_by_id(vote.anno_key.id())
                if anno is not None:
                    app_set.add(anno.app_name)
            for flag in Flag.query_flag_by_author(user):
                anno = Anno.get_by_id(flag.anno_key.id())
                if anno is not None:
                    app_set.add(anno.app_name)
            for followup in FollowUp.query_followup_by_author(user):
                anno = Anno.get_by_id(followup.anno_key.id())
                if anno is not None:
                    app_set.add(anno.app_name)

        if request.order_type == 'popular':
            return Anno.query_by_popular(request.limit, request.offset,
                                         request.search_string, request.app_name, app_set)
        elif request.order_type == 'active':
            return Anno.query_by_active(request.limit, request.offset, request.search_string, request.app_name, app_set)
        else:
            return Anno.query_by_recent(request.limit, request.offset, request.search_string, request.app_name, app_set)
Ejemplo n.º 19
0
 def anno_my_stuff(self, request):
     """
     Exposes an API endpoint to return all my anno list.
     """
     user = auth_user(self.request_state.headers)
     return Anno.query_by_last_modified(user)