From 00e78df964ad39007061f026c45200be3681685f Mon Sep 17 00:00:00 2001 From: Steven Gong Date: Mon, 19 Apr 2021 08:09:10 -0400 Subject: [PATCH] finalize profile update unsplash --- app/static/css/style.css | 60 ++++++++++++++++++++++++++++++++++++++ app/static/js/base.js | 15 ++++++++-- app/templates/explore.html | 6 ++-- app/urls.py | 1 + app/views.py | 32 ++++++++++++++++++++ 5 files changed, 109 insertions(+), 5 deletions(-) diff --git a/app/static/css/style.css b/app/static/css/style.css index e903075..9f90c99 100644 --- a/app/static/css/style.css +++ b/app/static/css/style.css @@ -44,6 +44,10 @@ h6 { color: #4f4f4f; } +.fa, .far, .fas { + font-family: "Font Awesome 5 Free" !important; +} + .fab { font-family: "Font Awesome 5 Brands" !important; } @@ -841,3 +845,59 @@ a > .fa-reddit:hover { margin-right: 50px !important; } } + +.lds-ellipsis { + display: inline-block; + position: relative; + width: 80px; + height: 80px; +} +.lds-ellipsis div { + position: absolute; + top: 33px; + width: 13px; + height: 13px; + border-radius: 50%; + background: #fff; + animation-timing-function: cubic-bezier(0, 1, 1, 0); +} +.lds-ellipsis div:nth-child(1) { + left: 8px; + animation: lds-ellipsis1 0.6s infinite; +} +.lds-ellipsis div:nth-child(2) { + left: 8px; + animation: lds-ellipsis2 0.6s infinite; +} +.lds-ellipsis div:nth-child(3) { + left: 32px; + animation: lds-ellipsis2 0.6s infinite; +} +.lds-ellipsis div:nth-child(4) { + left: 56px; + animation: lds-ellipsis3 0.6s infinite; +} +@keyframes lds-ellipsis1 { + 0% { + transform: scale(0); + } + 100% { + transform: scale(1); + } +} +@keyframes lds-ellipsis3 { + 0% { + transform: scale(1); + } + 100% { + transform: scale(0); + } +} +@keyframes lds-ellipsis2 { + 0% { + transform: translate(0, 0); + } + 100% { + transform: translate(24px, 0); + } +} diff --git a/app/static/js/base.js b/app/static/js/base.js index b967eb0..0416bed 100644 --- a/app/static/js/base.js +++ b/app/static/js/base.js @@ -167,8 +167,19 @@ function searchUnsplash() { var img = document.createElement('img'); img.src = urlArray[i]; // img[i] refers to the current URL. list_element.appendChild(img); - list_element.addEventListener("click", function(event) { //Detect when an image is being clicked - console.log(event.target.src) + list_element.addEventListener("click", function(event) { //Detect when an image is being clicked, and make AJAX request + container.innerHTML = '
' + $.ajax({ + type: "POST", + url: `/addUnsplashPicture/`, + data: { + 'url': event.target.src, + 'csrfmiddlewaretoken': $('input[name=csrfmiddlewaretoken').val(), + }, + success: function() { + location.reload() + } + }) }) container.appendChild(list_element); } diff --git a/app/templates/explore.html b/app/templates/explore.html index f345985..785c504 100644 --- a/app/templates/explore.html +++ b/app/templates/explore.html @@ -154,7 +154,7 @@

- +

@@ -174,7 +174,7 @@

/", views.follow_user, name="follow_user"), path("unfollow_user//", views.unfollow_user, name="unfollow_user"), + path("addUnsplashPicture/", views.addUnsplashPicture, name="addUnsplashPicture") ] diff --git a/app/views.py b/app/views.py index 931f2ec..6e856b9 100644 --- a/app/views.py +++ b/app/views.py @@ -1,6 +1,7 @@ from app.forms import ProfileUpdateForm, UserUpdateForm, SocialLinkFormSet, PasswordUpdateForm, OnboardingForm from app.models import Challenge, Profile, Project, SocialLinkAttachement, Tag, User, UserFollowing +from django.core import files from django.core.exceptions import PermissionDenied from django.contrib.auth import login, update_session_auth_hash from django.contrib.auth.forms import UserCreationForm, PasswordChangeForm @@ -19,6 +20,8 @@ from django.views.generic.edit import CreateView, FormMixin, FormView, UpdateView from django.http import JsonResponse +import requests +import tempfile class AboutView(TemplateView): @@ -411,6 +414,7 @@ def get_challenges_ajax(request): return JsonResponse(data) return JsonResponse(final_list, safe=False) + def add_comment(request): if request.method == "POST": comment = request.POST['comment'] @@ -420,6 +424,8 @@ def add_comment(request): initiative.comments.create(text=comment, profile=profile) return JsonResponse("Success", safe=False) + + def follow_user(request, pk): following_user_id = pk data = {} @@ -451,3 +457,29 @@ def unfollow_user(request, pk): data['error_message'] = 'error' return redirect('index') return redirect('user', following_user.username) + + +def addUnsplashPicture(request): + if request.method == "POST": + url = request.POST["url"] + url += ".jpg" + response = requests.get(url, stream=True) + # Get the filename from the url, used for saving later + file_name = url.split('/')[-1] + + # Create a temporary file + lf = tempfile.NamedTemporaryFile() + + # Read the streamed image in sections + for block in response.iter_content(1024 * 8): + + # If no more file then stop + if not block: + break + + # Write image block to temporary file + lf.write(block) + + request.user.profile.image.save(file_name, files.File(lf)) + + return JsonResponse("Success", safe=False) \ No newline at end of file