def job_templates(self): return [JobTemplate() for i in range(0, 10)]
def get_serializer_context(self, *args, **kwargs): full_context = super(OrganizationCountsMixin, self).get_serializer_context(*args, **kwargs) if self.request is None: return full_context db_results = {} org_qs = self.model.accessible_objects(self.request.user, 'read_role') org_id_list = org_qs.values('id') if len(org_id_list) == 0: if self.request.method == 'POST': full_context['related_field_counts'] = {} return full_context inv_qs = Inventory.accessible_objects(self.request.user, 'read_role') project_qs = Project.accessible_objects(self.request.user, 'read_role') jt_qs = JobTemplate.accessible_objects(self.request.user, 'read_role') # Produce counts of Foreign Key relationships db_results['inventories'] = inv_qs.values('organization').annotate( Count('organization')).order_by('organization') db_results['teams'] = (Team.accessible_objects( self.request.user, 'read_role').values('organization').annotate( Count('organization')).order_by('organization')) db_results['job_templates'] = jt_qs.values('organization').annotate( Count('organization')).order_by('organization') db_results['projects'] = project_qs.values('organization').annotate( Count('organization')).order_by('organization') # Other members and admins of organization are always viewable db_results['users'] = org_qs.annotate( users=Count('member_role__members', distinct=True), admins=Count('admin_role__members', distinct=True)).values('id', 'users', 'admins') count_context = {} for org in org_id_list: org_id = org['id'] count_context[org_id] = { 'inventories': 0, 'teams': 0, 'users': 0, 'job_templates': 0, 'admins': 0, 'projects': 0 } for res, count_qs in db_results.items(): if res == 'users': org_reference = 'id' else: org_reference = 'organization' for entry in count_qs: org_id = entry[org_reference] if org_id in count_context: if res == 'users': count_context[org_id]['admins'] = entry['admins'] count_context[org_id]['users'] = entry['users'] continue count_context[org_id][res] = entry['%s__count' % org_reference] full_context['related_field_counts'] = count_context return full_context
def job_template(organization): jt = JobTemplate(name='test-job_template') jt.save() return jt