Beispiel #1
0
    def login(self, request):
        """
        Attempts to login using user credentials

        """
        if request.method == 'POST':
            email = request.form['email']
            password = request.form['password']

            error = None
            # When email field is not filled in.
            if not email:
                error = 'An email is required.'
            # When password  field is not filled in.
            elif not password:
                error = 'Password is required.'
            else:
                try:
                    database = Database()
                    user = database.login(email, password)
                    # TODO Remove for production
                    #flask_app.logger.info(user)
                    self.user.set_user(user)
                except Exception as err:
                    error = err

        if error:
            raise Exception(error)
        else:
            return
Beispiel #2
0
    def get_images(self, limit=20):
        """ 
        Simple DB call to get X number of images,
        with a default image limit of 20.  

        Returns a Pyrebase object which may, 
        or may not contain results.
    
        """

        # Set variable defaults.
        error = None
        images = False

        # Set try to fetch images from DB.
        try:
            database = Database()
            images = database.get_images(limit)

        # Pass DB error back to controller.
        except Exception as err:
            flask_app.logger.info(err)
            error = err

        if error:
            # Pass DB error back to controller.
            raise Exception(error)
        else:
            # successfully returned Pyrebase.
            return images
Beispiel #3
0
    def delete_image(self, image_id):
        """
        Delete method.

        Fetches the image ID from the DB, and removes it from the DB.
        """

        error = None

        # Validates the request, fetches the ID from the DB, and deletes it from the DB.
        try:
            database = Database()
            database.delete_image(image_id)

        except Exception as err:
            # Identifies if flask is the cause of the error, and raises error if true.
            flask_app.logger.info(err)
            error = err

        if error:
            # Raise error from failed Firebase request.
            raise Exception(error)
        else:
            # Return on success.
            return
Beispiel #4
0
    def login(self, request):
        if request.method == 'POST':
            # reuqests form infomation for email, and password.
            email = request.form['email']
            password = request.form['password']

            error = None
            if not email:
                # No email gotten in the request, push readable error
                error = 'An email is required.'
            elif not password:
                # No password pulled in the request, push readable error
                error = 'Password is required.'
            else:
                try:
                    # Pulls infomation about email and password from database.
                    database = Database()
                    user = database.login(email, password)
                    # TODO Remove for production
                    #flask_app.logger.info(user)
                    self.user.set_user(user)
                except Exception as err:
                    error = err

        if error:
            raise Exception(error)
        else:
            return
Beispiel #5
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 #6
0
    def like(self, image_id, like, request):
                
        changed = False
        #Checks in the image like data is true or false 
        # and displays the approriate heart.
        likes = session['user']['likes']
        #If like = true and the image_id isnt in likes in the database 
        #add it to database with like being set to true
        if like == 'true':
            if image_id not in likes:
                likes.append(image_id)
                changed = True
        else:
            # If Image_id is in likes, remove image_id from likes.
            if image_id in likes:
                likes.remove(image_id)
                changed = True

        if changed:
            # If changed change appearance of the heart on screen 
            # by pulling the data from the database
            session['user']['likes'] = likes
            database = Database()
            database.update_user(session['user'])
            session.modified = True

        return changed
Beispiel #7
0
    def get_category_images(self, category, limit=20):
        """
        Get method

        Fetches image IDs from a selected category from the DB, and limits the number displayed to 20. 
        """

        error = None
        images = False
        # Validates and sends the request to the DB.
        try:
            database = Database()
            images = database.get_category_images(category, limit)

        except Exception as err:
            # Identifies if flask is the cause of the error, and raises error if true.
            flask_app.logger.info(err)
            error = err

        if error:
            # Raise error from failed Firebase request.
            raise Exception(error)
        else:
            # Return on success.
            return images
Beispiel #8
0
    def login(self, request):
        if request.method == 'POST':
            email = request.form['email']
            password = request.form['password']

            #If a email or password hasnt been provided error is provided.
            error = None
            if not email:
                error = 'An email is required.'
            elif not password:
                error = 'Password is required.'
            else:
                try:
                    #Sends request if email and password is provided
                    database = Database()
                    user = database.login(email, password)
                    # TODO Remove for production
                    #flask_app.logger.info(user)
                    self.user.set_user(user)
                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 login(self, request):
        """
        It gets the users login credentials from the form and then sends it to the database
        """

        if request.method == 'POST':
            email = request.form['email']
            password = request.form['password']

            error = None
            if not email:
                error = 'An email is required.'
            elif not password:
                error = 'Password is required.'
            else:
                try:
                    database = Database()
                    user = database.login(email, password)
                    # TODO Remove for production
                    #flask_app.logger.info(user)
                    self.user.set_user(user)
                except Exception as err:
                    error = err

        if error:
            raise Exception(error)
        else:
            return
