Beispiel #1
0
    def update(self, request):
        if request.method == 'POST':
            first_name = request.form['firstname']
            last_name = request.form['lastname']

            error = None
            if not first_name:
                error = 'A first name is required.'
            elif not last_name:
                error = 'A last name is required.'
            else:
                if 'avatar' in request.files:
                    file = request.files['avatar']
                    if file.filename:
                        flask_app.logger.info(file)
                        uploader = Upload()
                        avatar = uploader.upload(file, session['user']['localId'])
                        session['user']['avatar'] = avatar
                try:
                    session['user']['first_name'] = first_name
                    session['user']['last_name'] = last_name
                    database = Database()
                    user_auth = database.update_user(session['user'])
                except Exception as err:
                    error = err
            if error:
                flash(str(error))
Beispiel #2
0
    def update(self, request):
        if request.method == 'POST':
            first_name = request.form['firstname']
            last_name = request.form['lastname']

            error = None
            if not first_name:
                error = 'A first name is required.'
            elif not last_name:
                error = 'A last name is required.'
            else:
                if 'avatar' in request.files:
                    file = request.files['avatar']
                    if file.filename:
                        uploader = Upload()
                        avatar = uploader.upload(file,
                                                 session['user']['localId'])
                        session['user']['avatar'] = "/" + avatar.strip("/")
                try:
                    session['user']['first_name'] = first_name
                    session['user']['last_name'] = last_name
                    database = Database()
                    user_auth = database.update_user(session['user'])
                    session.modified = True
                except Exception as err:
                    error = err

        if error:
            raise Exception(error)
        else:
            return
Beispiel #3
0
    def upload(self, request):

        image_id        = str(uuid.uuid1())
        name            = request.form['name']
        description     = request.form['description']
        category        = request.form['category']
        image_filter    = request.form['filter']

        # Validates required registration fields
        error = None
        user_id = False

        if (session['user'] and session['user']['localId']):
            user_id     = session['user']['localId']
            user_name   = session['user']['first_name'] + " " + session['user']['last_name']
            user_avatar = session['user']['avatar']
        else: 
            error = 'You must be logged in to upload an image.'

        if 'image' not in request.files:
            error = 'A file is required.'
        else:
            file = request.files['image']

        if not error:
            if file.filename == '':
                error = 'A file is required.'
            elif not name:
                error = 'An name is required.'
            elif not description:
                error = 'A description is required.'
            elif not category:
                error = 'A category is required.'
            else:
                try:
                    uploader = Upload()
                    upload_location = uploader.upload(file, image_id)
                    image_data = {
                        "id":                   image_id,
                        "upload_location":      '/' + upload_location,
                        "user_id":              user_id,
                        "user_name":            user_name,
                        "user_avatar":          user_avatar,
                        "name":                 name,
                        "description":          description,
                        "category":             category,
                        "filter":               image_filter,
                        "created_at":           int(time.time())
                    }
                    database = Database()
                    uploaded = database.save_image(image_data, image_id)
                except Exception as err:
                    error = err
        if error:
            flask_app.logger.info('################ UPLOAD ERROR #######################')
            flask_app.logger.info(error)
            raise Exception(error)
        else:
            return image_id
Beispiel #4
0
    def update(self, request):
        """
        Update method.

        Processes update request, and edits users' data in Firebase upon success.

        Parameters: 
            request (obj): The POST request object
    
        Raises: 
            error (Exception): Error from failed Firebase request    

        """
        # Extracts the required fields from POST request.
        if request.method == 'POST':
            first_name = request.form['firstname']
            last_name = request.form['lastname']

            # Validates required fields from the update request.
            error = None
            if not first_name:
                error = 'A first name is required.'
            elif not last_name:
                error = 'A last name is required.'
            else:
                if 'avatar' in request.files:
                    file = request.files['avatar']
                    if file.filename:
                        uploader = Upload()
                        avatar = uploader.upload(file,
                                                 session['user']['localId'])
                        session['user']['avatar'] = "/" + avatar.strip("/")
                try:
                    session['user']['first_name'] = first_name
                    session['user']['last_name'] = last_name
                    database = Database()
                    user_auth = database.update_user(session['user'])
                    session.modified = True
                except Exception as err:
                    # Raises error from failed Firebase request.
                    error = err

        if error:
            # Raises error from failed Firebase request.
            raise Exception(error)
        else:
            # Return upon success
            return
