Commit 70fc955b authored by David Haynes's avatar David Haynes 🙆

Merge branch '156-sorting-my-links' into '2.3-dev'

Implemented sorting of links on the 'My Links' Page

See merge request !111
parents 85abe52b 9e5764c6
Pipeline #1691 passed with stages
in 2 minutes and 31 seconds
......@@ -27,6 +27,34 @@ SRCT Go • My Links
</div>
</div>
<!-- display sort menu if there are any links -->
{% if urls %}
<div class="dropdown" style="text-align:right;">
<!-- button for the dropdown menu -->
<button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">
Sort By <i class="fa fa-caret-down fa-fw"></i>
</button>
<!-- generate the list of sorting options based on given sort_methods -->
<ul class="dropdown-menu dropdown-menu-right" style="margin-top: 2px;">
{% for method, name in sort_methods.items %}
<!-- check if currently selected sort method is this one to highlight -->
{% if request.GET.sort == method %}
<li style="background: #eee;" {# class="active" Properly implmenet the .active class#}>
<!-- if there is no currently selected sort method, highlight the default case "Most Recent" -->
{% elif request.GET.sort == null and method == "-date_created" %}
<li style="background: #eee;" {# class="active" Properly implmenet the .active class#}>
<!-- no styling -->
{% else %}
<li>
{% endif %}
<!-- create link to sort method -->
<a href="?sort={{ method }}" style="color: #262626;">{{ name }}</a>
</li>
{% endfor %}
</ul>
</div>
{% endif %}
<!-- show all of the links -->
<div class="row">
<!-- for every url in the urls list-->
......
......@@ -41,16 +41,37 @@ def index(request):
if not request.user.registereduser.approved:
return render(request, 'not_registered.html')
# List of sort methods and their display name "Column" : "Name"
SORT_METHODS = {
"-date_created":"Most Recent",
"date_created": "Oldest",
"short":"Alphabetical (A-Z)",
"-short":"Alphabetical (Z-A)",
"-clicks":"Most Popular",
"clicks":"Least Popular",
"-expires":"Expiring Soon"
}
# Get the requested sort method, default to "-date_created" : "Most Recent"
sort_method = request.GET.get('sort', '-date_created')
# Get the current domain info
domain = "%ss://%s" % (request.scheme, request.META.get('HTTP_HOST')) + "/"
# Grab a list of all the URL's that are currently owned by the user
urls = URL.objects.filter(owner=request.user.registereduser)
# Render my_links passing the list of URL's and Domain to the template
# Check if provided sort method is valid, otherwise default
if sort_method in SORT_METHODS:
urls = urls.order_by(sort_method)
else:
urls = urls.order_by("-date_created")
# Render my_links passing the list of URL's, Domain, and Sort Methods to the template
return render(request, 'core/index.html', {
'urls': urls,
'domain': domain,
'sort_methods': SORT_METHODS
})
@login_required
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment