def test_5_create_duplicate_email(self):
     try:
         models.createUser(username='******',
                           email='*****@*****.**',
                           password='******')
         self.fail("exception not raised")
     except (EmailUsedError):
         pass
 def test_3_create_bademail(self):
     try:
         models.createUser(username='******',
                           email='john@@test.com',
                           password='******')
         self.fail("exception not raised")
     except (InvalidEmailError):
         pass
示例#3
0
def register():
    form = RegistrationForm(request.form)
    if request.method == 'GET':
        return render_template('register.html', form=form)

    if request.method == 'POST':
        #if form.validate_on_submit():
        fname = form.fname.data
        lname = form.lname.data
        username = form.username.data
        email = form.email.data
        password = sha256_crypt.encrypt((str(form.password.data)))
        createUser(fname, lname, username, email, password)
        return "success"
示例#4
0
文件: views.py 项目: elhexx/QrCode
def add():
    if request.method == 'POST':
        fname = request.form['fname']
        lname = request.form['lname']
        ch = 0
        msg = "User added successfully"
        try:
            createUser(fname, lname)
            jsonObject = getUsers()
            return render_template('add.html', msg=msg, data=jsonObject)
        except Exception as e:
            return e.message
    if request.method == 'GET':
        jsonObject = getUsers()
        #return jsonObject
        return render_template('add.html', data = jsonObject)
示例#5
0
def register():
    if request.method == 'GET':
        return render_template('register.html')
    elif request.method == 'POST':
        error = checkRegistrationForm(request.form)
        if error:
            return render_template('register.html', error=error)
        else:
            data = {}
            data['email'] = request.form['InputEmail']
            result = checkUser(data)
            if result['success']:
                if result['exists']:
                    flash('User already registered.', {
                        'header': 'Oops!!',
                        'class': 'alert-danger'
                    })
                    return redirect(url_for('register'))
                else:
                    data['firstName'] = request.form['InputFirstName']
                    data['lastName'] = request.form['InputLastName']
                    data['password'] = hash_password(
                        request.form['InputPassword'])
                    result = createUser(data)
                    if result['success']:
                        flash('User registered', {
                            'header': 'Success!!',
                            'class': 'alert-success'
                        })
                        return redirect(url_for('login'))
                    else:
                        flash('Some error occured. Please try again!', {
                            'header': 'Oops!!',
                            'class': 'alert-danger'
                        })
                        return redirect(url_for('register'))
            else:
                flash('Some error occured. Please try again!', {
                    'header': 'Oops!!',
                    'class': 'alert-danger'
                })
                return redirect(url_for('register'))
示例#6
0
    def post(self):
        #will need to be moved to occur after form submission
        id = get_user_id()
        user = models.getUser(id)
        if user is None:
            models.createUser(id)
            user = models.getUser(id)
        try:
            upload_files = self.get_uploads()
            blob_info = upload_files[0]
            type = blob_info.content_type
            models.createUser(id)
            name = self.request.get("name")
            year = self.request.get("year")
            interests = self.request.get("interests")
            employer = self.request.get("employer")
            bio = self.request.get("bio")
            username = self.request.get('username')
            id = get_user_id()
            if username == "":
                username = user.username
            # if the uploaded file is an image
            if type in ['image/jpeg', 'image/png', 'image/gif', 'image/webp']:
                image = blob_info.key()
                models.updateUser(id, name, year, interests, bio, employer,
                                  username, image)

            # if the uploaded file is not an image
            else:
                models.updateUser(id, name, year, interests, bio, employer,
                                  username, user.image_url)

            self.redirect('/profile?id=' + id)
        # no image to upload
        except IndexError:
            id = get_user_id()
            models.createUser(id)
            name = self.request.get("name")
            year = self.request.get("year")
            interests = self.request.get("interests")
            employer = self.request.get("employer")
            bio = self.request.get("bio")
            username = self.request.get('username')
            if username == "":
                username = user.username
            id = get_user_id()
            models.updateUser(id, name, year, interests, bio, employer,
                              username, user.image_url)
            self.redirect('/profile?id=' + id)
