Skip to content

Commit

Permalink
Merge pull request #1839 from MTG/login-checkbox
Browse files Browse the repository at this point in the history
Upgrade to Django 4.2, fix issue with login checkbox
  • Loading branch information
ffont authored Feb 12, 2025
2 parents 27b8004 + 312a241 commit 9b0bea2
Show file tree
Hide file tree
Showing 38 changed files with 139 additions and 180 deletions.
12 changes: 6 additions & 6 deletions accounts/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import logging

from captcha.fields import ReCaptchaField
from django_recaptcha.fields import ReCaptchaField
from django import forms
from django.conf import settings
from django.contrib.auth import get_user_model
Expand All @@ -37,7 +37,7 @@
from django.utils.encoding import force_bytes
from django.utils.http import urlsafe_base64_encode
from django.utils.safestring import mark_safe
from multiupload.fields import MultiFileField
from multiupload_plus.fields import MultiFileField

from accounts.models import Profile, EmailPreferenceType, OldUsername, DeletedUser
from utils.encryption import sign_with_timestamp, unsign_with_timestamp
Expand Down Expand Up @@ -101,8 +101,8 @@ class FlashUploadFileForm(forms.Form):
class TermsOfServiceForm(forms.Form):
accepted_tos = forms.BooleanField(
label='',
help_text='Check this box to accept the <a href="/help/tos_web/" target="_blank">terms of use</a> '
'and the <a href="/help/privacy/" target="_blank">privacy policy</a> of Freesound (required)',
help_text='Check this box to accept the <a href="/help/tos_web/" target="_blank" class="bw-link--grey">terms of use</a> '
'and the <a href="/help/privacy/" target="_blank" class="bw-link--grey">privacy policy</a> of Freesound (required)',
required=True,
error_messages={'required': 'You must accept the terms of use and the privacy poclicy in order to continue '
'using Freesound.'}
Expand Down Expand Up @@ -210,8 +210,8 @@ class RegistrationForm(forms.Form):
email2 = forms.EmailField(label=False, help_text=False, max_length=254)
password1 = forms.CharField(label=False, help_text=False, widget=forms.PasswordInput)
accepted_tos = forms.BooleanField(
label=mark_safe('Check this box to accept our <a href="/help/tos_web/" target="_blank">terms of '
'use</a> and the <a href="/help/privacy/" target="_blank">privacy policy</a>'),
label=mark_safe('Check this box to accept our <a href="/help/tos_web/" target="_blank" class="bw-link--grey">terms of '
'use</a> and the <a href="/help/privacy/" target="_blank" class="bw-link--grey">privacy policy</a>'),
required=True,
error_messages={'required': 'You must accept the terms of use in order to register to Freesound'}
)
Expand Down
6 changes: 3 additions & 3 deletions accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
from django.templatetags.static import static
from django.urls import reverse
from django.utils.encoding import smart_str
from django.utils.http import urlquote
from django.utils import timezone
from psycopg2.errors import ForeignKeyViolation
from urllib.parse import quote

import tickets.models
from apiv2.models import ApiV2Client
Expand Down Expand Up @@ -223,10 +223,10 @@ def get_absolute_url(self):
return reverse('account', args=[smart_str(self.user.username)])

def get_user_sounds_in_search_url(self):
return f'{reverse("sounds-search")}?f=username:"{ urlquote(self.user.username) }"&s=Date+added+(newest+first)&g=0'
return f'{reverse("sounds-search")}?f=username:"{ quote(self.user.username) }"&s=Date+added+(newest+first)&g=0'

def get_user_packs_in_search_url(self):
return f'{reverse("sounds-search")}?f=username:"{ urlquote(self.user.username) }"&s=Date+added+(newest+first)&g=1&dp=1'
return f'{reverse("sounds-search")}?f=username:"{ quote(self.user.username) }"&s=Date+added+(newest+first)&g=1&dp=1'

def get_latest_packs_for_profile_page(self):
latest_pack_ids = Pack.objects.select_related().filter(user=self.user, num_sounds__gt=0).exclude(is_deleted=True) \
Expand Down
2 changes: 1 addition & 1 deletion accounts/tests/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_user_save(self):
self.assertEqual(Profile.objects.filter(user=u).exists(), True)
u.save() # Check saving user again (with existing profile) does not fail

@mock.patch("captcha.fields.ReCaptchaField.validate")
@mock.patch("django_recaptcha.fields.ReCaptchaField.validate")
def test_user_registration(self, magic_mock_function):
username = 'new_user'

Expand Down
4 changes: 2 additions & 2 deletions apiv2/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ def test_oauth2_authorization_code_grant_flow(self):
}, secure=True)
self.assertEqual(resp.status_code, 302)
resp = self.client.get(resp.request['PATH_INFO'] + '?' + resp.request['QUERY_STRING'], secure=True)
self.assertEquals(resp.url.startswith(client.get_default_redirect_uri()), True)
self.assertEqual(resp.url.startswith(client.get_default_redirect_uri()), True)
resp_params = self.get_params_from_url(resp.url)
self.check_dict_has_fields(resp_params, ['error'])
self.assertEqual(resp_params['error'], 'unauthorized_client')
Expand Down Expand Up @@ -674,7 +674,7 @@ def test_oauth2_authorization_code_grant_flow(self):
}, secure=True)
self.assertTrue(resp.url.startswith(client.get_default_redirect_uri()))
resp_params = self.get_params_from_url(resp.url)
self.assertEquals(resp_params['state'], 'an_optional_state') # Check state is returned and preserved
self.assertEqual(resp_params['state'], 'an_optional_state') # Check state is returned and preserved
self.check_dict_has_fields(resp_params, ['code']) # Check code is there

