Esempio n. 1
0
 def wrapper(request, *args, **kwargs):
     resourceid = kwargs["resourceid"] if "resourceid" in kwargs else None
     if user_can_read_resources(request.user, resourceid=resourceid):
         return function(request, *args, **kwargs)
     else:
         raise PermissionDenied
     return function(request, *args, **kwargs)
Esempio n. 2
0
    def test_user_cannot_view_without_permission(self):
        """
        Tests if a user is allowed to view a resource with implicit permissions and explicit permissions, but
        not without explicit permission if a permission other than 'view_resourceinstance' is assigned.

        """

        implicit_permission = user_can_read_resources(
            self.user, self.resource_instance_id)
        resource = ResourceInstance.objects.get(
            resourceinstanceid=self.resource_instance_id)
        assign_perm("change_resourceinstance", self.group, resource)
        can_access_without_view_permission = user_can_read_resources(
            self.user, self.resource_instance_id)
        assign_perm("view_resourceinstance", self.group, resource)
        can_access_with_view_permission = user_can_read_resources(
            self.user, self.resource_instance_id)
        self.assertTrue(implicit_permission is True
                        and can_access_without_view_permission is False
                        and can_access_with_view_permission is True)
Esempio n. 3
0
    def get(self, request, resourceid=None, slug=None, graphid=None):
        if user_can_read_resources(user=request.user):
            allowed_formats = ['json', 'json-ld']
            format = request.GET.get('format', 'json-ld')
            if format not in allowed_formats:
                return JSONResponse(
                    status=406, reason='incorrect format specified, only %s formats allowed' % allowed_formats
                    )
            try:
                indent = int(request.GET.get('indent', None))
            except Exception:
                indent = None

            if resourceid:
                if format == 'json-ld':
                    try:
                        exporter = ResourceExporter(format=format)
                        output = exporter.writer.write_resources(
                            resourceinstanceids=[resourceid], indent=indent, user=request.user)
                        out = output[0]['outputfile'].getvalue()
                    except models.ResourceInstance.DoesNotExist:
                        logger.exception(
                            _("The specified resource '{0}' does not exist. JSON-LD export failed.".format(
                                resourceid
                                ))
                            )
                        return JSONResponse(status=404)
                elif format == 'json':
                    out = Resource.objects.get(pk=resourceid)
                    out.load_tiles()
            else:
                #
                # The following commented code would be what you would use if you wanted to use the rdflib module,
                # the problem with using this is that items in the "ldp:contains" array don't maintain a consistent order
                #

                # archesproject = Namespace(settings.ARCHES_NAMESPACE_FOR_DATA_EXPORT)
                # ldp = Namespace('https://www.w3.org/ns/ldp/')

                # g = Graph()
                # g.bind('archesproject', archesproject, False)
                # g.add((archesproject['resources'], RDF.type, ldp['BasicContainer']))

                # base_url = "%s%s" % (settings.ARCHES_NAMESPACE_FOR_DATA_EXPORT, reverse('resources',args=['']).lstrip('/'))
                # for resourceid in list(Resource.objects.values_list('pk', flat=True).order_by('pk')[:10]):
                #     g.add((archesproject['resources'], ldp['contains'], URIRef("%s%s") % (base_url, resourceid) ))

                # value = g.serialize(format='nt')
                # out = from_rdf(str(value), options={format:'application/nquads'})
                # framing = {
                #     "@omitDefault": True
                # }

                # out = frame(out, framing)
                # context = {
                #     "@context": {
                #         'ldp': 'https://www.w3.org/ns/ldp/',
                #         'arches': settings.ARCHES_NAMESPACE_FOR_DATA_EXPORT
                #     }
                # }
                # out = compact(out, context, options={'skipExpansion':False, 'compactArrays': False})

                page_size = settings.API_MAX_PAGE_SIZE
                try:
                    page = int(request.GET.get('page', None))
                except Exception:
                    page = 1

                start = ((page - 1) * page_size)
                end = start + page_size

                base_url = "%s%s" % (settings.ARCHES_NAMESPACE_FOR_DATA_EXPORT,
                                     reverse('resources', args=['']).lstrip('/'))
                out = {
                    "@context": "https://www.w3.org/ns/ldp/",
                    "@id": "",
                    "@type": "ldp:BasicContainer",
                    # Here we actually mean the name
                    #"label": str(model.name),
                    "ldp:contains": ["%s%s" % (base_url, resourceid) for resourceid in list(Resource.objects.values_list('pk', flat=True).
                                                                                            exclude(pk=settings.SYSTEM_SETTINGS_RESOURCE_ID).order_by('pk')[start:end])]
                }

            return JSONResponse(out, indent=indent)
        else:
            return JSONResponse(status=403)