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) # checking if same anno exists 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) # find all hashtags tags = extract_tags_from_text(entity.anno_text.lower()) for tag, count in tags.iteritems(): # Write the cumulative amount per tag Tag.add_tag_total(tag, total=count) # index this document. strange exception here. put_search_document(entity.generate_search_document(), SearchIndexName.ANNO) # send push notifications ActivityPushNotifications.send_push_notification(first_user=user, anno=entity, action_type=AnnoActionType.CREATED) return entity.to_response_message(user)
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, user) # 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(), SearchIndexName.ANNO) # send notifications ActivityPushNotifications.send_push_notification(first_user=user, anno=anno, action_type=AnnoActionType.EDITED) # update last_read of UserAnnoState from model.userannostate import UserAnnoState UserAnnoState.update_last_read(user=user, anno=anno) return anno.to_response_message(user)
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.last_update_type = 'create' anno.put() # update user anno state UserAnnoState.insert(user=user, anno=anno, type=AnnoActionType.UPVOTED) # update vote in search document put_search_document(anno.generate_search_document(), SearchIndexName.ANNO) return vote.to_message()
def flag_delete(self, request): """ Exposes an API endpoint to delete an existing flag. """ user = auth_user(self.request_state.headers) anno = None 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: flag = Flag.get_by_id(request.id) if flag is None: raise endpoints.NotFoundException('No flag entity with the id "%s" exists.' % request.id) anno = flag.anno_key.get() flag.key.delete() anno.flag_count -= 1 anno.put() elif request.anno_id is not None: anno = Anno.get_by_id(request.anno_id) for key in Flag.query(Flag.anno_key == anno.key, Flag.creator == user.key).iter(keys_only=True): key.delete() anno.flag_count -= 1 anno.put() put_search_document(anno.generate_search_document(), SearchIndexName.ANNO) return message_types.VoidMessage()
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.last_update_type = 'create' anno.put() # update user anno state UserAnnoState.insert(user=user, anno=anno, type=AnnoActionType.FLAGGED) # update flag in search document put_search_document(anno.generate_search_document(), SearchIndexName.ANNO) return flag.to_message()
def init_index_document(self, request): """ Exposes an API endpoint to insert search document for legacy documents. """ for anno in Anno.query(): logging.info("generating search document for anno(%s)." % anno.key.id()) put_search_document(anno.generate_search_document(), SearchIndexName.ANNO) return message_types.VoidMessage()
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 followup.tagged_users = request.tagged_users 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 user anno state UserAnnoState.insert(user=user, anno=anno, type=AnnoActionType.COMMENTED) for tagged_user_id in followup.tagged_users: tagged_user = User.get_by_id(int(tagged_user_id)) if tagged_user: UserAnnoState.insert(user=tagged_user, anno=anno, type=AnnoActionType.TAGGEDUSER) # update search document put_search_document(anno.generate_search_document(), SearchIndexName.ANNO) put_search_document(followup.generate_search_document(), SearchIndexName.FOLLOWUP) # find all hashtags tags = extract_tags_from_text(followup.comment.lower()) for tag, count in tags.iteritems(): # Write the cumulative amount per tag Tag.add_tag_total(tag, total=count) # send notifications ActivityPushNotifications.send_push_notification(first_user=user, anno=anno, action_type=AnnoActionType.COMMENTED, comment=request.comment) return followup.to_message(request.team_key)
def regenerate_index(entity, search_index_name): put_search_document(entity.generate_search_document(), search_index_name)