Exemplo n.º 1
0
    def get(self, request, filetype, project_slug,
            contractor_slug, filename):
        project = get_object_or_404(Project, slug=project_slug)
        if contractor_slug == 'x':
            contractor = None
        else:
            contractor = get_object_or_404(Contractor, slug=contractor_slug)

        if not has_access(request.user, project, contractor):
            return HttpResponseForbidden()

        if filetype == 'reports':
            directory = directories.reports_dir(project, contractor)
        elif filetype == 'results':
            directory = directories.reports_dir(project, contractor)
        elif filetype == 'locations':
            logger.debug("Looking for {0}".format(filename))
            directory = directories.location_shapefile_dir(project, contractor)
            logger.debug("Trying in {0}".format(directory))
            for path in directories.all_files_in(directory):
                logger.debug("... {0}".format(path))
                if os.path.basename(path) == filename:
                    directory = os.path.dirname(path)
                    break
            else:
                raise http.Http404()
        elif filetype == 'organization':
            directory = directories.organization_files_dir(
                project.organization)
        elif filetype == 'hydrovakken':
            directory = directories.hydrovakken_dir(project)
            for path in directories.all_files_in(directory):
                if os.path.basename(path) == filename:
                    directory = os.path.dirname(path)
                    break
            else:
                raise http.Http404()
        elif filetype == 'contractor_hydrovakken':
            directory = directories.shapefile_dir(project, contractor)
            for path in directories.all_files_in(directory):
                if os.path.basename(path) == filename:
                    directory = os.path.dirname(path)
                    break
            else:
                raise http.Http404()
        else:
            return HttpResponseForbidden()

        path = os.path.join(directory, filename)

        if not os.path.exists(path):
            raise http.Http404()

        return serve(request, path, '/')