def register(): form = RegisterForm() if request.method == 'POST' and form.validate(): import datetime birthday = is_valid_age(datetime.datetime.strptime(request.form['birthDay'] + "-" + request.form['birthMonth'] + "-" + request.form['birthYear'], '%d-%m-%Y')) user_saved = User.query.filter_by(email=request.form['mail']).first() if user_saved is None: new_user = User( prefix=request.form['prefix'], name=is_valid_string(request.form['name']), middleName=is_valid_string(request.form['middleName']), lastName=is_valid_string(request.form['lastName']), gender=request.form['gender'], birthday=birthday, nationality=request.form['nationality'], codeArea=is_valid_number(request.form['codeArea']), telephone=is_valid_number(request.form['telephone']), email=is_email(request.form['mail']) ) if new_user.prefix == "-" or new_user.gender == "-" or new_user.nationality == "-": raise Exception(u"Por favor, selecciona una opción válida.".encode('utf-8')) db.session.add(new_user) db.session.commit() return render_template('registered.html') else: flash(u'The user is already registered') return render_template('register.html', form=form) else: return render_template('register.html', form=form)
def post(self): """ GET /v1/login """ args = Arguments(request.json) args.string("username", required=True) args.string("password", required=True) args.validate() if is_email(args.username): user = User.get(email=args.username) else: user = User.get(username=args.username) if user and not user.email_verified: return {"message": "Account not validated"}, 401 elif user and user.check_password(args.password): identity = { "id": user.id, "username": user.username, "email": user.email } access_token = create_refresh_token(identity=identity) try: user.date_lastseen = datetime.now().strftime( '%Y-%m-%d %H:%M:%S') user.save() except Exception as e: return {"message": str(e)}, 401 return { "access_token": access_token, "user": get_full_user(user.id) }, 200 else: return {"message": "Failed to authenticate."}, 401
def submitname(request): first_name = request.POST.get("first_name", None) contact_info = request.POST.get("contact_info", None) context = {} custom_errors = "" if (first_name): context["first_name"] = first_name if (contact_info): context["contact_info"] = contact_info # Create a circle new_circle = Circle(circle_name=first_name + "'s circle", circle_created_date=timezone.now(), ) new_circle.save() # Create our new member new_member = Member(circle=new_circle, circle_owner=True, member_name=first_name, member_created_date=timezone.now(), ) # Check to see if current contact info is valid phone or email if is_phone(contact_info): new_phone = phonenumbers.parse(contact_info, "US") new_phone = phonenumbers.format_number(new_phone, phonenumbers.PhoneNumberFormat.E164) new_member.member_phone = new_phone if is_email(contact_info): new_member.member_email = contact_info if not is_phone(contact_info) and not is_email(contact_info): # Bad data error custom_errors += "<li>contact info must be either a valid phone number OR email</li>" new_member.save() set_member_and_circle(request, new_circle, new_member) else: # Missing contact data error custom_errors += "<li>name is present but contact info is missing</li>" else: # Missing name data error custom_errors += "<li>" if (contact_info): context["contact_info"] = contact_info custom_errors += "contact info is present but " custom_errors += "name is missing</li>" if custom_errors != "": custom_errors = format_html("<p><ul>{}</ul></p>", mark_safe(custom_errors)) # If there are any errors, kick out and display them context["custom_errors"] = custom_errors context["anchor"] = "signup" return render(request, "circly/index.html", context) return HttpResponseRedirect(reverse("connect:flow", kwargs={}))
def submitcircle(request): new_member = get_current_member(request) new_circle = get_current_circle(request) count = 2 posted_members = {} custom_errors = "" while count <= settings.CIRCLE_MAX_SIZE: member_contact = {} current_name = request.POST.get("name_" + str(count), None) current_contact = request.POST.get("contact_" + str(count), None) if (current_name): if (current_contact): # Check to see if current contact info is valid phone or email if is_phone(current_contact): member_contact["contact_type"] = "phone" if is_email(current_contact): member_contact["contact_type"] = "email" if not is_phone(current_contact) and not is_email(current_contact): # Bad data error custom_errors += "<li>contact_" + str(count) + " must be either a valid phone number OR email</li>" member_contact["contact_info"] = current_contact posted_members[current_name] = member_contact else: # Missing contact data error custom_errors += "<li>name_" + str(count) + " is present but contact_" + str(count) + " is missing</li>" else: if len(posted_members) < (settings.CIRCLE_MIN_SIZE - 1): # Missing name data error custom_errors += "<li>name_" + str(count) + " is missing</li>" count += 1 # Check to see if we have minimum more members added if len(posted_members) < (settings.CIRCLE_MIN_SIZE - 1): custom_errors += "<li>You need at least " + str(settings.CIRCLE_MIN_SIZE) + " members (including yourself) in your circle</li>" if custom_errors != "": custom_errors = format_html("<p><ul>{}</ul></p>", mark_safe(custom_errors)) # If there are any errors, kick out and display them context = {"member":new_member, "num_range_str":settings.CONTACT_RANGE_STR, "custom_errors":custom_errors, } return render(request, "circly/network.html", context) for each_member in posted_members.keys(): # Create new members and add to the circle if (posted_members[each_member]["contact_type"] == "email"): next_member = Member(circle=new_circle, circle_owner=False, member_name=each_member, member_email=posted_members[each_member]["contact_info"], member_created_date=timezone.now(), ) elif (posted_members[each_member]["contact_type"] == "phone"): new_phone = phonenumbers.parse(posted_members[each_member]["contact_info"], "US") new_phone = phonenumbers.format_number(new_phone, phonenumbers.PhoneNumberFormat.E164) next_member = Member(circle=new_circle, circle_owner=False, member_name=each_member, member_phone=new_phone, member_created_date=timezone.now(), ) next_member.save() # Create invite code with short link for profile sign up invite_code = hash_code(posted_members[each_member]["contact_info"]) invite_url = "http://www.circly.org/invite/" + invite_code new_short_url = random_bitly(invite_url) invite = Invitation(member=next_member, invite_code=invite_code, invite_short_url=new_short_url, invite_created_date=timezone.now(), invite_send_date=timezone.now()) invite.save() # Create reminders for all new members to join the circle remind = Reminder(member=next_member, reminder_subject=new_circle.circle_owner_name() + " would like you to join their circle of support", reminder_message="Hey " + each_member + ", visit " + new_short_url + " to fill in your profile and join a circle of preventive care.", reminder_created_date=timezone.now(), reminder_send_date=timezone.now(), ) remind.save() if new_member.member_email: owner_hash = hash_code(new_member.member_email) if new_member.member_phone: owner_hash = hash_code(new_member.member_phone) dashboard_url = "http://www.circly.org/dashboard/" + owner_hash new_short_dashboard_url = random_bitly(dashboard_url) new_circle.circle_short_url = new_short_dashboard_url new_circle.save() set_member_and_circle(request, new_circle, new_member) return HttpResponseRedirect(reverse("connect:dashboard", kwargs={"owner_hash":owner_hash}))