示例#7
0
    def post(self):
        #will need to be moved to occur after form submission
        id = get_user_id()
        user = models.getUser(id)
        if user is None:
            models.createUser(id)
            user = models.getUser(id)
        try:
            upload_files = self.get_uploads()
            blob_info = upload_files[0]
            type = blob_info.content_type
            models.createUser(id)
            name = self.request.get("name")
            year = self.request.get("year")
            interests = self.request.get("interests")
            employer = self.request.get("employer")
            bio = self.request.get("bio")
            username= self.request.get('username')
            id = get_user_id()
            if username == "":
                username = user.username
            # if the uploaded file is an image
            if type in ['image/jpeg', 'image/png', 'image/gif', 'image/webp']:
                image = blob_info.key()
                models.updateUser(id, name, year, interests, bio, employer,username, image)

            # if the uploaded file is not an image
            else:
                models.updateUser(id, name, year, interests, bio,
                        employer,username, user.image_url)

            self.redirect('/profile?id=' + id)
        # no image to upload
        except IndexError:
            id = get_user_id()
            models.createUser(id)
            name = self.request.get("name")
            year = self.request.get("year")
            interests = self.request.get("interests")
            employer = self.request.get("employer")
            bio = self.request.get("bio")
            username = self.request.get('username')
            if username == "":
                username = user.username
            id = get_user_id()
            models.updateUser(id, name, year, interests, bio,
                    employer,username, user.image_url)
            self.redirect('/profile?id=' + id)
示例#8
0
文件: views.py 项目: jasenc/catalog
def gconnect():
    # First we should validate that the state token the server sent to the
    # client matches the state token the client sent to the server.
    # If the state variable returned by the request arguments does not equal
    # the state variable we assigned above:
    if request.args.get('state') != login_session['state']:
        # We'll respond accordingly.
        response = make_response(json.dumps('Invalid state parameter.'), 401)
        response.headers['Content-Type'] = 'application/json'
        return response
    # Otherwise let's grab the authorization code.
    code = request.data

    try:
        # Upgrade the authorization code into a credentials object.
        oauth_flow = (flow_from_clientsecrets
                      ('/var/www/catalog/google_client_secrets.json', scope=''))
        # Somehow the following line of code indicates that this is the
        # one-time-use code the server will be sending off.
        oauth_flow.redirect_uri = 'postmessage'
        # The exchange is initiated.
        credentials = oauth_flow.step2_exchange(code)
    except FlowExchangeError:
        response = make_response(
            json.dumps('Failed to upgrade the authoerization code.'), 401)
        response.headers['Content-Type'] = 'applications/json'
        return response

    # Now that we have the credentials lets verify some things.
    # First that we were provided with a valid access token.
    access_token = credentials.access_token
    # This is the url where we can check the access_token.
    url = ('https://www.googleapis.com/oauth2/v1/tokeninfo?access_token={0}'
           .format(access_token))
    # We'll create an Http() object.
    h = httplib2.Http()
    # We'll store our result in a variable.
    result = json.loads(h.request(url, 'GET')[1])
    print result
    # The only way we know if the access token wasn't valid is if an error
    # is returned.
    if result.get('error') is not None:
        response = make_response(json.dumps(result.get('error')), 500)
        response.headers['Content-Type'] = 'application/json'

    # And we can verify that the access token is being used appropriately.
    gplus_id = credentials.id_token['sub']
    if result['user_id'] != gplus_id:
        response = make_response(
            json.dumps("Token's client ID does not match app's."), 401)
        response.headers['Content-Type'] = 'application/json'
        return response

    # Verify that the access token is valid for this app.
    if result['issued_to'] != CLIENT_ID:
        response = make_response(
            json.dumps("Token's client ID does not match app's."), 401)
        print "Token's client ID does not match app's."
        response.headers['Content-Type'] = 'application/json'
        return response

    # And let's make sure the user isn't already logged in.
    stored_credentials = login_session.get('credentials')
    stored_gplus_id = login_session.get('gplus_id')
    if stored_credentials is not None and gplus_id == stored_gplus_id:
        response = make_response(json.dumps('Current user is already \
                                 connected.'), 200)
        response.headers['Content-Type'] = 'application/json'
        return response

    # Store the access token in the session for later use.
    login_session['credentials'] = credentials.access_token
    print login_session['credentials']
    login_session['gplus_id'] = gplus_id

    # Get the user's info
    userinfo_url = "https://www.googleapis.com/oauth2/v1/userinfo"
    params = {'access_token': credentials.access_token, 'alt': 'json'}
    answer = requests.get(userinfo_url, params=params)

    data = answer.json()

    login_session['username'] = data['name']
    login_session['picture'] = data['picture']
    login_session['email'] = data['email']
    # Let's add how the user signed in.
    login_session['provider'] = 'google'

    # We can check if the user is in our database, and if not add them.
    user_id = models.getUserID(data["email"])
    if not user_id:
        user_id = models.createUser(login_session)
    login_session['user_id'] = user_id

    output = "SUCCESS!"

    return output
 def test_4_create_duplicate_username(self):
     try:
         models.createUser(username=self.usern, password='******')
         self.fail("exception not raised")
     except (UsernameUsedError):
         pass