-
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.
feat: use api to access campaigns stored in s3
- Loading branch information
Showing
17 changed files
with
305 additions
and
26 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from typing import List, Optional, Tuple | ||
from pydantic import BaseModel | ||
|
||
class Reference(BaseModel): | ||
citation: str | ||
doi: str | ||
|
||
|
||
class Measures(BaseModel): | ||
name: str | ||
description: Optional[str] = None | ||
datasets: Optional[List[str]] = None | ||
|
||
|
||
class Instrument(BaseModel): | ||
name: str | ||
description: Optional[str] = None | ||
measures: Optional[List[Measures]] | ||
|
||
class TrackColumns(BaseModel): | ||
latitude: str | ||
longitude: str | ||
timestamp: Optional[str] = None | ||
|
||
class Track(BaseModel): | ||
file: str | ||
columns: TrackColumns | ||
color: Optional[str] = None | ||
timestamp_format: Optional[str] | ||
|
||
|
||
class Campaign(BaseModel): | ||
id: Optional[int] = None | ||
name: str | ||
acronym: str | ||
website: Optional[str] = None | ||
type: Optional[str] = None | ||
color: Optional[str] = None | ||
objectives: Optional[str] = None | ||
start_date: Optional[str] = None | ||
end_date: Optional[str] = None | ||
location: Optional[str] = None | ||
platform: Optional[str] = None | ||
start_location: Tuple[float, float] | ||
end_location: Optional[Tuple[float, float]] = None | ||
images: Optional[List[str]] = None | ||
track: Optional[Track] = None | ||
fundings: Optional[List[str]] = None | ||
references: Optional[List[Reference]] = None | ||
instruments: Optional[List[Instrument]] = None | ||
|
||
|
||
class CampaignStore(BaseModel): | ||
campaigns: List[str] |
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,85 @@ | ||
import os | ||
import uuid | ||
import json | ||
import tempfile | ||
import urllib.parse | ||
from api.services.s3 import s3_client | ||
from api.config import config | ||
from api.models.campaigns import Campaign | ||
|
||
|
||
class CampaignsService: | ||
|
||
async def list(self) -> list[Campaign]: | ||
"""List all campaigns""" | ||
folder_path = "campaigns/" | ||
files = [file for file in await s3_client.list_files(folder_path) if file.endswith("/campaign.json")] | ||
|
||
campaigns = [] | ||
for file in files: | ||
content, mime_type = await s3_client.get_file(file) | ||
campaign_dict = json.loads(content) | ||
campaign = Campaign(**campaign_dict) | ||
campaigns.append(campaign) | ||
|
||
return campaigns | ||
|
||
# async def createOrUpdate(self, study: Study) -> Study: | ||
# if study.identifier is None or study.identifier == "" or study.identifier == "_draft": | ||
# study.identifier = str(uuid.uuid4()) | ||
|
||
# # Destination folder in s3 | ||
# s3_folder = f"draft/{study.identifier}" | ||
|
||
# # Move tmp files to their final location | ||
# if study.datasets is not None: | ||
# for dataset in study.datasets: | ||
# if "children" in dataset.folder: | ||
# for i, file in enumerate(dataset.folder["children"]): | ||
# if "/tmp/" in file["path"]: | ||
# dataset_file_path = f"{s3_folder}/files/{dataset.name}/{file['name']}" | ||
# new_key = await s3_client.move_file(file["path"], dataset_file_path) | ||
# file["path"] = urllib.parse.quote(new_key) | ||
# dataset.folder["children"][i] = file | ||
# dataset.folder["name"] = dataset.name | ||
# dataset.folder["path"] = s3_client.to_s3_path( | ||
# urllib.parse.quote(f"{s3_folder}/files/{dataset.name}")) | ||
|
||
# # TODO Remove files that are not linked to a dataset | ||
|
||
# # Create a temporary directory to dump JSON | ||
# with tempfile.TemporaryDirectory() as temp_dir: | ||
# print(f"Temporary directory created at: {temp_dir}") | ||
|
||
# # Use the temporary directory for file operations | ||
# temp_file_path = os.path.join(temp_dir, "study.json") | ||
|
||
# # Convert SQLModel object to dictionary | ||
# study_dict = study.model_dump() | ||
# with open(temp_file_path, "w") as temp_file: | ||
# json.dump(study_dict, temp_file, indent=2) | ||
# await s3_client.upload_local_file(temp_dir, "study.json", s3_folder=s3_folder) | ||
|
||
# return study | ||
|
||
# async def delete(self, identifier: str): | ||
# exists = await self.exists(identifier) | ||
# if not exists: | ||
# raise Exception( | ||
# f"Study with identifier {identifier} does not exist.") | ||
|
||
# await s3_client.delete_files(f"draft/{identifier}") | ||
|
||
# async def exists(self, identifier: str) -> bool: | ||
# return await s3_client.path_exists(f"draft/{identifier}/study.json") | ||
|
||
# async def get(self, identifier: str) -> StudyDraft: | ||
# exists = await self.exists(identifier) | ||
# if not exists: | ||
# raise Exception( | ||
# f"Study with identifier {identifier} does not exist.") | ||
|
||
# file_path = f"draft/{identifier}/study.json" | ||
# content, mime_type = await s3_client.get_file(file_path) | ||
# study_dict = json.loads(content) | ||
# return StudyDraft(**study_dict) |
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,21 @@ | ||
from typing import List | ||
from fastapi import APIRouter, Depends, Body | ||
from fastapi.responses import FileResponse | ||
from fastapi.datastructures import UploadFile | ||
from fastapi.param_functions import File | ||
from api.services.campaigns import CampaignsService | ||
from api.models.campaigns import Campaign | ||
from api.utils.file_size import size_checker | ||
from api.auth import require_admin, User | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.get("/campaigns", response_model=List[Campaign]) | ||
async def get_study_drafts( | ||
#user: User = Depends(require_admin), | ||
) -> List[Campaign]: | ||
"""Get all campaigns""" | ||
service = CampaignsService() | ||
return await service.list() | ||
|
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 |
---|---|---|
@@ -1,10 +1,15 @@ | ||
version=$(shell date +%FT%H:%M) | ||
bucket=10208-fcd9acb029f419e6493edf97f4592b96 | ||
path=icebreaker-dev | ||
|
||
help: | ||
@echo s3://${bucket}/${path}/${version}/ | ||
@echo s3://${bucket}/${path}/ | ||
|
||
cdn: | ||
s3cmd put --recursive --acl-public --guess-mime-type campaigns s3://${bucket}/${path}/${version}/ | ||
s3cmd put --recursive --acl-public --guess-mime-type campaigns s3://${bucket}/${path}/ | ||
|
||
rm: | ||
s3cmd rm --recursive s3://${bucket}/${path}/ | ||
|
||
sync: | ||
s3cmd sync s3://${bucket}/${path}/ cdn-local/ | ||
|
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
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,27 @@ | ||
<template> | ||
<div> | ||
<pre>{{ campaign }}</pre> | ||
</div> | ||
</template> | ||
|
||
|
||
<script lang="ts"> | ||
export default defineComponent({ | ||
name: 'CampaignForm', | ||
}); | ||
</script> | ||
<script setup lang="ts"> | ||
import { Campaign } from 'src/models'; | ||
import { cdnUrl } from 'src/boot/api'; | ||
interface Props { | ||
modelValue: Campaign; | ||
} | ||
const props = defineProps<Props>(); | ||
const campaign = ref(props.modelValue); | ||
watch(() => props.modelValue, (val) => { | ||
campaign.value = val; | ||
}); | ||
</script> |
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
Oops, something went wrong.