def index_or_update_project(self, uuid): """ Takes a project UUID and either creates a new document in the des-projects index or updates the document if one already exists for that project. """ from designsafe.apps.api.projects.models import Project client = get_service_account_client() project_model = Project(client) project = project_model.search({'uuid': uuid}, client)[0] project_meta = project.to_dict() to_index = { key: value for key, value in project_meta.iteritems() if key != '_links' } to_index['value'] = { key: value for key, value in project_meta['value'].iteritems() if key != 'teamMember' } if not isinstance(to_index['value'].get('awardNumber', []), list): to_index['value']['awardNumber'] = [{ 'number': to_index['value']['awardNumber'] }] if to_index['value'].get('guestMembers', []) == [None]: to_index['value']['guestMembers'] = [] project_search = IndexedProject.search().filter( Q({'term': { 'uuid._exact': uuid }})) res = project_search.execute() if res.hits.total.value == 0: # Create an ES record for the new metadata. # project_info_args = {key:value for key,value in project_info.iteritems() if key != '_links'} project_ES = IndexedProject(**to_index) project_ES.save() elif res.hits.total.value == 1: # Update the record. doc = res[0] doc.update(**to_index) else: # If we're here we've somehow indexed the same project multiple times. # Delete all records and replace with the metadata passed to the task. for doc in res: IndexedProject.get(doc.meta.id).delete() project_ES = IndexedProject(**to_index) project_ES.save()
def get(self, request, username): """Returns a list of Project for a specific user. If the requesting user is a super user then we can 'impersonate' another user. Else this is an unauthorized request. """ if not request.user.is_superuser: return HttpResponseForbidden() user = get_user_model().objects.get(username=username) ag = user.agave_oauth.client q = request.GET.get('q', None) if not q: projects = Project.list_projects(agave_client=ag) else: projects = Project.search(q=q, agave_client=ag) return JsonResponse({'projects': projects}, encoder=AgaveJSONEncoder)