def table(request, slugs=None): show_categs = bool(int(request.GET.get('show_categs', 1))) only_common = bool(int(request.GET.get('only_common', 1))) drugs = Drug.objects if slugs: drugs = drugs.filter(slug__in=slugs) elif only_common: drugs = drugs.filter(common=True) drugs = (drugs .prefetch_related('category') .order_by(F('category__name').asc(nulls_last=True), 'name') ) interactions = Interaction.objects.between(drugs, prefetch=True) dummy_risks = Interaction.get_dummy_risks() dummy_synergies = Interaction.get_dummy_synergies() chart_data = {drug: {} for drug in drugs} for inter in interactions: chart_data[inter.from_drug][inter.to_drug] = inter chart_data[inter.to_drug][inter.from_drug] = inter return render(request, 'drugcombinator/table.html', locals())
def test_interactants_ordering(self): inter = Interaction( from_drug=self.drug_c, to_drug=self.drug_a ) inter.save() self.assertSequenceEqual( inter.interactants, sorted(inter.interactants, key=lambda d: d.name) )
def get_context(self, *args, **kwargs): ctx = super().get_context(*args, **kwargs) ctx.interactions = ctx.interactions.filter(is_draft=False) ctx.dummy_risks = Interaction.get_dummy_risks() ctx.dummy_synergies = Interaction.get_dummy_synergies() for inter in ctx.interactions: inter.drug = inter.other_interactant(ctx.drug) return ctx
def test_interactants_unique_together(self): with self.assertRaises(IntegrityError): Interaction( from_drug=self.drug_b, to_drug=self.drug_a ).save()
def test_interactants_inequals(self): with self.assertRaises(IntegrityError): Interaction( from_drug=self.drug_a, to_drug=self.drug_a ).save()
def portal(request): portal = get_object_or_404(Portal, drug=request.drug) interactions = (portal.drug.interactions.filter( is_draft=False).prefetch_related('from_drug', 'to_drug')) for inter in interactions: inter.drug = inter.other_interactant(portal.drug) interactions = sorted(interactions, key=attrgetter('drug.name')) dummy_risks = Interaction.get_dummy_risks() return render(request, 'drugportals/portal.html', locals())
def portal(request, drug): try: portal = Portal.objects.get(drug__slug=drug) except Portal.DoesNotExist: raise Http404("Ce portail n'existe pas.") interactions = (portal.drug.interactions.filter( is_draft=False).prefetch_related('from_drug', 'to_drug').order_by_name()) for inter in interactions: inter.drug = inter.other_interactant(portal.drug) dummy_risks = Interaction.get_dummy_risks() return render(request, 'drugportals/portal.html', locals())
class InteractionModelTestCase(TestCase): drug_a = Drug(name="DrugA", slug='drug-a') drug_b = Drug(name="DrugB", slug='drug-b') drug_c = Drug(name="DrugC", slug='drug-c') inter_a_b = Interaction( from_drug=drug_a, to_drug=drug_b ) inter_b_c = Interaction( from_drug=drug_b, to_drug=drug_c ) def setUp(self): self.drug_a.save() self.drug_b.save() self.drug_c.save() self.inter_a_b.save() self.inter_b_c.save() def test_interactants_inequals(self): with self.assertRaises(IntegrityError): Interaction( from_drug=self.drug_a, to_drug=self.drug_a ).save() def test_interactants_unique_together(self): with self.assertRaises(IntegrityError): Interaction( from_drug=self.drug_b, to_drug=self.drug_a ).save() def test_interactants_ordering(self): inter = Interaction( from_drug=self.drug_c, to_drug=self.drug_a ) inter.save() self.assertSequenceEqual( inter.interactants, sorted(inter.interactants, key=lambda d: d.name) ) def test_interactants_fetching(self): self.assertEqual( self.drug_b.all_interactants.count(), self.drug_b.interactions.count(), ) def test_interactants_fetching_django_not_fixed(self): self.assertNotEqual( self.drug_b.all_interactants.count(), self.drug_b.interactants.count(), "The Django framework now seems to support recursive M2M " \ "fields better. The Drug.all_interactants tweak is maybe " \ "not needed anymore." ) def test_other_interactant(self): self.assertIs( self.inter_a_b.other_interactant(self.drug_a), self.drug_b ) def test_other_interactant_invalid(self): with self.assertRaises(ValueError): self.inter_a_b.other_interactant(self.drug_c)