# Return 200 OK when requesting access token setting client_id and client_secret in body params
Expand Down
6 changes: 6 additions & 0 deletions freesound.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@
"command": "docker compose run --rm web python manage.py test --settings=freesound.test_settings",
"problemMatcher": []
},
{
"label": "Run tests with warnings",
"type": "shell",
"command": "docker compose run --rm web python -Wa manage.py test --settings=freesound.test_settings",
"problemMatcher": []
},
{
"label": "Run tests verbose with warnings",
"type": "shell",
Expand Down
23 changes: 0 additions & 23 deletions freesound/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import logging

from admin_reorder.middleware import ModelAdminReorder
from django.conf import settings
from django.http import HttpResponseRedirect
from django.urls import reverse
Expand Down Expand Up @@ -114,25 +113,3 @@ def __call__(self, request):

response = self.get_response(request)
return response


class ModelAdminReorderWithNav(ModelAdminReorder):
# Customize ModelAdminReorder middleware so that it also reorders new django admin sidebar in 3.1+
# from https://github.com/mishbahr/django-modeladmin-reorder/issues/47

def process_template_response(self, request, response):

if (
getattr(response, 'context_data', None)
and not response.context_data.get('app_list')
and response.context_data.get('available_apps')
):
available_apps = response.context_data.get('available_apps')
response.context_data['app_list'] = available_apps
response = super().process_template_response(request, response)
response.context_data['available_apps'] = response.context_data[
'app_list'
]
return response

return super().process_template_response(request, response)
50 changes: 4 additions & 46 deletions freesound/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'silk.middleware.SilkyMiddleware',
#'freesound.middleware.ModelAdminReorderWithNav',
'ratelimit.middleware.RatelimitMiddleware',
'django_ratelimit.middleware.RatelimitMiddleware',
'freesound.middleware.TosAcceptanceHandler',
'freesound.middleware.BulkChangeLicenseHandler',
'freesound.middleware.UpdateEmailHandler',
Expand Down Expand Up @@ -74,59 +73,18 @@
'monitor',
'django_object_actions',
'silk',
'admin_reorder',
'captcha',
'django_recaptcha',
'adminsortable',
]

