Пример #1
0
def signup(response):
	fname = response.get_field("fname")
	lname = response.get_field("lname")
	email = response.get_field("email")
	username = response.get_field("username")
	password = response.get_field("password")
	# check for invalid input

	errors = []

	if not fname:
		errors.append("First name required")

	if not lname:
		errors.append("Last name required")

	if not re.match(r"^[-a-zA-Z0-9+\._]+@([-a-zA-Z0-9]+\.)+[a-zA-Z]+$", email):
		errors.append("Valid email address required")

	if not re.match(r"^[a-zA-Z0-9_]+$", username):
		errors.append("Username can only contain letters, numbers or underscores")

	if len(password) < 6:
		errors.append("Password must be longer than 5 characters")

	if not errors:
		try:
			User.find(username)
		except UserNotFound:
			pass
		else:
			errors.append("Username is taken")

	if errors:
		scope = {
			"errors": errors,
			"logged_in": get_current_user(response),
			"fname": fname,
			"lname": lname,
			"email": email,
			"username": username
		}

		response.write(epyc.render("templates/login.html", scope))

	else:

		user = User.create(fname, lname, username, email, password)
		response.set_secure_cookie('userid', user.username)

		listname = "{}'s wishlist".format(user.username)
		Wishlist.create(listname, user)

		response.redirect('/users/' + user.username)
Пример #2
0
def profile(response, username):
	logged_in = get_current_user(response)

	try:
		current_user = User.find(username)
	except UserNotFound:
		handle_error(response, message="Unable to find the specified user.")
		return

	user_lists = current_user.get_wishlists()

	if not user_lists:
		wishlist_name = "{}'s wishlist".format(username)
		Wishlist.create(wishlist_name, current_user)

		user_lists = current_user.get_wishlists()

	current_wishlist = user_lists[0]
	products = current_wishlist.get_items()

	for product in products:
		product.price = format_price(product.price)

	error_code = response.get_field('error')
	errors = []

	if error_code == '0':
		errors.append("Wish name cannot be empty")

	scope = {
		"username": username,
		"products": products,
		"listname": current_wishlist.list_name,
		"logged_in": logged_in,
		"current_user_fullname": display_name(current_user),
		"is_current_users_wishlist_page": is_current_users_wishlist_page(response, username),
		"response": response,
		"errors": errors,
		"profile_image_filename": '/static/images/profiles/%s' % current_user.image
	}

	if logged_in:
		logged_in_user = User.find(logged_in)

		scope["mutual_friend"] = logged_in_user.check_friend(current_user)
		scope["pending_friend_request"] = logged_in_user.check_pending_friend(current_user)
		scope["pending_friend_invite"] = current_user.check_pending_friend(logged_in_user)

	response.write(epyc.render("templates/wishlist.html", scope))