def lithology_geojson(request): realtime = request.GET.get('realtime') in ('True', 'true') if realtime: sw_long = request.query_params.get('sw_long') sw_lat = request.query_params.get('sw_lat') ne_long = request.query_params.get('ne_long') ne_lat = request.query_params.get('ne_lat') bounds = None bounds_sql = '' if sw_long and sw_lat and ne_long and ne_lat: bounds_sql = 'and geom @ ST_MakeEnvelope(%s, %s, %s, %s, 4326)' bounds = (sw_long, sw_lat, ne_long, ne_lat) iterator = GeoJSONIterator(LITHOLOGY_SQL.format(bounds=bounds_sql), LITHOLOGY_CHUNK_SIZE, connection.cursor(), bounds) response = StreamingHttpResponse((item for item in iterator), content_type='application/json') response[ 'Content-Disposition'] = 'attachment; filename="lithology.json"' return response else: # Generating spatial data realtime is much too slow, # so we have to redirect to a pre-generated instance. url = 'https://{}/{}/{}'.format( get_env_variable('S3_HOST'), get_env_variable('S3_WELL_EXPORT_BUCKET'), 'api/v1/gis/lithology.json') return HttpResponseRedirect(url)
def handle(self, *args, **options): User = get_user_model() TEST_USER = get_env_variable('GWELLS_API_TEST_USER', None, strict=True) TEST_PASSWD = get_env_variable('GWELLS_API_TEST_PASSWORD', None, strict=True) if not TEST_USER or not TEST_PASSWD: raise CommandError( 'Please set GWELLS_API_TEST_USER and GWELLS_API_TEST_PASSWORD') elif TEST_USER and TEST_PASSWD and not User.objects.filter( username=TEST_USER).exists(): user = User.objects.create_user(TEST_USER, '*****@*****.**', TEST_PASSWD) user.is_staff = True user.save() self.stdout.write( self.style.SUCCESS('Successfully created test user "%s"' % user.username)) elif User.objects.filter(username=TEST_USER).exists(): self.stdout.write( self.style.SUCCESS( 'Test user already exists (username: "******").' % TEST_USER))
def get(self, request, **kwargs): host = get_env_variable('S3_HOST') use_secure = int(get_env_variable('S3_USE_SECURE', 1)) minioClient = Minio(host, access_key=get_env_variable( 'S3_PUBLIC_ACCESS_KEY'), secret_key=get_env_variable( 'S3_PUBLIC_SECRET_KEY'), secure=use_secure) objects = minioClient.list_objects( get_env_variable('S3_WELL_EXPORT_BUCKET'), 'export/') urls = list( map( lambda document: { 'url': 'https://{}/{}/{}'.format(host, quote( document.bucket_name), quote(document.object_name)), 'name': document.object_name, 'size': document.size, 'last_modified': document.last_modified, 'description': self.create_description(document.object_name) }, objects) ) return Response(urls)
def handle(self, *args, **options): User = get_user_model() ADMIN_USER = get_env_variable('DJANGO_ADMIN_USER', None, strict=True) ADMIN_PASSWD = get_env_variable('DJANGO_ADMIN_PASSWORD', None, strict=True) if not ADMIN_USER or not ADMIN_PASSWD: raise CommandError( 'Please set DJANGO_ADMIN_USER and DJANGO_ADMIN_PASSWORD') elif ADMIN_USER and ADMIN_PASSWD and not User.objects.filter( username=ADMIN_USER).exists(): user = User.objects.create_superuser(ADMIN_USER, '*****@*****.**', ADMIN_PASSWD) user.save() self.stdout.write( self.style.SUCCESS('Successfully created Admin user "%s"' % user.username)) elif User.objects.filter(username=ADMIN_USER).exists(): self.stdout.write( self.style.SUCCESS( 'Admin user already exists (username: "******").' % ADMIN_USER))
def aquifer_geojson_v2(request, **kwargs): realtime = request.GET.get('realtime') in ('True', 'true') if realtime: sw_long = request.query_params.get('sw_long') sw_lat = request.query_params.get('sw_lat') ne_long = request.query_params.get('ne_long') ne_lat = request.query_params.get('ne_lat') if sw_long and sw_lat and ne_long and ne_lat: bounds_sql = 'and geom @ ST_Transform(ST_MakeEnvelope(%s, %s, %s, %s, 4326), 3005)' bounds = (sw_long, sw_lat, ne_long, ne_lat) else: bounds = None bounds_sql = '' iterator = GeoJSONIterator(AQUIFERS_SQL_V2.format(bounds=bounds_sql), AQUIFER_CHUNK_SIZE, connection.cursor(), bounds) response = StreamingHttpResponse((item for item in iterator), content_type='application/json') response[ 'Content-Disposition'] = 'attachment; filename="aquifers.json"' return response else: # TODO: Update export_databc command to upload a v2 version of the aquifers JSON: # https://apps.nrs.gov.bc.ca/int/jira/browse/WATER-1049 # Generating spatial data realtime is much too slow, # so we have to redirect to a pre-generated instance. url = 'https://{}/{}/{}'.format( get_env_variable('S3_HOST'), get_env_variable('S3_WELL_EXPORT_BUCKET'), 'api/v1/gis/aquifers.json') return HttpResponseRedirect(url)
def get(self, request): config = { "enable_data_entry": get_env_variable("ENABLE_DATA_ENTRY") == "True", "enable_google_analytics": get_env_variable("ENABLE_GOOGLE_ANALYTICS") == "True", "enable_aquifers_search": get_env_variable("ENABLE_AQUIFERS_SEARCH") == "True", "sso_idp_hint": get_env_variable("SSO_IDP_HINT", "idir") } return Response(config)
def get(self, request): config = { "enable_data_entry": get_env_variable("ENABLE_DATA_ENTRY") == "True", "enable_google_analytics": get_env_variable("ENABLE_GOOGLE_ANALYTICS") == "True" } return Response(config)
def get(self, request): config = { "enable_aquifers_search": get_env_variable("ENABLE_AQUIFERS_SEARCH") == "True", "sso_idp_hint": get_env_variable("SSO_IDP_HINT", "idir") } return Response(config)
def __init__(self): self.access_key = "" self.secret_key = "" self.host = get_env_variable('S3_HOST', strict=True) self.minio_client = Minio(self.host, access_key=self.access_key, secret_key=self.secret_key, secure=True) self.top_bucket = get_env_variable('S3_ROOT_BUCKET', strict=True)
def create_private_client(self): self.private_access_key = get_env_variable('MINIO_ACCESS_KEY') self.private_secret_key = get_env_variable('MINIO_SECRET_KEY') self.private_host = get_env_variable('S3_PRIVATE_HOST') self.private_bucket = get_env_variable('S3_PRIVATE_BUCKET') return Minio(self.private_host, access_key=self.private_access_key, secret_key=self.private_secret_key, secure=True)
def get(self, request): config = { "realm": get_env_variable("SSO_REALM"), "auth-server-url": get_env_variable("SSO_AUTH_HOST"), "ssl-required": "external", "resource": get_env_variable("SSO_CLIENT"), "public-client": True, "confidential-port": int(get_env_variable("SSO_PORT", "0")) } return Response(config)
def upload_files(self, zip_filename, spreadsheet_filename): minioClient = Minio(get_env_variable('S3_HOST'), access_key=get_env_variable('S3_PUBLIC_ACCESS_KEY'), secret_key=get_env_variable('S3_PUBLIC_SECRET_KEY'), secure=True) for filename in (zip_filename, spreadsheet_filename): logger.info('uploading {}'.format(filename)) with open(filename, 'rb') as file_data: file_stat = os.stat(filename) # Do we need to remove the existing files 1st? minioClient.put_object(get_env_variable('S3_WELL_EXPORT_BUCKET'), filename, file_data, file_stat.st_size)
def delete(self, request, person_guid): person = get_object_or_404(self.queryset, pk=person_guid) client = MinioClient( request=request, disable_private=False) is_private = False bucket_name = get_env_variable("S3_REGISTRANT_BUCKET") if request.GET.get("private") == "true": is_private = True bucket_name = get_env_variable("S3_PRIVATE_REGISTRANT_BUCKET") object_name = request.GET.get("filename") client.delete_document(object_name, bucket_name=bucket_name, private=is_private) return HttpResponse(status=204)
def upload_files(self, zip_filename, spreadsheet_filename): is_secure = get_env_variable('S3_USE_SECURE', '1', warn=False) is '1' minioClient = Minio( get_env_variable('S3_HOST'), access_key=get_env_variable('S3_PUBLIC_ACCESS_KEY'), secret_key=get_env_variable('S3_PUBLIC_SECRET_KEY'), secure=is_secure) for filename in (zip_filename, spreadsheet_filename): logger.info('uploading {}'.format(filename)) with open(filename, 'rb') as file_data: file_stat = os.stat(filename) # Do we need to remove the existing files 1st? target = 'export/{}'.format(filename) minioClient.put_object( get_env_variable('S3_WELL_EXPORT_BUCKET'), target, file_data, file_stat.st_size)
def delete(self, request, aquifer_id, **kwargs): client = MinioClient(request=request, disable_private=False) is_private = False bucket_name = get_env_variable("S3_AQUIFER_BUCKET") if request.GET.get("private") == "true": is_private = True bucket_name = get_env_variable("S3_PRIVATE_AQUIFER_BUCKET") object_name = client.get_bucket_folder( int(aquifer_id), "aquifer") + "/" + request.GET.get("filename") client.delete_document(object_name, bucket_name=bucket_name, private=is_private) return HttpResponse(status=204)
def __init__(self, request=None, disable_public=False, disable_private=False): self.request = request if not disable_public: self.public_host = get_env_variable('S3_HOST', strict=True) self.public_bucket = get_env_variable('S3_ROOT_BUCKET', strict=True) self.public_aquifers_bucket = get_env_variable( 'S3_AQUIFER_BUCKET', default_value="aquifer-docs") self.public_drillers_bucket = get_env_variable( 'S3_REGISTRANT_BUCKET', default_value="driller-docs") self.public_access_key = get_env_variable('S3_PUBLIC_ACCESS_KEY', warn=False) self.public_secret_key = get_env_variable('S3_PUBLIC_SECRET_KEY', warn=False) self.use_secure = int( get_env_variable('S3_USE_SECURE', 1, warn=False)) self.public_client = Minio(self.public_host, access_key=self.public_access_key, secret_key=self.public_secret_key, secure=self.use_secure) if not disable_private: self.private_client = self.create_private_client() self.disable_private = disable_private
def upload_files(self, files): """Upload files to S3 bucket.""" is_secure = get_env_variable('S3_USE_SECURE', '1', warn=False) is '1' minioClient = Minio( get_env_variable('S3_HOST'), access_key=get_env_variable('S3_PUBLIC_ACCESS_KEY'), secret_key=get_env_variable('S3_PUBLIC_SECRET_KEY'), secure=is_secure) for filename in files: logger.info('uploading {}'.format(filename)) with open(filename, 'rb') as file_data: file_stat = os.stat(filename) target = 'api/v1/gis/{}'.format(filename) bucket = get_env_variable('S3_WELL_EXPORT_BUCKET') logger.debug('uploading {} to {}/{}'.format( filename, bucket, target)) minioClient.put_object(bucket, target, file_data, file_stat.st_size)
def get(self, request, aquifer_id, **kwargs): client = MinioClient(request=request, disable_private=False) object_name = request.GET.get("filename") filename = client.format_object_name(object_name, int(aquifer_id), "aquifer") bucket_name = get_env_variable("S3_AQUIFER_BUCKET") is_private = False if request.GET.get("private") == "true": is_private = True bucket_name = get_env_variable("S3_PRIVATE_AQUIFER_BUCKET") url = client.get_presigned_put_url(filename, bucket_name=bucket_name, private=is_private) return JsonResponse({"object_name": object_name, "url": url})
def get(self, request, submission_id): submission = get_object_or_404(self.queryset, pk=submission_id) client = MinioClient( request=request, disable_private=False) object_name = request.GET.get("filename") filename = client.format_object_name(object_name, int(submission.well.well_tag_number), "well") bucket_name = get_env_variable("S3_ROOT_BUCKET") is_private = False if request.GET.get("private") == "true": is_private = True bucket_name = get_env_variable("S3_PRIVATE_ROOT_BUCKET") url = client.get_presigned_put_url( filename, bucket_name=bucket_name, private=is_private) return JsonResponse({"object_name": object_name, "url": url})
def __init__(self, request=None, disable_public=False, disable_private=False): self.request = request if not disable_public: self.public_host = get_env_variable('S3_HOST', strict=True) self.public_bucket = get_env_variable('S3_ROOT_BUCKET', strict=True) self.public_access_key = get_env_variable('S3_PUBLIC_ACCESS_KEY', warn=False) self.public_secret_key = get_env_variable('S3_PUBLIC_SECRET_KEY', warn=False) self.public_client = Minio(self.public_host, access_key=self.public_access_key, secret_key=self.public_secret_key, secure=True) self.disable_private = disable_private
def get(self, request, tag, **kwargs): well = get_object_or_404(self.queryset, pk=tag) client = MinioClient(request=request, disable_private=False) object_name = request.GET.get("filename") filename = client.format_object_name(object_name, int(well.well_tag_number), "well") bucket_name = get_env_variable("S3_ROOT_BUCKET") is_private = False if request.GET.get("private") == "true": is_private = True bucket_name = get_env_variable("S3_PRIVATE_ROOT_BUCKET") # TODO: This should probably be "S3_WELL_BUCKET" but that will require a file migration url = client.get_presigned_put_url(filename, bucket_name=bucket_name, private=is_private) return JsonResponse({"object_name": object_name, "url": url})
def delete(self, request, tag, **kwargs): well = get_object_or_404(self.queryset, pk=tag) client = MinioClient(request=request, disable_private=False) is_private = False bucket_name = get_env_variable("S3_ROOT_BUCKET") if request.GET.get("private") == "true": is_private = True bucket_name = get_env_variable("S3_PRIVATE_ROOT_BUCKET") object_name = client.get_bucket_folder(int( well.well_tag_number), "well") + "/" + request.GET.get("filename") # TODO: This should probably be "S3_WELL_BUCKET" but that will require a file migration client.delete_document(object_name, bucket_name=bucket_name, private=is_private) return HttpResponse(status=204)
def well_licensing(request, **kwargs): tag = request.GET.get('well_tag_number') url = get_env_variable('E_LICENSING_URL') + '{}'.format(tag) api_success = False headers = { 'content_type': 'application/json', 'AuthUsername': get_env_variable('E_LICENSING_AUTH_USERNAME'), 'AuthPass': get_env_variable('E_LICENSING_AUTH_PASSWORD') } try: response = requests.get(url, headers=headers) if response.ok: try: licence = response.json( )[-1] # Use the latest licensing value, fails purposely if empty array licence_status = 'Licensed' if licence.get( 'authorization_status') == 'ACTIVE' else 'Unlicensed' data = { 'status': licence_status, 'number': licence.get('authorization_number'), 'date': licence.get('authorization_status_date') } api_success = True except: pass except: pass if not api_success: well = Well.objects.get(well_tag_number=tag) data = { 'status': well.licenced_status.description, 'number': '', 'date': '' } return HttpResponse(json.dumps(data), content_type="application/json")
def get(self, request, person_guid): person = get_object_or_404(self.queryset, pk=person_guid) client = MinioClient( request=request, disable_private=False) object_name = request.GET.get("filename") filename = client.format_object_name(object_name, person.person_guid, "driller") bucket_name = get_env_variable("S3_REGISTRANT_BUCKET") # All documents are private for drillers url = client.get_presigned_put_url( filename, bucket_name=bucket_name, private=True) return JsonResponse({"object_name": object_name, "url": url})
def config(): service_name = get_env_variable('DATABASE_SERVICE_NAME', '').upper().replace('-', '_') name = get_env_variable('DATABASE_NAME') return { 'ENGINE': 'django.contrib.gis.db.backends.postgis', 'NAME': name, 'USER': get_env_variable('DATABASE_USER'), 'PASSWORD': get_env_variable('DATABASE_PASSWORD'), 'HOST': get_env_variable('{}_SERVICE_HOST'.format(service_name)), 'PORT': get_env_variable('{}_SERVICE_PORT'.format(service_name)), }
def config(): service_name = get_env_variable('DATABASE_SERVICE_NAME', '').upper().replace('-', '_') if service_name: engine = engines.get(get_env_variable('DATABASE_ENGINE'), engines['sqlite']) else: engine = engines['sqlite'] name = get_env_variable('DATABASE_NAME') if not name and engine == engines['sqlite']: name = os.path.join(settings.BASE_DIR, 'db.sqlite3') return { 'ENGINE': engine, 'NAME': name, 'USER': get_env_variable('DATABASE_USER'), 'PASSWORD': get_env_variable('DATABASE_PASSWORD'), 'HOST': get_env_variable('{}_SERVICE_HOST'.format(service_name)), 'PORT': get_env_variable('{}_SERVICE_PORT'.format(service_name)), }
def create_private_client(self): self.private_access_key = get_env_variable('MINIO_ACCESS_KEY') self.private_secret_key = get_env_variable('MINIO_SECRET_KEY') self.private_host = get_env_variable('S3_PRIVATE_HOST') self.private_bucket = get_env_variable('S3_PRIVATE_BUCKET') self.private_aquifers_bucket = get_env_variable( 'S3_PRIVATE_AQUIFER_BUCKET', default_value="aquifer-docs") self.private_drillers_bucket = get_env_variable( 'S3_PRIVATE_REGISTRANT_BUCKET', default_value="driller-docs") return Minio(self.private_host, access_key=self.private_access_key, secret_key=self.private_secret_key, secure=self.use_secure)
import datetime import logging.config from pathlib import Path from gwells import database from gwells.settings.base import get_env_variable BASE_DIR = str(Path(__file__).parents[2]) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! # The SECRET_KEY is provided via an environment variable in OpenShift SECRET_KEY = get_env_variable( 'DJANGO_SECRET_KEY', # safe value used for development when DJANGO_SECRET_KEY might not be set '9e4@&tw46$l31)zrqe3wi+-slqm(ruvz&se0^%9#6(_w3ui!c0') # Security Settings SECURE_BROWSER_XSS_FILTER = True SECURE_CONTENT_TYPE_NOSNIFF = True SESSION_COOKIE_SECURE = get_env_variable('SESSION_COOKIE_SECURE', 'False') == 'True' CSRF_COOKIE_SECURE = get_env_variable('CSRF_COOKIE_SECURE', 'False') == 'True' CSRF_COOKIE_HTTPONLY = True SESSION_COOKIE_HTTPONLY = True # SECURITY WARNING: don't run with debug turned on in production! DEBUG = get_env_variable('DJANGO_DEBUG', 'False') == 'True' # Controls availability of the data entry functionality
import requests from gwells import database from gwells.settings.base import get_env_variable BASE_DIR = str(Path(__file__).parents[2]) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! # The SECRET_KEY is provided via an environment variable in OpenShift SECRET_KEY = get_env_variable( 'DJANGO_SECRET_KEY', # safe value used for development when DJANGO_SECRET_KEY might not be set '9e4@&tw46$l31)zrqe3wi+-slqm(ruvz&se0^%9#6(_w3ui!c0' ) # Security Settings SECURE_BROWSER_XSS_FILTER = True SECURE_CONTENT_TYPE_NOSNIFF = True SESSION_COOKIE_SECURE = get_env_variable( 'SESSION_COOKIE_SECURE', 'False') == 'True' CSRF_COOKIE_SECURE = get_env_variable('CSRF_COOKIE_SECURE', 'False') == 'True' CSRF_COOKIE_HTTPONLY = True SESSION_COOKIE_HTTPONLY = True # SECURITY WARNING: don't run with debug turned on in production! DEBUG = get_env_variable('DJANGO_DEBUG', 'False') == 'True'
from gwells.views import SurveyListView, SearchView, RegistryView, HealthView from gwells.views.admin import * from gwells.views import api from gwells.settings.base import get_env_variable # Creating 2 versions of the app_root. One without and one with trailing slash # This will allow for any or no additional app_root context to be provided app_root = settings.APP_CONTEXT_ROOT if app_root: app_root_slash = app_root + '/' else: app_root_slash = app_root DJANGO_ADMIN_URL = get_env_variable( 'DJANGO_ADMIN_URL', # safe value used for development when DJANGO_ADMIN_URL might not be set 'admin', strict=True) urlpatterns = [ # url(r'^'+ app_root +'$', views.HomeView.as_view(), name='home'), url(r'^' + app_root_slash + 'robots\.txt$', TemplateView.as_view(template_name='robots.txt', content_type='text/plain'), name='robots'), url(r'^' + app_root_slash + '$', SearchView.well_search, name='home'), url(r'^' + app_root_slash + 'search$', SearchView.well_search, name='search'), # url(r'^(?P<pk>[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/$', # views.DetailView.as_view(), name='detail'),