From 9e5764c6a06a7cf5d871466291666819d2e046b2 Mon Sep 17 00:00:00 2001 From: Chris Gallarno Date: Fri, 13 Oct 2017 16:09:10 -0400 Subject: [PATCH] Implemented sorting of links on the 'My Links' Page - Implemented sorting with sort methods of -- Most Recent -- Oldest -- Alphabetical (A-Z) -- Alphabetical (Z-A) -- Most Popular -- Least Popular -- Expires Soon - Closes #156 --- go/go/templates/core/index.html | 28 ++++++++++++++++++++++++++++ go/go/views.py | 23 ++++++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/go/go/templates/core/index.html b/go/go/templates/core/index.html index 4b2c81b..cd727d6 100644 --- a/go/go/templates/core/index.html +++ b/go/go/templates/core/index.html @@ -27,6 +27,34 @@ SRCT Go • My Links + +{% if urls %} + +{% endif %} +
diff --git a/go/go/views.py b/go/go/views.py index 237c9cf..ab85cba 100644 --- a/go/go/views.py +++ b/go/go/views.py @@ -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 -- GitLab