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, '/')
def _shapefile_files(self): for contractor in self.project.contractor_set.all(): if has_access(self.user, self.project, contractor): for path in directories.all_files_in( directories.shapefile_dir( self.project, contractor)): yield { 'type': 'Ingevulde hydrovakken shapefile '.format( contractor.organization.name), 'filename': os.path.basename(path), 'size': directories.human_size(path), 'url': self._make_url( 'contractor_hydrovakken', self.project, contractor, path) }
def process_file(self, path): ext = os.path.splitext(path)[1].lower() if not ext in self.exts: msg = "Allowed file types: %s." % self.exts return json_response({'error': {'details': msg}}) # TODO: perform a sanity check before copying # the shapefile to its permanent location? # The destination directory. dst = directories.shapefile_dir( self.contractor.project, self.contractor) # Copy the report. shutil.copy(path, dst) return json_response({})