Exemple #1
0
    def form_valid(self, form):
        """
        Retrun whether source input is valid and then update API backend.
        """
        self.get_args()

        data = form.cleaned_data
        short_code = data.pop('short_name')
        data['short_code'] = short_code
        data['id'] = short_code
        data['name'] = short_code

        api = OclApi(self.request, debug=True)
        result = api.create_source(self.owner_type, self.owner_id, data)
        if result.status_code == requests.codes.created:
            messages.add_message(self.request, messages.INFO, _('Source created'))
            if self.from_org:
                return HttpResponseRedirect(reverse("source-home",
                                                    kwargs={"org": self.org_id,
                                                            'source': short_code}))
            else:
                return HttpResponseRedirect(reverse("source-home",
                                                    kwargs={"user": self.user_id,
                                                            'source': short_code}))
        else:
            emsg = result.json().get('detail', 'Error')
            messages.add_message(self.request, messages.ERROR, emsg)
            return HttpResponseRedirect(self.request.path)
Exemple #2
0
    def get_source_concepts(self, owner_type, owner_id, source_id,
                            source_version_id=None, search_params=None):
        """
        Load source concepts from the API and return OclSearch instance with results.
        """
        # TODO([email protected]): Validate the input parameters

        # Perform the search, applying source_version_id if not None
        searcher = OclSearch(search_type=OclConstants.RESOURCE_NAME_CONCEPTS,
                             search_scope=OclConstants.SEARCH_SCOPE_RESTRICTED,
                             params=search_params)
        api = OclApi(self.request, debug=True, facets=True)
        if source_version_id:
            search_response = api.get(
                owner_type, owner_id, 'sources', source_id, source_version_id, 'concepts',
                params=searcher.search_params)
        else:
            search_response = api.get(
                owner_type, owner_id, 'sources', source_id, 'concepts',
                params=searcher.search_params)
        if search_response.status_code == 404:
            raise Http404
        elif search_response.status_code != 200:
            search_response.raise_for_status()

        # Process the results
        searcher.process_search_results(
            search_type=searcher.search_type, search_response=search_response,
            search_params=search_params)

        return searcher
Exemple #3
0
    def get_initial(self):
        super(CollectionVersionsNewView, self).get_initial()
        self.get_args()

        api = OclApi(self.request, debug=True)
        # collection_version = None
        if self.from_org:
            collection_version = api.get('orgs', self.org_id, 'collections', self.collection_id,
                                         'versions', params={'limit': 1}).json()
        else:
            collection_version = api.get('users', self.user_id, 'collections', self.collection_id,
                                         'versions', params={'limit': 1}).json()

        data = {
            'request': self.request,
            'from_user': self.from_user,
            'from_org': self.from_org,
            'user_id': self.user_id,
            'org_id': self.org_id,
            'owner_type': self.owner_type,
            'owner_id': self.owner_id,
            'collection_id': self.collection_id,
            'previous_version': collection_version[0]['id'],
            'released': False
        }
        return data
Exemple #4
0
class Importer(object):
    """ Importer class for importing objects into OCL """

    def __init__(self):
        self.ocl = None
        self.username = None
        self.password = None
        self.web_host = os.environ['OCL_WEB_HOST']

    def load_user(self):
        """
            Load access info for specified username to create test data.
            :param username: is an existing user in the system.
        """
        user = User.objects.get(username=self.username)
        print user.password
        self.password = user.password

    def login(self):
        """
            Perform a login for the user to get authenticated access
            for subsequence create calls.
        """
        self.ocl = OclApi(admin=True, debug=True)

        result = self.ocl.get_user_auth(self.username, self.password)
        print 'get auth:', result.status_code
        if len(result.text) > 0:
            print result.json()

        # now use a "normal" API interface, save the user's access permission
        self.request = FakeRequest()
        self.ocl.save_auth_token(self.request, result.json())
        self.ocl = OclApi(self.request, debug=True)

    def connect(self):
        """ Login to OCL """
        self.load_user()
        self.login()

    def load_csv(self):
        """ Load data from CSV file and save to self.reader """
        print 'Loading from %s...' % self.filename
        f = open(self.filename, 'r')
        self.reader = csv.DictReader(f)

    def get_args(self, args, options):
        """
        Pick up common arguments like CSV file path and username
        """
        print args, options
        self.test_mode = options['test_mode']
        self.username = options['username']
        self.filename = options['filename']

        if self.username is None:
            raise CommandError('--username is required.')

        if self.test_mode:
            print 'Testing only...'
Exemple #5
0
    def get_context_data(self, *args, **kwargs):
        context = super(CollectionVersionsView, self).get_context_data(*args, **kwargs)

        self.get_args()
        api = OclApi(self.request, debug=True)
        results = api.get(self.owner_type, self.owner_id, 'collections', self.collection_id)
        collection = results.json()

        # Load the collection versions
        params = self.request.GET.copy()
        params['verbose'] = 'true'
        params['limit'] = '10'
        searcher = self.get_collection_versions(
            self.owner_type, self.owner_id, self.collection_id,
            search_params=params)
        search_results_paginator = Paginator(range(searcher.num_found), searcher.num_per_page)
        search_results_current_page = search_results_paginator.page(searcher.current_page)

        for collection_version in searcher.search_results:
            if '_ocl_processing' in collection_version and collection_version['_ocl_processing']:
                collection_version['is_processing'] = 'True'

        # Set the context
        context['kwargs'] = self.kwargs
        context['url_params'] = self.request.GET
        context['current_page'] = search_results_current_page
        context['pagination_url'] = self.request.get_full_path()
        context['selected_tab'] = 'Versions'
        context['collection'] = collection
        context['collection_versions'] = searcher.search_results

        return context
Exemple #6
0
    def form_valid(self, form):
        """
            collection input is good, update API backend.
        """
        self.get_args()

        data = form.cleaned_data
        short_code = data.pop('short_code')
        data['id'] = short_code
        if re.compile('^[a-zA-Z0-9\-]+$').match(short_code):
            api = OclApi(self.request, debug=True)
            result = api.post(self.owner_type, self.owner_id, 'collections', **data)
            if not result.status_code == requests.codes.created:
                emsg = result.json().get('detail', None)
                if not emsg:
                    for msg in result.json().get('__all__'):
                        messages.add_message(self.request, messages.ERROR, msg)
                else:
                    messages.add_message(self.request, messages.ERROR, emsg)
                return HttpResponseRedirect(self.request.path)

            messages.add_message(self.request, messages.INFO, _('Collection created'))

            if self.from_org:
                return HttpResponseRedirect(reverse("collection-home",
                                                    kwargs={"org": self.org_id,
                                                            'collection': short_code}))
            else:
                return HttpResponseRedirect(reverse("collection-home",
                                                    kwargs={"user": self.user_id,
                                                            'collection': short_code}))
        else:
            validator_template = ' Short Code \'%s\' is not valid. Allowed characters are : Alphabets(a-z,A-Z), Numbers(0-9) and Hyphen(-) '
            messages.add_message(self.request, messages.ERROR, validator_template % short_code)
            return HttpResponseRedirect(self.request.path)
Exemple #7
0
    def get_context_data(self, *args, **kwargs):
        """Set the context for OCL user
        """

        context = super(UserDetailView, self).get_context_data(*args, **kwargs)

        # Setup API calls
        username = (kwargs["object"].username)
        api = OclApi(self.request, debug=True)

        # Set the limit of records for standard display
        # TODO([email protected]): Create page for each user resource list to handle > than limit
        limit = 20

        ocl_user = api.get('users', username).json()
        ocl_user_orgs = api.get('users', username, 'orgs', params={'limit':limit}).json()
        ocl_user_sources = api.get('users', username, 'sources', params={'limit':limit}).json()
        #ocl_user_collections = api.get('users', username, 'collections',
        #                               params={'limit':limit}).json()

        # Set the context
        context['ocl_user'] = ocl_user
        context['orgs'] = ocl_user_orgs
        context['sources'] = ocl_user_sources
        #context['collections'] = ocl_user_collections

        if self.request.user.username == username:
            context['api_token'] = api.api_key
        return context
