-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
153 additions
and
33 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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() | ||
|
@@ -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='', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 = [] | ||
|
@@ -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) | ||
|
||
|
@@ -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 | ||
|
||
|
@@ -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')) |