|
6 | 6 | from bs4 import BeautifulSoup |
7 | 7 | from django.conf import settings |
8 | 8 | from django.contrib.auth import get_user_model |
| 9 | +from django.core.exceptions import BadRequest |
9 | 10 | from django.shortcuts import get_object_or_404 |
10 | 11 | from django.utils.translation import gettext as _ |
11 | 12 | from pytz import utc |
12 | 13 | from waffle import get_waffle_flag_model # pylint: disable=invalid-django-waffle-import |
13 | 14 |
|
14 | 15 | from lms.djangoapps.branding.api import get_logo_url_for_email |
15 | | -from lms.djangoapps.discussion.notification_prefs.views import UsernameCipher |
| 16 | +from lms.djangoapps.discussion.notification_prefs.views import UsernameCipher, UsernameDecryptionException |
16 | 17 | from openedx.core.djangoapps.lang_pref import LANGUAGE_KEY |
17 | 18 | from openedx.core.djangoapps.notifications.base_notification import COURSE_NOTIFICATION_APPS, COURSE_NOTIFICATION_TYPES |
18 | 19 | from openedx.core.djangoapps.notifications.config.waffle import ENABLE_EMAIL_NOTIFICATIONS |
@@ -384,6 +385,19 @@ def decrypt_string(string): |
384 | 385 | return UsernameCipher.decrypt(string).decode() |
385 | 386 |
|
386 | 387 |
|
| 388 | +def username_from_hash(group, request): |
| 389 | + """ |
| 390 | + Django ratelimit key to return username from hash |
| 391 | + """ |
| 392 | + username = request.resolver_match.kwargs.get("username") |
| 393 | + if username: |
| 394 | + try: |
| 395 | + return decrypt_string(username) |
| 396 | + except UsernameDecryptionException as exc: |
| 397 | + raise BadRequest("Bad request") from exc |
| 398 | + return None |
| 399 | + |
| 400 | + |
387 | 401 | def update_user_preferences_from_patch(encrypted_username): |
388 | 402 | """ |
389 | 403 | Decrypt username and patch and updates user preferences |
|
0 commit comments