diff --git a/ippisite/ippidb/tests.py b/ippisite/ippidb/tests.py index 52c7ea439ddd36d5b60f4902a7481ef728a55d47..ea1a73f8f5e0e528db4402582841222a6a73d883 100644 --- a/ippisite/ippidb/tests.py +++ b/ippisite/ippidb/tests.py @@ -298,7 +298,7 @@ class QuestionCompoundViews(TestCase): The detail view of an existing compound returns a 200 """ - url = reverse('compound_card', args=(1,)) + url = reverse('compound_card', kwargs={'pk':1}) response = self.client.get(url) self.assertEqual(response.status_code, 200) @@ -307,7 +307,7 @@ class QuestionCompoundViews(TestCase): The detail view of a non-existing compound returns a 404 not found. """ - url = reverse('compound_card', args=(9,)) + url = reverse('compound_card', kwargs={'pk':9}) response = self.client.get(url) self.assertEqual(response.status_code, 404) diff --git a/ippisite/ippidb/urls.py b/ippisite/ippidb/urls.py index 7b69861ffe1a8bb45e4fbca14abd698eaaa1ff1f..0a37de2e2ce2d41001a02c57e94a2ef573b888c8 100644 --- a/ippisite/ippidb/urls.py +++ b/ippisite/ippidb/urls.py @@ -17,7 +17,7 @@ urlpatterns = [ url(r'^about/general/$', views.general, name='general'), url(r'^compounds/$', views.CompoundListView.as_view(), name='compound_list'), url(r'^compounds/IPPIDB-(?P<compound_id>\w+)$', views.CompoundCardBDCHEMRedirectView.as_view(), name='redirect_compound_card'), - url(r'^compounds/(?P<compound_id>\w+)$', views.compound_card, name='compound_card'), + url(r'^compounds/(?P<pk>\w+)$', views.CompoundDetailView.as_view(), name='compound_card'), url(r'^tutorials$', views.tutorials, name='tutorials'), url(r'^contribute$', views.contribute, name='contribute'), url(r'^admin-session$', views.adminSession, name='admin-session'), diff --git a/ippisite/ippidb/views/compound_query.py b/ippisite/ippidb/views/compound_query.py index 9921ef9dc4badf81cc646f6dcfae3b42555ef003..da4d49859816b2500fe42cfef89766d1d1a02ad7 100644 --- a/ippisite/ippidb/views/compound_query.py +++ b/ippisite/ippidb/views/compound_query.py @@ -10,6 +10,7 @@ from django.shortcuts import render from django.urls import reverse from django.http import Http404, JsonResponse from django.views.generic.list import ListView +from django.views.generic.detail import DetailView from django.views.generic.base import RedirectView from ippidb.utils import mol2smi, smi2mol @@ -497,18 +498,21 @@ class CompoundListView(ListView): # return queryset return qs +class CompoundDetailView(DetailView): + """ + Compound detail view + """ -def compound_card(request, compound_id): - try: - compound = Compound.objects.get(id=int(compound_id)) - except Compound.DoesNotExist: - raise Http404("No compound data for %s" % (compound_id)) - le_lle_biplot_data = LeLleBiplotData.objects.get().le_lle_biplot_data - pca_biplot = json.loads(PcaBiplotData.objects.get().pca_biplot_data) - pca_biplot_data = json.dumps(pca_biplot['data']) - pca_biplot_cc = pca_biplot['correlation_circle'] - return render(request, 'compound_card.html', {'compound': compound, 'le_lle_biplot_data': le_lle_biplot_data, 'pca_biplot_data': pca_biplot_data, 'pca_biplot_cc': pca_biplot_cc}) + model = Compound + template_name = "compound_card.html" + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context['le_lle_biplot_data'] = LeLleBiplotData.objects.get().le_lle_biplot_data + pca_biplot = json.loads(PcaBiplotData.objects.get().pca_biplot_data) + context['pca_biplot_data'] = json.dumps(pca_biplot['data']) + context['pca_biplot_cc'] = pca_biplot['correlation_circle'] + return context class CompoundCardBDCHEMRedirectView(RedirectView):