def create_chinese_article(): url = 'https://zh.wikipedia.org/wiki/%E9%97%B4%E9%9A%94%E9%87%8D%E5%A4%8D' scraper = ChineseScraper(url) dm = DeckMaker(scraper.process_page()) deck = dm.create_article(title=scraper.title, url=scraper.url) ud = UserDeck() ud.populate(deck)
def test_cards_relationship(self): d = UserDeck() c = UserCard() d.cards.append(c) dd = UserDeck() dd.cards.append(c) db.session.add(d) db.session.add(dd) self.assertEqual( len(UserCard.query.all()), 1 )
def test_deck_play_first_card_unknown(self): deck = make_test_deck() user_deck = UserDeck() user_deck.populate(deck) learning_card = user_deck.get_learning_cards()[0] outcomes = { '1': {'id': str(learning_card.id), 'result': 'x'}, 'non_card_data': True, } user_deck.play_outcomes(outcomes) user_deck.organise_cards() self.assertEqual(len(user_deck.get_learning_cards()), 20)
def test_deck_first_shuffle_organise_seen(self): deck = make_test_deck() user_deck = UserDeck() user_deck.populate(deck) user_deck.shuffle() user_deck.organise_cards() for c in user_deck.learning_cards: c.last_time = datetime.now() user_deck.organise_cards() self.assertEqual(len(user_deck.seen_cards), 20) self.assertEqual(len(user_deck.unseen_cards), 32) self.assertEqual(len(user_deck.learning_cards), 20)
def user_decks(user_id): user = current_user form = URLForm() url = '' if form.validate_on_submit(): url = form.url.data ad = ArticleDeck.query.filter_by(url=url) if any(a for a in ad): deck = ad[0] ud = UserDeck.query.filter_by(user=user, deck=deck) if not any(d for d in ud): ud = UserDeck(user=user) ud.populate(ad[0]) else: scraper = get_scraper(url) dm = DeckMaker(scraper.process_page()) deck = dm.create_article(title=scraper.title, url=scraper.url) ud = UserDeck(user=user) ud.populate(deck) decks = user.get_decks() return render_template('user_decks.html', title='My decks', url=url, form=form, user=user, user_decks=decks)
def test_deck_first_shuffle(self): deck = make_test_deck() user_deck = UserDeck() user_deck.populate(deck) user_deck.shuffle() user_deck.organise_cards() self.assertEqual(len(user_deck.seen_cards), 0) self.assertEqual(len(user_deck.unseen_cards), 52) self.assertEqual(len(user_deck.learning_cards), 20)
def test_deck_play_first_20_known(self): deck = make_test_deck() user_deck = UserDeck() user_deck.populate(deck) learning_cards = user_deck.get_learning_cards() outcomes = {'non_card_data': True} i = 0 for learning_card in learning_cards: i += 1 outcomes[str(i)] = {'id': str(learning_card.id), 'result': 'z'} user_deck.play_outcomes(outcomes) eases = [card.ease for card in user_deck.get_learning_cards()] self.assertEqual(len([e for e in eases if e == 2]), 20) self.assertEqual(len(user_deck.get_learning_cards()), 30)
def clip_decks(user_id): user = current_user form = ClipForm() if form.validate_on_submit(): title = form.title.data text = form.text.data dm = DeckMaker() dm.process_text(text) cd = dm.create_clip(title=title, text=text) ud = UserDeck(user=user) ud.populate(cd) decks = user.get_decks(type="clip_deck") return render_template('clip_decks.html', title='Clip decks', form=form, user=user, user_decks=decks)
def test_user_deck_populate(self): deck = make_test_deck() user_deck = UserDeck() user_deck.populate(deck) self.assertEqual(len(user_deck.cards), 52)
def test_decks_relationship(self): u = User() d = UserDeck() u.decks.append(d) self.assertEqual(len(u.decks), 1)
def test_deck_get_learning_cards_initial(self): deck = make_test_deck() user_deck = UserDeck() user_deck.populate(deck) learning_cards = user_deck.get_learning_cards() self.assertEqual(len(learning_cards), 20)
def add_deck(deck_id): deck = Deck.query.get(deck_id) user_deck = UserDeck(user=current_user) user_deck.populate(deck) return redirect(url_for('user_decks', user_id=current_user.id))