def register(): if current_user.is_authenticated: return redirect(url_for("home")) if request.method == 'POST': name = request.form.get('name') location = request.form.get("location") month = request.form.get('month') day = request.form.get('day') year = request.form.get('year') username = request.form.get("username") email = request.form.get("email") password = request.form.get('password') if not name: return json.dumps({'status': 'Name must be filled in', 'box_ids': ['name']}) if not location: return json.dumps({'status': 'Location must be filled in', 'box_ids': ['location']}) if not month or not day or not year: return json.dumps({'status': 'Birthday must be filled in', 'box_ids': ['birthdate']}) if not username: return json.dumps({'status': 'Username must be filled in', 'box_ids': ['username']}) if not email: return json.dumps({'status': 'Email must be filled in', 'box_ids': ['email']}) if not password: return json.dumps({'status': 'Password must be filled in', 'box_ids': ['password']}) birthdate = date(month=int(month), day=int(day), year=int(year)) if not get_age(birthdate) >= 13: return json.dumps({'status': 'You must be over the age of 13', 'box_ids': ['birthdate']}) if not User.query.filter_by(username=username).first() is None: return json.dumps({'status': 'Username taken', 'box_ids': ['username']}) if not re.match(r"[^@]+@[^@]+\.[^@]+", email): print(email) return json.dumps({'status': 'Invalid email', 'box_ids': ['email']}) location = geocode(location) if not location: return json.dumps({'status': 'Non-valid location', 'box_ids': ['location']}) user = User(name=name, username=username, email=email) user.set_location(location, prelocated=True) user.set_birthdate(birthdate) user.set_password(password) db.session.add(user) db.session.commit() login_user(user, remember=True) return json.dumps({'status': 'Successfully registered'}) return render_template("register.html", title="Register")
def set_location(self, location, prelocated=False): if not prelocated: location = geocode(location) if location: self.location = location.address self.latitude = location.latitude self.longitude = location.longitude self.sin_rad_lat = math.sin(math.pi * location.latitude / 180) self.cos_rad_lat = math.cos(math.pi * location.latitude / 180) self.rad_lng = math.pi * location.longitude / 180 return location
def create_group(): if request.method == 'POST': handle = request.form.get("handle") name = request.form.get("name") description = request.form.get("description") privacy = request.form.get("privacy") location_is_fixed = int(request.form.get("location_is_fixed")) address = request.form.get("address") members = eval(request.form.get("members")) file = request.files.get("image") if not handle: return json.dumps({'status': 'Handle must be filled in', 'box_id': 'handle'}) if not name: return json.dumps({'status': 'Name must be filled in', 'box_id': 'name'}) if not address and location_is_fixed: return json.dumps({'status': 'Location must be filled in if fixed', 'box_id': 'location'}) if not Group.query.filter_by(handle=handle).first() is None: return json.dumps({'status': 'Handle taken', 'box_ids': ['handle']}) location = geocode(address) if not location: return json.dumps({'status': 'Non-valid location', 'box_id': 'location'}) group = Group(handle=handle, name=name, description=description, privacy=privacy, location_is_fixed=location_is_fixed) group.add_members([User.query.filter_by(username=username).first() for username in members] + [current_user]) if location_is_fixed: group.set_location(location, prelocated=True) if file: image = Image.open(file) new_image = image.resize((256, 256), Image.ANTIALIAS) new_image.format = image.format group.profile_pic.save(image=new_image) db.session.commit() return json.dumps({'status': 'Successfully saved'}) connections = current_user.connections.all() return render_template('connections.html', profile=current_user, connections=connections, create_group=True)
def edit_profile(): if request.method == 'POST': name = request.form.get("name") bio = request.form.get("bio") location = request.form.get("location") month = request.form.get("month") day = request.form.get("day") year = request.form.get("year") gender = request.form.get("gender") skills = eval(request.form.get("skills")) file = request.files.get("image") if not name: print("All fields required") return json.dumps({'status': 'Name must be filled in', 'box_id': 'name'}) if not location: print("All fields required") return json.dumps({'status': 'Location must be filled in', 'box_id': 'location'}) if not month or not day or not year: print("All fields required") return json.dumps({'status': 'Birthday must be filled in', 'box_id': 'birthdate'}) birthdate = date(month=int(month), day=int(day), year=int(year)) if not get_age(birthdate) >= 13: return json.dumps({'status': 'You must be over the age of 13', 'box_id': 'birthdate'}) location = geocode(location) if not location: print("Non-valid location") return json.dumps({'status': 'Non-valid location', 'box_id': 'location'}) if file: image = Image.open(file) new_image = image.resize((256, 256), Image.ANTIALIAS) new_image.format = image.format current_user.profile_pic.save(image=new_image) current_user.name = name.strip() current_user.bio = bio.strip() current_user.set_location(location=location, prelocated=True) current_user.set_birthdate(birthdate) current_user.gender = gender # Add skills that are not already there for skill in skills: if not current_user.skills.filter_by(title=skill).first(): skill = Skill(owner=current_user, title=skill) db.session.add(skill) # Delete skills that are meant to be deleted for skill in current_user.skills: if not skill.title in skills: db.session.delete(skill) db.session.commit() return json.dumps({'status': 'Successfully saved'}) return render_template('profile.html', edit_profile=True, profile=current_user, available_skills=available_skills, selected_month=current_user.birthdate.month, selected_day=current_user.birthdate.day, selected_year=current_user.birthdate.year)
def explore(): q_address = request.args.get('loc') q_radius = request.args.get('rad') q_skill = request.args.get('ski') q_gender = request.args.get('gen') q_min_age = request.args.get('min') q_max_age = request.args.get('max') q_strings = {"address": q_address, "radius": q_radius, "skill": q_skill, "gender": q_gender, "min_age": q_min_age, "max_age": q_max_age} if request.method == 'POST': address = request.form.get("location") skill = request.form.get("skill") radius = request.form.get("radius") gender = request.form.get("gender") min_age = request.form.get("min_age") max_age = request.form.get("max_age") if not address and not radius: return json.dumps({'status': 'All fields required', 'box_ids': ['location', 'radius']}) if not address: return json.dumps({'status': 'Location required', 'box_ids': ['location']}) if not radius: return json.dumps({'status': 'Radius required', 'box_ids': ['radius']}) location = geocode(address) if not location: return json.dumps({'status': 'Non-valid location', 'box_ids': ['location']}) try: float(radius) except ValueError: return json.dumps({'status': 'Non-valid radius', 'box_ids': ['radius']}) url = f'/explore?loc={address}&rad={radius}' if skill: if skill in available_skills: url += f'&ski={skill}' if gender: if gender in ["Male", "Female", "Other"]: url += f'&gen={gender}' if min_age: url += f'&min={min_age}' if max_age: url += f'&max={max_age}' return json.dumps({'status': 'Successfully validated', 'url': url}) if not q_address or not q_radius: return render_template("explore.html", search=False, available_skills=available_skills, **q_strings) q_location = geocode(q_address) if not q_location: return render_template("explore.html", search=False, available_skills=available_skills, **q_strings) try: query = get_explore_query(latitude=q_location.latitude, longitude=q_location.longitude, radius=q_radius, skill=q_skill, gender=q_gender, min_age=q_min_age, max_age=q_max_age) except ValueError: abort(404) profiles = query.limit(5).all() distances = get_distances_from_to(profiles=profiles, latitude=q_location.latitude, longitude=q_location.longitude) return render_template("explore.html", search=True, profiles=profiles, distances=distances, zip=zip, available_skills=available_skills, **q_strings)