Esempio n. 1
0
    'description': fields.String,
    'created_at': fields.DateTime,
    'metrics': fields.Raw,
}


class OrganizationField(fields.Raw):
    def format(self, organization):
        return {
            'id': str(organization.id),
            'uri': url_for('api.organization', slug=organization.slug, _external=True),
            'page': url_for('organizations.show', org=organization, _external=True),
        }


class OrganizationListAPI(ModelListAPI):
    model = Organization
    fields = org_fields
    form = OrganizationForm
    search_adapter = OrganizationSearch


class OrganizationAPI(ModelAPI):
    model = Organization
    fields = org_fields
    form = OrganizationForm


api.add_resource(OrganizationListAPI, '/organizations/', endpoint=b'api.organizations')
api.add_resource(OrganizationAPI, '/organizations/<slug>', endpoint=b'api.organization')
Esempio n. 2
0

class ResourceAPI(API):
    def put(self, slug, rid):
        dataset = Dataset.objects.get_or_404(slug=slug)
        resource = get_by(dataset.resources, 'id', UUID(rid))
        if not resource:
            abort(404)
        form = ResourceForm(request.form, instance=resource, csrf_enabled=False)
        if not form.validate():
            return {'errors': form.errors}, 400
        form.populate_obj(resource)
        dataset.save()
        return marshal(resource, resource_fields)

    def delete(self, slug, rid):
        dataset = Dataset.objects.get_or_404(slug=slug)
        resource = get_by(dataset.resources, 'id', UUID(rid))
        if not resource:
            abort(404)
        dataset.resources.remove(resource)
        dataset.save()
        return '', 204


api.add_resource(DatasetListAPI, '/datasets/', endpoint=b'api.datasets')
api.add_resource(DatasetAPI, '/datasets/<string:slug>', endpoint=b'api.dataset')
api.add_resource(DatasetFeaturedAPI, '/datasets/<string:slug>/featured', endpoint=b'api.dataset_featured')
api.add_resource(ResourcesAPI, '/datasets/<string:slug>/resources', endpoint=b'api.resources')
api.add_resource(ResourceAPI, '/datasets/<string:slug>/resources/<string:rid>', endpoint=b'api.resource')
Esempio n. 3
0
            return marshal(current_user, user_fields)

    def delete(self, slug):
        if not current_user.is_authenticated():
            abort(401)
        obj = self.get_or_404(slug=slug)
        key = obj.__class__.__name__.lower()
        starred = getattr(current_user, 'starred_{0}s'.format(key))
        starred.remove(obj)
        current_user.save()
        obj.on_unstar.send(obj)
        return marshal(current_user, user_fields), 204


class StarredDatasetsAPI(StarredModelAPI):
    model = Dataset


class StarredReusesAPI(StarredModelAPI):
    model = Reuse


class StarredOrganizationsAPI(StarredModelAPI):
    model = Organization


api.add_resource(MeAPI, '/me/', endpoint=b'api.me')
api.add_resource(StarredDatasetsAPI, '/me/starred_datasets/<string:slug>', endpoint=b'api.starred_datasets')
api.add_resource(StarredReusesAPI, '/me/starred_reuses/<string:slug>', endpoint=b'api.starred_reuses')
api.add_resource(StarredOrganizationsAPI, '/me/starred_organizations/<string:slug>', endpoint=b'api.starred_organizations')
Esempio n. 4
0
    form = ReuseForm
    fields = reuse_fields
    search_adapter = ReuseSearch


class ReuseAPI(ModelAPI):
    model = Reuse
    form = ReuseForm
    fields = reuse_fields


class ReuseFeaturedAPI(SingleObjectAPI, API):
    model = Reuse

    def post(self, slug):
        reuse = self.get_or_404(slug=slug)
        reuse.featured = True
        reuse.save()
        return marshal(reuse, reuse_fields)

    def delete(self, slug):
        reuse = self.get_or_404(slug=slug)
        reuse.featured = False
        reuse.save()
        return marshal(reuse, reuse_fields)


api.add_resource(ReuseListAPI, '/reuses/', endpoint=b'api.reuses')
api.add_resource(ReuseAPI, '/reuses/<string:slug>', endpoint=b'api.reuse')
api.add_resource(ReuseFeaturedAPI, '/reuses/<string:slug>/featured', endpoint=b'api.reuse_featured')
Esempio n. 5
0
def parse_period(value):
    if '+' in value:
        start, end = value.split('+')
        return start, end
    else:
        return value


class MetricsAPI(API):
    def get(self, id, period=None, names=None):
        try:
            object_id = ObjectId(id)
        except:
            object_id = id
        queryset = Metrics.objects(object_id=object_id).order_by('-date')
        if period:
            period = parse_period(period)
            if isinstance(period, basestring):
                result = queryset(date=period).first_or_404()
            else:
                result = list(queryset(date__gte=period[0], date__lte=period[1]))
        else:
            result = queryset.first_or_404()
        return marshal(result, metrics_fields)


api.add_resource(MetricsAPI, '/metrics/<id>/', endpoint=b'api.metrics')
api.add_resource(MetricsAPI, '/metrics/<id>/<period>/', endpoint=b'api.metrics_period')
api.add_resource(MetricsAPI, '/metrics/<id>/<period>/<names>/', endpoint=b'api.metrics_by_name')
Esempio n. 6
0
class SuggestUsersAPI(API):
    def get(self):
        q = request.args.get('q', '')
        size = request.args.get('size', DEFAULT_SIZE)
        result = es.suggest(index=es.index_name, body={
            'users': {
                'text': q,
                'completion': {
                    'field': 'user_suggest',
                    'size': size
                }
            }
        })
        return [
            {
                'id': opt['payload']['id'],
                'fullname': opt['payload']['fullname'],
                'avatar_url': opt['payload']['avatar_url'],
                'score': opt['score'],
            }
            for opt in result['users'][0]['options']
        ]


api.add_resource(SuggestTagsAPI, '/suggest/tags', endpoint=b'api.suggest_tags')
api.add_resource(SuggestOrgsAPI, '/suggest/organizations', endpoint=b'api.suggest_orgs')
api.add_resource(SuggestDatasetsAPI, '/suggest/datasets', endpoint=b'api.suggest_datasets')
api.add_resource(SuggestReusesAPI, '/suggest/reuses', endpoint=b'api.suggest_reuses')
api.add_resource(SuggestFormatsAPI, '/suggest/formats', endpoint=b'api.suggest_formats')
api.add_resource(SuggestUsersAPI, '/suggest/users', endpoint=b'api.suggest_users')