# Specify custom ordering of models in Django Admin index
ADMIN_REORDER = (

{'app': 'accounts', 'models': (
'auth.User',
'accounts.Profile',
'accounts.DeletedUser',
'accounts.UserDeletionRequest',
'accounts.UserFlag',
'accounts.OldUsername',
'accounts.EmailBounce',
'auth.Groups',
'fsmessages.Message',
'accounts.GdprAcceptance',
)},
{'app': 'sounds', 'models': (
'sounds.Sound',
{'model': 'sounds.SoundAnalysis', 'label': 'Sound analyses'},
'sounds.Pack',
'sounds.DeletedSound',
'sounds.License',
{'model': 'sounds.Flag', 'label': 'Sound flags'},
'sounds.BulkUploadProgress',
{'model': 'sounds.SoundOfTheDay', 'label': 'Sound of the day'}
)},
{'app': 'apiv2', 'label': 'API', 'models': (
{'model': 'apiv2.ApiV2Client', 'label': 'API V2 Application'},
'oauth2_provider.AccessToken',
'oauth2_provider.RefreshToken',
'oauth2_provider.Grant',
)},
'forum',
{'app': 'donations', 'models': (
'donations.Donation',
'donations.DonationsEmailSettings',
'donations.DonationsModalSettings',
)},
'sites',
)

# Silk is the Request/SQL logging platform. We install it but leave it disabled
# It can be activated in local_settings by changing INTERCEPT_FUNC
SILKY_AUTHENTICATION = True # User must login
SILKY_AUTHORISATION = True # User must have permissions
SILKY_PERMISSIONS = lambda user: user.is_superuser
SILKY_INTERCEPT_FUNC = lambda request: False

CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_ALL_ORIGINS = True

AUTHENTICATION_BACKENDS = ('accounts.modelbackend.CustomModelBackend',)

Expand Down Expand Up @@ -712,7 +670,7 @@
# To bypass the security check that prevents the test keys from being used unknowingly add
# SILENCED_SYSTEM_CHECKS = [..., 'captcha.recaptcha_test_key_error', ...] to your settings.

SILENCED_SYSTEM_CHECKS += ['captcha.recaptcha_test_key_error']
SILENCED_SYSTEM_CHECKS += ['django_recaptcha.recaptcha_test_key_error']


# -------------------------------------------------------------------------------
Expand Down
6 changes: 5 additions & 1 deletion freesound/static/bw-frontend/src/components/loginModals.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { showToast } from './toast';
import serialize from '../utils/formSerializer'
import { activateModal, dismissModal, handleGenericModalWithForm } from "../components/modal";
import { activateModal, dismissModal, handleGenericModalWithForm } from "./modal";
import { addRecaptchaScriptTagToMainHead } from '../utils/recaptchaDynamicReload'
import { initializeStuffInContainer } from '../utils/initHelper';

const checkUsernameAvailability = (username, baseURL, callback) => {
const req = new XMLHttpRequest();
Expand Down Expand Up @@ -42,6 +43,9 @@ const customProblemsLoggingInSubmit = (event) => {
};

const initRegistrationForm = (registrationForm) => {

// Initialize UI elements
initializeStuffInContainer(registrationForm, false, false);

// Bind click actions on links to move to other login modals
initLoginAndRegistrationModalLinks('registerModal');
Expand Down
16 changes: 16 additions & 0 deletions freesound/static/bw-frontend/styles/molecules/forms.scss
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,20 @@
label {
color: $navy-grey;
}
}

.bw-form-less-spacing {

p {
margin-top: 0!important;
}

input,
textarea {
&:not(.tags-input):not(.mapboxgl-ctrl-geocoder--input) {
margin-top: 10px!important;
margin-bottom: 15px!important;
}
}

}
4 changes: 2 additions & 2 deletions geotags/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
from django.http import Http404, HttpResponse
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from django.utils.http import urlquote
from django.views.decorators.cache import cache_page
from django.views.decorators.clickjacking import xframe_options_exempt
from urllib.parse import quote
from accounts.models import Profile

from sounds.models import Sound, Pack
Expand Down Expand Up @@ -211,7 +211,7 @@ def for_user(request, username):
'pack': None,
'sound': None,
'url': reverse('geotags-for-user-barray', args=[username]),
'query_search_page_url': reverse('sounds-search') + f'?f=username:{urlquote(username)}&mm=1'
'query_search_page_url': reverse('sounds-search') + f'?f=username:{quote(username)}&mm=1'
})
return render(request, 'geotags/geotags.html', tvars)

