From afe05ff429e22f8ca5306a3679db2b6363dc54f4 Mon Sep 17 00:00:00 2001 From: quimmrc Date: Thu, 21 Nov 2024 15:27:00 +0100 Subject: [PATCH 1/4] check whether if request user is coincident with "from" or "to" user and decide the recipient accordingly --- messages/views.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/messages/views.py b/messages/views.py index f090ef029..d5f3cc92c 100644 --- a/messages/views.py +++ b/messages/views.py @@ -166,7 +166,11 @@ def new_message(request, username=None, message_id=None): if message.user_from != request.user and message.user_to != request.user: raise Http404 - + elif message.user_from == request.user: + to = message.user_to.username + else: + to = message.user_from.username + body = message.body.body.replace("\r\n", "\n").replace("\r", "\n") body = quote_message_for_reply(body, message.user_from.username) From ac82b732c4913bafa1a485df1f6dbe1c931a7035 Mon Sep 17 00:00:00 2001 From: quimmrc Date: Tue, 3 Dec 2024 13:16:10 +0100 Subject: [PATCH 2/4] set reloadOnSucces to false by default, making it a specific field to fill in the element declaration --- bookmarks/forms.py | 2 +- bookmarks/views.py | 9 ++++----- .../static/bw-frontend/src/components/modal.js | 14 ++++++++------ templates/bookmarks/bookmarks.html | 2 +- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/bookmarks/forms.py b/bookmarks/forms.py index c1aa1786a..7da05e6bd 100644 --- a/bookmarks/forms.py +++ b/bookmarks/forms.py @@ -36,7 +36,7 @@ def clean(self): name = self.cleaned_data.get("name", ) if BookmarkCategory.objects.filter(user=self.instance.user, name=name).exists(): - raise forms.ValidationError("This name already exists for a bookmark category") + raise forms.ValidationError("You have already created a Bookmark Category with this name") return cleaned_data diff --git a/bookmarks/views.py b/bookmarks/views.py index 95fc298a5..68712058f 100755 --- a/bookmarks/views.py +++ b/bookmarks/views.py @@ -92,20 +92,19 @@ def edit_bookmark_category(request, category_id): return HttpResponseRedirect(reverse("bookmarks-for-user", args=[request.user.username])) category = get_object_or_404(BookmarkCategory, id=category_id, user=request.user) - + if request.method == "POST": edit_form = BookmarkCategoryForm(request.POST, instance=category) - print(edit_form.is_bound) + if edit_form.is_valid(): category.name = edit_form.cleaned_data["name"] category.save() return JsonResponse({"success":True}) - if not edit_form.is_valid(): - print(edit_form.errors.as_json()) + else: initial = {"name":category.name} edit_form = BookmarkCategoryForm(initial=initial) - + tvars = {"category": category, "form": edit_form} return render(request, 'bookmarks/modal_edit_bookmark_category.html', tvars) diff --git a/freesound/static/bw-frontend/src/components/modal.js b/freesound/static/bw-frontend/src/components/modal.js index af5700d58..a62834681 100644 --- a/freesound/static/bw-frontend/src/components/modal.js +++ b/freesound/static/bw-frontend/src/components/modal.js @@ -12,7 +12,7 @@ const bindModalActivationElements = (querySelectorStr, handleModalFunction, cont element.dataset.alreadyBinded = true; element.addEventListener('click', (evt) => { evt.preventDefault(); - handleModalFunction(element.dataset.modalContentUrl, element.dataset.modalActivationParam); + handleModalFunction(element.dataset.modalContentUrl, element.dataset.modalActivationParam, element); }); }); } @@ -85,12 +85,14 @@ const bindConfirmationModalElements = (container) => { // Logic to bind default modals -const handleDefaultModal = (modalUrl, modalActivationParam) => { +const handleDefaultModal = (modalUrl, modalActivationParam, element) => { handleGenericModal(modalUrl, undefined, undefined, true, true, modalActivationParam); } -const handleDefaultModalWithForm = (modalUrl, modalActivationParam) => { - handleGenericModalWithForm(modalUrl, undefined, undefined, (req) => {showToast('Form submitted succesfully!')}, undefined, true, true, modalActivationParam, true); +const handleDefaultModalWithForm = (modalUrl, modalActivationParam, element) => { + handleGenericModalWithForm(modalUrl, undefined, undefined, (req) => {showToast(element.dataset.successMessage || 'Form submitted succesfully!')}, + (req) => {showToast(element.dataset.errorMessage || "There were errors processing the form...")}, true, true, modalActivationParam, + element.dataset.reloadOnSuccess || false); } const bindDefaultModals = (container) => { @@ -198,7 +200,7 @@ const handleGenericModal = (fetchContentUrl, onLoadedCallback, onClosedCallback, }; -const handleGenericModalWithForm = (fetchContentUrl, onLoadedCallback, onClosedCallback, onFormSubmissionSucceeded, onFormSubmissionError, doRequestAsync, showLoadingToast, modalActivationParam, dataReloadOnSuccess) => { +const handleGenericModalWithForm = (fetchContentUrl, onLoadedCallback, onClosedCallback, onFormSubmissionSucceeded, onFormSubmissionError, doRequestAsync, showLoadingToast, modalActivationParam, triggerPageReloadOnSuccess=false) => { // This version of the generic modal is useful for modal contents that contain forms which, upon submission, will return HTML content if there were form errors // which should be used to replace the current contents of the form, and will return a JSON response if the form validated correctly in the backend. That JSON // response could include some relevant data or no data at all, but is used to differentiate from the HTML response @@ -221,7 +223,7 @@ const handleGenericModalWithForm = (fetchContentUrl, onLoadedCallback, onClosedC if (onFormSubmissionSucceeded !== undefined){ onFormSubmissionSucceeded(req); } - if(dataReloadOnSuccess == true){ + if(triggerPageReloadOnSuccess == true){ location.reload() } } else { diff --git a/templates/bookmarks/bookmarks.html b/templates/bookmarks/bookmarks.html index 29655befe..a1cf1040e 100644 --- a/templates/bookmarks/bookmarks.html +++ b/templates/bookmarks/bookmarks.html @@ -20,7 +20,7 @@

Bookmark categories

  • {{cat.name}} ยท {{cat.num_bookmarks|bw_intcomma}} bookmark{{ cat.num_bookmarks|pluralize }} {% if is_owner %} {% bw_icon 'trash' %} - {% bw_icon 'edit' %} + {% bw_icon 'edit' %} {% endif %}
  • {% endfor %} From e9c88b5a48423c68437ff262264bb30cae30879e Mon Sep 17 00:00:00 2001 From: quimmrc Date: Wed, 4 Dec 2024 10:49:30 +0100 Subject: [PATCH 3/4] handle page reloading for GenericModalsWithForms + developers comments --- DEVELOPERS.md | 6 +++++- freesound/static/bw-frontend/src/components/modal.js | 11 ++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/DEVELOPERS.md b/DEVELOPERS.md index a881b89d9..3099bcb98 100644 --- a/DEVELOPERS.md +++ b/DEVELOPERS.md @@ -127,10 +127,14 @@ in the `accounts.Profile.delete_user` method (which anonymizes a user account). ### Adding new fields to the user Sound model -When adding new fields to the `sounds.Sound` mode, we should make sure that we handle this fields correctly when +When adding new fields to the `sounds.Sound` model, we should make sure that we handle this fields correctly when creating `DeletedSound` objects in the `sounds-models.on_delete_sound` function triggered by the `pre_delete` signal of the `Sound` model. +### Working with new modals for UX + +Upon creating a new modal for any UX, please consider using `handleGenericModal` or `handleGenericModalWithForm` in `modal.js`. +These functions ease the logic behind modals: initialization, error handling, toast messages, and URL handles. ### Adding new search options in the search page diff --git a/freesound/static/bw-frontend/src/components/modal.js b/freesound/static/bw-frontend/src/components/modal.js index a62834681..d3c01e781 100644 --- a/freesound/static/bw-frontend/src/components/modal.js +++ b/freesound/static/bw-frontend/src/components/modal.js @@ -9,6 +9,8 @@ const bwPageElement = document.getElementsByClassName('bw-page')[0]; const bindModalActivationElements = (querySelectorStr, handleModalFunction, container) => { container.querySelectorAll(querySelectorStr).forEach(element => { if (element.dataset.alreadyBinded !== undefined){ return; } + if (element.dataset.reloadOnSuccess === undefined){element.dataset.reloadOnSuccess=false} + else{element.dataset.reloadOnSuccess = JSON.parse(element.dataset.reloadOnSuccess)} element.dataset.alreadyBinded = true; element.addEventListener('click', (evt) => { evt.preventDefault(); @@ -90,9 +92,7 @@ const handleDefaultModal = (modalUrl, modalActivationParam, element) => { } const handleDefaultModalWithForm = (modalUrl, modalActivationParam, element) => { - handleGenericModalWithForm(modalUrl, undefined, undefined, (req) => {showToast(element.dataset.successMessage || 'Form submitted succesfully!')}, - (req) => {showToast(element.dataset.errorMessage || "There were errors processing the form...")}, true, true, modalActivationParam, - element.dataset.reloadOnSuccess || false); + handleGenericModalWithForm(modalUrl, undefined, undefined, (req) => {showToast(element.dataset.successMessage || 'Form submitted succesfully!')}, (req) => {showToast(element.dataset.errorMessage || "There were errors processing the form...")}, true, true, modalActivationParam, element.dataset.reloadOnSuccess); } const bindDefaultModals = (container) => { @@ -200,7 +200,7 @@ const handleGenericModal = (fetchContentUrl, onLoadedCallback, onClosedCallback, }; -const handleGenericModalWithForm = (fetchContentUrl, onLoadedCallback, onClosedCallback, onFormSubmissionSucceeded, onFormSubmissionError, doRequestAsync, showLoadingToast, modalActivationParam, triggerPageReloadOnSuccess=false) => { +const handleGenericModalWithForm = (fetchContentUrl, onLoadedCallback, onClosedCallback, onFormSubmissionSucceeded, onFormSubmissionError, doRequestAsync, showLoadingToast, modalActivationParam, triggerPageReloadOnSuccess) => { // This version of the generic modal is useful for modal contents that contain forms which, upon submission, will return HTML content if there were form errors // which should be used to replace the current contents of the form, and will return a JSON response if the form validated correctly in the backend. That JSON // response could include some relevant data or no data at all, but is used to differentiate from the HTML response @@ -223,7 +223,8 @@ const handleGenericModalWithForm = (fetchContentUrl, onLoadedCallback, onClosedC if (onFormSubmissionSucceeded !== undefined){ onFormSubmissionSucceeded(req); } - if(triggerPageReloadOnSuccess == true){ + if(triggerPageReloadOnSuccess == "true"){ + console.log(triggerPageReloadOnSuccess) location.reload() } } else { From 0135f2fbfd934f1e67160884a90a4809c115d8b5 Mon Sep 17 00:00:00 2001 From: quimmrc Date: Mon, 9 Dec 2024 18:06:05 +0100 Subject: [PATCH 4/4] improve reload on success functionality for modals with forms --- freesound/static/bw-frontend/src/components/modal.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/freesound/static/bw-frontend/src/components/modal.js b/freesound/static/bw-frontend/src/components/modal.js index d3c01e781..4bc2c472d 100644 --- a/freesound/static/bw-frontend/src/components/modal.js +++ b/freesound/static/bw-frontend/src/components/modal.js @@ -9,8 +9,6 @@ const bwPageElement = document.getElementsByClassName('bw-page')[0]; const bindModalActivationElements = (querySelectorStr, handleModalFunction, container) => { container.querySelectorAll(querySelectorStr).forEach(element => { if (element.dataset.alreadyBinded !== undefined){ return; } - if (element.dataset.reloadOnSuccess === undefined){element.dataset.reloadOnSuccess=false} - else{element.dataset.reloadOnSuccess = JSON.parse(element.dataset.reloadOnSuccess)} element.dataset.alreadyBinded = true; element.addEventListener('click', (evt) => { evt.preventDefault(); @@ -92,7 +90,7 @@ const handleDefaultModal = (modalUrl, modalActivationParam, element) => { } const handleDefaultModalWithForm = (modalUrl, modalActivationParam, element) => { - handleGenericModalWithForm(modalUrl, undefined, undefined, (req) => {showToast(element.dataset.successMessage || 'Form submitted succesfully!')}, (req) => {showToast(element.dataset.errorMessage || "There were errors processing the form...")}, true, true, modalActivationParam, element.dataset.reloadOnSuccess); + handleGenericModalWithForm(modalUrl, undefined, undefined, (req) => {showToast(element.dataset.successMessage || 'Form submitted succesfully!')}, (req) => {showToast(element.dataset.errorMessage || "There were errors processing the form...")}, true, true, modalActivationParam, element.dataset.reloadOnSuccess==="true"); } const bindDefaultModals = (container) => { @@ -223,8 +221,7 @@ const handleGenericModalWithForm = (fetchContentUrl, onLoadedCallback, onClosedC if (onFormSubmissionSucceeded !== undefined){ onFormSubmissionSucceeded(req); } - if(triggerPageReloadOnSuccess == "true"){ - console.log(triggerPageReloadOnSuccess) + if(triggerPageReloadOnSuccess){ location.reload() } } else {