def typeahead_user_organisations(request): user = request.user is_admin = user.is_active and (user.is_superuser or user.is_admin) organisations = user.approved_organisations( ) if not is_admin else Organisation.objects.all() return Response( rejig(organisations, TypeaheadOrganisationSerializer(organisations, many=True)))
def update_directory(request): """REST view for the update directory.""" # Fetch updates based on whether we are on Akvo site or RSR main site page = request.rsr_page all_updates = _all_updates() if not page else page.updates() # Filter updates based on query parameters filter_, text_filter = _create_filters_query(request) updates = all_updates.filter( filter_).distinct() if filter_ is not None else all_updates updates_text_filtered = updates.filter( text_filter) if text_filter is not None else updates if updates_text_filtered.exists(): updates = updates_text_filtered # Get the relevant data for typeaheads based on filtered updates (minus # text filtering, if no updates were found) locations = [{ 'id': choice[0], 'name': choice[1] } for choice in location_choices(updates)] project_ids = updates.values_list('project__id', flat=True) projects = Project.objects.filter(id__in=project_ids) organisations = projects.all_partners().values('id', 'name', 'long_name') # FIXME: Currently only vocabulary 2 is supported (as was the case with # static filters). This could be extended to other vocabularies, in future. valid_sectors = dict(codelist_choices(SECTOR_CATEGORY)) sectors = projects.sectors().filter( vocabulary='2', sector_code__in=valid_sectors).values('sector_code').distinct() count = updates_text_filtered.count() display_updates = get_qs_elements_for_page(updates_text_filtered, request, count) display_updates = display_updates.select_related( 'project', 'project__primary_location', 'project__primary_organisation', 'user', ).prefetch_related('project__partners', 'project__sectors', 'locations', 'locations__country') response = { 'project_count': count, 'projects': ProjectUpdateDirectorySerializer(display_updates, many=True).data, 'organisation': TypeaheadOrganisationSerializer(organisations, many=True).data, 'location': locations, 'sector': TypeaheadSectorSerializer(sectors, many=True).data, 'page_size_default': settings.PROJECT_DIRECTORY_PAGE_SIZES[0], } return Response(response)
def typeahead_organisation(request): page = request.rsr_page if request.GET.get('partners', '0') == '1' and page: organisations = page.partners() else: # Project editor - all organizations organisations = Organisation.objects.all() organisations = organisations.values('id', 'name', 'long_name') return Response( rejig(organisations, TypeaheadOrganisationSerializer(organisations, many=True)))
def project_directory(request): """Return the values for various project filters. Based on the current filters, it returns new options for all the (other) filters. This is used to generate dynamic filters. """ page = request.rsr_page projects = _project_list(request) projects_data = [ serialized_project(project_id) for project_id in projects.values_list('pk', flat=True) ] organisations = list(projects.all_partners().values( 'id', 'name', 'long_name')) organisations = TypeaheadOrganisationSerializer(organisations, many=True).data custom_fields = (OrganisationCustomField.objects.filter( type='dropdown', organisation=page.organisation, show_in_searchbar=True).order_by('order', 'id') if page else []) sectors = [{ 'id': id_, 'name': name } for id_, name in codelist_choices(SECTOR_CATEGORY)] response = { 'projects': projects_data, 'organisation': organisations, 'sector': sectors, 'custom_fields': OrganisationCustomFieldSerializer(custom_fields, many=True).data, } return Response(response)
def typeahead_organisation(request): organisations = Organisation.objects.all() return Response( rejig(organisations, TypeaheadOrganisationSerializer(organisations, many=True)))