Пример #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
Пример #2
0
def check_duplicate_email():
    if request.method == 'POST':
        email = request.form.get('email')
        user = models.User.query.filter(models.User.email==email).first()

        if user:
            return cors_response(("That email is already registered", 400))

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

        if (firstName and lastName):
            avatar = generator.generate(firstName + lastName, 240, 240, output_format="png")
            return cors_response(base64.b64encode(avatar))
        else:
            return cors_response(("Bad Request.", 400))
    else:
        pass