Beispiel #5
0
    def update(self, request):
        """
        Updates User's First & Last Name

        """
        if request.method == 'POST':
            first_name = request.form['firstname']
            last_name = request.form['lastname']

            error = None
            # When first name field is not filled out
            if not first_name:
                error = 'A first name is required.'
            # When last name field is not filled out
            elif not last_name:
                error = 'A last name is required.'
            else:
                """
                Checks if user has uploaded a file. If so
                it will update users avatar
                """
                if 'avatar' in request.files:
                    file = request.files['avatar']
                    if file.filename:
                        uploader = Upload()
                        avatar = uploader.upload(file,
                                                 session['user']['localId'])
                        session['user']['avatar'] = "/" + avatar.strip("/")
                try:
                    session['user']['first_name'] = first_name
                    session['user']['last_name'] = last_name
                    database = Database()
                    user_auth = database.update_user(session['user'])
                    session.modified = True
                except Exception as err:
                    error = err

        if error:
            raise Exception(error)
        else:
            return
Beispiel #6
0
    def update(self, request):
        """
        It gets the users data from the form and then sends it to the database
        """
        if request.method == 'POST':
            first_name = request.form['firstname']
            last_name = request.form['lastname']

            error = None
            if not first_name:
                error = 'A first name is required.'
            elif not last_name:
                error = 'A last name is required.'
            else:
                #If the user has uplaoded an image, handle the file upload
                if 'avatar' in request.files:
                    file = request.files['avatar']
                    if file.filename:
                        uploader = Upload()
                        avatar = uploader.upload(file,
                                                 session['user']['localId'])
                        session['user']['avatar'] = "/" + avatar.strip("/")

                        #Try to update the user data
                try:
                    session['user']['first_name'] = first_name
                    session['user']['last_name'] = last_name
                    database = Database()
                    user_auth = database.update_user(session['user'])
                    session.modified = True
                except Exception as err:
                    error = err

        if error:
            raise Exception(error)
        else:
            return
Beispiel #7
0
    def update(self, request):
        if request.method == 'POST':
            # request form infomation for firstname, and lastname.
            first_name = request.form['firstname']
            last_name = request.form['lastname']

            error = None
            if not first_name:
                # If error / empty field with the firstname will display readable error
                error = 'A first name is required.'
            elif not last_name:
                # If error / empty field with the lastname will display readable error
                error = 'A last name is required.'
            else:
                # If avatar is in request files
                if 'avatar' in request.files:
                    # Request the avatar files
                    file = request.files['avatar']
                    if file.filename:
                        # Pulls avatar, user and localid from session data. 
                        uploader = Upload()
                        avatar = uploader.upload(file, session['user']['localId'])
                        session['user']['avatar'] = "/" + avatar.strip("/")
                try:
                    # sets session users firstname and lastname to firstname and lastname.
                    session['user']['first_name'] = first_name
                    session['user']['last_name'] = last_name
                    database = Database()
                    user_auth = database.update_user(session['user'])
                    session.modified = True
                except Exception as err:
                    error = err

        if error:
            raise Exception(error)
        else:
            return
Beispiel #8
0
    def update(self, request):
        if request.method == 'POST':
            #requests first name and last name
            first_name = request.form['firstname']
            last_name = request.form['lastname']

            error = None
            #if a first or last name is not provided an error is sent. if profile is updated via a form post
            if not first_name:
                error = 'A first name is required.'
            elif not last_name:
                error = 'A last name is required.'
            else:
                #
                if 'avatar' in request.files:
                    file = request.files['avatar']
                    if file.filename:
                        uploader = Upload()
                        avatar = uploader.upload(file,
                                                 session['user']['localId'])
                        session['user']['avatar'] = "/" + avatar.strip("/")
                try:
                    session['user']['first_name'] = first_name
                    session['user']['last_name'] = last_name
                    database = Database()
                    user_auth = database.update_user(session['user'])
                    session.modified = True
                except Exception as err:
                    error = err

        if error:
            raise Exception(error)
            # Raise error from failed Firebase request
        else:
            # Return on success
            return