Exemple #8
0
    def form_valid(self, form, *args, **kwargs):
        """
        Submits the validated form data
        """

        api = OclApi(self.request, debug=True)

        # Prepare form data for submission, incl. renaming fields as necessary
        org_id = form.cleaned_data.pop('short_name')
        data = {
            'id': org_id,
        }
        data.update(form.cleaned_data)
        print form.cleaned_data
        print data
        result = api.create_org(data)

        # TODO:  Catch exceptions that will be raised by Ocl lib.
        if result.ok:
            messages.add_message(self.request, messages.INFO, _('Organization Added'))
            return redirect(reverse('org-details', kwargs={'org': org_id}))

        # TODO:  Add error messages from API to form.
        else:
            return super(OrganizationNewView, self).form_invalid(self, *args, **kwargs)
Exemple #9
0
 def get_form_class(self):
     """ Trick to load initial form data """
     self.get_args()
     api = OclApi(self.request, debug=True)
     self.source_version = api.get(self.owner_type, self.owner_id, 'sources', self.source_id,
                                   self.source_version_id).json()
     return SourceVersionsEditForm
Exemple #10
0
 def get_org(self):
     """
     Load the organization
     """
     self.org_id = self.kwargs.get('org')
     api = OclApi(self.request, debug=True)
     self.org = api.get('orgs', self.org_id).json()
Exemple #11
0
    def post(self, request, *args, **kwargs):
        self.get_args()
        data = json.loads(request.body)
        api = OclApi(self.request, debug=True)

        if not data['expressions']:
            return HttpResponseBadRequest(json.dumps({
                'errors': ('%s' % EXPRESSIONS_SHOULD_EXIST)
            }))


        result = api.put(
            self.owner_type,
            self.owner_id,
            'collections',
            self.collection_id,
            'references',
            data=data
        )

        results = result.json()
        errors = results if result.status_code == requests.codes.bad else None

        if len(filter(lambda result: result['added'], results)) > 0:
            self.add_version_warning_to_session(data, request, results)

        return HttpResponse(
            json.dumps({
                'update_results': results,
                'success_url': self.get_success_url(),
                'errors': errors,
            }),
            content_type="application/json"
        )
Exemple #12
0
    def test_create_user(self):
        """ Test create OCL user """

#        ocl = OclApi(debug=True)
        ocl = OclApi(admin=True)

        username = '******'
        data = {
            "username":username,
            "name":"Test User995",
            "email":"*****@*****.**",
            'hashed_password':"******",
            "company":"Some Company",
            "location":"Eldoret, Kenya",
            "preferred_locale":"en,sw",
            "extras":{"my-field":"my-value"}
        }

        result = ocl.create_user(data)
        print 'create:', result.status_code
        print result.text
        print len(result.text)
        print result.json()
        if result.status_code == 400:
            # try reactivate
            print 'reactivate?'
            result = ocl.reactivate_user('blah') # username)
            print result.status_code # , result.json()
            print result.json()

        result = ocl.get_user_auth(username, 'aaaaaa')
        print 'get auth:', result.status_code
        if len(result.text) > 0:
            print result.json()
Exemple #13
0
    def form_valid(self, form):
        """ If form data is valid, then update API backend. """
        self.get_args()

        # Submit updated source version description to the API
        data = {
            'description':form.cleaned_data.get('description')
        }
        api = OclApi(self.request, debug=True)
        result = api.update_resource_version(self.owner_type, self.owner_id, self.source_id,
                                             self.source_version_id, 'sources', data)

        # Check if successful
        if result.status_code == requests.codes.ok:
            messages.add_message(self.request, messages.INFO, _('Source version updated'))
            if self.from_org:
                return HttpResponseRedirect(reverse('source-versions',
                                                    kwargs={'org': self.org_id,
                                                            'source': self.source_id}))
            else:
                return HttpResponseRedirect(reverse('source-versions',
                                                    kwargs={'user': self.user_id,
                                                            'source': self.source_id}))
        else:
            emsg = result.text
            messages.add_message(self.request, messages.ERROR, emsg)
            return HttpResponseRedirect(self.request.path)
Exemple #14
0
    def form_valid(self, form):
        """
            collection input is good, update API backend.
        """
        print form.cleaned_data

        self.get_args()

        data = form.cleaned_data
        short_code = data.pop('short_code')
        data['id'] = short_code

        api = OclApi(self.request, debug=True)
        result = api.post(self.owner_type, self.owner_id, 'collections', **data)
        if not result.status_code == requests.codes.created:
            emsg = result.json().get('detail', 'Error')
            messages.add_message(self.request, messages.ERROR, emsg)
            return HttpResponseRedirect(self.request.path)

        messages.add_message(self.request, messages.INFO, _('Collection created'))

        if self.from_org:
            return HttpResponseRedirect(reverse("collection-home",
                                                kwargs={"org": self.org_id,
                                                        'collection': short_code}))
        else:
            return HttpResponseRedirect(reverse("collection-home",
                                                kwargs={"user": self.user_id,
                                                        'collection': short_code}))
Exemple #15
0
    def get_context_data(self, *args, **kwargs):
        """
        Loads the concept details and its version history.
        """

        # Setup the context and args
        context = super(ConceptHistoryView, self).get_context_data(*args, **kwargs)
        self.get_args()

        api = OclApi(self.request, debug=True, facets=True)

        # Load the concept details
        concept = self.get_concept_details(
            self.owner_type, self.owner_id, self.source_id, self.concept_id,
            source_version_id=self.source_version_id, concept_version_id=self.concept_version_id)

        # Load the concept version history
        searcher = self.get_concept_history(
            self.owner_type, self.owner_id, self.source_id, self.concept_id)
        # search_results_paginator = Paginator(range(searcher.num_found), searcher.num_per_page)
        # search_results_current_page = search_results_paginator.page(searcher.current_page)

        if self.request.user.is_authenticated():
            context['all_collections'] = api.get_all_collections_for_user(self.request.user.username)

        # Set the context
        context['kwargs'] = self.kwargs
        context['url_params'] = self.request.GET
        context['selected_tab'] = 'History'
        context['concept'] = concept
        context['concept_versions'] = searcher.search_results
        # context['current_page'] = search_results_current_page
        # context['pagination_url'] = self.request.get_full_path()

        return context
Exemple #16
0
    def add(self):
        """Create new concept
        """

        print self.request_json
        data = {}
        data['id'] = self.request_json.get('concept_id')
        msg = self.clean_concept_id(self.request, data['id'])
        if msg is not None:
            return self.render_bad_request_response({'message': msg})

        data['concept_class'] = self.request_json.get('concept_class')
        data['datatype'] = self.request_json.get('datatype')
        data['external_id'] = self.request_json.get('external_id')

        name = {}
        name['description'] = self.request_json.get('description')
        name['locale'] = self.request_json.get('locale')
        name['preferred'] = self.request_json.get('preferred_locale')
        name['description_type'] = self.request_json.get('description_type')
        names = [name]

        # TEMP for faster testing
        # return self.render_json_response({'message': _('Concept created')})

        api = OclApi(self.request, debug=True)
        result = api.create_concept(
            self.owner_type, self.owner_id, self.source_id, data, names=names)
        if result.status_code != 201:
            logger.warning('Concept create POST failed: %s' % result.content)
            return self.render_bad_request_response({'message': result.content})
        else:
            # TODO: If successful, redirect browser to new concept details page
            return self.render_json_response({'message': _('Concept created!')})
Exemple #17
0
    def get_concept_history(self, owner_type, owner_id, source_id, concept_id,
                            search_params=None):
        """
        Get the concept version history.
        Note that source_version_id and concept_version_id are not applied here.
        """
        # TODO([email protected]): Validate input parameters

        # Perform the search
        searcher = OclSearch(search_type=OclConstants.RESOURCE_NAME_CONCEPT_VERSIONS,
                             search_scope=OclConstants.SEARCH_SCOPE_RESTRICTED,
                             params=search_params)
        api = OclApi(self.request, debug=True, facets=False)
        search_response = api.get(
            owner_type, owner_id, 'sources', source_id,
            'concepts', concept_id, 'versions')
        if search_response.status_code == 404:
            raise Http404
        elif search_response.status_code != 200:
            search_response.raise_for_status()

        # Process the results
        searcher.process_search_results(
            search_type=searcher.search_type, search_response=search_response,
            search_params=search_params)

        return searcher
