def get_participants(self, obj): def calculate(): return obj.get_response_counts(self.request.region, self.request.include_subregions) counts = get_obj_cacheable(obj, '_response_counts', calculate) return sum(counts.values())
def get_archived_count(self, recalculate=False): """ Number of archived messages with this label """ return get_obj_cacheable(self, self.ARCHIVED_COUNT_CACHE_ATTR, lambda: self._get_archived_count(), recalculate)
def derive_pollrun(self): def fetch(): pollruns = PollRun.objects.by_org(self.request.org) pollruns = pollruns.select_related("poll") return pollruns.get(pk=self.kwargs["pollrun"]) return get_obj_cacheable(self, "_pollrun", fetch)
def derive_pollrun(self): def fetch(): pollrun_qs = PollRun.objects.by_org(self.request.org) pollrun_qs = pollrun_qs.select_related('poll') return get_object_or_404(pollrun_qs, pk=self.kwargs['pollrun']) return get_obj_cacheable(self, '_pollrun', fetch)
def derive_contact(self): def fetch(): contact_qs = Contact.objects.filter(org=self.request.org) contact_qs = contact_qs.select_related('region') return get_object_or_404(contact_qs, pk=self.kwargs['contact']) return get_obj_cacheable(self, '_contact', fetch)
def derive_questions(self): def fetch(): questions = OrderedDict() for question in self.derive_pollrun().poll.get_questions(): questions["question_%d" % question.pk] = question return questions return get_obj_cacheable(self, "_questions", fetch)
def _user_get_all_regions(user, org): """Return org regions user has direct or implied (by hierarchy) permission for.""" def calculate(): regions = user.get_direct_regions(org).get_descendants(include_self=True) regions = regions.filter(is_active=True) return regions attr_name = '_regions_with_descendants_{}'.format(org.pk) # cache per org return get_obj_cacheable(user, attr_name, calculate)
def derive_questions(self): def fetch(): questions = OrderedDict() for question in self.derive_pollrun().poll.questions.active(): questions['question_%d' % question.pk] = question return questions return get_obj_cacheable(self, '_questions', fetch)
def _user_get_all_regions(user, org): """Return org regions user has direct or implied (by hierarchy) permission for.""" def calculate(): regions = user.get_direct_regions(org).get_descendants(include_self=True) regions = regions.filter(is_active=True) return regions attr_name = '_regions_with_descendants_{}'.format(org.pk) # cache per org return get_obj_cacheable(user, attr_name, calculate)
def derive_questions(self): def fetch(): questions = OrderedDict() for question in self.derive_pollrun().poll.questions.active(): questions['question_%d' % question.pk] = question return questions return get_obj_cacheable(self, '_questions', fetch)
def _user_get_rooms(user, org): def calculate(): # org admins have implicit access to all rooms if user.is_admin_for(org): return Room.get_all(org) else: return user.rooms.filter(is_active=True) return get_obj_cacheable(user, "_rooms", calculate)
def _user_get_regions(user, org): def calculate(): # org admins have implicit access to all regions if user.is_admin_for(org): return Region.get_all(org) else: return user.regions.filter(is_active=True) return get_obj_cacheable(user, '_regions', calculate)
def _user_get_direct_regions(user, org): """Return org regions user has direct permission for.""" def calculate(): # org admins have implicit access to all regions if user.is_admin_for(org): return Region.get_all(org) else: return user.regions.filter(is_active=True) attr_name = '_regions_{}'.format(org.pk) # cache per org return get_obj_cacheable(user, attr_name, calculate)
def _user_get_direct_regions(user, org): """Return org regions user has direct permission for.""" def calculate(): # org admins have implicit access to all regions if user.is_admin_for(org): return Region.get_all(org) else: return user.regions.filter(is_active=True) attr_name = '_regions_{}'.format(org.pk) # cache per org return get_obj_cacheable(user, attr_name, calculate)
def derive_issues(self): def fetch(): issues = OrderedDict() for issue in Issue.get_all( self.request.org, self.request.region).order_by('-conducted_on')[0:3]: issues['issue_%d' % issue.pk] = issue return issues return get_obj_cacheable(self, '_issues', fetch)
def derive_room(self): def fetch(): room = Room.objects.filter(pk=self.kwargs['room'], org=self.request.org).first() if not room: raise Http404("No such room in this org") if room not in self.request.user.get_rooms(self.request.org): raise PermissionDenied() return room return get_obj_cacheable(self, '_room', fetch)
def derive_pollruns(self): def fetch(): pollruns = OrderedDict() qs = PollRun.objects.get_all( self.request.region, self.request.include_subregions) qs = qs.order_by('-conducted_on') for pollrun in qs[0:3]: pollruns['pollrun_%d' % pollrun.pk] = pollrun return pollruns return get_obj_cacheable(self, '_pollruns', fetch)
def get_responses(self, obj): def calculate(): return obj.get_response_counts(self.request.region, self.request.include_subregions) counts = get_obj_cacheable(obj, "_response_counts", calculate) if counts[Response.STATUS_PARTIAL]: return "{complete} ({partial})".format( complete=counts[Response.STATUS_COMPLETE], partial=counts[Response.STATUS_PARTIAL] ) else: return counts[Response.STATUS_COMPLETE]
def derive_pollruns(self): def fetch(): pollruns = OrderedDict() qs = PollRun.objects.get_all(self.request.org, self.request.region, self.request.include_subregions) qs = qs.order_by('-conducted_on') for pollrun in qs[0:3]: pollruns['pollrun_%d' % pollrun.pk] = pollrun return pollruns return get_obj_cacheable(self, '_pollruns', fetch)
def derive_room(self): def fetch(): room = Room.objects.filter(pk=self.kwargs['room'], org=self.request.org).first() if not room: raise Http404("No such room in this org") if room not in self.request.user.get_rooms(self.request.org): raise PermissionDenied() return room return get_obj_cacheable(self, '_room', fetch)
def get_responses(self, obj): def calculate(): return obj.get_response_counts(self.request.region, self.request.include_subregions) counts = get_obj_cacheable(obj, '_response_counts', calculate) if counts[Response.STATUS_PARTIAL]: return "{complete} ({partial})".format( complete=counts[Response.STATUS_COMPLETE], partial=counts[Response.STATUS_PARTIAL], ) else: return counts[Response.STATUS_COMPLETE]
def get_contacts(self, obj): group_sizes = get_obj_cacheable(self, '_group_sizes', lambda: Group.fetch_sizes(self.request.org, self.derive_queryset())) return group_sizes[obj]
def get_inbox_count(self, recalculate=False): """ Number of inbox (non-archived) messages with this label """ return get_obj_cacheable(self, self.INBOX_COUNT_CACHE_ATTR, lambda: self._get_inbox_count(), recalculate)
def derive_contact(self): def fetch(): return Contact.objects.get(pk=self.kwargs["contact"], org=self.request.org) return get_obj_cacheable(self, "_contact", fetch)
def derive_pollrun(self): def fetch(): pollrun_qs = PollRun.objects.by_org(self.request.org) pollrun_qs = pollrun_qs.select_related('poll') return get_object_or_404(pollrun_qs, pk=self.kwargs['pollrun']) return get_obj_cacheable(self, '_pollrun', fetch)
def get_tests(self): return get_obj_cacheable(self, "_tests", lambda: self._get_tests())
def get_tests(self): return get_obj_cacheable(self, '_tests', lambda: self._get_tests())
def get_participants(self, obj): def calculate(): return obj.get_response_counts(self.request.region, self.request.include_subregions) counts = get_obj_cacheable(obj, "_response_counts", calculate) return chain(counts.values())
def get_archived_count(self, recalculate=False): """ Number of archived messages with this label """ return get_obj_cacheable(self, self.ARCHIVED_COUNT_CACHE_ATTR, lambda: self._get_archived_count(), recalculate)
def get_inbox_count(self, recalculate=False): """ Number of inbox (non-archived) messages with this label """ return get_obj_cacheable(self, self.INBOX_COUNT_CACHE_ATTR, lambda: self._get_inbox_count(), recalculate)
def derive_contact(self): def fetch(): contact_qs = Contact.objects.filter(org=self.request.org) contact_qs = contact_qs.select_related('region') return get_object_or_404(contact_qs, pk=self.kwargs['contact']) return get_obj_cacheable(self, '_contact', fetch)
def derive_poll(self): def fetch(): poll_qs = Poll.objects.active().by_org(self.request.org) return get_object_or_404(poll_qs, pk=self.kwargs['poll']) return get_obj_cacheable(self, '_poll', fetch)
def derive_poll(self): def fetch(): return Poll.objects.get(pk=self.kwargs["poll"], org=self.request.org, is_active=True) return get_obj_cacheable(self, "_poll", fetch)
def derive_contact(self): def fetch(): return Contact.objects.get(pk=self.kwargs['contact'], org=self.request.org) return get_obj_cacheable(self, '_contact', fetch)
def get_actions(self): return get_obj_cacheable(self, '_actions', lambda: self._get_actions())
def get_contacts(self, obj): group_sizes = get_obj_cacheable( self, '_group_sizes', lambda: Group.fetch_sizes( self.request.org, self.derive_queryset())) return group_sizes[obj]
def get_actions(self): return get_obj_cacheable(self, "_actions", lambda: self._get_actions())
def derive_poll(self): def fetch(): poll_qs = Poll.objects.active().by_org(self.request.org) return get_object_or_404(poll_qs, pk=self.kwargs['poll']) return get_obj_cacheable(self, '_poll', fetch)