Repository: comic/grand-challenge.org

Issue:
Add the host filter
Filtering by host disappeared from the all challenge page disappeared at some point, add it back.


Discussion:

Resolving patch:
diff --git a/app/grandchallenge/challenges/models.py b/app/grandchallenge/challenges/models.py
--- a/app/grandchallenge/challenges/models.py
+++ b/app/grandchallenge/challenges/models.py
@@ -3,6 +3,7 @@
 import logging
 import os
 import re
+from collections import namedtuple
 
 from django.conf import settings
 from django.contrib.auth.models import Group
@@ -13,6 +14,7 @@
 from django.utils._os import safe_join
 from guardian.shortcuts import assign_perm, remove_perm
 from guardian.utils import get_anonymous_user
+from tldextract import extract
 
 from grandchallenge.core.urlresolvers import reverse
 
@@ -286,7 +288,8 @@ def get_filter_classes(self):
         """
         classes = set()
 
-        classes.add(self.get_host_id())
+        if self.host_filter.host:
+            classes.add(self.host_filter.filter_tag)
 
         # Filter by modality
         for mod in self.modalities.all():
@@ -303,41 +306,36 @@ def get_filter_classes(self):
 
         return list(classes)
 
-    def get_host_id(self):
+    @property
+    def host_filter(self):
+        host_filter = namedtuple("host_filter", ["host", "filter_tag"])
+        domain = self.registered_domain
+        return host_filter(domain, re.sub(r"\W+", "", domain))
+
+    @property
+    def registered_domain(self):
         """
         Copied from grandchallenge_tags
 
         Try to find out what framework this challenge is hosted on, return
         a string which can also be an id or class in HTML
         """
-        if self.hosted_on_comic:
-            return "grand-challenge"
+        return extract(self.get_absolute_url()).registered_domain
 
-        if "codalab.org" in self.get_absolute_url():
-            return "codalab"
-
-        else:
-            return "Unknown"
-
-    def get_host_link(self):
+    @property
+    def host_link(self):
         """
         Copied from grandchallenge tags
 
         Try to find out what framework this challenge is hosted on
         """
-        host_id = self.get_host_id()
-
-        if host_id == "grand-challenge":
-            framework_name = "grand-challenge.org"
-            framework_url = "http://grand-challenge.org"
-        elif host_id == "codalab":
-            framework_name = "codalab.org"
-            framework_url = "http://codalab.org"
+        domain = self.registered_domain
+
+        if domain:
+            return f'<a href="http://{domain}">{domain}</a>'
         else:
             return None
 
-        return f"<a href={framework_url}>{framework_name}</a>"
-
     class Meta:
         abstract = True
 
diff --git a/app/grandchallenge/challenges/views.py b/app/grandchallenge/challenges/views.py
--- a/app/grandchallenge/challenges/views.py
+++ b/app/grandchallenge/challenges/views.py
@@ -58,9 +58,11 @@ def get_context_data(self, *, object_list=None, **kwargs):
         )
 
         challenges_by_year = defaultdict(list)
+        hosts = set()
 
         for c in challenges:
             challenges_by_year[c.year].append(c)
+            hosts.add(c.host_filter)
 
         modalities = ImagingModality.objects.all()
         task_types = TaskType.objects.all()
@@ -82,6 +84,7 @@ def get_context_data(self, *, object_list=None, **kwargs):
                         reverse=True,
                     )
                 ),
+                "hosts": sorted([h for h in hosts if h.host]),
             }
         )
 


Test patch: