def get(self): user = users.get_current_user() user_id = user.user_id() current_user = Profile.query().filter(Profile.user_id == user_id).get() signout_link_html = users.create_logout_url('/') template = JINJA_ENVIRONMENT.get_template('templates/flashcard.html') cards = Card.query().filter(Card.owner == current_user.user_id).fetch() if cards == []: self.redirect('/profile0') else: i = random.randint(0, len(cards) - 1) card = cards[i] if card not in already_viewed: dict_for_template = { "my_answer": card.answer, "my_question": card.question, "login_url": signout_link_html } self.response.write(template.render(dict_for_template)) already_viewed.append(card) else: if len(already_viewed) == len(cards): del already_viewed[:] self.redirect('/profile0') self.redirect('/sort')
def get_single_house_2(house_name): house_list = House.query(House.name == house_name).fetch() if house_list: house = house_list[0] house.view = house.view + 1 house.put() card_list = Card.query(ancestor=house.key).fetch() return map(lambda s: {str(s.card_key): str(s.value)}, card_list) else: return
def create_subscription(pigeon_id, house_name): pigeon_key = ndb.Key(Pigeon, pigeon_id) house_list = House.query(House.name == house_name).fetch() house_key = house_list[0].key subscription = Subscription(pigeon_key=pigeon_key, house_key=house_key, num_per_day=0) subscription.put() card_list = Card.query(ancestor=house_key).fetch() for card in card_list: _initailize_progress(pigeon_key, card.key) house = house_list[0] house.num_of_subed = house.num_of_subed + 1 house.put() return
def search_cards(): search_query = request.json['url'][1:] if not search_query: searched_terms = [] search_args = {} else: search_args = { arg.split('=')[0]: arg.split('=')[1] for arg in search_query.split('&') } searched_terms = search_args['q'].lower().split('%20') searched_cards = Card.query() card_set_orderings = CardSetOrdering.table() if searched_terms: for searched_term in searched_terms: searched_cards = searched_cards.filter( lambda card: card['fulltext'].split().contains(searched_term)) total_pages = ( (copy(searched_cards).count() - 1) // CARDS_PER_PAGE) + 1 searched_cards = searched_cards.eq_join('set_code', card_set_orderings, index='set_code').without({ 'right': 'id' }).zip().order_by( 'number', 'set_order') if 'page' in search_args: searched_cards = searched_cards.skip( CARDS_PER_PAGE * (int(search_args['page']) - 1)).limit(CARDS_PER_PAGE) page = search_args['page'] else: searched_cards = searched_cards.limit(CARDS_PER_PAGE) page = 1 searched_cards = searched_cards.run() cards = list(searched_cards) per_line = str( search_args['per_line']) if 'per_line' in search_args else None if not per_line: per_line = request.cookies.get('per_line') if not per_line: per_line = int(request.json['w']) // 350 else: cards = [] page = 0 total_pages = 0 per_line = 0 response = make_response( render_template('widgets/card_widget.jinja2', cards=cards, page=page, total_pages=total_pages, per_line=per_line, searched_terms=searched_terms)) if 'per_line' in search_args: response.set_cookie('per_line', per_line) return response
def _get_all_cards(house_name): house_list = House.query(House.name == house_name).fetch() if house_list: house = house_list[0] return Card.query(ancestor=house.key).fetch() return
def make_move(self, request): """Makes a move. Returns a game state with message. The two positions are a and b""" game = get_by_urlsafe(request.urlsafe_game_key, Game) if game.game_over: return game.to_form('Game already over!') cards = Card.query(Card.game == game.key, Card.matched == False) position1 = request.a position2 = request.b mark_card = Card.query(Card.position == position1) for card in mark_card: card.seen = True card.put() mark_card = Card.query(Card.position == position2) for card in mark_card: card.seen = True card.put() unmatched_cards = [] for card in cards: unmatched_cards.append(card) unmatched_cards_count = len(unmatched_cards) if unmatched_cards_count < 2: raise IndexError('No unmatched pair found but the game is not ' 'over') cards_list = [] msg = 'Boo' game.attempts += 1 move = Move() move.card1 = position1 move.card2 = position2 move.game = game.key move.result = msg for card in unmatched_cards: if card.position == position1 or card.position == position2: cards_list.append(card) try: if cards_list[0].matched or cards_list[1].matched: move.result = 'Card already matched' move.put() return game.to_form('Card already matched') except: msg = 'One of the cards is already matched. Please Try again.' move.result = msg move.put() return game.to_form(msg) if cards_list[0].value == cards_list[1].value: cards_list[0].matched = cards_list[1].matched = True cards_list[0].put() cards_list[1].put() msg = 'Yay' move.result = msg if unmatched_cards_count == 2: game.game_over = True game.end_game() msg = 'You win, Game Over' move.put() users = User.query(User.key == game.user) for user in users: user.games_played += 1 user.average_attempt = game.attempts user.calculate_score() user.put() return game.to_form(msg) game.put() move.put() return game.to_form(msg)