Skip to content

Commit

Permalink
Refactoring a method that checks global variables, organizing imports…
Browse files Browse the repository at this point in the history
…, and correcting the method renaming.
  • Loading branch information
Chrystinne committed Sep 29, 2023
1 parent f40db5f commit 1299127
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -388,9 +388,11 @@ <h5 class="card-title mt-3 mb-1">Google Cloud</h5>
<li class="list-group-item">
<h5 class="card-title mt-3 mb-1">AWS</h5>
{% if not has_s3_credentials %}
<p>You are missing the AWS credentials.</p>
{% elif missing_S3_open_data %}
<p>You are missing the S3_OPEN_ACCESS_BUCKET information.</p>
<p>
Support for Amazon S3 is not enabled. (Check that
<code>AWS_PROFILE</code> and <code>S3_OPEN_ACCESS_BUCKET</code>
are set.)
</p>
{% elif not aws_bucket_exists %}
<p>Create a bucket on AWS to store the files associated with this project.</p>
<form action="" method="post">
Expand Down
14 changes: 5 additions & 9 deletions physionet-django/console/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@
update_bucket_policy,
get_bucket_name_and_prefix,
check_s3_bucket_with_prefix_exists,
has_aws_credentials,
missing_S3_open_data_info,
has_s3_credentials,
)

LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -904,11 +903,9 @@ def manage_published_project(request, project_slug, version):
topic_form.set_initial()
deprecate_form = None if project.deprecated_files else forms.DeprecateFilesForm()
has_credentials = bool(settings.GOOGLE_APPLICATION_CREDENTIALS)
has_s3_credentials = has_aws_credentials()
if has_s3_credentials:
if has_s3_credentials():
s3_bucket_exists = check_s3_bucket_with_prefix_exists(project)
s3_bucket_name = get_bucket_name_and_prefix(project)
missing_S3_open_data = missing_S3_open_data_info(project)
data_access_form = forms.DataAccessForm(project=project)
contact_form = forms.PublishedProjectContactForm(project=project,
instance=project.contact)
Expand Down Expand Up @@ -971,7 +968,7 @@ def manage_published_project(request, project_slug, version):
messages.error(request, 'Project has tasks pending.')
else:
gcp_bucket_management(request, project, user)
elif 'aws-bucket' in request.POST and has_aws_credentials:
elif 'aws-bucket' in request.POST and has_s3_credentials():
if any(get_associated_tasks(project, read_only=False)):
messages.error(request, 'Project has tasks pending.')
else:
Expand Down Expand Up @@ -1031,8 +1028,7 @@ def manage_published_project(request, project_slug, version):
'topic_form': topic_form,
'deprecate_form': deprecate_form,
'has_credentials': has_credentials,
'has_s3_credentials': has_s3_credentials,
'missing_S3_open_data': missing_S3_open_data,
'has_s3_credentials': has_s3_credentials(),
'aws_bucket_exists': s3_bucket_exists,
's3_bucket_name': s3_bucket_name,
'data_access_form': data_access_form,
Expand Down Expand Up @@ -1074,7 +1070,7 @@ def gcp_bucket_management(request, project, user):
LOGGER.info("The bucket {0} already exists, skipping bucket and \
group creation".format(bucket_name))
else:
create_s3_bucket(project.slug, project.version, project.title, is_private)
utility.create_bucket(project.slug, project.version, project.title, is_private)
messages.success(request, "The GCP bucket for project {0} was \
successfully created.".format(project))
GCP.objects.create(project=project, bucket_name=bucket_name,
Expand Down
29 changes: 5 additions & 24 deletions physionet-django/project/cloud/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,6 @@


# Manage AWS buckets and objects
def missing_S3_open_data_info(project):
"""
Check if S3 open data information is missing for a project.
This function evaluates whether a project lacks the necessary
S3 open data information, based on its access policy and the
availability of an S3 open data bucket name.
Args:
project (project.models.Project): The project to check for
S3 open data information.
Returns:
bool: True if S3 open data information is missing,
False otherwise.
"""
if project.access_policy != AccessPolicy.OPEN:
return False
else:
return not has_S3_open_data_bucket_name()


def has_S3_open_data_bucket_name():
"""
Check if AWS credentials (AWS_PROFILE) have been set in
Expand All @@ -43,15 +21,18 @@ def has_S3_open_data_bucket_name():
return bool(settings.S3_OPEN_ACCESS_BUCKET)


def has_aws_credentials():
def has_s3_credentials():
"""
Check if AWS credentials (AWS_PROFILE) have been set in
the project's settings.
Returns:
bool: True if AWS_PROFILE is set, False otherwise.
"""
return bool(settings.AWS_PROFILE)
return all([
settings.AWS_PROFILE,
settings.S3_OPEN_ACCESS_BUCKET,
])


def create_s3_access_object():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ <h5>Access the files</h5>
<pre class="shell-command">wget -r -N -c -np{% if project.access_policy %} --user {{ user }} --ask-password{% endif %} {{ bulk_url_prefix }}{% url 'serve_published_project_file' project.slug project.version '' %}</pre>
</li>
{% endif %}
{% if not missing_S3_open_data and aws_bucket_exists %}
{% if has_s3_credentials and aws_bucket_exists %}
<li>
Download the files using AWS command line tools:
<pre class="shell-command">aws s3 sync s3://{{ s3_bucket_name }} DESTINATION</pre>
Expand Down
15 changes: 8 additions & 7 deletions physionet-django/project/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@
from project.validators import validate_filename, validate_gcs_bucket_object
from user.forms import AssociatedEmailChoiceForm
from user.models import CloudInformation, CredentialApplication, LegacyCredential, User, Training

from project.cloud.s3 import (
get_bucket_name_and_prefix,
check_s3_bucket_with_prefix_exists,
has_s3_credentials,
)
from django.db.models import F, DateTimeField, ExpressionWrapper

LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -1772,7 +1776,6 @@ def published_project(request, project_slug, version, subdir=''):
"""
Displays a published project
"""
from project.cloud.s3 import get_bucket_name_and_prefix, check_s3_bucket_with_prefix_exists, has_aws_credentials
s3_bucket_exists = None
s3_bucket_name = None
try:
Expand All @@ -1799,8 +1802,7 @@ def published_project(request, project_slug, version, subdir=''):
platform_citations = project.get_platform_citation()
show_platform_wide_citation = any(platform_citations.values())
main_platform_citation = next((item for item in platform_citations.values() if item is not None), '')
has_s3_credentials = has_aws_credentials()
if has_s3_credentials:
if has_s3_credentials():
s3_bucket_exists = check_s3_bucket_with_prefix_exists(project)
s3_bucket_name = get_bucket_name_and_prefix(project)

Expand Down Expand Up @@ -1860,6 +1862,7 @@ def published_project(request, project_slug, version, subdir=''):
'platform_citations': platform_citations,
'is_lightwave_supported': project.files.is_lightwave_supported(),
'is_wget_supported': project.files.is_wget_supported(),
'has_s3_credentials': has_s3_credentials(),
'aws_bucket_exists': s3_bucket_exists,
's3_bucket_name': s3_bucket_name,
'show_platform_wide_citation': show_platform_wide_citation,
Expand Down Expand Up @@ -1918,8 +1921,6 @@ def sign_dua(request, project_slug, version):
Page to sign the dua for a protected project.
Both restricted and credentialed policies.
"""
from project.cloud.s3 import has_aws_credentials
from console import views
user = request.user
project = PublishedProject.objects.filter(slug=project_slug, version=version)
if project:
Expand All @@ -1944,7 +1945,7 @@ def sign_dua(request, project_slug, version):

if request.method == 'POST' and 'agree' in request.POST:
DUASignature.objects.create(user=user, project=project)
if has_aws_credentials():
if has_s3_credentials():
views.update_aws_bucket_policy(project.id)
return render(request, 'project/sign_dua_complete.html', {
'project':project})
Expand Down

0 comments on commit 1299127

Please sign in to comment.