Exemple #18
0
    def get(self, request, *args, **kwargs):
        """Get concept
        """

        self.get_args()
        data = {}
        print self.request.is_ajax()
        api = OclApi(self.request, debug=True)
        source = api.get(self.owner_type, self.owner_id, 'sources', self.source_id).json()
        data['source'] = source

        if self.concept_id is not None:
            # edit
            concept = api.get(
                self.owner_type, self.owner_id, 'sources', self.source_id,
                'concepts', self.concept_id).json()
            data['concept'] = concept

            if request.is_ajax():
                return self.render_json_response(concept)

        if self.concept_id is None:
            return TemplateResponse(request, 'concepts/concept_create.html', data)
        else:
            return TemplateResponse(request, 'concepts/concept_edit.html', data)
Exemple #19
0
    def form_valid(self, form):
        """
        Submits the form to the API
        """

        print form.cleaned_data
        self.get_args()

        # Submit the new source version
        data = form.cleaned_data
        api = OclApi(self.request, debug=True)
        result = api.create_source_version(self.owner_type, self.owner_id, self.source_id, data)
        if result.status_code == requests.codes.created:
            messages.add_message(self.request, messages.INFO, _('Source version created!'))
            if self.from_org:
                return HttpResponseRedirect(reverse('source-versions',
                                                    kwargs={'org': self.org_id,
                                                            'source': self.source_id}))
            else:
                return HttpResponseRedirect(reverse('source-versions',
                                                    kwargs={'user': self.user_id,
                                                            'source': self.source_id}))
        else:
            error_msg = result.json().get('detail', 'Error')
            messages.add_message(self.request, messages.ERROR, error_msg)
            return HttpResponseRedirect(self.request.path)
Exemple #20
0
    def form_valid(self, form, *args, **kwargs):
        """
        Submits the validated form data
        """

        api = OclApi(self.request, debug=True)

        # Prepare form data for submission, incl. renaming fields as necessary
        org_id = form.cleaned_data.pop('short_name')
        data = {
            'id': org_id,
        }
        data.update(form.cleaned_data)
        if re.compile('^[a-zA-Z0-9\-]+$').match(org_id):
            result = api.create_org(data)
            # TODO:  Catch exceptions that will be raised by Ocl lib.
            if result.ok:
                messages.add_message(self.request, messages.INFO, _('Organization Added'))
                return redirect(reverse('org-details', kwargs={'org': org_id}))

            # TODO:  Add error messages from API to form.
            else:
                messages.add_message(self.request, messages.ERROR, result.json()['mnemonic'])
                return super(OrganizationNewView, self).form_invalid(form)
        else:
            validator_template = ' Short Name \'%s\' is not valid. Allowed characters are : Alphabets(a-z,A-Z), Numbers(0-9) and Hyphen(-) '
            messages.add_message(self.request, messages.ERROR, validator_template % org_id)
            return super(OrganizationNewView, self).form_invalid(form)
Exemple #21
0
    def get_initial(self):
        """ Load initial form data """
        context = super(SourceVersionsNewView, self).get_initial()
        self.get_args()

        # Load the most recent source version
        api = OclApi(self.request, debug=True)
        source_version = None
        if self.from_org:
            source_version = api.get('orgs', self.org_id, 'sources', self.source_id,
                                     'versions', params={'limit':1}).json()
        else:
            source_version = api.get('users', self.user_id, 'sources', self.source_id,
                                     'versions', params={'limit':1}).json()

        data = {
            'request': self.request,
            'from_user': self.from_user,
            'from_org': self.from_org,
            'user_id': self.user_id,
            'org_id': self.org_id,
            'owner_type': self.owner_type,
            'owner_id': self.owner_id,
            'source_id': self.source_id,
            'previous_version': source_version[0]['id'],
            'released': False
        }
        return data
Exemple #22
0
    def get_source_versions(self, owner_type, owner_id, source_id, search_params=None):
        """
        Load source versions from the API and return OclSearch instance with results.
        """
        # TODO([email protected]): Validate the input parameters

        # Perform the search
        searcher = OclSearch(search_type=OclConstants.RESOURCE_NAME_SOURCE_VERSIONS,
                             params=search_params)

        api = OclApi(self.request, debug=True, facets=False)
        search_response = api.get(owner_type, owner_id, 'sources', source_id, 'versions',
            params=searcher.search_params)

        if search_response.status_code == 404:
            raise Http404
        elif search_response.status_code != 200:
            search_response.raise_for_status()

        # Process the results
        searcher.process_search_results(
            search_type=searcher.search_type, search_response=search_response,
            search_params=search_params)

        return searcher
Exemple #23
0
    def get_form_class(self):
        """
        A sneaky way to hook into the generic form processing view, to grep args
        from the URL, retrieve some application data and store them in the view.
        """
        self.get_args()
        self.source_id = self.kwargs.get('source')
        self.concept_id = self.kwargs.get('concept')

        api = OclApi(self.request, debug=True)

        if self.from_org:
            self.source = api.get(
                self.owner_type, self.org_id, 'sources', self.source_id).json()
            self.concept = api.get(
                self.owner_type, self.org_id, 'sources', self.source_id,
                'concepts', self.concept_id).json()
        else:
            self.source = api.get(
                self.owner_type, self.user_id, 'sources', self.source_id).json()
            self.concept = api.get(
                self.owner_type, self.user_id, 'sources', self.source_id,
                'concepts', self.concept_id).json()

        return ConceptEditForm
Exemple #24
0
    def form_valid(self, form):
        """
            Source input is good, update API backend.
        """
        print form.cleaned_data

        self.get_args()

        data = form.cleaned_data

        api = OclApi(self.request, debug=True)
        result = api.update_source(self.owner_type, self.owner_id, self.source_id, data)
        print result
        if len(result.text) > 0:
            print result.json()

        messages.add_message(self.request, messages.INFO, _('Source updated'))

        if self.from_org:
            return HttpResponseRedirect(reverse("source-home",
                                                kwargs={"org": self.org_id,
                                                        'source': self.source_id}))
        else:
            return HttpResponseRedirect(reverse("source-home",
                                                kwargs={"user": self.user_id,
                                                        'source': self.source_id}))
Exemple #25
0
    def post(self, request, *args, **kwargs):
        """ Post the form data """

        self.get_all_args()

        # Convert back to OCLAPI format for extras, the dictionnary
        # key is the attribute name.
        data = {}
        fn = fv = None
        try:
            print 'request json:', self.request_json
            fn = self.request_json.get('extra_name')
            fv = self.request_json.get('extra_value')
            data[fn] = fv
        except KeyError:
            resp = {'message': _('Invalid input')}
            return self.render_bad_request_response(resp)

        api = OclApi(self.request, debug=True)
        if self.is_edit():
            result = api.put(self.owner_type, self.owner_id, 'sources', self.source_id,
                             'concepts', self.concept_id, 'extras', fn,
                             **data)
            msg = _('Extra updated')
        else:
            result = api.put(self.owner_type, self.owner_id, 'sources', self.source_id,
                             'concepts', self.concept_id, 'extras', fn, **data)
            msg = _('Extra added')

        if not result.ok:
            logger.warning('Extra GET failed %s' % result.content)
            return self.render_bad_request_response(result.content)
        else:
            return self.render_json_response({'message': msg})
Exemple #26
0
    def get_collection_data(self, owner_type, owner_id, collection_id, field_name,
                            collection_version_id=None, search_params=None):

        searcher = OclSearch(search_type=field_name, params=search_params)
        api = OclApi(self.request, debug=True, facets=True)

        if collection_version_id:
            search_response = api.get(
                owner_type, owner_id, 'collections', collection_id,
                collection_version_id, field_name,
                params=searcher.search_params)
        else:
            search_response = api.get(
                owner_type, owner_id, 'collections', collection_id, field_name,
                params=searcher.search_params)
        if search_response.status_code == 404:
            raise Http404
        elif search_response.status_code != 200:
            search_response.raise_for_status()

        # Process the results
        searcher.process_search_results(
            search_type=None, search_response=search_response,
            search_params=search_params)

        return searcher
Exemple #27
0
    def form_valid(self, form):
        """ If Source input is valid, then update API backend. """
        self.get_args()

        # Submit updated source data to the API
        data = form.cleaned_data
        api = OclApi(self.request, debug=True)
        result = api.update_source(self.owner_type, self.owner_id, self.source_id, data)
        print result

        if len(result.text) > 0:
            print result.json()

        if result.status_code <= 201:
            messages.add_message(self.request, messages.INFO, _('Source updated'))
            if self.from_org:
                return HttpResponseRedirect(reverse('source-details',
                                                    kwargs={'org': self.org_id,
                                                            'source': self.source_id}))
            else:
                return HttpResponseRedirect(reverse('source-details',
                                                    kwargs={'user': self.user_id,
                                                            'source': self.source_id}))
        else:
            try:
                self.failed_concept_validations = result.json().get('failed_concept_validations', None)
                messages.add_message(self.request, messages.ERROR, _('Some concepts failed validation'))

            except JSONDecodeError as error:
                messages.add_message(self.request, messages.ERROR, _(error))

            return super(SourceEditView, self).form_invalid(form)
