Skip to content

Commit

Permalink
feat: update list search index id
Browse files Browse the repository at this point in the history
  • Loading branch information
YiqingQu committed Nov 26, 2023
1 parent bc4ebac commit 7d391b3
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 33 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
23 changes: 23 additions & 0 deletions myportal/migrations/0006_auto_20231120_0842.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.2.16 on 2023-11-20 08:42

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('myportal', '0005_fileinfo'),
]

operations = [
migrations.AddField(
model_name='resource',
name='description',
field=models.CharField(max_length=1024, null=True),
),
migrations.AddField(
model_name='resource',
name='extra_info',
field=models.CharField(max_length=1024, null=True),
),
]
2 changes: 2 additions & 0 deletions myportal/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ class Resource(models.Model):
uuid = models.CharField(default=uuid.uuid4, editable=False, unique=True, max_length=50)
resource_type = models.CharField(default="single", max_length=50)
publication_name = models.CharField(max_length=255)
description = models.CharField(max_length=1024, null=True)
path = models.CharField(max_length=255)
task_id = models.CharField(null=True, max_length=50)
user_id = models.CharField(null=True, max_length=50)
created_time = models.DateTimeField(null=True, auto_now_add=True)
modified_time = models.DateTimeField(null=True, auto_now=True)
extra_info = models.CharField(max_length=1024, null=True)

def __str__(self):
return f"Resource {self.uuid}: " \
Expand Down
6 changes: 3 additions & 3 deletions myportal/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
# LOGIN_URL = '/login/globus'
ACCOUNT_LOGOUT_REDIRECT_URL = '/accounts/login/'
LOGIN_REDIRECT_URL = '/accounts/profile/'
ACCOUNT_DEFAULT_HTTP_PROTOCOL = 'https' # todo local
ACCOUNT_DEFAULT_HTTP_PROTOCOL = 'http' # todo local

# This dictates which scopes will be requested on each user login
SOCIAL_AUTH_GLOBUS_SCOPE = [
Expand Down Expand Up @@ -153,8 +153,8 @@
],
},
'list-search-index': {
'uuid': '15a6acc8-3a23-42ed-98cf-a32833acaae3',
'name': 'Schema.org Json Index',
'uuid': 'dfbccff7-36f8-43e2-9e6e-c38059184985',
'name': 'GeoEDF List Search Index',
'fields': [
('extension', fields.extension),
('size_bytes', fields.size_bytes),
Expand Down
70 changes: 51 additions & 19 deletions myportal/templates/file-management/file-manager.html
Original file line number Diff line number Diff line change
Expand Up @@ -243,30 +243,46 @@ <h5 class="modal-title fs-5">Destination Directory:</h5>
<div class="modal-header">
<h3 class="modal-title fs-5" id="exampleModalLabel">Publish File</h3>
</div>
<div class="modal-body" >
<div class="modal-body">
<div id="publish-content">
Files to be published will be displayed here.
</div>
<h4 class="modal-title fs-5">Metadata:</h4>
<h4 class="modal-title fs-5">Metadata:</h4>
<form method="post" action="{% url 'publish_file' %}">
{% csrf_token %}
<input type="hidden" name="path" id="path">

<div class="mb-3">
<label for="publicationName" class="form-label">Publication Name:</label>
<input type="text" class="form-control" id="publicationName"
name="publication_name" required>
</div>
<div class="mb-3">
<label for="description" class="form-label">Description:</label>
<input type="text" class="form-control" id="description" name="description"
required oninput="validateDescription()">
<small id="descriptionHelp" class="form-text text-muted"
style="display: none;">Description
must be longer than 50 characters.</small>
</div>
<div class="mb-3">
<label for="keywords" class="form-label">Keywords:</label>
<input type="text" class="form-control" id="keywords" name="keywords"
required>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
Cancel
</button>
<button type="submit" class="btn btn-outline-primary">
Publish
</button>
</div>
</form>

<div class="mb-3">
<label for="projectName" class="form-label">Project Name:</label>
<input type="text" class="form-control" id="projectName"
name="project_name" required>
</div>
<div class="mb-3">
<label for="notes" class="form-label">Notes:</label>
<input type="text" class="form-control" id="notes"
name="notes" required>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
Cancel
</button>
<a class="btn btn-outline-primary"
href="{% url 'file-manage' directory.path|encoded_path %}">Publish</a>
</div>


</div>
</div>
</div>
Expand Down Expand Up @@ -551,6 +567,22 @@ <h3 class="modal-title fs-5" id="exampleModalLabel">Delete File</h3>
{#$('#publish-content').text(values);#}
return values.join('\n');
}

function updateFilePath() {
var checkedValues = getCheckedCheckboxesFor('select');
document.getElementById('path').value = checkedValues.join(','); // Join the values if multiple
}
</script>
<script>
function validateDescription() {
var description = document.getElementById('description').value;
var helpText = document.getElementById('descriptionHelp');
if (description.length <= 50) {
helpText.style.display = 'block';
} else {
helpText.style.display = 'none';
}
}
</script>

{% endblock extrascripts %}
3 changes: 2 additions & 1 deletion myportal/views/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.urls import path, include, re_path
from myportal.views import api_account, api_resource
from myportal.views.file_manager_views import FileManager, delete_file, download_file, upload_file, save_info, \
create_directory
create_directory, publish_file
from myportal.views.views import temp_view, file_detail, mysearch, GetAccountProfile
from globus_portal_framework.urls import register_custom_index

Expand Down Expand Up @@ -35,4 +35,5 @@
path('file/upload/', upload_file, name='upload_file'),
path('file/update/<str:file_path>/', save_info, name='save_info'),
path('file/create-directory/', create_directory, name='create_directory'),
path('file/publish/', publish_file, name='publish_file'),
]
24 changes: 19 additions & 5 deletions myportal/views/api_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ class PublishResource(APIView):
def post(self, request):
"""
Calling this API will create a Resource object in database and publish it.
Upon successful creation, a message will be sent to a RabbitMQ queue named 'geoedf-all'. The message will then be consumed by a metadata extractor.
Upon successful creation, a message will be sent to a RabbitMQ queue named 'geoedf-all'. The message will
then be consumed by a metadata extractor.
"""
print(f"[PublishResource] user={request.user}")
# todo recover login check
Expand All @@ -176,12 +177,14 @@ def post(self, request):
# data={"status": "Please log in first"},
# status=status.HTTP_200_OK,
# )
user_id = "[email protected]"
serializer = PublishResourceRequest(data=request.data)
if serializer.is_valid():
file_uuid = str(uuid.uuid4())
resource = Resource(uuid=file_uuid,
path=serializer.validated_data['path'],
resource_type=serializer.validated_data['resource_type'])
resource_type=serializer.validated_data['resource_type'],
user_id=user_id)
if 'publication_name' in serializer.validated_data:
resource.publication_name = serializer.validated_data['publication_name']
resource.save()
Expand Down Expand Up @@ -212,13 +215,24 @@ def publish_to_globus_index(resource):