Beispiel #10
0
    def like(self, image_id, like, request):
        """
        Like method. 

        If user likes image, then that image will be added to their 
        liked images list.

        If the user unlikes image, then that iamge will be removed
        from their liked images list.
        
        """
        changed = False
        likes = session['user']['likes']
        # User has liked Image.
        if like == 'true':
            # Check if user has already liked it
            if image_id not in likes:
                likes.append(image_id)
                changed = True
        else:
            # User unlikes image.
            if image_id in likes:
                likes.remove(image_id)
                changed = True
        # If likes are changed will be updated in the database.
        if changed:
            session['user']['likes'] = likes
            database = Database()
            database.update_user(session['user'])
            # Updates the session.
            session.modified = True

        return changed
Beispiel #11
0
    def login(self, request):
        """
        login method.

        Processes POST request, and logs the user into Firebase upon success.

        """
        # Request is sent to Firebase DB.
        if request.method == 'POST':
            email = request.form['email']
            password = request.form['password']

            # Validates required login fields.
            error = None
            if not email:
                error = 'An email is required.'
            elif not password:
                error = 'Password is required.'
            else:
                try:
                    database = Database()
                    user = database.login(email, password)
                    # TODO Remove for production
                    #flask_app.logger.info(user)
                    self.user.set_user(user)
                except Exception as err:
                    error = err

        if error:
            # Raise error from failed Firebase request.
            raise Exception(error)
        else:
            # Return on success.
            return
Beispiel #12
0
    def get_user_images(self, limit=20):
        """
        Get method.

        Fetches the user's images from the 'my images' list in the DB, and displays a maximum of 20 images from this list.
        """

        # Validates required registration fields.
        error = None
        images = False
        user_id = False
        if (session['user'] and session['user']['localId']):
            user_id = session['user']['localId']
        try:
            # Attempts to fetch the images associated with the users' ID from the DB.
            database = Database()
            images = database.get_images(limit, user_id)

        except Exception as err:
            # Identifies if flask is the cause of the error, and raises error if true.
            flask_app.logger.info(err)
            error = err

        if error:
            # Raise error on failed Firebase request.
            raise Exception(error)
        else:
            # Return on success.
            return images
Beispiel #13
0
    def like(self, image_id, like, request):
        """
        Like method.

        Processes POST request, adds/removes the image from the user's 'liked images' or 'unliked images'

        Parameters:
            request (obj): The POST request object (the like)

        Raises:
            error (exception): Error from failed Firebase data request

        """
        # DB checks the users likes in regards to image IDs
        changed = False
        likes = session['user']['likes']

        # Firebase checks the whether the image is in the user's 'liked images' or 'unliked images' lists.
        if like == 'true':
            if image_id not in likes:
                likes.append(image_id)
                changed = True
        else:
            if image_id in likes:
                likes.remove(image_id)
                changed = True

        if changed:
            session['user']['likes'] = likes
            database = Database()
            database.update_user(session['user'])
            session.modified = True
        # Return change upon success
        return changed
    def get_user_images(self, limit=20):
        # Inital Variable Values
        error = None
        images = False
        user_id = False
        #Checks for images that have been uploaded by the sesson user.
        if (session['user'] and session['user']['localId']):
            user_id = session['user']['localId']
        try:
            #Pulls images from database with a limit of 20
            #and has the same user_id
            database = Database()
            images = database.get_images(limit, user_id)

        except Exception as err:
            # We have error, pass it back to controller
            flask_app.logger.info(err)
            error = err

        if error:
            # We have error, pass it back to controller
            raise Exception(error)
        else:
            # We have no errors, return images
            return images
Beispiel #15
0
    def get_image(self, image_id):
        """
        Get method.

        Processes request and fetches image ID, and displays the fetched image for the user.
        """
        error = None
        image = False

        # Validates the request and fetches the ID from the DB.
        try:
            database = Database()
            image = database.get_image(image_id)

        except Exception as err:
            # Identifies if flask is the cause of the error, and raises error if true.
            flask_app.logger.info(err)
            error = err

        if error:
            # Raise error from failed Firebase request.
            raise Exception(error)
        else:
            # Return on success
            return image
Beispiel #16
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 #17
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 #18
0
    def update(self, image_id, request):

        name = request.form['name']
        description = request.form['description']
        category = request.form['category']
        image_filter = request.form['filter']
        created_at = request.form['created_at']
        upload_location = request.form['upload_location']

        # 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 update an image.'

        if not error:
            if 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:
                    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": created_at
                    }
                    database = Database()
                    uploaded = database.save_image(image_data, image_id)
                except Exception as err:
                    error = err
        if error:
            flask_app.logger.info(
                '################ UPDATE ERROR #######################')
            flask_app.logger.info(error)
            raise Exception(error)
        else:
            return