Exemple #28
0
 def get_form_class(self):
     """ Trick to load initial data """
     self.get_args()
     api = OclApi(self.request, debug=True)
     self.collection = api.get(self.owner_type, self.owner_id, 'collections',
                               self.collection_id).json()
     return CollectionEditForm
Exemple #29
0
    def get_context_data(self, *args, **kwargs):
        """
        Loads the mapping details.
        """

        # Setup the context and args
        context = super(MappingVersionsView, self).get_context_data(*args, **kwargs)
        self.get_args()

        # Load the mapping details
        if self.mapping_version_id:
            mapping = self.get_mapping_details(
                self.owner_type, self.owner_id, self.source_id, self.mapping_id,
                mapping_version_id=self.mapping_version_id)
        else:
            mapping = self.get_mapping_details(
                self.owner_type, self.owner_id, self.source_id, self.mapping_id)

        searcher = self.get_mapping_versions(
            self.owner_type, self.owner_id, self.source_id, self.mapping_id, search_params={'limit': '0'})

        # Set the context
        context['kwargs'] = self.kwargs
        context['url_params'] = self.request.GET
        context['selected_tab'] = 'History'
        context['mapping'] = mapping
        context['mapping_versions'] = searcher.search_results

        if self.request.user.is_authenticated():
            api = OclApi(self.request, debug=True, facets=True)
            context['all_collections'] = api.get_all_collections_for_user(self.request.user.username)

        return context
Exemple #30
0
    def create_test_user(self):
        """ Creates the OCL sysadmin user """
        from users.models import User
        from allauth.account.models import EmailAddress

        print '==================================='


        print 'Creating Test User'

        if User.objects.filter(username=self.username).count() > 0:
            test_user = User.objects.get(username=self.username)
        else:
            test_user = User.objects.create_user(username=self.username)
        test_user.email = self.username + '@example.com'
        test_user.set_password(self.password)
        test_user.first_name = 'Test'
        test_user.last_name = 'User'
        test_user.save()

        print test_user

        if EmailAddress.objects.filter(user=test_user).count() > 0:
            email = EmailAddress.objects.get(user=test_user)

        else:
            email = EmailAddress.objects.create(user=test_user)

        email.email = test_user.email
        email.verified = True
        email.save()

        ocl = OclApi(admin=True)

        data = {
            "username": test_user.username,
            "name": test_user.first_name + ' ' + test_user.last_name,
            "email": test_user.email,
            'hashed_password': test_user.password,
            "company": self.company_name,
            "location": self.location,
            "preferred_locale": 'en'
        }



        result = ocl.create_user(data)
        print 'Test User Submitted to API: username='******', password='******', status_code=' + str(result.status_code)

        if result.status_code == 201:
            print '==================================='
        elif result.status_code == 400:
            # try reactivate for now, this is very not secure, #TODO
            result = ocl.reactivate_user(test_user.username)
            if result == 204:
                print 'reactivated'
                print '==================================='
        else:
            exit(1)
Exemple #31
0
    def get_initial(self):
        super(CollectionVersionsNewView, self).get_initial()
        self.get_args()

        api = OclApi(self.request, debug=True)
        # collection_version = None
        if self.from_org:
            collection_version = api.get('orgs',
                                         self.org_id,
                                         'collections',
                                         self.collection_id,
                                         'versions',
                                         params={
                                             'limit': 1
                                         }).json()
        else:
            collection_version = api.get('users',
                                         self.user_id,
                                         'collections',
                                         self.collection_id,
                                         'versions',
                                         params={
                                             'limit': 1
                                         }).json()

        data = {
            'request': self.request,
            'from_user': self.from_user,
            'from_org': self.from_org,
            'user_id': self.user_id,
            'org_id': self.org_id,
            'owner_type': self.owner_type,
            'owner_id': self.owner_id,
            'collection_id': self.collection_id,
            'previous_version': collection_version[0]['id'],
            'released': False
        }
        return data
Exemple #32
0
    def get_context_data(self, *args, **kwargs):
        context = super(CollectionVersionsView,
                        self).get_context_data(*args, **kwargs)

        self.get_args()
        api = OclApi(self.request, debug=True)
        results = api.get(self.owner_type, self.owner_id, 'collections',
                          self.collection_id)
        collection = results.json()

        # Load the collection versions
        params = self.request.GET.copy()
        params['verbose'] = 'true'
        params['limit'] = '10'
        searcher = self.get_collection_versions(self.owner_type,
                                                self.owner_id,
                                                self.collection_id,
                                                search_params=params)
        search_results_paginator = Paginator(range(searcher.num_found),
                                             searcher.num_per_page)
        search_results_current_page = search_results_paginator.page(
            searcher.current_page)

        for collection_version in searcher.search_results:
            if '_ocl_processing' in collection_version and collection_version[
                    '_ocl_processing']:
                collection_version['is_processing'] = 'True'

        # Set the context
        context['kwargs'] = self.kwargs
        context['url_params'] = self.request.GET
        context['current_page'] = search_results_current_page
        context['pagination_url'] = self.request.get_full_path()
        context['selected_tab'] = 'Versions'
        context['collection'] = collection
        context['collection_versions'] = searcher.search_results

        return context
Exemple #33
0
    def get_context_data(self, *args, **kwargs):
        """
        Load context data needed for the view
        """
        context = super(SourceVersionsNewView,
                        self).get_context_data(*args, **kwargs)
        self.get_args()

        # Load the source
        api = OclApi(self.request, debug=True)
        source = None
        if self.from_org:
            source = api.get('orgs', self.org_id, 'sources',
                             self.source_id).json()
        else:
            source = api.get('users', self.user_id, 'sources',
                             self.source_id).json()

        # Set the context
        context['kwargs'] = self.kwargs
        context['source'] = source

        return context
Exemple #34
0
    def get_context_data(self, *args, **kwargs):
        print 'CollectionReferencesView get_context_data'
        print 'CollectionReferencesView self: ', self
        print 'CollectionReferencesView args: ', args
        print 'CollectionReferencesView kwargs: ', kwargs

        context = super(CollectionAddReferenceView,
                        self).get_context_data(*args, **kwargs)

        self.get_args()
        api = OclApi(self.request, debug=True)
        results = api.get(self.owner_type, self.owner_id, 'collections',
                          self.collection_id)
        collection = results.json()
        # Set the context
        context['kwargs'] = self.kwargs
        context['url_params'] = self.request.GET
        context['collection'] = collection

        print 'CollectionReferencesView results: ', results.json()
        print 'CollectionReferencesView context: ', context

        return context
Exemple #35
0
 def get_mapping_details(self,
                         owner_type,
                         owner_id,
                         source_id,
                         mapping_id,
                         mapping_version_id=None):
     """
     Load mapping details from the API and return as dictionary.
     """
     # TODO([email protected]): Validate the input parameters
     api = OclApi(self.request, debug=True)
     if mapping_version_id:
         search_response = api.get(owner_type, owner_id, 'sources',
                                   source_id, 'mappings', mapping_id,
                                   mapping_version_id)
     else:
         search_response = api.get(owner_type, owner_id, 'sources',
                                   source_id, 'mappings', mapping_id)
     if search_response.status_code == 404:
         raise Http404
     elif search_response.status_code != 200:
         search_response.raise_for_status()
     return search_response.json()
Exemple #36
0
def _get_org_or_user_sources_list2(request, username):
    sources = []
    oclApi = OclApi(request, debug=True)
    response_user_sources = oclApi.get('users',
                                       username,
                                       'sources',
                                       params={'limit': 20})
    sources.extend([] if response_user_sources.status_code == 404 else
                   [source for source in response_user_sources.json()])
    response_user_orgs = oclApi.get('users',
                                    username,
                                    'orgs',
                                    params={'limit': 20})
    for user_org in response_user_orgs.json():
        response_user_orgs_sources = oclApi.get('orgs',
                                                user_org['id'],
                                                'sources',
                                                params={'limit': 20})
        sources.extend(
            [] if response_user_orgs_sources.status_code ==
            404 else [source for source in response_user_orgs_sources.json()])

    return sources