channel.queue_declare(queue=RMQ_NAME)

# Send the message to the queue
message = json.dumps({
publish_data = {
"uuid": resource.uuid,
"publication_name": resource.publication_name,
"type": resource.resource_type,
"path": resource.path,
})
"user_id": resource.user_id,
}
if resource.description is not None:
publish_data['description'] = resource.description
if resource.task_id is not None:
publish_data['task_id'] = resource.task_id
if resource.extra_info is not None:
publish_data['extra_info'] = resource.extra_info

print(f'publish_data={publish_data}')

# Send the message to the queue
message = json.dumps(publish_data)
# todo get task id

channel.basic_publish(exchange='',
Expand Down
58 changes: 53 additions & 5 deletions myportal/views/file_manager_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
import datetime
import json
import os
import uuid

import requests
from allauth.socialaccount.models import SocialToken
from allauth.socialaccount.models import SocialToken, SocialAccount
from django.conf import settings
from django.contrib.auth.models import AnonymousUser
from django.contrib.sites.models import Site
Expand All @@ -18,14 +19,20 @@
from django.shortcuts import render, redirect
from django.contrib.sites.shortcuts import get_current_site
from django.utils.functional import SimpleLazyObject
from rest_framework import permissions

from myportal.constants import GLOBUS_INDEX_NAME, FILES_ROOT
from myportal.models import Resource
from myportal.utils import generate_nested_directory
from django.shortcuts import render, redirect
from django.core.files.storage import FileSystemStorage

from myportal.views.api_resource import has_valid_cilogon_token, publish_to_globus_index, get_globus_index_submit_taskid


class FileManager(View):
permission_classes = (permissions.AllowAny,)

def get_breadcrumbs(self, request):
path_components = [component for component in request.path.split("/") if component]
breadcrumbs = []
Expand Down Expand Up @@ -98,14 +105,23 @@ def get_files_from_directory(self, directory_path):
return files, directories

def get(self, request, directory=settings.MEDIA_ROOT, *args, **kwargs):
media_path = os.path.join(settings.MEDIA_ROOT)
print(request.user)

user = SocialAccount.objects.get(user=request.user)
email = user.extra_data["email"]
# print(f'user= {user.extra_data["email"]}')

personal_media_path = os.path.join(settings.MEDIA_ROOT, email)
print(f'personal_media_path= {personal_media_path}')

directories = generate_nested_directory(media_path, media_path)
if not os.path.exists(personal_media_path):
os.makedirs(personal_media_path)
directories = generate_nested_directory(personal_media_path, personal_media_path)
selected_directory = directory

files = []
dirs = []
selected_directory_path = os.path.join(media_path, selected_directory)
selected_directory_path = os.path.join(personal_media_path, selected_directory)
if os.path.isdir(selected_directory_path):
files, dirs = self.get_files_from_directory(selected_directory_path)

Expand Down Expand Up @@ -152,7 +168,8 @@ def upload_file(request):

directory = request.POST.get('directory')

fs = FileSystemStorage(location=os.path.join(settings.MEDIA_ROOT, directory)) # saves to the 'files' directory under MEDIA_ROOT
fs = FileSystemStorage(
location=os.path.join(settings.MEDIA_ROOT, directory)) # saves to the 'files' directory under MEDIA_ROOT
filename = fs.save(uploaded_file.name, uploaded_file)
file_url = fs.url(filename) # You can get the file URL if needed

Expand Down Expand Up @@ -196,3 +213,34 @@ def save_info(request, file_path):
# )

return redirect(request.META.get('HTTP_REFERER'))


def publish_file(request):
user_id = "[email protected]"
publication_name = request.POST.get('publication_name')
# path = request.POST.get('path')
path = "data/files/user/wrfinput_private.nc"
description = request.POST.get('description')
keywords = request.POST.get('keywords')
file_uuid = str(uuid.uuid4())
resource = Resource(uuid=file_uuid,
path=path,
resource_type="single",
user_id=user_id)

resource.save()
print(f"[PublishResource] resource={resource.__str__()}")

publish_to_globus_index(resource)
task_id = get_globus_index_submit_taskid(resource)
resource.task_id = task_id
resource.save()

resp = {
"status": "Submitted",
"uuid": file_uuid,
"path": path,
"task_id": task_id,
}

return redirect(request.META.get('HTTP_REFERER'))

0 comments on commit 7d391b3

Please sign in to comment.