def add_list(): """" Route to enable the user to create/add lists """ user = app_class.get_user(session['email']) if not user: flash('You need to login first') return redirect(url_for('login')) lists = user.get_lists() title = request.form['title'] description = request.form['description'] if title and description: list_id = app_class.random_id() list1 = List(title, description, list_id) if user.check_valid_list(title): if user.create_list(list1): flash('List successfully created') return redirect(url_for('shopping_list')) flash("List title already exists. Try again") flash('Invalid title. Only numbers and letters accepted') return render_template('shopping_list.html', lists=lists, user=user) else: flash('You did not input a title or description. Try Again') return render_template('shopping_list.html', lists=lists, user=user)
def list(listname=None): formChar = CharacterCreateForm() formList = ListCreateForm() if request.method == "POST": if listname: list = List.query.filter_by(name=listname).first() if list: char = Character.query.filter_by( name=formChar.name.data.lower(), server=formChar.server.data.lower(), region=formChar.region.data).first() if char in list.characters: char.refresh() flash("Character already in this list.") else: if not char: char = Character(name=formChar.name.data.lower(), server=formChar.server.data.lower(), region=formChar.region.data) db.session.add(char) if char.refresh() == 200: list.characters.append(char) db.session.commit() flash("Character added") else: flash("Character didn't exist") else: flash("Error, list didn't exist.") else: list = List.query.filter_by(name=formList.name.data, user_id=current_user.id).first() if not list: list = List(name=formList.name.data, user_id=current_user.id) db.session.add(list) current_user.lists.append(list) db.session.commit() flash("List create") else: flash("List already exist") elif request.method == "GET" and listname: list = List.query.filter_by(name=listname, user_id=current_user.id).first() char = Character.query.filter_by( name=request.args.get('charname'), server=request.args.get('server'), region=request.args.get('region')).first() list.characters.remove(char) db.session.commit() flash("Char remove from list.") next_page = request.args.get('next') if not next_page or url_parse(next_page).netloc != '': next_page = url_for('index') return redirect(next_page) return render_template('list.html', formList=formList, formChar=formChar)
def dashboard_company(id): target = Company.query.filter_by(id=id).first() if not target: abort(404) _list = current_user.lists.first() if not _list: _list = List.make(current_user) db.session.add(_list) db.session.flush() _list.add_company(target) db.session.add(_list) db.session.commit() return redirect(f"/company/{id}")
def setUp(self): self.list = List('TRAVEL', 'I want to travel the world', 'ASGTJVCDSR') self.new_item = ListItems('Food', '5', '$12', 'Not Done', 'AWDBFTHICG')
def test_a_list_id_is_key_for_dictionary(self): list1 = List('Food', 'I want to buy food supplies', 'ASGTJVCDSR') self.list.list_items = {"ASGTJVCDSR": list1} self.assertEqual(self.list.get_item("ASGTJVCDSR"), list1)
def dashboard(): if request.method == "POST": _list = current_user.lists.first() if not _list: _list = List.make(current_user) db.session.add(_list) db.session.flush() symbol = request.form.to_dict().get("symbol") # TODO: check if valid symbol here, capitalization too company = Company.query.filter_by(symbol=symbol).first() if not company: company = Company.make(symbol) db.session.add(company) db.session.flush() _list.add_company(company) snapshot = (Snapshot.query.filter_by(company_id=company.id).order_by( Snapshot.creation_time.desc()).first()) if not snapshot: snapshot = Snapshot.make(symbol, company) if snapshot: db.session.add(snapshot) db.session.commit() else: db.session.rollback() db.session.commit() companies = [] if _list: companies = _list.companies() return Company.repr_card_grid(_list.companies()) if request.method == "DELETE": target = request.form.get("target") _list = current_user.lists.first() if not _list: return "" # should not happen company = Company.query.filter_by(id=target).first() _list.remove_company(company) db.session.commit() return "" if request.method == "PUT": target = request.form.get("target") company = Company.query.filter_by(id=target).first() if not company: return "" latest_snapshot = (Snapshot.query.filter_by( company_id=company.id).order_by( Snapshot.creation_time.desc()).first()) if not latest_snapshot.stale(): return company.repr_card() snapshot = Snapshot.make(company.symbol, company) if snapshot: db.session.add(snapshot) db.session.commit() else: db.session.rollback() return company.repr_card() _list = current_user.lists.first() if not _list: _list = List.make(current_user) db.session.add(_list) db.session.commit() card_grid = Company.repr_card_grid(_list.companies()) return render_template("dashboard.html", card_grid=card_grid)
def setUp(self): self.user = User('CeciliaCaroline', '*****@*****.**', '123456') self.new_list = List('Food', 'I want to buy food supplies', 'ASGTJVCDSR')
def test_list_to_delete_doesnt_exists(self): list1 = List('Food', 'I love food', 'ASGTJVCDSR') self.user.create_list(list1) self.assertFalse(self.user.del_list('AWDBFTHICG'))
def test_list_to_edit_doesnt_exists(self): list1 = List('Food', 'I want to buy food supplies', 'ASGTJVCDSR') self.user.create_list(list1) self.assertFalse( self.user.edit_list('Travel', 'I want to buy travel ticket', 'AWDBFTHICG'))
def test_a_list_is_returned_when_an_id_is_specified(self): list1 = List('Food', 'I want to buy food supplies', 'ASGTJVCDSR') self.user.lists = {"ASGTJVCDSR": list1} self.assertEqual(self.user.get_list("ASGTJVCDSR"), list1)
def test_bucket_to_create_already_exists(self): list1 = List('Food', 'I want to buy food supplies', 'ASGTJVCDSR') self.user.lists = {"ASGTJVCDSR": list1} self.assertFalse(self.user.create_list(list1))