Exemple #37
0
    def form_valid(self, form, *args, **kwargs):
        """ Use validated form data to delete the collection"""

        self.get_args()

        api = OclApi(self.request, debug=True)
        if self.collection_version_id:
            result = api.delete(self.owner_type, self.owner_id, 'collections',
                                self.collection_id, self.collection_version_id,
                                **kwargs)
        else:
            result = api.delete(self.owner_type, self.owner_id, 'collections',
                                self.collection_id, **kwargs)
        if result.status_code != 204:
            emsg = result.json().get('detail', 'Error')
            messages.add_message(self.request, messages.ERROR, emsg)
            return HttpResponseRedirect(self.request.path)

        else:
            messages.add_message(self.request, messages.INFO,
                                 _('Collection Deleted'))

            return HttpResponseRedirect(self.get_success_url())
Exemple #38
0
    def get_org_collections(self, org_id, search_params=None):

        # Perform the search
        searcher = OclSearch(
            search_type=OclConstants.RESOURCE_NAME_COLLECTIONS,
            search_scope=OclConstants.SEARCH_SCOPE_RESTRICTED,
            params=search_params)
        api = OclApi(self.request, debug=True, facets=True)
        search_response = api.get('orgs',
                                  org_id,
                                  'collections',
                                  params=searcher.search_params)
        if search_response.status_code == 404:
            raise Http404
        elif search_response.status_code != 200:
            search_response.raise_for_status()

        # Process the results
        searcher.process_search_results(search_type=searcher.search_type,
                                        search_response=search_response,
                                        search_params=self.request.GET)

        return searcher
Exemple #39
0
    def get_context_data(self, *args, **kwargs):
        """ Loads the context data for creating a new concept. """

        # Setup the form context
        context = super(ConceptNewView, self).get_context_data(*args, **kwargs)
        self.get_args()

        # Load the source that the new concept will belong to
        api = OclApi(self.request, debug=True)
        source = api.get(self.owner_type, self.owner_id, 'sources',
                         self.source_id).json()

        # TODO: Load list of names types
        # TODO: Load list of description types
        # TODO: Load list of locales
        # TODO: Load list of datatypes
        # TODO: Load list of concept classes

        # Set the context
        context['kwargs'] = self.kwargs
        context['source'] = source

        return context
Exemple #40
0
    def post(self, request, *args, **kwargs):
        self.get_args()
        data = json.loads(request.body)
        api = OclApi(self.request, debug=True)

        if self.adding_single_reference(data) and not data['expressions']:
            return HttpResponseBadRequest(json.dumps({
                'errors': ('%s' % EXPRESSIONS_SHOULD_EXIST)
            }))

        result = api.put(
            self.owner_type,
            self.owner_id,
            'collections',
            self.collection_id,
            'references',
            data=data,
            params={'cascade': request.GET.get('cascade', 'sourcemappings')}
        )

        results = result.json()
        errors = results if result.status_code == requests.codes.bad else None

        added_result_count = len(filter(lambda result: result['added'], results))

        if added_result_count > 0 and self.show_warning(request.GET.get('warning', 'hide')):
            self.add_version_warning_to_session(data, request, results)

        return HttpResponse(
            json.dumps({
                'update_results': results,
                'success_url': self.get_success_url(),
                'errors': errors,
            }),
            content_type="application/json"
        )
Exemple #41
0
def user_created_handler(sender, request, user, **kwargs):
    """
    Signal handler called when a new user is created, so that we can create
    a corresponding user at the backend.
    """
    print 'user created handler %s' % user.username
    ocl = OclApi(admin=True, debug=True)
    data = {
        'username': user.username,
        'email': user.email,
        'hashed_password': user.password,
        'name': '%s %s' % (user.first_name, user.last_name),  # not great
    }
    result = ocl.create_user(data)
    print result.status_code
    if result.status_code == 201:
        # result.json() has data
        pass

    elif result.status_code == 400:
        # try reactivate for now, this is very not secure, #TODO
        result = ocl.reactivate_user(user.username)
        if result == 204:
            print 'reactivated'
Exemple #42
0
    def get_mapping_versions(self, owner_type, owner_id, source_id, mapping_id, search_params=None):
        """
        Load mapping details from the API and return as dictionary.
        """
        # TODO([email protected]): Validate the input parameters

        searcher = OclSearch(search_type=OclConstants.RESOURCE_NAME_MAPPING_VERSIONS,
                             search_scope=OclConstants.SEARCH_SCOPE_RESTRICTED,
                             params=search_params)

        api = OclApi(self.request, debug=True, facets=False)
        search_response = api.get(
            owner_type, owner_id, 'sources', source_id, 'mappings', mapping_id, 'versions',
            params=searcher.search_params)

        if search_response.status_code == 404:
            raise Http404
        elif search_response.status_code != 200:
            search_response.raise_for_status()
        searcher.process_search_results(
            search_type=searcher.search_type, search_response=search_response,
            search_params=search_params)

        return searcher
Exemple #43
0
    def get_context_data(self, *args, **kwargs):
        """
        Return org details
        """
        context = super(SourceNewView, self).get_context_data(*args, **kwargs)

        self.get_args()

        api = OclApi(self.request, debug=True)
        org = ocl_user = None

        if self.from_org:
            org = api.get('orgs', self.org_id).json()
        else:
            ocl_user = api.get('users', self.user_id).json()

        # Set the context
        context['kwargs'] = self.kwargs
        context['org'] = org
        context['ocl_user'] = ocl_user
        context['from_user'] = self.from_user
        context['from_org'] = self.from_org

        return context
Exemple #44
0
    def form_valid(self, form):
        """
        Return whether source input is valid and then update API backend.
        """
        self.get_args()

        data = form.cleaned_data
        short_code = data.pop('short_name')
        data['short_code'] = short_code
        data['id'] = short_code
        data['name'] = short_code
        if re.compile('^[a-zA-Z0-9\-]+$').match(short_code):
            api = OclApi(self.request, debug=True)
            result = api.create_source(self.owner_type, self.owner_id, data)
            if result.status_code == requests.codes.created:
                messages.add_message(self.request, messages.INFO, _('Source created'))
                if self.from_org:
                    return HttpResponseRedirect(reverse("source-home",
                                                        kwargs={"org": self.org_id,
                                                                'source': short_code}))
                else:
                    return HttpResponseRedirect(reverse("source-home",
                                                        kwargs={"user": self.user_id,
                                                                'source': short_code}))
            else:
                emsg = result.json().get('detail', None)
                if not emsg:
                    for msg in result.json().get('__all__'):
                        messages.add_message(self.request, messages.ERROR, msg)
                else:
                    messages.add_message(self.request, messages.ERROR, emsg)
                return HttpResponseRedirect(self.request.path)
        else:
            validator_template = ' Short Code \'%s\' is not valid. Allowed characters are : Alphabets(a-z,A-Z), Numbers(0-9) and Hyphen(-) '
            messages.add_message(self.request, messages.ERROR, validator_template % short_code)
            return HttpResponseRedirect(self.request.path)
Exemple #45
0
    def edit(self):
        """Edit concept
        """

        data = {}
        data['concept_class'] = self.request_json.get('concept_class')
        data['datatype'] = self.request_json.get('datatype')
        data['external_id'] = self.request_json.get('external_id')
        data['update_comment'] = self.request_json.get('update_comment')
        print data
        # TEMP for faster testing
        # return self.render_json_response({'message': _('Concept updated')})

        api = OclApi(self.request, debug=True)
        result = api.update_concept(self.owner_type, self.owner_id,
                                    self.source_id, self.concept_id, data)
        if result.status_code != requests.codes.ok:
            emsg = result.json().get('detail', 'Error')
            logger.warning('Concept update POST failed %s' % emsg)
            return self.render_bad_request_response({'message': emsg})
        else:
            # TODO: If successful, redirect browser to concept details page
            return self.render_json_response(
                {'message': _('Concept updated!')})
    def test_create_user(self):
        """ Test create OCL user """

        #        ocl = OclApi(debug=True)
        ocl = OclApi(admin=True)

        username = '******'
        data = {
            "username": username,
            "name": "Test User995",
            "email": "*****@*****.**",
            'hashed_password': "******",
            "company": "Some Company",
            "location": "Eldoret, Kenya",
            "preferred_locale": "en,sw",
            "extras": {
                "my-field": "my-value"
            }
        }

        result = ocl.create_user(data)
        print 'create:', result.status_code
        print result.text
        print len(result.text)
        print result.json()
        if result.status_code == 400:
            # try reactivate
            print 'reactivate?'
            result = ocl.reactivate_user('blah')  # username)
            print result.status_code  # , result.json()
            print result.json()

        result = ocl.get_user_auth(username, 'aaaaaa')
        print 'get auth:', result.status_code
        if len(result.text) > 0:
            print result.json()
