def validator(): invalid_user_ids = User.select(User.id).where( peewee.fn.length(User.username) < 6 ^ peewee.fn.length(User.password) < 8) new_q = Quote.delete().where(Quote.user_id.in_(invalid_user_ids)) new_q.execute() q = User.delete().where( peewee.fn.length(User.username) < 6 ^ peewee.fn.length(User.password) < 8) q.execute() q = Quote.delete().where(peewee.fn.length(Quote.description) < 15) q.execute() return 'removed bad users and bad quotes.'
def quote(): if 'user_id' not in session: return render_template("add-quote.html", islogged=False) else: user = User.get_by_id(int(session['user_id'])) username = user.username if request.method == 'POST': quote = request.form.get('quote') if Quote.select().where(Quote.description == quote).exists(): # handles case that quote already exists in the system. return render_template( "add-quote.html", username=username, boards=get_boards(), message="the quote you entered already exists- try again.", islogged='user_id' in session) elif len(quote) < 15: return render_template( "add-quote.html", username=username, boards=get_boards(), message="the quote you entered is too short- try again.", islogged='user_id' in session) else: try: new_quote = Quote.create(description=quote, user_id=session['user_id']) new_quote.save() except peewee.DataError: database.rollback() return render_template( "add-quote.html", username=username, boards=get_boards(), message= 'quote is too long- try adding a shorter quote.', islogged='user_id' in session) quote_id = Quote.select().where( Quote.description == quote).get().id boards_matching = request.form.getlist('boards') for b in boards_matching: newlink = QuotesBoards.create(quote_id=quote_id, board_id=b) newlink.save() return render_template( "add-quote.html", username=username, boards=get_boards(), message= f'{username}, your quote was added to our main gallery.', islogged='user_id' in session) else: return render_template("add-quote.html", username=username, boards=get_boards(), message='', islogged='user_id' in session)
def init_db(): # Base.metadata.create_all(bind=engine) # users = [('albert', '408 Lakeshore Lane', 'Chapel Hill', 'NC', '27514', 'US', 'Buyer'), # ('john', '1 some street', 'New Haven', 'CT', '00000', 'US', 'Seller'), # ('paul', '2 some street', 'Cupertino', 'CA', '11111', 'US', 'Seller')] # products = [('chair', 'furniture', 10, 10, 10, 10), # ('desk', 'furniture', 20, 20, 20, 20)] # items = [('chair', 'paul', 10), # ('chair', 'john', 20), # ('desk', 'paul', 30)] users = np.array(pd.read_csv('data/users.csv'))[:, 1:] products = np.array(pd.read_csv('data/products.csv'))[:, 1:] items = np.array(pd.read_csv('data/items.csv'))[:, 1:] for instance in users: db_session.add( User(name=instance[0].lower(), street=instance[1], city=instance[2], state=instance[3], zip=instance[4], country=instance[5], user_type=instance[6])) for instance in products: db_session.add( Product(name=instance[0].lower(), category=instance[1], width=instance[2], height=instance[3], length=instance[4], weight=instance[5])) db_session.commit() for instance in items: item = Item(product=instance[0].lower(), seller=instance[1].lower(), price=instance[2], quantity=True) db_session.add(item) product = db_session.query(Product).filter( Product.name == instance[0].lower()).first() seller = db_session.query(User).\ filter(User.name==instance[1].lower()).\ filter(User.user_type=='Seller').first() if product and seller: # Add child to Product parent models product.items.append(item) seller.items.append(item) db_session.commit()
def register(): if request.method == 'POST': username = request.form.get('username') password = request.form.get('password') # return f'answer is {User.select().where(User.username == username).exists()}' if User.select().where(User.username == username).exists(): # handles case where username is already taken return render_template( "register.html", message='username already taken. try again.', islogged='user_id' in session) elif len(password) < 8: return render_template( "register.html", message='Your password must be at least 8 characters-try again', islogged='user_id' in session) elif len(username) < 7: return render_template( "register.html", message='Your name must be at least 7 characters-try again', islogged='user_id' in session) else: # input is totaly valid- user is created. if (password == request.form.get('confirm-password')): #checks that password was typed correctrly newuser = User.create(username=username, password=password) newuser.save() session['user_id'] = User.select().where( User.username == username).get().id return redirect(url_for('quote', islogged='user_id' in session)) else: return render_template( "register.html", message='passwords not matching- try again!', islogged='user_id' in session) else: return render_template("register.html", message='', islogged='user_id' in session)
def my_quotes(): if 'user_id' in session: my_quotes = User.select( peewee.fn.ARRAY_AGG(User.username).alias('username'), Quote.description, peewee.fn.ARRAY_TO_STRING(peewee.fn.ARRAY_AGG( Board.title), ', ').alias('title')).join(Quote).join( QuotesBoards, peewee.JOIN.LEFT_OUTER, on=(QuotesBoards.quote_id == Quote.id)).join( Board, peewee.JOIN.LEFT_OUTER).where( Quote.user_id == User.get_by_id( session['user_id'])).group_by( User.username, Quote.description) my_quotes = list(my_quotes.dicts()) return render_template("quote_display.html", quotes=my_quotes, user=session['user_id'], islogged='user_id' in session, display='user') else: return render_template("quote_display.html", islogged=False)
def user_index(): form = UserForm(request.form) if request.method == 'POST' and form.validate(): user = User() save_user(user, form, new=True) return redirect('/users') qry = db_session.query(User) results = qry.all() # table = UserResults(results) # table.border = True return render_template('users.html', results=results, form=form)
def new_board(): if 'user_id' not in session: return render_template("add-quote.html", islogged=False) else: user = User.get_by_id(int(session['user_id'])) username = user.username if request.method == 'POST': title = request.form.get('title') description = request.form.get('description') if Board.select().where(Board.title == title).exists(): # handles case when board with this title already exists. return render_template( "add-board.html", username=username, quotes=get_quotes(), message='board with this title already exists.', islogged='user_id' in session) elif (len(title) < 3): return render_template( "add-board.html", username=username, quotes=get_quotes(), message= 'title of board must be at least 3 characters long..', islogged='user_id' in session) else: new_board = Board.create(title=title, description=description) new_board.save() # adding the quotes related to the board board_id = Board.select().where(Board.title == title).get().id quotes_in_board = request.form.getlist('quotes') # return f'number of qoutes in board- {quotes_in_board}' for q in quotes_in_board: newlink = QuotesBoards.create(quote_id=q, board_id=board_id) newlink.save() return render_template("add-board.html", username=username, quotes=get_quotes(), message='Board created Successfuly!', islogged='user_id' in session) else: return render_template("add-board.html", username=username, quotes=get_quotes(), message='', islogged='user_id' in session)
def login(): if request.method == 'POST': username = request.form.get('username') password = request.form.get('password') user = User.select().where((User.username == username) & (User.password == password)) if not user.exists(): return render_template( "login.html", message='wrong username/password- try again!', islogged='user_id' in session) else: session['user_id'] = user.get().id return redirect(url_for('quote')) else: return render_template("login.html", message='', islogged='user_id' in session)