Beispiel #9
0
    def upload(self, request):
        """
        Upload method.

        Requests to upload an image from the user to the DB. 
        """

        # Checks that the required fields are filled, and sent to the DB.
        image_id = str(uuid.uuid1())
        name = request.form['name']
        description = request.form['description']
        category = request.form['category']
        image_filter = request.form['filter']

        # Validates required registration fields
        error = None
        user_id = False

        # Associates the session User ID with the localID on the DB.
        if (session['user'] and session['user']['localId']):
            user_id = session['user']['localId']
            user_name = session['user']['first_name'] + " " + session['user'][
                'last_name']
            user_avatar = session['user']['avatar']
        else:
            # Raises error if the user is not logged in as the localID and the user ID are not able to be associated.
            error = 'You must be logged in to upload an image.'

        # Raises error if there is no image requested to be sent to the DB.
        if 'image' not in request.files:
            error = 'A file is required.'
        else:
            # Requests that the image be sent to the DB, if the above requirements are met.
            file = request.files['image']

        # Raises a specific error based on which of the following requirements are not met.
        if not error:
            if file.filename == '':
                error = 'A file is required.'
            elif not name:
                error = 'An name is required.'
            elif not description:
                error = 'A description is required.'
            elif not category:
                error = 'A category is required.'
            else:
                # Attempts to send the following information to the DB asssociated with the image ID in the following format.
                try:
                    uploader = Upload()
                    upload_location = uploader.upload(file, image_id)
                    image_data = {
                        "id": image_id,
                        "upload_location": '/' + upload_location,
                        "user_id": user_id,
                        "user_name": user_name,
                        "user_avatar": user_avatar,
                        "name": name,
                        "description": description,
                        "category": category,
                        "filter": image_filter,
                        "created_at": int(time.time())
                    }
                    database = Database()
                    uploaded = database.save_image(image_data, image_id)
                except Exception as err:
                    # Raise error upon failed Firebase upload.
                    error = err
        if error:
            # Raises an error if Flask fails to upload the information to Firebase, and displays a message for the user.
            flask_app.logger.info(
                '################ UPLOAD ERROR #######################')
            flask_app.logger.info(error)
            raise Exception(error)
        else:
            # Returns image ID upon successful upload.
            return image_id
    def upload(self, request):
        # sets data base variables to the form requests infomation.
        image_id = str(uuid.uuid1())
        name = request.form['name']
        description = request.form['description']
        category = request.form['category']
        image_filter = request.form['filter']

        # Validates required registration fields
        error = None
        user_id = False
        # Pulls session user and local_Id to attack to image.
        # Also checks in their is a user login in the session.
        if (session['user'] and session['user']['localId']):
            user_id = session['user']['localId']
            user_name = session['user']['first_name'] + " " + session['user'][
                'last_name']
            user_avatar = session['user']['avatar']
        else:
            # Displays readable error
            error = 'You must be logged in to upload an image.'
            # Checks request file for a image file
        if 'image' not in request.files:
            # If image file isnt found display readable error.
            error = 'A file is required.'
        else:
            #Re-request the image file.
            file = request.files['image']
        #List of readable errors
        if not error:
            # If file isnt present display error
            if file.filename == '':
                error = 'A file is required.'
                # If name blank display error
            elif not name:
                error = 'An name is required.'
                # If Description is blank display error
            elif not description:
                error = 'A description is required.'
                # If category is blank display error
            elif not category:
                error = 'A category is required.'
            else:
                try:
                    # sets image data to all the data that makes up the image
                    uploader = Upload()
                    upload_location = uploader.upload(file, image_id)
                    image_data = {
                        "id": image_id,
                        "upload_location": '/' + upload_location,
                        "user_id": user_id,
                        "user_name": user_name,
                        "user_avatar": user_avatar,
                        "name": name,
                        "description": description,
                        "category": category,
                        "filter": image_filter,
                        "created_at": int(time.time())
                    }
                    database = Database()
                    # Uploads and saves image data to database
                    uploaded = database.save_image(image_data, image_id)
                except Exception as err:
                    error = err
        if error:
            # We have error, pass it back to controller
            # and display a readable error in git
            flask_app.logger.info(
                '################ UPLOAD ERROR #######################')
            flask_app.logger.info(error)
            raise Exception(error)
        else:
            # We have no errors, return image_id
            return image_id