Exemple #47
0
    def form_valid(self, form, *args, **kwargs):
        """
        Validates the form data and submits if valid
        """
        print args
        print kwargs
        self.get_org()
        new_username = form.cleaned_data.pop('member_username')

        api = OclApi(self.request, debug=True)

        result = api.put('orgs', self.org['id'], 'members', new_username)

        # TODO:  Catch exceptions that will be raised by
        # Ocl lib.
        if result.status_code == 204:
            messages.add_message(self.request, messages.INFO,
                                 _('Member Added'))
            return redirect(
                reverse('org-details', kwargs={'org': self.org['id']}))
        elif result.status_code == 404:
            messages.add_message(self.request, messages.ERROR,
                                 'Member doesn\'t exist')
        return super(OrganizationMemberAddView, self).form_invalid(form)
Exemple #48
0
    def get_context_data(self, *args, **kwargs):
        """Set the context for OCL user
        """

        context = super(UserDetailView, self).get_context_data(*args, **kwargs)

        # Setup API calls
        username = (kwargs["object"].username)
        api = OclApi(self.request, debug=True)

        # Set the limit of records for standard display
        # TODO([email protected]): Create page for each user resource list to handle > than limit
        limit = 20

        ocl_user = api.get('users', username).json()
        ocl_user_orgs = api.get('users',
                                username,
                                'orgs',
                                params={
                                    'limit': limit
                                }).json()
        ocl_user_sources = api.get('users',
                                   username,
                                   'sources',
                                   params={
                                       'limit': limit
                                   }).json()
        ocl_user_collections = api.get('users',
                                       username,
                                       'collections',
                                       params={
                                           'limit': limit
                                       }).json()

        # Set the selected tab
        default_tab = 'repositories'
        if 'tab' in self.request.GET:
            selected_tab = self.request.GET.get('tab').lower()
            if selected_tab not in ('repositories', 'organizations'):
                selected_tab = default_tab
        else:
            selected_tab = default_tab

        # Set the context
        context['ocl_user'] = ocl_user
        context['orgs'] = ocl_user_orgs
        context['sources'] = ocl_user_sources
        context['collections'] = ocl_user_collections
        context['selected_tab'] = selected_tab

        if self.request.user.username == username:
            context['api_token'] = api.api_key
        return context
    def test_concept_create(self):
        """ Test concept create
            Need to login first with hard coded password.
         """
        ocl = OclApi(admin=True, debug=True)

        user = User.objects.create_user(username=self.username)
        user.password = self.password
        user.save()

        result = ocl.get_user_auth(user.username, user.password)
        print 'get auth:', result.status_code
        if len(result.text) > 0:
            print result.json()

        request = FakeRequest()
        ocl.save_auth_token(request, result.json())

        ocl = OclApi(request, debug=True)
        org_id = 'TESTORG1'
        source_id = 'S1'

        data = {
            'id': 'CTEST001',
            'concept_class': 'Diagnosis',
            'datatype': 'String',
        }
        names = [{
            'name': 'concept name',
            'locale': 'en',
        }]
        result = ocl.create_concept('orgs',
                                    org_id,
                                    source_id,
                                    base_data=data,
                                    names=names)
        print result.status_code
        print result
        if len(result.text) > 0:
            print result.json()
Exemple #50
0
def user_logged_in_handler(sender, request, user, **kwargs):
    """
    Signal handler called when a user logged into the web app.
    We need to retrieve the backend auth token for subsequent access.
    The token is saved in the session.
    """
    ocl = OclApi()
    if 'password' in request.POST:
        #Login with not hashed password by default, because some users have been created prior to api and web using to the same hashing
        result = ocl.get_user_auth(user.username, request.POST['password'],
                                   False)
    else:
        result = ocl.get_user_auth(user.username, user.password)
    if result.status_code == 200:
        ocl.save_auth_token(request, result.json())
Exemple #51
0
    def render(self, context):
        # Init state storage
        try:
            obj = self.obj_var.resolve(context)
        except template.VariableDoesNotExist:
            return ''

        user = context['user']
        has_access = False
        if not user.is_authenticated():
            # User must be authenticated if checking for access
            has_access = False

        elif obj.get('type') == 'Organization':  # pylint: disable=E1101
            # If org, authenticated user can access only if they are a member of the org
            api = OclApi(context['request'], debug=True)
            results = api.get('orgs', obj.get('id'), 'members', user.username)  # pylint: disable=E1101
            if results.status_code == 204:
                has_access = True
            print 'ACCESS Check on org:', results.status_code

        elif obj.get('type') == 'User':  # pylint: disable=E1101
            # If user, authenticated user can access only if they are that user
            if user.username == obj.get('username'):  # pylint: disable=E1101
                has_access = True

        elif obj.get('owner_type') == 'Organization':  # pylint: disable=E1101
            # If resource is owned by an org, then user must be a member of the org
            api = OclApi(context['request'], debug=True)
            results = api.get('orgs', obj.get('owner'), 'members',
                              user.username)  # pylint: disable=E1101
            if results.status_code == 204:
                has_access = True
            print 'ACCESS Check on ' + obj.get(
                'type') + ':', results.status_code  # pylint: disable=E1101

        elif obj.get('owner_type') == 'User':  # pylint: disable=E1101
            # If resource is owned by a user, then authenticated user must own the resource
            if obj.get('owner') == user.username:  # pylint: disable=E1101
                has_access = True

        if has_access:
            return self.nodelist_true.render(context)
        else:
            return self.nodelist_false.render(context)
Exemple #52
0
    def post(self, request, *args, **kwargs):
        """ Post the data """

        self.get_all_args()
        data = {}
        try:
            print 'request json:', self.request_json
            for n in self.field_names:
                # Skipping over fields that are not given -- exception is never thrown now...??
                v = self.request_json.get(n, None)
                if v is not None:
                    data[n] = v
        except KeyError:
            resp = {u"message": _('Invalid input')}
            return self.render_bad_request_response(resp)

        api = OclApi(self.request, debug=True)
        if self.is_edit():
            if self.item_name == 'mappings':
                result = api.put(
                    self.owner_type, self.owner_id, 'sources', self.source_id,
                    self.item_name, **data)
            else:
                result = api.put(
                    self.owner_type, self.owner_id, 'sources', self.source_id,
                    'concepts', self.concept_id, self.item_name, self.item_id, **data)
            msg = _('updated')
        else:
            if self.item_name == 'mappings':
                result = api.post(
                    self.owner_type, self.owner_id, 'sources', self.source_id,
                    self.item_name, **data)
            else:
                result = api.post(
                    self.owner_type, self.owner_id, 'sources', self.source_id,
                    'concepts', self.concept_id, self.item_name, **data)
            msg = _('added')

        if not result.ok:
            logger.warning('Update failed %s' % result.content)
            return self.render_bad_request_response(result.content)

        return self.render_json_response({'message': msg})
Exemple #53
0
    def get(self, request, *args, **kwargs):
        """Return a ... number !
        """
        key = self.kwargs['key']
        cnt = 0

        api = OclApi(self.request, debug=True)
        if key == 'concepts':
            response = api.head('concepts')
            cnt = response.headers.get('num_found')
        if key == 'users':
            response = api.head('users')
            cnt = response.headers.get('num_found')
        if key == 'orgs':
            response = api.head('orgs')
            cnt = response.headers.get('num_found')
        if key == 'sources':
            response = api.head('sources')
            cnt = response.headers.get('num_found')

        return HttpResponse(cnt)
