コード例 #1
0
ファイル: formapi.py プロジェクト: icmurray/ckanext-dgu
    def harvest_source_create(self):
        try:
            # Check user authorization.
            user = self._get_required_authorization_credentials()
            am_authz = self.authorizer.is_sysadmin(user.name) # simple for now
            if not am_authz:
                self._abort_not_authorized('User %r not authorized for harvesting' % user.name)

            # Get the fieldset.
            fieldset = harvest_source_form.get_harvest_source_fieldset()
            if request.method == 'GET':
                # Render the fields.
                fieldset_html = fieldset.render()
                return self._finish_ok(fieldset_html, content_type='html')
            if request.method == 'POST':
                # Read request.
                try:
                    request_data = self._get_request_data()
                except ValueError, error:
                    self._abort_bad_request('Extracting request data: %r' % error.args)
                try:
                    form_data = request_data['form_data']
                    user_id = request_data['user_id']
                    publisher_id = request_data['publisher_id']
                except KeyError, error:
                    self._abort_bad_request()
                if isinstance(form_data, list):
                    form_data = dict(form_data)
                # Bind form data to fieldset.
                try:
                    form_data['HarvestSource--url'] = form_data.get('HarvestSource--url', '').strip()
                    bound_fieldset = fieldset.bind(HarvestSource, data=form_data, session=model.Session)
                except Exception, error:
                    # Todo: Replace 'Exception' with bind error.
                    self._abort_bad_request()
コード例 #2
0
 def test_form_bound_to_new_object(self):
     source = HarvestSource(url=u'http://localhost/', description=u'My source', type=u'Gemini')
     fs = form.get_harvest_source_fieldset()
     fs = fs.bind(source)
     text = fs.render()
     assert 'url' in text
     assert 'http://localhost/' in text
     assert 'description' in text
     assert 'My source' in text
コード例 #3
0
 def test_form_invalidate_new_object_no_type(self):
     fs = form.get_harvest_source_fieldset()
     register = HarvestSource
     data = {
         'HarvestSource--url': u'htp:', 
         'HarvestSource--type': u'',
         'HarvestSource--description': u'My source'
     }
     fs = fs.bind(register, data=data)
     # Test bound_fields.validate().
     fs.validate()
     assert fs.errors
コード例 #4
0
 def test_form_bound_to_existing_object(self):
     source = HarvestSource(url=u'http://localhost/', description=u'My source', type=u'Gemini')
     model.Session.add(source)
     model.Session.commit()
     model.Session.remove()
     fs = form.get_harvest_source_fieldset()
     fs = fs.bind(source)
     text = fs.render()
     assert 'url' in text
     assert 'http://localhost/' in text
     assert 'description' in text
     assert 'My source' in text
コード例 #5
0
ファイル: formapi.py プロジェクト: fone4u/ckanext-dgu
    def harvest_source_edit(self, id):
        try:
            # Check user authorization.
            user = self._get_required_authorization_credentials()
            am_authz = self.authorizer.is_sysadmin(user.name)  # simple for now
            if not am_authz:
                self._abort_not_authorized(
                    'User %r not authorized for harvesting' % user.name)

            # Find the entity.
            entity = self._get_harvest_source(id)
            if entity is None:
                self._abort_not_found()

            # Get the fieldset.
            fieldset = harvest_source_form.get_harvest_source_fieldset()
            if request.method == 'GET':
                # Bind entity to fieldset.
                bound_fieldset = fieldset.bind(entity)
                # Render the fields.
                fieldset_html = bound_fieldset.render()
                return self._finish_ok(fieldset_html, content_type='html')
            if request.method == 'POST':
                # Read request.
                try:
                    request_data = self._get_request_data()
                except ValueError, error:
                    self._abort_bad_request('Extracting request data: %r' %
                                            error.args)
                try:
                    form_data = request_data['form_data']
                    user_id = request_data['user_id']
                    publisher_id = request_data['publisher_id']
                except KeyError, error:
                    self._abort_bad_request()
                if isinstance(form_data, list):
                    form_data = dict(form_data)
                # Bind form data to fieldset.
                try:
                    form_data['HarvestSource--url'] = form_data.get(
                        'HarvestSource--url', '').strip()
                    form_data['HarvestSource--type'] = form_data.get(
                        'HarvestSource--type', '').strip()
                    form_data['HarvestSource--description'] = form_data.get(
                        'HarvestSource--description', '').strip()
                    bound_fieldset = fieldset.bind(entity, data=form_data)
                    # Todo: Replace 'Exception' with bind error.
                except Exception, error:
                    self._abort_bad_request()
コード例 #6
0
 def test_form_validate_new_object_and_sync(self):
     assert not HarvestSource.get(u'http://localhost/', None, 'url')
     fs = form.get_harvest_source_fieldset()
     register = HarvestSource
     data = {
         'HarvestSource--url': u'http://localhost/', 
         'HarvestSource--type': u'Gemini',
         'HarvestSource--description': u'My source'
     }
     fs = fs.bind(register, data=data, session=model.Session)
     # Test bound_fields.validate().
     fs.validate()
     assert not fs.errors
     # Test bound_fields.sync().
     fs.sync()
     model.Session.commit()
     source = HarvestSource.get(u'http://localhost/', None, 'url')
     assert source.id
コード例 #7
0
 def test_form_raw(self):
     fs = form.get_harvest_source_fieldset()
     text = fs.render()
     assert 'url' in text
     assert 'type' in text
     assert 'description' in text