def index(): sorted_deck = Deck() #create new deck object in order saved_sorted_deck = copy.deepcopy( sorted_deck) #create save of sorted deck to display entire deck sorted_deck_draw_card = sorted_deck.remove_card() #print("sorted_deck_draw_card "+str(sorted_deck_draw_card)) shuffled_deck = Deck() shuffled_deck.shuffle( ) #save for displaying because remove_card takes it out of array saved_shuffled_deck = copy.deepcopy(shuffled_deck) # copy shuffled_deck_draw_card = shuffled_deck.remove_card() #print("shuffled_deck_draw_card "+str(shuffled_deck_draw_card)) #print to terminal for resting pruposes #counter = 0 #for card in shuffled_deck.cards: # print(shuffled_deck.cards[counter]) # counter = counter + 1 #firstDeckCard = sorted_deck.cards[1] #print("@@@@@@@@@@") #print(firstDeckCard.suit) # returh the three objects with home/index.html which will post the decks # and the cards drawn from the top of each deck return render_template("home/index.html", saved_shuffled_deck=saved_shuffled_deck, shuffled_deck_draw_card=shuffled_deck_draw_card, saved_sorted_deck=saved_sorted_deck, sorted_deck_draw_card=sorted_deck_draw_card)
def sign_up(): """ Creates a new user and logs them in """ form = SignUpForm() form['csrf_token'].data = request.cookies['csrf_token'] if form.validate_on_submit(): user = User(username=form.data['username'], email=form.data['email'], password=form.data['password'], free_currency=1000) db.session.add(user) db.session.commit() new_deck = Deck(user_id=user.to_dict()['id'], name=f"{user.to_dict()['username']}'s deck") db.session.add(new_deck) db.session.commit() card_arr = random.choice(decks) for card in card_arr: new_card = Card( user_id=user.to_dict()['id'], card_type=card, deck_id=new_deck.to_dict_lite()['id'], ) db.session.add(new_card) db.session.commit() login_user(user) return user.to_dict() return {'errors': validation_errors_to_error_messages(form.errors)}, 401
def create_deck(): form = DeckCreationForm() if form.validate_on_submit(): new_deck = Deck(deck_name=form.deck_name.data, deck_info=form.deck_info.data, user_id=current_user.id) # сохраняем изображение в static, а путь к нему в бд try: # проверять размер!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! f = request.files['file'] except BadRequestKeyError: f = None if f and allowed_file(f.filename): image = secure_filename(f.filename) path_to_save = os.path.join('app/static/img/decks_img', image) f.save(path_to_save) ref_for_db = os.path.join('/static/img/decks_img', image) new_deck.deckpic = ref_for_db db.session.add(new_deck) db.session.commit() flash('Колода создана.') return redirect(url_for('user', username=current_user.username)) return render_template('create_deck.html', form=form, title='Deck creation')
def make_test_deck(): location = os.path.join(os.getcwd(), '') filename = 'tests\\test_words.txt' with open(os.path.join(location, filename), encoding='utf-8') as f: raw = f.read() lines = raw.split('\n') structures = set(len(l.split('\t')) for l in lines) assert len(structures) == 1 words = [] for l in lines: data = l.split('\t') fields = { 'zi_simp': get_chinese(data[0]), 'zi_trad': get_chinese(data[1]), 'pinyin_number': data[2], 'pinyin_tone': data[3], 'english': data[4], 'hsk': 6, } words.append(ChineseWord(**fields)) deck = Deck() for w in words: db.session.add(w) deck.cards.append(w) db.session.add(deck) db.session.commit() return deck
def seed_decks(): demo = Deck(name='Demo Deck', subject='Spanish', tags=['vocabulary', 'words'], created_by=1) demo2 = Deck(name='Demo Deck 2', subject='Math', tags=['addition'], created_by=1, cards=[1, 2]) db.session.add(demo) # db.session.add(demo2) db.session.commit()
def make_decks(): words = Card.query.filter_by(type='word') decks = {n: Deck(name='HSK{}'.format(n)) for n in range(1, 7)} for w in words: decks[w.hsk].cards.append(w) for _, d in decks.items(): db.session.add(d) db.session.commit()
def user(username): user = User.query.filter_by(username=username).first_or_404() decks = user.decks form = DeckForm() if form.validate_on_submit(): deck = Deck(name=form.name.data, user_id=current_user.id) db.session.add(deck) db.session.commit() flash('New deck "{}" created successfully'.format(deck.name)) return redirect(url_for('user', username=current_user.username)) return render_template('user.html', user=user, decks=decks, form=form)
def seed_ai_decks(): for d in decks: new_deck = Deck( user_id=1, name='Default Deck', ) db.session.add(new_deck) db.session.commit() for card in d: new_card = Card(user_id=1, card_type=card, deck_id=new_deck.to_dict()['id']) db.session.add(new_card) db.session.commit()
def init_db(test_client): with test_client: db.create_all() u = User(username='******', email='*****@*****.**') u.set_password('password') d1 = Deck(deckname='Fall Out Boy songs', user=u) Card(term='This is side one.', definition='Flip me over.', deck=d1) Card(term='I know I am not your favorite record.', definition='But the songs you grow to like.', deck=d1) d2 = Deck(deckname='Feels Like This songs', user=u) Card(term='No more stuttering.', definition='Two hearts fluttering.', deck=d2) Card(term='This Narrow Gate.', definition='But by this grace, there is room for two.', deck=d2) db.session.add(u) db.session.commit() # TODO must admit I don't understand how yield is applicable, pytest. yield db # what does this fixture have in common with generators? db.drop_all()
def decks(): if request.method == 'POST': # Extract data from the request title = request.form.get('title') description = request.form.get('description') cards = request.form.get('cards') # Add new record to database deck = Deck(title=title, description=description, cards=cards) db.session.add(deck) db.session.commit() return redirect(os.path.join('decks', str(deck.id))) # Query titles and descriptions of all decks decks = Deck.query.with_entities(Deck.title, Deck.description) return render_template('decks.html', decks=decks)
from app.models import User, Deck, Card from app import app, db from dotenv import load_dotenv load_dotenv() with app.app_context(): db.drop_all() db.create_all() user = User(email="*****@*****.**", username="******", password="******") deck = Deck(title="Python", user_id=1, description="") deck2 = Deck(title="Algorithms", user_id=1, description="") cards = [ Card(deck_id=1, question="1 + 1", answer="2"), Card(deck_id=1, question="2 + 2", answer=" 4"), Card(deck_id=1, question="3 + 3", answer="6"), Card(deck_id=1, question="4 + 4", answer="8"), ] for card in cards: db.session.add(card) db.session.add(user) db.session.add(deck) db.session.add(deck2) db.session.commit()
User.query.get(1) # Adding a card to a user u = User.query.get(1) c = Card(front='Hello', back='Talofa', owner=u) db.session.add(c) db.session.commit() # Getting all cards belonging to user u u.cards.all() # Creating a user with a password u = User(username='******', email='*****@*****.**') u.set_password('Passw0rd') db.session.add(u) db.session.commit() # Creating a deck d = Deck(name='Samoan Language', user_id=u.id) # Adding a card to a deck c = Card.query.get(1) c.deck_id = 1 # 9/11: Set an admin user # Migrate and update db, then: users = User.query.all() for u in users: u.ADMIN = 0 user[3].ADMIN = 1
def seed_deck_cards(): demo = User(username='******', email='*****@*****.**', password='******', classrooms_id=1) demo_classroom = Classroom(name='Demo Classroom', password='******') demo_deck = Deck(name='Demo Deck', subject='Spanish', tags=['vocabulary', 'words'], created_by=1) demo_deck2 = Deck(name='Demo Deck 2', subject='Math', tags=['addition'], created_by=1) demo_card1 = Card(title='What is 2 + 2 = ?', subject='Math', possible_answers=['1', '3', '4', '5'], answer='4', created_by=1) demo_card2 = Card(title='What is 4 + 6 = ?', subject='Math', possible_answers=['5', '10', '4', '6'], answer='10', created_by=1) demo_card3 = Card(title='What is 1 + 3 = ?', subject='Math', possible_answers=['7', '13', '4', '5'], answer='4', created_by=1) demo_card4 = Card(title='What is 6 - 2 = ?', subject='Math', possible_answers=['3', '2', '4', '5'], answer='4', created_by=1) demo_card5 = Card(title='What is 5 + 1 = ?', subject='Math', possible_answers=['6', '15', '4', '5'], answer='6', created_by=1) demo_card6 = Card(title='What is 5 - 4 = ?', subject='Math', possible_answers=['2', '1', '4', '3'], answer='1', created_by=1) demo_card7 = Card(title='What is 1 + 6 = ?', subject='Math', possible_answers=['7', '0', '4', '5'], answer='7', created_by=1) demo_card8 = Card(title='What is 1 + 1 = ?', subject='Math', possible_answers=['3', '0', '2', '1'], answer='2', created_by=1) demo_card9 = Card(title='How do you say "hello"?', subject='Spanish', possible_answers=['hola', 'gato', 'perro', 'loco'], answer='hola', created_by=1) demo_card10 = Card(title='How do you say "no"?', subject='Spanish', possible_answers=['No', 'Nien', 'Non', 'Null'], answer='No', created_by=1) demo_card11 = Card(title='How do you say "Thank You"?', subject='Spanish', possible_answers=['Mas', 'Gracias', 'Gato', 'Loco'], answer='Gracias', created_by=1) demo_card12 = Card( title='How do you say "Good Morning"?', subject='Spanish', possible_answers=['Buenas tardes', 'Buenas noches', 'Buenos días'], answer='Buenos días', created_by=1) demo_card13 = Card( title='How do you say "Good Afternoon"?', subject='Spanish', possible_answers=['Buenas tardes', 'Buenas noches', 'Buenos días'], answer='Buenos tardes', created_by=1) demo_card14 = Card( title='How do you say "Good Evening"?', subject='Spanish', possible_answers=['Buenas tardes', 'Buenas noches', 'Buenos días'], answer='Buenos noches', created_by=1) db.session.add(demo) db.session.add(demo_classroom) db.session.commit() db.session.add(demo_deck) db.session.add(demo_deck2) db.session.add(demo_card1) db.session.add(demo_card2) db.session.add(demo_card3) db.session.commit() demo_classroom.decks.append(demo_deck) demo_deck.cards.append(demo_card9) demo_deck.cards.append(demo_card10) demo_deck.cards.append(demo_card11) demo_deck.cards.append(demo_card12) demo_deck.cards.append(demo_card13) demo_deck.cards.append(demo_card14) demo_deck2.cards.append(demo_card1) demo_deck2.cards.append(demo_card2) demo_deck2.cards.append(demo_card3) demo_deck2.cards.append(demo_card4) demo_deck2.cards.append(demo_card5) demo_deck2.cards.append(demo_card6) demo_deck2.cards.append(demo_card7) demo_deck2.cards.append(demo_card8) db.session.commit()
def new_deck(id): req = request.json new_deck = Deck(user_id=id, name=req) db.session.add(new_deck) db.session.commit() return new_deck.to_dict_lite()
def seed_decks(): deck1 = Deck( userId=1, name='Goblins', format='Historic', color=json.dumps(['Red']), devotion=json.dumps({'red': 65}), numberOfLands=20, manaCurve=json.dumps({ 'one': 4, 'two': 9, 'three': 16, 'four': 6, 'five': 1, 'six': 0 }), decklist="""1 Gempalm Incinerator 1 Goblin Trashmaster 1 Siege-Gang Commander 2 Wily Goblin 2 Goblin Chieftain 3 Goblin Instigator 4 Conspicuous Snoop 4 Skirk Prospector 4 Goblin Warchief 4 Krenko, Mob Boss 4 Goblin Matron 4 Muxus, Goblin Grandee 2 Herald's Horn 1 Irencrag Feat 3 Shatterskull Smashing 1 Phyrexian Tower 3 Castle Embereth 16 Mountain 1 Tormod's Crypt 2 Goblin Chainwhirler 2 Gempalm Incinerator 2 Fry 2 Lava Coil 3 Goblin Trashmaster 3 Unchained Berserker""", notes='Took first at The Dive Down Nation Eternal Nights Historic') deck2 = Deck(userId=1, name='Gruul Aggro', format='Historic', color=json.dumps(['Red', 'Green']), devotion=json.dumps({ 'red': 22, 'green': 30 }), numberOfLands=20, manaCurve=json.dumps({ 'one': 8, 'two': 14, 'three': 10, 'four': 4, 'five': 0, 'six': 4 }), decklist="""4 Bonecrusher Giant 4 Burning-Tree Emissary 1 Collected Company 4 Cragcrown Pathway 4 Embercleave 4 Gruul Spellbreaker 3 Hashep Oasis 2 Kazandu Mammoth 4 Llanowar Elves 4 Pelt Collector 3 Questing Beast 2 Ramunap Ruins 4 Scavenging Ooze 2 Shatterskull Smashing 4 Stomping Ground 4 Voltaic Brawler 4 Forest 3 Mountain""", notes='') db.session.add(deck1) db.session.add(deck2) db.session.commit()