Exemple #54
0
    def get_form_class(self):
        """
        A sneaky way to hook into the generic form processing view, to grep args
        from the URL, retrieve some application data and store them in the view.
        """
        self.get_args()
        self.source_id = self.kwargs.get('source')
        self.mapping_id = self.kwargs.get('mapping')

        api = OclApi(self.request, debug=True)
        if self.from_org:
            self.source = api.get(self.owner_type, self.org_id, 'sources',
                                  self.source_id).json()
            self.mapping = api.get(self.owner_type, self.org_id, 'sources',
                                   self.source_id, 'mappings',
                                   self.mapping_id).json()
        else:
            self.source = api.get(self.owner_type, self.user_id, 'sources',
                                  self.source_id).json()
            self.mapping = api.get(self.owner_type, self.user_id, 'sources',
                                   self.source_id, 'mappings',
                                   self.mapping_id).json()

        return MappingEditForm
Exemple #55
0
"""
OCL Web Core Functionality
"""
# import requests
import logging

from django.core import cache
from django.http import HttpResponse
from django.views.generic.edit import View
from django.utils.translation import ugettext as _
from braces.views import JsonRequestResponseMixin

from libs.ocl import OclApi

logger = logging.getLogger('oclweb')
api = OclApi()


class UserOrOrgMixin(object):
    """
    Figure out if a view is called from a user or an organization "owner".
    """
    def get_args(self):
        """
        Helper method to determine resource ownership (user/org) and resource identifiers.

        Method will set the following based on the passed kwargs:
        :param self.from_user: sets to true/false depending on source type
        :param self.from_org: sets to true/false depending on source type
        :param self.user_id: sets to username if source is from user
        :param self.org_id: sets to org id if source is from org
Exemple #56
0
    def get_context_data(self, *args, **kwargs):
        """ Set context for OCL global search_type """

        context = super(GlobalSearchView,
                        self).get_context_data(*args, **kwargs)

        # Perform the primary search via the API

        search_string = self.request.GET.get('q', '')
        SearchStringFormatter.add_wildcard(self.request)

        searcher = OclSearch(params=self.request.GET)

        api = OclApi(self.request,
                     debug=True,
                     facets=OclConstants.resource_has_facets(
                         searcher.search_type))

        search_response = api.get(searcher.search_type,
                                  params=searcher.search_params)
        if search_response.status_code == 404:
            raise Http404
        elif search_response.status_code != 200:
            search_response.raise_for_status()

        # Process the primary search results
        searcher.process_search_results(search_type=searcher.search_type,
                                        search_response=search_response,
                                        search_params=self.request.GET)

        # Setup paginator for primary search
        search_paginator = Paginator(range(searcher.num_found),
                                     searcher.num_per_page)
        search_current_page = search_paginator.page(searcher.current_page)

        # Set context for primary search
        context['results'] = searcher.search_results
        context['page'] = search_current_page
        context['pagination_url'] = self.request.get_full_path()
        context['search_type'] = searcher.search_type
        context['search_type_name'] = OclConstants.resource_display_name(
            searcher.search_type)
        context['search_sort_options'] = searcher.get_sort_options()
        context['search_sort'] = searcher.get_sort()
        context['search_filters'] = searcher.search_filter_list
        context['search_query'] = search_string

        # Build URL params for navigating to other resources
        other_resource_search_params = {}
        for param in OclSearch.TRANSFERRABLE_SEARCH_PARAMS:
            if param in self.request.GET:
                other_resource_search_params[param] = self.request.GET.get(
                    param)

        context['other_resource_search_params'] = ''

        # Following code breaks for unicode characters -- couldn't figure out why this code is here -- Sny/Anshu
        if other_resource_search_params:
            context['other_resource_search_params'] = (
                '&' + urlencode(other_resource_search_params))
        #
        # Perform the counter searches for the other resources
        resource_count = {}
        for resource_type in OclConstants.RESOURCE_TYPE_INFO:
            if resource_type == searcher.search_type:
                # Primary search has already been performed, so just set value from above
                resource_count[searcher.search_type] = searcher.num_found
            else:
                # Get resource count applying transferrable search criteria
                count_response = api.head(resource_type,
                                          params=other_resource_search_params)
                if 'num_found' in count_response.headers:
                    resource_count[resource_type] = int(
                        count_response.headers['num_found'])
                else:
                    resource_count[resource_type] = 0
        context['resource_count'] = resource_count

        # Set debug variables
        context['url_params'] = self.request.GET
        context['search_params'] = searcher.search_params
        context['search_response_headers'] = search_response.headers
        context['search_facets_json'] = searcher.search_facets
        context['search_filters_debug'] = str(searcher.search_filter_list)

        # Set to remove closing form tag in nav.html -- retire in the future
        context['extend_nav_form'] = True

        return context
Exemple #57
0
    def get_context_data(self, *args, **kwargs):
        """ Set context for OCL global search """

        context = super(GlobalSearchView, self).get_context_data(*args, **kwargs)

        # Perform the primary search via the API

        original_search_string = self.request.GET.get('q', '')
        SearchStringFormatter.add_wildcard(self.request)

        searcher = OclSearch(params=self.request.GET)

        api = OclApi(
            self.request, debug=True,
            facets=OclConstants.resource_has_facets(searcher.search_type))

        search_response = api.get(searcher.search_type, params=searcher.search_params)
        if search_response.status_code == 404:
            raise Http404
        elif search_response.status_code != 200:
            search_response.raise_for_status()

        # Process the primary search results
        searcher.process_search_results(
            search_type=searcher.search_type,
            search_response=search_response,
            search_params=self.request.GET)

        # Setup paginator for primary search
        search_paginator = Paginator(range(searcher.num_found), searcher.num_per_page)
        search_current_page = search_paginator.page(searcher.current_page)

        # Set context for primary search
        context['results'] = searcher.search_results
        context['page'] = search_current_page
        context['pagination_url'] = self.request.get_full_path()
        context['search_type'] = searcher.search_type
        context['search_type_name'] = OclConstants.resource_display_name(searcher.search_type)
        context['search_sort_option_defs'] = searcher.get_sort_option_definitions()
        context['search_sort'] = searcher.get_sort()
        context['search_filters'] = searcher.search_filter_list
        context['search_query'] = original_search_string
        context['hide_nav_search'] = True

        # Build URL params for navigating to other resources
        other_resource_search_params = {}
        for param in OclSearch.TRANSFERRABLE_SEARCH_PARAMS:
            if param in self.request.GET:
                if param == 'q':
                    other_resource_search_params[param] = original_search_string
                else:
                    other_resource_search_params[param] = self.request.GET.get(param)

        # This code encodes the search parameters into a single URL-encoded string
        #    so that it can easily be appended onto URL links on the search page
        context['other_resource_search_params'] = ''
        if other_resource_search_params:
            context['other_resource_search_params'] = (
                '&' + urlencode(other_resource_search_params))

        # Perform the counter searches for the other resources
        resource_count = {}
        for resource_type in OclConstants.RESOURCE_TYPE_INFO:
            if resource_type == searcher.search_type:
                # Primary search has already been performed, so just set value from above
                resource_count[searcher.search_type] = searcher.num_found
            elif OclConstants.RESOURCE_TYPE_INFO[resource_type]['show_on_global_search']:
                # Get resource count applying transferrable search criteria
                count_response = api.head(resource_type, params=other_resource_search_params)
                if 'num_found' in count_response.headers:
                    resource_count[resource_type] = int(count_response.headers['num_found'])
                else:
                    resource_count[resource_type] = 0
        context['resource_count'] = resource_count

        # Set debug variables
        context['url_params'] = self.request.GET
        context['search_params'] = searcher.search_params
        context['search_response_headers'] = search_response.headers
        context['search_facets_json'] = searcher.search_facets
        context['search_filters_debug'] = str(searcher.search_filter_list)

        return context
Exemple #58
0
    def form_valid(self, form, *args, **kwargs):
        """ Submits the validated form data using the API: new mapping """
        # TODO: move regex validation to form
        user_concept_format = r'^/users/(' + OclConstants.NAMESPACE_PATTERN + ')/sources/(' + OclConstants.NAMESPACE_PATTERN + ')/concepts/(' + OclConstants.CONCEPT_ID_PATTERN + ')/$'
        org_concept_format = r'^/orgs/(' + OclConstants.ORG_PATTERN + ')/sources/(' + OclConstants.NAMESPACE_PATTERN + ')/concepts/(' + OclConstants.CONCEPT_ID_PATTERN + ')/$'
        # Prepare the data form submission, incl. renaming fields as needed
        mapping_destination = form.cleaned_data.get('is_internal_or_external')
        base_data = {
            'from_concept_url': form.cleaned_data.get('from_concept_url'),
            'map_type': form.cleaned_data.get('map_type', ''),
            'external_id': form.cleaned_data.get('external_id', '')
        }
        if not (re.compile(user_concept_format).match(
                base_data['from_concept_url'])
                or re.compile(org_concept_format).match(
                    base_data['from_concept_url'])):
            emsg = 'Invalid format of "From Concept URL" \'%s\'. valid url format is /[orgs or users]/[:org or :user]/sources/:source/concepts/:concept/' % \
                   base_data['from_concept_url']
            messages.add_message(self.request, messages.ERROR, emsg)
            return super(MappingNewView, self).form_invalid(form)

        if mapping_destination == 'Internal':
            base_data['to_concept_url'] = form.cleaned_data.get(
                'internal_to_concept_url')
            if not (re.compile(user_concept_format).match(
                    base_data['to_concept_url'])
                    or re.compile(org_concept_format).match(
                        base_data['to_concept_url'])):
                emsg = 'Invalid format of "To Concept URL" \'%s\'. valid url format is /[orgs or users]/[:org or :user]/sources/:source/concepts/:concept/' % base_data[
                    'to_concept_url']
                messages.add_message(self.request, messages.ERROR, emsg)
                return super(MappingNewView, self).form_invalid(form)

        elif mapping_destination == 'External':
            base_data['to_source_url'] = form.cleaned_data.get(
                'external_to_source_url')
            base_data['to_concept_code'] = form.cleaned_data.get(
                'external_to_concept_code')
            base_data['to_concept_name'] = form.cleaned_data.get(
                'external_to_concept_name')

        # Create the mapping
        api = OclApi(self.request, debug=True)
        result = api.create_mapping(self.owner_type, self.owner_id,
                                    self.source_id, base_data)
        if result.ok:
            new_mapping_id = result.json()['id']
            messages.add_message(self.request, messages.INFO,
                                 _('Mapping created.'))
            if self.from_org:
                return redirect(
                    reverse('mapping-home',
                            kwargs={
                                'org': self.owner_id,
                                'source': self.source_id,
                                'mapping': new_mapping_id
                            }))
            else:
                return redirect(
                    reverse('mapping-home',
                            kwargs={
                                'user': self.owner_id,
                                'source': self.source_id,
                                'mapping': new_mapping_id
                            }))
        else:
            emsg = result.json().get('errors', 'Error: ' + result.content)
            messages.add_message(self.request, messages.ERROR, emsg)
            logger.warning('Mapping create POST failed: %s' % result.content)
            return super(MappingNewView, self).form_invalid(form)
Exemple #59
0
    def get_context_data(self, *args, **kwargs):
        context = super(CollectionReferencesView,
                        self).get_context_data(*args, **kwargs)
        self.get_args()

        api = OclApi(self.request, debug=True)
        results = api.get(self.owner_type, self.owner_id, 'collections',
                          self.collection_id)
        collection = results.json()

        params = self.request.GET.copy()
        params['verbose'] = 'true'
        params['limit'] = '10'

        versions = self.get_collection_versions(self.owner_type,
                                                self.owner_id,
                                                self.collection_id,
                                                search_params={'limit': '0'})

        # Load the references in this collection, applying search parameters
        original_search_string = self.request.GET.get('q', '')
        # TODO: SearchStringFormatter.add_wildcard(self.request)
        params = self.request.GET.copy()
        params['verbose'] = 'true'
        params['limit'] = '10'
        searcher = self.get_collection_data(
            self.owner_type,
            self.owner_id,
            self.collection_id,
            'references',
            collection_version_id=self.collection_version_id,
            search_params=params)
        search_results_paginator = Paginator(range(searcher.num_found),
                                             searcher.num_per_page)
        search_results_current_page = search_results_paginator.page(
            searcher.current_page)

        add_reference_warning = self.request.session.get(
            'add_reference_warning', None)
        add_reference_success = self.request.session.get(
            'add_reference_success', None)
        self.request.session['add_reference_warning'] = None
        self.request.session['add_reference_success'] = None

        # Build URL params
        transferrable_search_params = {}
        for param in OclSearch.TRANSFERRABLE_SEARCH_PARAMS:
            if param in self.request.GET:
                if param == 'q':
                    transferrable_search_params[param] = original_search_string
                else:
                    transferrable_search_params[param] = self.request.GET.get(
                        param)

        # Encode the search parameters into a single URL-encoded string so that it can
        #   easily be appended onto URL links on the search page
        context['transferrable_search_params'] = ''
        if transferrable_search_params:
            context['transferrable_search_params'] = urlencode(
                transferrable_search_params)

        # Set the context
        context['kwargs'] = self.kwargs
        context['url_params'] = self.request.GET
        context['selected_tab'] = 'References'
        context['collection'] = collection
        context['collection_version'] = self.collection_version_id
        context['collection_versions'] = versions.search_results
        context['selected_tab'] = 'References'
        context['results'] = searcher.search_results
        context['current_page'] = search_results_current_page
        context['pagination_url'] = self.request.get_full_path()
        context[
            'search_sort_option_defs'] = searcher.get_sort_option_definitions(
            )
        context['search_sort'] = self.request.GET.get('search_sort', 'ASC')
        context['search_query'] = searcher.get_query()
        context['search_filters'] = searcher.search_filter_list

        # Set debug variables
        context['url_params'] = self.request.GET
        context['search_params'] = searcher.search_params
        context['search_facets_json'] = searcher.search_facets
        context['search_filters_debug'] = str(searcher.search_filter_list)
        context['collection_versions'] = versions.search_results

        context['warning'] = add_reference_warning
        context['success'] = add_reference_success

        return context
Exemple #60
0
    def get_context_data(self, *args, **kwargs):
        """ Loads the concepts that are in the collection. """

        # Setup the context and args
        context = super(CollectionConceptsView,
                        self).get_context_data(*args, **kwargs)
        self.get_args()
        api = OclApi(self.request, debug=True)
        results = api.get(self.owner_type, self.owner_id, 'collections',
                          self.collection_id)
        collection = results.json()
        params = self.request.GET.copy()
        params['verbose'] = 'true'
        params['limit'] = '10'

        # to fetch all , set limit to 0
        versions = self.get_collection_versions(self.owner_type,
                                                self.owner_id,
                                                self.collection_id,
                                                search_params={'limit': '0'})

        # Load the concepts in this collection, applying search parameters
        original_search_string = self.request.GET.get('q', '')
        # TODO: SearchStringFormatter.add_wildcard(self.request)
        params = self.request.GET.copy()
        params['verbose'] = 'true'
        params['limit'] = '10'
        searcher = self.get_collection_data(
            self.owner_type,
            self.owner_id,
            self.collection_id,
            OclConstants.RESOURCE_NAME_CONCEPTS,
            collection_version_id=self.collection_version_id,
            search_params=params)

        search_results_paginator = Paginator(range(searcher.num_found),
                                             searcher.num_per_page)
        search_results_current_page = search_results_paginator.page(
            searcher.current_page)

        # Build URL params
        transferrable_search_params = {}
        for param in OclSearch.TRANSFERRABLE_SEARCH_PARAMS:
            if param in self.request.GET:
                if param == 'q':
                    transferrable_search_params[param] = original_search_string
                else:
                    transferrable_search_params[param] = self.request.GET.get(
                        param)

        # Encode the search parameters into a single URL-encoded string so that it can
        #   easily be appended onto URL links on the search page
        context['transferrable_search_params'] = ''
        if transferrable_search_params:
            context['transferrable_search_params'] = urlencode(
                transferrable_search_params)

        # Set the context
        context['kwargs'] = self.kwargs
        context['selected_tab'] = 'Concepts'
        context['collection'] = collection
        context['collection_version'] = self.collection_version_id
        context['collection_versions'] = versions.search_results
        context['selected_tab'] = 'Concepts'
        context['results'] = searcher.search_results
        context['current_page'] = search_results_current_page
        context['pagination_url'] = self.request.get_full_path()
        context['search_sort_option_defs'] = searcher.get_sort_options()
        context['search_sort'] = searcher.get_sort()
        context['search_query'] = self.search_string if hasattr(
            self, 'search_string') else ''
        context['search_filters'] = searcher.search_filter_list
        context['search_sort_options'] = searcher.get_sort_options()
        context['search_sort'] = searcher.get_sort()
        context['search_facets_json'] = searcher.search_facets
        context['search_filters_debug'] = str(searcher.search_filter_list)
        context['collection_versions'] = versions.search_results

        return context