def signup(): """Handle renter signup. Create new renter and add to DB. Redirect to home page. If form not valid, present form. If the there already is a renter with that username: flash message and re-present form. """ if CURR_RENTER_KEY in session: del session[CURR_RENTER_KEY] form = RenterForm() if form.validate_on_submit(): try: renter = Renter.signup(username=form.username.data, password=form.password.data, email=form.email.data, location=form.location.data) # print("commit", renter) db.session.commit() except IntegrityError as e: # print("error", e) flash("Username already taken", 'danger') return render_template('renter/signup.html', form=form) # print("hit") do_login(renter) return redirect("/") else: return render_template('renter/signup.html', form=form)
def demo(): """Handle user login.""" renter = Renter.authenticate("SAM", "sampass") print("RENTER", renter) do_login(renter) flash(f"Hello, {renter.username}!", "success") return redirect(url_for('pets'))
def test_invalid_username_signup(self): invalid = Renter.signup(None, "*****@*****.**", "testPassword", "11373") rid = 123456789 invalid.id = rid with self.assertRaises(exc.IntegrityError) as context: db.session.commit()
def test_renter_model(self): """Does basic model work?""" r = Renter(email="*****@*****.**", username="******", password="******", location="11373") db.session.add(r) db.session.commit() self.assertEqual(len(r.bookings), 0)
def test_valid_signup(self): r_test = Renter.signup("testName", "*****@*****.**", "testPassword", "11373") rid = 99999 r_test.id = rid db.session.commit() r_test = Renter.query.get(rid) self.assertIsNotNone(r_test) self.assertEqual(r_test.username, "testName") self.assertEqual(r_test.email, "*****@*****.**") self.assertEqual(r_test.password, "testPassword") self.assertEqual(r_test.location, "11373")
def post(self, request, format=None): if 'username' in request.data: username = request.data['username'] else: data = { 'status': 500, 'msg': 'usernmae must required to create renter.' } return HttpResponse(json.dumps(data), content_type="application/json") if 'first_name' in request.data: first_name = request.data['first_name'] else: first_name = None if 'last_name' in request.data: last_name = request.data['last_name'] else: last_name = None if 'email' in request.data: email = request.data['email'] else: email = None if 'password' in request.data: password = request.data['password'] else: password = None renter_object = Renter(username=username, first_name=first_name, last_name=last_name, email=email, password=password) renter_object.save() data = {'status': 200, 'msg': 'Renter account created succesfully.'} return HttpResponse(json.dumps(data), content_type="application/json")
def setUp(self): """Create test client, add sample data.""" db.drop_all() db.create_all() self.client = app.test_client() self.testRenter = Renter.signup(username="******", email="*****@*****.**", password="******", location=11373) self.testRenter_id = 8989 self.testRenter.id = self.testRenter_id db.session.commit()
def setUp(self): """Create test client, add sample data.""" db.drop_all() db.create_all() r1 = Renter.signup("test1", "*****@*****.**", "password", 11373) rid1 = 1111 r1.id = rid1 db.session.commit() r1 = Renter.query.get(rid1) self.r1 = r1 self.rid1 = rid1 self.client = app.test_client()
def login(): """Handle user login.""" form = LoginForm() if form.validate_on_submit(): renter = Renter.authenticate(form.username.data, form.password.data) print("RENTER", renter, form.username.data, form.password.data) if renter: do_login(renter) flash(f"Hello, {renter.username}!", "success") return redirect("/") flash("Invalid credentials.", 'danger') return render_template('renter/login.html', form=form)
def renter_edit(): """update renter profile""" if is_not_renter(): return redirect("/") renter = g.renter form = RenterForm(obj=renter) if form.validate_on_submit(): if Renter.authenticate(renter.username, form.password.data): renter.username = form.username.data renter.email = form.email.data renter.location = form.location.data db.session.commit() return redirect('/renter') flash("Wrong password, please try again.", 'danger') return render_template('renter/edit.html', form=form, renter_id=renter.id)
def test_valid_authentication(self): r = Renter.authenticate(self.r1.username, "password") self.assertIsNotNone(r) self.assertEqual(r.id, self.rid1)
def test_invalid_password_signup(self): # with self.assertRaises(ValueError) as context: # Renter.signup("testName", "*****@*****.**", "", None) with self.assertRaises(ValueError) as context: Renter.signup("testName", "*****@*****.**", None, None)
def test_invalid_email_signup(self): invalid = Renter.signup("testName", None, "testPassword", "11373") rid = 123789 invalid.id = rid with self.assertRaises(exc.IntegrityError) as context: db.session.commit()
def test_wrong_password(self): self.assertFalse(Renter.authenticate(self.r1.username, "badpassword"))
def test_invalid_username(self): self.assertFalse(Renter.authenticate("badusername", "password"))
from models import db, Renter, Booking from app import app import datetime db.drop_all() db.create_all() renter1 = Renter(id=100, username="******", email="*****@*****.**", password="******", location="11379") book1 = Booking(id=20, pet_id=48038022, renter_id=100, startTime=datetime.date.today(), endTime=datetime.date.today() + datetime.timedelta(days=1), rating=4) db.session.add_all([renter1, book1]) db.session.commit()