Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

S3 image creation #43

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion application/api/profile.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""REST API for account."""
import flask
import application
import os
from application import model

@application.app.route('/api/v1/profile/<username>/', methods=['GET'])
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion application/js/post.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export default function Post(props) {
<div className="post-border">
<div className="profile-right">
<div className="profile-info circle">
<img src="/static/assets/logo.png" alt="pfp" className="circle"></img>
<img src="https://d1yh0yxb8mxq43.cloudfront.net/designTeamIcon.png" alt="pfp" className="circle"></img>
<div>
<p class="user-name">{post.username} </p>
<p class="email">{post.email}</p>
Expand Down
2 changes: 1 addition & 1 deletion application/templates/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ <h2><u>Account setting</u></h2>
</div>
<div class="column">
<p>profile picture</p>
<img src="{{ profile_picture }}">
<img src="{{ cloudfront_domain_name }}/designTeamIcon.png">
<button>+</button>
</div>
</div>
Expand Down
144 changes: 143 additions & 1 deletion application/views/profile.py
Original file line number Diff line number Diff line change
@@ -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/<username>/")
def show_profile(username):
"""Display /profile route."""
context = application.model.get_user(username)

return render_template('profile.html', **context)
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()
4 changes: 1 addition & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 1 addition & 18 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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