Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# (c) 2018-2020
2# MPIB <https://www.mpib-berlin.mpg.de/>,
3# MPI-CBS <https://www.cbs.mpg.de/>,
4# MPIP <http://www.psych.mpg.de/>
5#
6# This file is part of Castellum.
7#
8# Castellum is free software; you can redistribute it and/or modify it
9# under the terms of the GNU Affero General Public License as published
10# by the Free Software Foundation; either version 3 of the License, or
11# (at your option) any later version.
12#
13# Castellum is distributed in the hope that it will be useful, but
14# WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16# Affero General Public License for more details.
17#
18# You should have received a copy of the GNU Affero General Public
19# License along with Castellum. If not, see
20# <http://www.gnu.org/licenses/>.
22from django.contrib.postgres.fields import JSONField
23from django.db import models
24from django.urls import reverse
26from castellum.recruitment import filter_queries
27from castellum.studies.models import Study
28from castellum.subjects.models import Subject
30from ..models import AttributeDescription
33class SubjectFilterGroupManager(models.Manager):
34 def clone(self, original_group):
35 cloned_group = SubjectFilterGroup.objects.create(study=original_group.study)
36 SubjectFilter.objects.bulk_create(self.clone_filters(original_group, cloned_group))
37 return cloned_group
39 def clone_filters(self, original_group, cloned_group):
40 for f in original_group.subjectfilter_set.all():
41 yield SubjectFilter(
42 group=cloned_group, description=f.description, operator=f.operator, value=f.value
43 )
46class SubjectFilterGroup(models.Model):
47 study = models.ForeignKey(Study, on_delete=models.CASCADE)
49 objects = SubjectFilterGroupManager()
51 def get_absolute_url(self):
52 return reverse('studies:filtergroup-update', args=[self.study.pk, self.pk])
54 def get_matches(self):
55 return Subject.objects.filter(
56 filter_queries.study_except_filters(self.study),
57 filter_queries.subjectfilters(self),
58 filter_queries.has_consent(),
59 )
62class SubjectFilter(models.Model):
63 group = models.ForeignKey(SubjectFilterGroup, on_delete=models.CASCADE)
64 description = models.ForeignKey(AttributeDescription, on_delete=models.CASCADE)
65 operator = models.CharField(max_length=64)
66 value = JSONField(editable=True)
68 def __str__(self):
69 return self.description.field.get_filter_display(self.operator, self.value)
71 def to_q(self, include_unknown=True):
72 return self.description.field.filter_to_q(self.operator, self.value, include_unknown)