2014-09-02 17:52:55 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Copyright (c) 2014 CoNWeT Lab., Universidad Politécnica de Madrid
|
|
|
|
|
|
|
|
# This file is part of CKAN Private Dataset Extension.
|
|
|
|
|
|
|
|
# CKAN Private Dataset Extension is free software: you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
|
|
|
# CKAN Private Dataset Extension is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with CKAN Private Dataset Extension. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2014-07-14 11:05:56 +02:00
|
|
|
import ckan.plugins.toolkit as tk
|
2014-06-24 17:21:06 +02:00
|
|
|
import re
|
|
|
|
|
|
|
|
from urlparse import urlparse
|
|
|
|
from ckan.common import request
|
|
|
|
|
|
|
|
|
|
|
|
class FiWareNotificationParser(object):
|
|
|
|
|
2014-07-14 11:05:56 +02:00
|
|
|
def parse_notification(self, request_data):
|
2014-06-24 17:21:06 +02:00
|
|
|
my_host = request.host
|
|
|
|
|
2014-07-15 12:16:20 +02:00
|
|
|
fields = ['customer_name', 'resources']
|
2014-07-14 11:50:59 +02:00
|
|
|
|
|
|
|
for field in fields:
|
|
|
|
if not field in request_data:
|
|
|
|
raise tk.ValidationError({'message': '%s not found in the request' % field})
|
|
|
|
|
2014-06-24 17:21:06 +02:00
|
|
|
# Parse the body
|
2014-07-14 11:05:56 +02:00
|
|
|
resources = request_data['resources']
|
|
|
|
user_name = request_data['customer_name']
|
2014-06-24 17:21:06 +02:00
|
|
|
datasets = []
|
|
|
|
|
2014-07-14 11:50:59 +02:00
|
|
|
if not isinstance(user_name, basestring):
|
|
|
|
raise tk.ValidationError({'message': 'Invalid customer_name format'})
|
|
|
|
|
2014-07-15 12:16:20 +02:00
|
|
|
if not isinstance(resources, list):
|
|
|
|
raise tk.ValidationError({'message': 'Invalid resources format'})
|
|
|
|
|
2014-06-24 17:21:06 +02:00
|
|
|
for resource in resources:
|
2014-07-14 11:50:59 +02:00
|
|
|
if isinstance(resource, dict) and 'url' in resource:
|
2014-06-24 17:21:06 +02:00
|
|
|
parsed_url = urlparse(resource['url'])
|
2014-06-25 17:13:44 +02:00
|
|
|
dataset_name = re.findall('^/dataset/([^/]+).*$', parsed_url.path)
|
2014-06-24 17:21:06 +02:00
|
|
|
|
|
|
|
if len(dataset_name) == 1:
|
|
|
|
if parsed_url.netloc == my_host:
|
|
|
|
datasets.append(dataset_name[0])
|
|
|
|
else:
|
2014-07-14 11:05:56 +02:00
|
|
|
raise tk.ValidationError({'message': 'Dataset %s is associated with the CKAN instance located at %s'
|
|
|
|
% (dataset_name[0], parsed_url.netloc)})
|
2014-07-14 11:50:59 +02:00
|
|
|
else:
|
|
|
|
raise tk.ValidationError({'message': 'Invalid resource format'})
|
2014-06-24 17:21:06 +02:00
|
|
|
|
2016-07-07 14:36:12 +02:00
|
|
|
return {'users_datasets': [{'user': user_name, 'datasets': datasets}]}
|
|
|
|
|