Skip to content

Commit

Permalink
Merge branch 'post-release-bw-fixes'
Browse files Browse the repository at this point in the history
  • Loading branch information
ffont committed Oct 31, 2023
2 parents 46b2ee5 + 16aa01a commit aec6a68
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 12 deletions.
5 changes: 5 additions & 0 deletions accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@ def get_user_sounds_in_search_url(self):

def get_user_packs_in_search_url(self):
return f'{reverse("sounds-search")}?f=username:"{ self.user.username }"&s=Date+added+(newest+first)&g=1&only_p=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) \
.order_by("-last_updated").values_list('id', flat=True)[0:15]
return Pack.objects.ordered_ids(pack_ids=latest_pack_ids)

@staticmethod
def locations_static(user_id, has_avatar):
Expand Down
12 changes: 3 additions & 9 deletions accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1485,18 +1485,12 @@ def account_stats_section(request, username):
def account_latest_packs_section(request, username):
if not request.GET.get('ajax'):
raise Http404 # Only accessible via ajax

user = request.parameter_user
latest_packs_from_cache = cache.get(settings.USER_LATEST_PACKS_CACHE_KEY.format(user.id), None)
if latest_packs_from_cache is None:
latest_pack_ids = Pack.objects.select_related().filter(user=user, num_sounds__gt=0).exclude(is_deleted=True) \
.order_by("-last_updated").values_list('id', flat=True)[0:15]
latest_packs = Pack.objects.ordered_ids(pack_ids=latest_pack_ids)
cache.set(settings.USER_LATEST_PACKS_CACHE_KEY.format(user.id), pickle.dumps(latest_packs), 60 * 60 * 24)
else:
latest_packs = pickle.loads(latest_packs_from_cache)
tvars = {
'user': user,
'latest_packs': latest_packs,
# Note we don't pass latest packs data because it is requested from the template
# if there is no cache available
}
return render(request, 'accounts/account_latest_packs_section.html', tvars)

Expand Down
1 change: 0 additions & 1 deletion freesound/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,6 @@

# User profile page cache key templates
USER_STATS_CACHE_KEY = 'user_stats_{}'
USER_LATEST_PACKS_CACHE_KEY = 'user_latest_packs_{}'

# User flagging notification thresholds
USERFLAG_THRESHOLD_FOR_NOTIFICATION = 3
Expand Down
7 changes: 6 additions & 1 deletion templates_bw/accounts/account_latest_packs_section.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{% load display_pack %}
{% load cache %}
{% cache 43200 bw_user_profile_latest_packs_section is_authenticated %}
{% with user.profile.get_latest_packs_for_profile_page as latest_packs %}
{% if latest_packs %}
<div class="row">
{% for pack in latest_packs %}
Expand All @@ -15,4 +18,6 @@
<h5>No packs... &#128543</h5>
<div class="text-grey v-spacing-top-1">Looks like {{ user.username }} has not uploaded any packs yet</div>
</div>
{% endif %}
{% endif %}
{% endwith %}
{% endcache %}
3 changes: 2 additions & 1 deletion utils/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ def invalidate_user_template_caches(user_id):
invalidate_template_cache('bw_user_profile_followers_count', user_id)
invalidate_template_cache('bw_user_profile_following_count', user_id)
invalidate_template_cache('bw_user_profile_following_tags_count', user_id)
invalidate_template_cache('bw_user_profile_latest_packs_section', True)
invalidate_template_cache('bw_user_profile_latest_packs_section', False)
cache.delete(settings.USER_STATS_CACHE_KEY.format(user_id))
cache.delete(settings.USER_LATEST_PACKS_CACHE_KEY.format(user_id))


def invalidate_all_moderators_header_cache():
Expand Down

0 comments on commit aec6a68

Please sign in to comment.