Expand Down
2 changes: 1 addition & 1 deletion messages/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# See AUTHORS file.
#

from captcha.fields import ReCaptchaField
from django_recaptcha.fields import ReCaptchaField
from django import forms
from django.contrib.auth.models import User
from django.urls import reverse
Expand Down
4 changes: 2 additions & 2 deletions messages/tests/test_message_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def setUp(self):
self.sender = User.objects.create_user(username='sender', email='[email protected]')
self.receiver = User.objects.create_user(username='receiver', email='[email protected]')

@mock.patch("captcha.fields.ReCaptchaField.validate")
@mock.patch("django_recaptcha.fields.ReCaptchaField.validate")
def test_message_email_preference_enabled(self, magic_mock):
self.client.force_login(user=self.sender)
resp = self.client.post(reverse('messages-new'), data={
Expand All @@ -52,7 +52,7 @@ def test_message_email_preference_enabled(self, magic_mock):
self.assertTrue(settings.EMAIL_SUBJECT_PREFIX in mail.outbox[0].subject)
self.assertTrue(settings.EMAIL_SUBJECT_PRIVATE_MESSAGE in mail.outbox[0].subject)

@mock.patch("captcha.fields.ReCaptchaField.validate")
@mock.patch("django_recaptcha.fields.ReCaptchaField.validate")
def test_message_email_preference_disabled(self, magic_mock):
# Create email preference object for the email type (which will mean user does not want message emails as
# it is enabled by default and the preference indicates user does not want it).
Expand Down
31 changes: 15 additions & 16 deletions requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,24 @@ bleach==5.0.1
boto3==1.26.130
celery==5.3.4
debugpy==1.5.1
dj-database-url==0.5.0
django-admin-sortable==2.2.4
dj-database-url==2.3.0
django-admin-sortable==2.3
django-amazon-ses==4.0.1
django-cors-headers==3.13.0
django-debug-toolbar==3.1.1
django-extensions==3.1.5
django-modeladmin-reorder==0.3.1
django-multiupload==0.6.1
django-oauth-toolkit==2.2.0
django-object-actions==4.1.0
django-ratelimit==3.0.1
django-recaptcha==3.0.0
django-redis==5.2.0
django-silk==5.0.3
Django~=3.2.23
django-cors-headers==4.7.0
django-debug-toolbar==5.0.1
django-extensions==3.2.3
django-multiupload-plus==0.1.0
django-oauth-toolkit==3.0.1
django-object-actions==4.3.0
django-ratelimit==4.1.0
django-recaptcha==4.0.0
django-redis==5.4.0
django-silk==5.3.2
Django~=4.2.19
djangorestframework-jsonp==1.0.2
djangorestframework-xml==2.0.0
djangorestframework-yaml==2.0.0
djangorestframework==3.13.1
djangorestframework==3.15.2
fabric==2.6.0
feedparser~=6.0.10
freezegun==1.2.2
Expand Down Expand Up @@ -52,7 +51,7 @@ PyYAML==6.0.1
redis==3.2.0
scikit-learn==1.4.1.post1 # clustering
scipy==1.12.0 # clustering
sentry-sdk[django]~=1.31
sentry-sdk[django]~=2.21.0
Sphinx==1.6.3
stripe==2.28.1
xlrd==2.0.1 # for reading .xls files (but not .xlsx)
Expand Down
Loading

0 comments on commit 9b0bea2

Please sign in to comment.