def card_import(handler): """Import another user's exisiting Card the current users account. Called with the form: /api/card/<card_id>/import """ user_data = get_current_user(handler) if not user_data: return path = handler.request.path card_key = path[len('/api/card/'):-len('/import')] card = ndb.Key(urlsafe=card_key).get() if not card: return "card not found" if user_data.key == card.user_key: # Disallow importing a card this user already owns return "can't import your own card" # Finally ready to do the update new_card = models.Card() new_card.populate(**card.to_dict()) new_card.user_key = user_data.key new_card.update_email_and_nickname() new_card.put() search.insert_cards([new_card]) # Update the list of all known tags for this user user_data.update_card_tags([], new_card.tags) user_data.put() # TODO(jace): only put if necessary # TODO(jace) Notify followers return new_card.key.urlsafe()
def card_import(handler): """Import another user's existing Card to the current user's account. Called with the form: /api/card/<card_id>/import """ user_data = get_current_user(handler) if not user_data: return path = handler.request.path card_key = path[len('/api/card/'):-len('/import')] card = ndb.Key(urlsafe=card_key).get() if not card: return "card not found" if user_data.key == card.user_key: # Disallow importing a card this user already owns return "can't import your own card" # Finally ready to do the update new_card = models.Card() new_card.populate(**card.to_dict()) new_card.user_key = user_data.key new_card.update_email_and_nickname() new_card.put() search.insert_cards([new_card]) # Update the list of all known tags for this user user_data.update_card_tags([], new_card.tags) user_data.put() # TODO(jace): only put if necessary # TODO(jace) Notify followers return new_card.key.urlsafe()
def card_update(handler, delete=False, review=False): """Update or Delete an exisiting Card.""" user_data = get_current_user(handler) if not user_data: return path = handler.request.path route_root = '/api/card/' err_response = '{}' if not path.startswith(route_root) and len(path) > len(route_root): return err_response card_key = path[len(route_root):] if card_key.endswith('/review'): card_key = card_key[:-len('/review')] card = ndb.Key(urlsafe=card_key).get() if not card: return err_response if user_data.key != card.user_key: # Disallow modification of other people's cards return err_response # Finally ready to do the update card_tags_original = set(card.tags) if delete: card_tags_updated = set() card.key.delete() search.delete_cards([card]) elif review: data = json.loads(handler.request.body) card.record_review(data.get('grade')) card_tags_updated = set(card.tags) # unchanged in this case card.put() else: data = json.loads(handler.request.body) card.update_from_dict(data) card_tags_updated = set(card.tags) card.put() search.insert_cards([card]) # Update the list of all known tags for this user user_data.update_card_tags(card_tags_original, card_tags_updated) user_data.put() # TODO(jace): only put if necessary # TODO(jace) Notify followers return card.key.urlsafe()
def card_bulk_import(handler): """Create POSTed cards for the current user.""" user_data = get_current_user(handler) user = users.get_current_user() if not user_data or not user: return archive_json_str = handler.request.body archive = _JSONCardArchive.from_json(archive_json_str) tags = set() cards = archive.get_cards() # TODO(chris): support streaming JSON w/a fixed memory buffer to # avoid OOMs due to large card content. for card in cards: card.user_key = user_data.key card.update_email_and_nickname(user) tags.update(card.tags) # Update the list of all known tags for this user. user_data.update_card_tags([], tags) ndb.put_multi(cards + [user_data]) # TODO(chris): only put if necessary search.insert_cards(cards)
def card_add(handler): """Add a new Card.""" user_data = get_current_user(handler) if not user_data: return data = json.loads(handler.request.body) card = models.Card(user_key=user_data.key) card.update_from_dict(data) card.update_email_and_nickname() card.put() search.insert_cards([card]) # Update the list of all known tags for this user # Update the list of all known tags for this user user_data.update_card_tags([], data.get('tags', [])) user_data.put() # TODO(jace): only put if necessary # TODO(jace) Notify followers return card.key.urlsafe()