def profile(): """Should be able to view profile""" form = MyForm() if request.method == 'POST' and form.validate(): userid = genrateid(form.fname.data, form.lname.data) created_on = timenow() photo = form.photo.data filename = secure_filename(photo.filename) photo.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) photo = photo.filename users = User(userid, form.fname.data, form.lname.data, form.sex.data, form.email.data, form.address.data, form.bio.data, photo, created_on) db.session.add(users) db.session.commit() flash('You have been successfully added!') return redirect(url_for('allusers')) return render_template('profile.html', form=form)
def home(): global suffix_gen, counter, first_enter, server_suffix suffix = "uninitial" form = MyForm() # checks if url from form is legal if form.validate() == False: if not first_enter and form.long_url._value != '': flash('Illegal URL, try again') first_enter = False return render_template('home.html', form=form) else: try: url = request.form['long_url'] with sql.connect('urls_database.db') as con: cur = con.cursor() cur.execute( "SELECT * FROM urls WHERE long_url='{}'".format(url)) rec = cur.fetchone() # for bonus task 1 cur.execute( "INSERT INTO requests (request_time, long_url) VALUES (datetime('now'),'{}')" .format(url)) if rec is not None: # long_url exists suffix = rec[1] else: # url doesn't in db yet try: while True: suffix = tuple2string(next(suffix_gen)) # verify that suffix is unique if suffix in server_suffix: continue if cur.execute( "SELECT * FROM urls WHERE url_suffix='{}'". format(suffix)).fetchone() is None: break except StopIteration: counter += 1 suffix_gen = product(chars, repeat=counter) while True: suffix = tuple2string(next(suffix_gen)) if suffix in server_suffix: continue # verify that suffix is unique if cur.execute( "SELECT * FROM urls WHERE url_suffix='{}'". format(suffix)).fetchone() is None: break cur.execute( "INSERT INTO urls (long_url, url_suffix) VALUES ('{}','{}')" .format(url, suffix)) con.commit() except Exception as e: con.rollback() return "error: {}".format(e) finally: first_enter = True con.close() return redirect('http://localhost:5000/result={}'.format(suffix))