Exemple #1
0
def users():
    """ GET REQUEST """
    if request.method == 'GET':
        return cors_response((jsonify(users = [user.serialize for user in models.User.query.all()]),200))
    
    """ POST REQUEST """
    if request.method == 'POST':
        email = request.form.get('email')
        user = models.User.query.filter(models.User.email==email).first()

        if user:
            return cors_response(("Email already registered",400))

        firstName = request.form.get('firstName')
        lastName = request.form.get('lastName')
        password = request.form.get('password')
        university = request.form.get('university')

        if (firstName and lastName and email and password and university):
            pw_hash = bcrypt.generate_password_hash(password)

            user = models.User(firstName = firstName,
                               lastName = lastName,
                               email = email,
                               password = pw_hash,
                               university = university)
            profile = models.Profile()
            grad_year = request.form.get('grad_year')
            major = request.form.get('major')
            courses = request.form.get('classes')
            bio = request.form.get('bio')
            avatar = generator.generate(firstName + lastName, 240, 240, output_format="png")

            if (grad_year):
                profile.grad_year = grad_year

            if (major):
                profile.major = major

            if (courses):
                courseList = json.loads(courses)
                user.courses = ",".join(courseList)

            if (bio):
                profile.bio = bio
            
            profile.avatar = avatar

            user.profile = profile
            db.session.add(user)
            db.session.commit()
            # Send email to new user.
            regEmail = generateEmail("registration", firstName = firstName)
            sendEmail(email, "Welcome to Deku!", regEmail[0], regEmail[1])
            return cors_response((jsonify(user = user.serialize), 201))
        
        else:
            return cors_response(("Bad Request.", 400))
    else:
        pass
Exemple #2
0
def generateTemporaryPassword():
    if request.method == 'POST':
        email = request.form.get('email')
        if email:
            # Find user by email address.
            user = models.User.query.filter(models.User.email==email).first()
            if user:
                # Send reset email
                tempPassword = ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(32)])
                user.password = bcrypt.generate_password_hash(tempPassword)
                db.session.commit()
                resetEmail = generateEmail("reset", firstName = user.firstName, tempPassword = tempPassword)
                sendEmail(email, "Forgot your password?", resetEmail[0], resetEmail[1])
                return cors_response(("Email sent.", 200))                
            else:
                return cors_response(("User not found.", 404))
        else:
            return cors_response(("Bad Request.", 400))
    else:
        pass