diff --git a/application/api/profile.py b/application/api/profile.py index 83cee34..65092c5 100644 --- a/application/api/profile.py +++ b/application/api/profile.py @@ -1,6 +1,7 @@ """REST API for account.""" import flask import application +import os from application import model @application.app.route('/api/v1/profile//', methods=['GET']) @@ -31,4 +32,5 @@ def show_profile(username): "profile_picture": user["profile_picture"], "posts" : posts_list } - return flask.jsonify(**context), 200 + cloudfront_domain_name = os.environ.get('CLOUDFRONT_DOMAIN_NAME') + return flask.jsonify(**context, cloudfront_domain_name), 200 diff --git a/application/js/post.jsx b/application/js/post.jsx index 93835b1..3f35327 100644 --- a/application/js/post.jsx +++ b/application/js/post.jsx @@ -80,7 +80,7 @@ export default function Post(props) {
- pfp + pfp

{post.username}

diff --git a/application/templates/profile.html b/application/templates/profile.html index 07f5c89..ec77530 100644 --- a/application/templates/profile.html +++ b/application/templates/profile.html @@ -15,7 +15,7 @@

Account setting

profile picture

- +
diff --git a/application/views/profile.py b/application/views/profile.py index f20cc7a..d86d270 100644 --- a/application/views/profile.py +++ b/application/views/profile.py @@ -1,9 +1,151 @@ import flask, application from flask import render_template +import hashlib +import uuid +import pathlib +import boto3 +import os @application.app.route("/profile//") def show_profile(username): """Display /profile route.""" context = application.model.get_user(username) - return render_template('profile.html', **context) \ No newline at end of file + return render_template('profile.html', **context) + + +def add_item(): + """Add item for online shopping.""" + if 'username' not in flask.session: + flask.abort(403) + try: + name = flask.request.form['name'] + description = flask.request.form['description'] + width = flask.request.form['width'] + price = flask.request.form['price'] + fileobj = flask.request.files['image'] + category = flask.request.form['category'] + except KeyError: + flask.abort(400) + + filename = fileobj.filename + if name == '' or description == '' or price == '' or filename == '' \ + or category == '' or category == 'Choose a category': + flask.abort(400) + + stem = uuid.uuid4().hex + suffix = pathlib.Path(filename).suffix.lower() + uuid_basename = f"{stem}{suffix}" + + access_key = os.environ.get('AWS_ACCESS_KEY') + secret_access_key = os.environ.get('AWS_SECRET_ACCESS_KEY') + bucket_name = os.environ.get('S3_BUCKET_NAME') + + # Save to content delivery network + s3 = boto3.client('s3', aws_access_key_id=access_key, aws_secret_access_key=secret_access_key) + s3.upload_fileobj(fileobj, bucket_name, uuid_basename) + # insert into the items database with the info + conn = application.model.get_db() + cur = conn.cursor() + cur.execute( + "INSERT INTO items " + "(name, description, width, price, image, category) " + "VALUES (%s, %s, %s, %s, %s, %s)", + (name, description, width, price, uuid_basename, category) + ) + conn.commit() + + +def edit_item(): + """Make an edit to a currently existing item.""" + if 'username' not in flask.session: + flask.abort(403) + try: + itemid = flask.request.form['edit-itemid'] + name = flask.request.form['name'] + description = flask.request.form['description'] + width = flask.request.form['width'] + price = flask.request.form['price'] + fileobj = flask.request.files['image'] + category = flask.request.form['category'] + except KeyError: + flask.abort(400) + + filename = fileobj.filename + if itemid == '' or name == '' or description == '' or price == '' \ + or category == '' or category == 'Choose a category': + flask.abort(400) + + conn = application.model.get_db() + uuid_basename = '' + if filename == '': + cur1 = conn.cursor() + cur1.execute( + "SELECT image FROM items WHERE itemid = %s", + (itemid,) + ) + img_name = + uuid_basename = img_name + + if filename != '': + access_key = os.environ.get('AWS_ACCESS_KEY') + secret_access_key = os.environ.get('AWS_SECRET_ACCESS_KEY') + bucket_name = os.environ.get('S3_BUCKET_NAME') + # Delete from content delivery network + cur2 = conn.cursor() + cur2.execute( + # "SELECT image FROM items WHERE itemid = %s", + # (itemid,) + ) + img_name = cur2.fetchone()[0] + s3 = boto3.resource('s3', aws_access_key_id=access_key, aws_secret_access_key=secret_access_key) + bucket = s3.Bucket(bucket_name) + bucket.Object(img_name).delete() + stem = uuid.uuid4().hex + suffix = pathlib.Path(filename).suffix.lower() + uuid_basename = f"{stem}{suffix}" + s3_client = boto3.client('s3', aws_access_key_id=access_key, aws_secret_access_key=secret_access_key) + s3_client.upload_fileobj(fileobj, bucket_name, uuid_basename) + # insert into the items database with the info + cur3 = conn.cursor() + cur3.execute( + # "UPDATE items " + # "SET name = %s, description = %s, width = %s, price = %s, image = %s, category = %s " + # "WHERE itemid = %s", + # (name, description, width, price, uuid_basename, category, itemid) + ) + conn.commit() + + +def delete_item(): + """Delete an item from online store.""" + if 'username' not in flask.session: + flask.abort(403) + try: + itemid = flask.request.form['delete-itemid'] + except KeyError: + flask.abort(400) + if itemid == '': + flask.abort(400) + + # Get the filename from the database + conn = application.model.get_db() + cur1 = conn.cursor() + cur1.execute( + + ) + img_name = + + # Delete from content delivery network + access_key = os.environ.get('AWS_ACCESS_KEY') + secret_access_key = os.environ.get('AWS_SECRET_ACCESS_KEY') + bucket_name = os.environ.get('S3_BUCKET_NAME') + s3 = boto3.resource('s3', aws_access_key_id=access_key, aws_secret_access_key=secret_access_key) + bucket = s3.Bucket(bucket_name) + bucket.Object(img_name).delete() + # insert into the items database with the info + cur2 = conn.cursor() + cur2.execute( + + ) + conn.commit() \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index c738481..a1ef104 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5766,9 +5766,7 @@ "resolved": "https://registry.npmjs.org/ajv-formats/-/ajv-formats-2.1.1.tgz", "integrity": "sha512-Wx0Kx52hxE7C18hkMEggYlEifqWZtYaRgouJor+WMdPnQyEK13vgEWyVNup7SoeeoLMsr4kf5h6dOW11I15MUA==", "dev": true, - "requires": { - "ajv": "^8.0.0" - } + "requires": {} }, "ajv-keywords": { "version": "5.1.0", diff --git a/requirements.txt b/requirements.txt index 49c6420..c1c5eba 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,37 +1,20 @@ -arrow==1.2.3 -astroid==2.15.6 -beautifulsoup4==4.12.2 -blinker==1.6.2 -bs4==0.0.1 +boto3>=1.15.0 certifi==2023.7.22 charset-normalizer==3.2.0 click==8.1.7 -dill==0.3.7 Flask==2.3.3 -idna==3.4 iniconfig==2.0.0 isort==5.12.0 -itsdangerous==2.1.2 Jinja2==3.1.2 lazy-object-proxy==1.9.0 MarkupSafe==2.1.3 -mccabe==0.7.0 packaging==23.1 -platformdirs==3.10.0 -pluggy==1.3.0 psycopg2==2.9.6 pycodestyle==2.11.0 pydocstyle==6.3.0 -pylint==2.17.5 -pytest==7.4.2 -pytest-mock==3.11.1 python-dateutil==2.8.2 PyYAML==6.0.1 requests==2.31.0 -six==1.16.0 -snowballstemmer==2.2.0 soupsieve==2.5 -tomlkit==0.12.1 urllib3==2.0.4 Werkzeug==2.3.7 -wrapt==1.15.0