Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature - Add ATOM Feed and refactor controller #10

Open
5 tasks
mjanez opened this issue Aug 24, 2023 · 0 comments
Open
5 tasks

Feature - Add ATOM Feed and refactor controller #10

mjanez opened this issue Aug 24, 2023 · 0 comments
Assignees
Labels
documentation Improvements or additions to documentation enhancement New feature or request

Comments

@mjanez
Copy link
Owner

mjanez commented Aug 24, 2023

  • Refactor all controllers to controller.py:

  • ATOM Feed

    • Add Feed plugin:
      def before_map(self, map):
          with SubMapper(map, controller='ckanext.scheming_dcat.controller:SchemingDcatFeedController') as m:
              m.connect('/feeds/organization/{id}.atom', action='organization')
          map.connect(
              'general', '/feeds/dataset/{pkg_id}.atom',
              controller='ckanext.scheming_dcat.controller:SchemingDcatFeedController',
              action='dataset',
          )
          return map
    • Add FeedController for ATOM Feed:
      class SchemingDcatFeedController(FeedController):
          def general(self):
              data_dict, params = self._parse_url_params()
              data_dict['q'] = '*:*'
      
              item_count, results = _package_search(data_dict)
      
              navigation_urls = self._navigation_urls(params,
                                                      item_count=item_count,
                                                      limit=data_dict['rows'],
                                                      controller='feed',
                                                      action='general')
      
              feed_url = self._feed_url(params,
                                        controller='feed',
                                        action='general')
      
              alternate_url = self._alternate_url(params)
      
              return self.output_feed(
                  results,
                  feed_title=_(u'Open Government Dataset Feed'),
                  feed_description='',
                  feed_link=alternate_url,
                  feed_guid=_create_atom_id(
                      u'/feeds/dataset.atom'),
                  feed_url=feed_url,
                  navigation_urls=navigation_urls
              )
      
          def dataset(self, pkg_id):
              try:
                  context = {'model': model, 'session': model.Session,
                             'user': c.user, 'auth_user_obj': c.userobj}
                  get_action('package_show')(context,
                                              {'id': pkg_id})
              except NotFound:
                  abort(404, _('Dataset not found'))
      
              data_dict, params = self._parse_url_params()
      
              data_dict['fq'] = '{0}:"{1}"'.format('id', pkg_id)
      
              item_count, results = _package_search(data_dict)
      
              navigation_urls = self._navigation_urls(params,
                                                      item_count=item_count,
                                                      limit=data_dict['rows'],
                                                      controller='feed',
                                                      action='dataset',
                                                      id=pkg_id)
      
              feed_url = self._feed_url(params,
                                        controller='feed',
                                        action='dataset',
                                        id=pkg_id)
      
              alternate_url = self._alternate_url(params, id=pkg_id)
      
              return self.output_feed(
                  results,
                  feed_title=_(u'Open Government Dataset Feed'),
                  feed_description='',
                  feed_link=alternate_url,
                  feed_guid=_create_atom_id(
                      u'/feeds/dataset/{0}.atom'.format(pkg_id)),
                  feed_url=feed_url,
                  navigation_urls=navigation_urls
              )
      
          def output_feed(self, results, feed_title, feed_description,
                          feed_link, feed_url, navigation_urls, feed_guid):
      
              author_name = config.get('ckan.feeds.author_name', '').strip() or \
                  config.get('ckan.site_id', '').strip()
              author_link = config.get('ckan.feeds.author_link', '').strip() or \
                  config.get('ckan.site_url', '').strip()
      
              feed = _FixedAtom1Feed(
                  title=feed_title,
                  link=feed_link,
                  description=feed_description,
                  language=u'en',
                  author_name=author_name,
                  author_link=author_link,
                  feed_guid=feed_guid,
                  feed_url=feed_url,
                  previous_page=navigation_urls['previous'],
                  next_page=navigation_urls['next'],
                  first_page=navigation_urls['first'],
                  last_page=navigation_urls['last'],
              )
      
              for pkg in results:
                  feed.add_item(
                      title= h.get_translated(pkg, 'title'),
                      link=h.url_for(controller='package', action='read', id=pkg['id']),
                      description= h.get_translated(pkg, 'notes'),
                      updated=date_str_to_datetime(pkg.get('metadata_modified')),
                      published=date_str_to_datetime(pkg.get('metadata_created')),
                      unique_id=_create_atom_id(u'/dataset/%s' % pkg['id']),
                      author_name=pkg.get('author', ''),
                      author_email=pkg.get('author_email', ''),
                      categories=''.join(e['value']
                                         for e in pkg.get('extras', [])
                                         if e['key'] == lx('keywords')).split(','),
                      enclosure=webhelpers.feedgenerator.Enclosure(
                          self.base_url + url(str(
                              '/api/action/package_show?id=%s' % pkg['name'])),
                          unicode(len(json.dumps(pkg))),   # TODO fix this
                          u'application/json')
                  )
              response.content_type = feed.mime_type
              return feed.writeString('utf-8')
@mjanez mjanez added documentation Improvements or additions to documentation enhancement New feature or request labels Aug 24, 2023
@mjanez mjanez self-assigned this Aug 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant