def create_new_collection(): """Adds a new collection for the user.""" email = request.form['user-email'] user = crud.get_user_by_email(email) name = request.form['new-collection-name'] new_collection = crud.create_collection(user, name) podcast_id = request.form['podcast-id'] podcast_name = request.form['podcast-title'] podcast_cover = request.form['podcast-cover'] if crud.get_podcast_by_id(podcast_id) is None: crud.create_podcast(podcast_id, podcast_name, podcast_cover) podcast = crud.get_podcast_by_id(podcast_id) crud.add_to_podcast_collection(new_collection, podcast) flash(f'Added {podcast_name} to {new_collection.name}!') redirect('/user-profile') return redirect(f'/podcast/{podcast_id}')
def example_data(): """Create sample data""" User.query.delete() Collection.query.delete() Picture.query.delete() user1 = crud.create_user('*****@*****.**', 'test') user2 = crud.create_user('*****@*****.**', 'test') collection1 = crud.create_collection(1, "notes", "01/01/2021") collection2 = crud.create_collection(2, "notes", "01/01/2021") picture1 = crud.create_picture(1, "google.com") picture2 = crud.create_picture(2, "google.com") db.session.add_all([user1, user2]) db.session.commit()
def create_collection(): user_id = session['user.id'] user = User.query.get(user_id) collection_type = request.args.get('collection_name') collection_type = collection_type.lower() collection_type = '_'.join(collection_type.split(' ')) lendable = bool(request.args.get('lendable')) private = bool(request.args.get('private')) if lendable and private: collection = crud.create_collection(user, collection_type, lendable, private) elif lendable: collection = crud.create_collection(user, collection_type, lendable) elif private: collection = crud.create_collection(user, collection_type, private=private) else: collection = crud.create_collection(user, collection_type) return redirect(f'/user?id={user.id}')
def save_collection(): """Creates and returns a collection to database.""" user_id = request.form.get('user_id') date_saved = request.form.get('date_saved') notes = request.form.get('notes') urls = request.form.get('urls') new_collection = crud.create_collection(user_id=user_id, date_saved=date_saved, notes=notes) collection_id = new_collection.collection_id url_list = urls.split(", ") for url in url_list: crud.create_picture(collection_id=collection_id, url=url) if new_collection: flash("Your images have been saved in your saved searches.") return "Your images have been saved in your saved searches."
def create_user(): """Creates a user account""" email = request.form.get('email') password = request.form.get('password') username = (request.form.get('username')).lower() pw_hash = sha256_crypt.encrypt(password) user = crud.get_user_by_email(email) user_name = crud.get_user_by_username(username) if user: flash('Email already has an account. Please try again.') elif user_name: flash('Username is already in use, please select a new one.') else: user = crud.create_user(email, username, pw_hash) crud.create_collection(user, 'home', True) crud.create_collection(user, 'to_read') crud.create_collection(user, 'read') flash('Account created! Log in.') return redirect('/')
password, created_on, profile_picture, user_bio, website, birthday) all_users.append(new_user) for p in range(5): random_podcast = choice(podcasts_in_db) score = randint(1, 5) review_text = fake.sentence(nb_words=30) crud.create_review(new_user, random_podcast, review_text, score) new_collection = crud.create_collection(new_user, "Top Favs") second_new_collection = crud.create_collection(new_user, "Informative") collects = [new_collection, second_new_collection] for collect in collects: for r in range(5): pods = podcasts_in_db random_podcast = choice(podcasts_in_db) crud.add_to_podcast_collection(collect, random_podcast) for user in all_users: crud.become_friends(user.user_id, choice(range(1, 9)))
def test_create_picture(self): """Check picture creation""" crud.create_collection(1, "notes", "01/01/2021") self.assertEqual(str(crud.create_picture(1, "google.com")), "<Picture picture_id=3 URL=google.com>")
def test_create_collection(self): """Check collection creation""" self.assertEqual(str(crud.create_collection(1, "notes", "01/01/2021")), "<Collection collection_id=3 user=1>")
else: book = crud.Book.query.get(google_id) database_books.append(book) i += 1 counter += 1 names = [ 'lizzie', 'jane', 'lydia', 'kitty', 'mary', 'fitzwilliam', 'charles', 'gigi', 'bennet', 'caroline' ] user_email = f'{names[0]}@test.com' pw_hash = server.sha256_crypt.encrypt('test') user = crud.create_user(user_email, names[0], pw_hash) collection = crud.create_collection(user, 'home', lendable=True) crud.create_collection(user, 'to_read') crud.create_collection(user, 'read') for i in range(130): book_info = database_books[i] book = crud.Book.query.get(book_info.google_id) collection.add_book(book) for i in range(1, 10): user_email = f'{names[i]}@test.com' pw_hash = server.sha256_crypt.encrypt('test') user = crud.create_user(user_email, names[i], pw_hash) collection = crud.create_collection(user, 'home', lendable=True)