Beispiel #19
0
    def register(self, request):
        """ 
        Registration method. 
    
        Processes POST request, and registers user in Firebase on success

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

        # Extract required fields from POST request
        email = request.form['email']
        password = request.form['password']
        password_confirm = request.form['password_confirm']

        # Validates required registration fields
        error = None
        if not email:
            error = 'An email is required.'
        elif not password:
            error = 'Password is required.'
        elif 6 > len(password):
            error = 'Your password must be at least 6 characters long.'
        elif not password_confirm:
            error = 'Password confirmation is required.'
        elif password != password_confirm:
            error = 'Password and password confirmation should match.'
        else:
            try:
                user_data = {
                    "localId": "",
                    "email": email,
                    "first_name": "",
                    "last_name": "",
                    "avatar": ""
                }
                # Attempt to process valid registration request
                database = Database()
                user_auth = database.register(user_data, password)
            except Exception as err:
                # Raise error from failed Firebase request
                error = err
        if error:
            # Raise error from failed Firebase request
            raise Exception(error)
        else:
            # Return on success
            return
Beispiel #20
0
def index():

    error = None
    latest_images = []
    try:
        database = Database()
        latest_images = database.get_latest_images()
    except Exception as err:
        error = err

    if error:
        flash(error)

    return render_template('home.html', latest_images=latest_images)
Beispiel #21
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 #22
0
    def delete_image(self, image_id):
        #Hello, this bit tells the server that the chosen image should be designated "gone".
        error = None

        try:
            database = Database()
            database.delete_image(image_id)

        except Exception as err:
            flask_app.logger.info(err)
            error = err

        if error:
            raise Exception(error)
        else:
            return
Beispiel #23
0
    def delete_image(self, image_id):
        
        error = None
        
        try:
            database = Database()
            database.delete_image(image_id)

        except Exception as err:
            flask_app.logger.info(err)
            error = err

        if error:
            raise Exception(error)
        else: 
            return
Beispiel #24
0
    def get_images(self, limit=20):
        #Presumably looks for all the images in the server.
        error = None
        images = False

        try:
            database = Database()
            images = database.get_images(limit)

        except Exception as err:
            flask_app.logger.info(err)
            error = err
            #These two tell you if things failed.
        if error:
            raise Exception(error)
        else:
            return images
Beispiel #25
0
    def get_image(self, image_id):
        
        error = None
        image = False
        
        try:
            database = Database()
            image = database.get_image(image_id)

        except Exception as err:
            flask_app.logger.info(err)
            error = err

        if error:
            raise Exception(error)
        else:
            return image
Beispiel #26
0
    def get_category_images(self, category, limit=20):
        
        error = None
        images = False
        
        try:
            database = Database()
            images = database.get_category_images(category, limit)

        except Exception as err:
            flask_app.logger.info(err)
            error = err

        if error:
            raise Exception(error)
        else:
            return images
Beispiel #27
0
    def delete_image(self, image_id):

        error = None

        try:
            #request is made to database to delete image
            database = Database()
            database.delete_image(image_id)

        except Exception as err:
            flask_app.logger.info(err)
            error = err

        if error:
            # Raise error from failed Firebase request
            raise Exception(error)
        else:
            return
    def delete_image(self, image_id):
        # Inital Variable Values
        error = None
        # Tries to delete image id in database.
        try:
            database = Database()
            database.delete_image(image_id)
        # Failed to get delete image raise a exception
        except Exception as err:
            flask_app.logger.info(err)
            error = err

        if error:
            # We have error, pass it back to controller
            raise Exception(error)
        else:
            # We have no errors, return
            return
    def get_images(self, limit=20):
        # Inital Variable Values
        error = None
        images = False

        try:
            # Pull images from database with the limit in place.
            database = Database()
            images = database.get_images(limit)

        except Exception as err:
            flask_app.logger.info(err)
            error = err

        if error:
            raise Exception(error)
        else:
            # Successfully returned pyrebase objects
            return images
 def get_image(self, image_id):
     # Inital Variable Values
     error = None
     image = False
     #Tries to pull image id
     try:
         database = Database()
         image = database.get_image(image_id)
     # Failed to get images raise a exception.
     except Exception as err:
         flask_app.logger.info(err)
         error = err
     #Check if we have an error
     if error:
         # We have error, pass it back to controller
         raise Exception(error)
     else:
         # We have no errors, return images
         return image