Skip to content

Commit

Permalink
A few small path changes/ fixed to use abs paths not relative paths, …
Browse files Browse the repository at this point in the history
…/static vs static,
  • Loading branch information
Jackywathy committed Feb 9, 2017
1 parent 79db34c commit 7436265
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion back_end/Imaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ def is_image(img):

def open_image(data, change_icon=False, b64=False):
"""Returns an image object, given image binary data (gotten from request.get_file) """

if b64:
try:
with open('out.txt', 'w') as f:
f.write(data[22:])
data = base64.b64decode(data[22:] if data.startswith("data:image/png;base64,") else data)
except binascii.Error:
print("unable to decode b64!")
raise
return INVALID_IMAGE

io = BytesIO(data)
Expand Down
2 changes: 1 addition & 1 deletion back_end/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def fetch_file(url, filename):

#TODO: maybe move this to db_api instead?
def get_user_picture(userObj):
return userObj.picture if userObj.picture is not None and userObj.picture != "" else "nouser.png"
return userObj.picture if userObj.picture is not None and userObj.picture != "" else os.path.join('/static', 'uploads', 'user_image', 'nouser.png')

def reply_malformed(request, header_text="The request was malformed", text="Sorry!\n It looks like your client has sent a invalid request.\n Please reload the page or contact us."):
"""Sets the header of request to 400 (bad request) and writes the text supplied"""
Expand Down
1 change: 1 addition & 0 deletions back_end/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def view_handler_post(request, username):
def view_handler(request, username):
user = db.User.find(username=username)
print('on profile page, profile pic = ' + str(user.picture) + ' check if user logged in or user of profile page')
print(get_user_picture(user))
if user is None:
request.write("username is not in db")
else:
Expand Down
24 changes: 16 additions & 8 deletions back_end/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,20 @@ def signup_handler_post(request):
if image_type == 'profile_img_webcam':
# webcam
print("reading from webcam")
print(request.get_field('webcam-input'))
image = Imaging.open_image(request.get_field('webcam-input'), change_icon=True, b64=True)
if request.get_field('webcam-input') is None:
image = None
else:
image = Imaging.open_image(request.get_field('webcam-input'), change_icon=True, b64=True)

elif image_type == 'profile_img_upload':
# file upload
print("Reading from file upload")
image = Imaging.open_image(request.get_file('profile_picture')[2], change_icon=True, b64=False)
if request.get_file('profile_picture') == (None, None, None):
# no upload
image = None
else:
image = Imaging.open_image(request.get_file('profile_picture')[2], change_icon=True, b64=False)

else:
print("No file received! img_type=", image_type)
image = None
Expand Down Expand Up @@ -62,11 +70,11 @@ def signup_handler_post(request):

elif Imaging.is_image(image):
ext = image.type

file_path = os.path.abspath(os.path.join('static', 'uploads', 'user_image', str(new_user.id) + '.' + ext))
print("Saving img to", file_path)
image.img.save(file_path)
db.User.update_some(picture=file_path)
file_path = os.path.join('static', 'uploads', 'user_image', str(new_user.id) + '.' + ext)
abs_file_path = os.path.abspath(file_path)
print("Saving img to", abs_file_path, "with link", file_path)
image.img.save(abs_file_path)
db.User.update_some(picture='/' + file_path) # need a / to make it absolute path so it doesnt do /profile/static...
request.set_secure_cookie("current_user", username)
request.redirect('/')

Expand Down
2 changes: 1 addition & 1 deletion templates/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
{% end if %}
<p> Bio </p>
<article class="profile_bio">{{ user.bio }}</article>
<p class="image"> Picture: <img src= "/static/{{ picture }}">
<p class="image"> Picture: <img src= "{{ picture }}">
</p>
</div>
</main>
Expand Down

5 comments on commit 7436265

@Jackywathy
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#38 webcam submitting "should" work now

@tracey-le
Copy link
Collaborator

@tracey-le tracey-le commented on 7436265 Feb 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh that is so awesome @Jackywathy

😭 gimme a bit of time and ill come back to all this <3

@tracey-le
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ooh what's the difference between using absolute and relative paths?

@ekohilas
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its in the name
relative paths use things like . and .. to represent current directory and previous directory (respectively)
whereas absolute paths require the entire link from the base

your browser will parse paths in certain ways.
you might notice if you inspect elements in chrome and edit some of the hyperlinks, you'll find that links such as '/test' will re-direct to "github.com/test"
whereas links without a beginning slash will work relatively to where you currently are on the website
and so, clicking a link that hyperlinks to "test" will redirect from "github.com/here/I/am" to "github.com/here/I/test"

the use cases depend on what you're doing, so play around :)

@tracey-le
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ekohilas oooh, thanks so much Evan!! 😊😄😄 ah okay, I can see a lot from what you've said ☺️

Please sign in to comment.