Skip to content

Commit

Permalink
Merge branch 'bhtom2-test'
Browse files Browse the repository at this point in the history
  • Loading branch information
arturkrawczyk committed Aug 27, 2024
2 parents 449cd89 + 0e9d253 commit edeba50
Show file tree
Hide file tree
Showing 19 changed files with 740 additions and 297 deletions.
77 changes: 65 additions & 12 deletions Documentation/DocumentationAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,14 @@ The script will display the response from the API with a calibration result, so

<!-- ### Description -->

<!-- This Python script allows you to retrieve a list of catalogues from the BHTOM2 system using the `get_catalogs` API. It provides a command-line interface for making GET requests with proper authorization. -->
<!-- This Python script allows you to retrieve a list of catalogues from the BHTOM2 system using the `get_catalogs` API. It provides a command-line interface for making GET requests with proper authorization. API returns 1000 records by request, use parametr "page" to get more-->

<!-- TOKEN is required! -->
<!--
### Endpoint
- **Method**: GET
- **URL**: `/calibration/get-catalogs/`
- **URL**: `/calibration/get-catalogs/?page=1`
## Usage
Expand All @@ -195,7 +195,7 @@ python get_catalogs_script.py <token>
### Example Usage
python get_catalogs_script.py https://example.com/get_catalogs_api abc123
python get_catalogs_script.py abc123 1
### Response
Expand All @@ -206,15 +206,13 @@ The script will make a GET request to the API and display the list of available
Python 3.x
The requests library (install with pip install requests)
Certainly! Here's the documentation for the `curl` command you provided in Markdown format:
--- -->


### Description
<!-- /calibration/get-calibration-res/ -->

This API endpoint allows users to retrieve calibration results for previously uploaded observations.
This API endpoint allows users to retrieve calibration results for previously uploaded observations. For 1 request you can get only 1000 records, if you want more - use "page" in request to get other records.

<!-- TOKEN is required! -->

Expand All @@ -232,9 +230,9 @@ This API endpoint allows users to retrieve calibration results for previously up

### Request Body

- `files` (array): Array containing file IDs or files name or both of them for calibration
- `files` (array, required): Array containing file IDs or files name or both of them for calibration
- `getPlot` (boolean): Flag to indicate whether to retrieve the calibration plot

- `page` (integer): The number of page
### Example Request

```bash
Expand All @@ -246,7 +244,8 @@ curl -X 'POST' \
-H 'X-CSRFToken: uUz2fRnXhPuvD9YuuiDW9cD1LsajeaQnE4hwtEAfR00SgV9bD5HCe5i8n4m4KcOr' \
-d '{
"files": [1, 'fileName', 2 'fileName2'],
"getPlot": true
"getPlot": true,
"page": 2
}'
```
You can use script as well
Expand All @@ -264,7 +263,7 @@ curl -X 'POST' \
## 1. Observatory List
<!-- /observatory/getObservatoryList/ -->

This API endpoint allows users to get the list of observatories registered in the system.
This API endpoint allows users to get the list of observatories registered in the system. You can get 1000 record per one request, use "page" to get more records

<!-- TOKEN is required! -->

Expand All @@ -281,6 +280,8 @@ curl -X 'POST' \
- `active_flg` (boolean): A flag to indicate whether an item is active.
- `created_start` (string): A date and time parameter for specifying a start date.
- `created_end` (string): A date and time parameter for specifying an end date.
- `page` (number): The number of requested page.


### Example Request Body (Optional)

Expand All @@ -291,7 +292,8 @@ curl -X 'POST' \
"lat": 45.678,
"active_flg": true,
"created_start": "2023-09-01T12:00:00Z",
"created_end": "2023-09-30T23:59:59Z"
"created_end": "2023-09-30T23:59:59Z",
"page": 1,
}
```

Expand All @@ -316,7 +318,8 @@ curl -X 'POST' \
"lat": 0,
"active_flg": true,
"created_start": "2023-09-28T14:00:09.440Z",
"created_end": "2023-09-28T14:00:09.440Z"
"created_end": "2023-09-28T14:00:09.440Z",
"page": 1
}'
```

Expand Down Expand Up @@ -535,13 +538,15 @@ python create_observatory.py --name "My Observatory" --lon 45.12345 --lat -120.6

## Request Parameters

You can requested only 1000 records per request, use pramater "page" to get more records
The request to retrieve observatory matrix data can include the following parameters in the request body:

- `user` (string, optional): Filter observatory matrix data by the username of the user associated with the observatory.
- `active_flg` (boolean, optional): Filter observatory matrix data by the active flag, indicating whether the observatory is active.
- `camera` (string, optional): Filter observatory matrix data by the camera name or identifier.
- `created_start` (string, datetime format, optional): Filter observatory matrix data to include only records created on or after the specified date and time.
- `created_end` (string, datetime format, optional): Filter observatory matrix data to include only records created on or before the specified date and time.
- `page` (number): The number of requested page.

### Example Request Body

Expand Down Expand Up @@ -917,6 +922,7 @@ You can use a Python script to update an existing target with Token Authenticati
This API allows users to obtain a list of targets.
This API supports filtering targets by name, Right Ascension (RA) range, and Declination (Dec) range.
You can requested only 1000 records per request, use pramater "page" to get more records
### Request
Expand All @@ -932,6 +938,7 @@ The request to retrieve the target list may include the following query paramete
- `raMax` (number, optional): The maximum Right Ascension (RA) value, represented as a floating-point number.
- `decMin` (number, optional): The minimum Declination (Dec) value, represented as a floating-point number.
- `decMax` (number, optional): The maximum Declination (Dec) value, represented as a floating-point number.
- `page` (number, optional): The number of requested page
<!-- TODO: need more fields!! -->
### Example Request
Expand Down Expand Up @@ -1097,6 +1104,52 @@ curl -X POST \
Replace `<yourToken>` with your valid authentication token.
## 8 Downloaded Target List (ADMIN ONLY)
<!-- targets/get-downloaded-target-list/ -->
This API allows admin users to obtain a list of downloaded targets.
This API supports filtering downloaded targets by target name, username, creation date range, and download type.
You can request only 1000 records per request; use the parameter "page" to get more records.
### Request
- **Method**: POST
- **URL**: `/targets/get-downloaded-target-list/`
### Request Parameters
The request to retrieve the downloaded target list may include the following query parameters:
- `target` (string, optional): The name or identifier of the target.
- `user` (string, optional): The username of the user who downloaded the target.
- `created_from` (string, optional): The start date-time (ISO format) for filtering downloads based on creation date.
- `created_to` (string, optional): The end date-time (ISO format) for filtering downloads based on creation date.
- `download_type` (string, optional): The type of download.
- `page` (number, optional): The number of the requested page.
### Example Request
You can make a POST request to retrieve a list of downloaded targets based on the specified criteria using the `curl` command or a web browser.
### Using `curl`
```bash
curl -X POST \
-H "Authorization: Token <yourToken>" \
-H "Content-Type: application/json" \
-d '{
"target": "MyTarget",
"user": "admin",
"created_from": "2023-01-01T00:00:00Z",
"created_to": "2024-01-01T00:00:00Z",
"download_type": "R",
"page": 1
}' \
"https://bh-tom2.astrolabs.pl/targets/getDownloadedTargetList/"
```
# DATAPRODUCT API
### List Data Products
Expand Down
7 changes: 4 additions & 3 deletions Documentation/documentation_scripts/get_catalogs_script.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import requests
import argparse

def get_catalogs(token):
def get_catalogs(token, page):
headers = {
'Authorization': f'Token {token}'
}
api_url = "https://bh-tom2.astrolabs.pl/calibration/get-catalogs/"
api_url = f'https://bh-tom2.astrolabs.pl/calibration/get-catalogs/?page={page}',
response = requests.get(api_url, headers=headers)
print(response.json())

if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Retrieve catalogs from the BHTOM2 API")
parser.add_argument("token", type=str, help="Authentication token")
parser.add_argument("page", type=int, help="Page")

args = parser.parse_args()

get_catalogs(args.token)
get_catalogs(args.token, args.page)
107 changes: 78 additions & 29 deletions bhtom2/bhtom_calibration/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from bhtom2.bhtom_calibration.models import Catalogs as calibration_catalog
from bhtom_base.bhtom_dataproducts.models import DataProduct, ReducedDatum
from bhtom_base.bhtom_targets.models import Target
from bhtom2.utils.api_pagination import StandardResultsSetPagination
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from bhtom2.utils.bhtom_logger import BHTOMLogger
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
Expand All @@ -19,83 +21,130 @@
from django.conf import settings
logger: BHTOMLogger = BHTOMLogger(__name__, 'Bhtom: bhtom_calibration.views')


class CalibrationResultsApiView(APIView):
authentication_classes = [TokenAuthentication]
permission_classes = [IsAuthenticated]

pagination_class = StandardResultsSetPagination

@swagger_auto_schema(
request_body=openapi.Schema(
type=openapi.TYPE_OBJECT,
properties={
'files': openapi.Schema(type=openapi.TYPE_ARRAY,
items=openapi.Schema(type=openapi.TYPE_INTEGER),),
items=openapi.Schema(type=openapi.TYPE_INTEGER),
),
'getPlot': openapi.Schema(type=openapi.TYPE_BOOLEAN),
'page': openapi.Schema(type=openapi.TYPE_INTEGER, description='Page number for pagination'),
},
required=['fileId', 'getPlot']
required=['files', 'getPlot']
),
manual_parameters=[
openapi.Parameter(
name='Authorization',
in_=openapi.IN_HEADER,
type=openapi.TYPE_STRING,
required=True,
description='Token <Your Token>'
),
],
name='Authorization',
in_=openapi.IN_HEADER,
type=openapi.TYPE_STRING,
required=True,
description='Token <Your Token>'
),
],
)

def post(self, request):
files = request.data['files']
getPlot = request.data['getPlot']
results = {}
files = request.data.get('files')
getPlot = request.data.get('getPlot', False)
page = request.data.get('page', 1)
base_path = settings.DATA_PLOTS_PATH

if files is None:
return Response({"Error": "'files' field are required."}, status=status.HTTP_400_BAD_REQUEST)

try:
user = Token.objects.get(key=request.auth.key).user
calibration_data_list = []
for file in files:
if isinstance(file, str):
instance = Calibration_data.objects.get(dataproduct__data__contains= file)
instance = Calibration_data.objects.get(dataproduct__data__contains=file)
dp = DataProduct.objects.get(data__contains=file)
elif isinstance(file, int):
instance = Calibration_data.objects.get(dataproduct_id=file)
dp = DataProduct.objects.get(id=file)
if(instance.dataproduct.user_id == user.id or user.is_superuser):

if instance.dataproduct.user_id == user.id or user.is_superuser:
serialized_data = serializers.serialize('json', [instance])
data = json.loads(serialized_data)[0]
results[instance.id] = data["fields"]
data = json.loads(serialized_data)[0]
result = data["fields"]
if getPlot:
target = dp.target
if target.photometry_plot:
with open(base_path + str(target.photometry_plot), 'r') as json_file:
plot = json.load(json_file)
results[instance.id] = {"calib-res": data["fields"] ,"plot": plot}
result["plot"] = plot
else:
result["plot"] = None
else:
results[instance.id] ={"calib-res": data["fields"] ,"plot": None}
result["plot"] = None
calibration_data_list.append(result)
else:
results[instance.id] ={"calib-res": "It's not yours data","plot": None}
calibration_data_list.append({"calib-res": "It's not your data", "plot": None})

paginator = Paginator(calibration_data_list, self.pagination_class.max_page_size)
try:
paginated_data = paginator.page(page)
except PageNotAnInteger:
paginated_data = paginator.page(1)
except EmptyPage:
paginated_data = paginator.page(paginator.num_pages)

response_data = {
'count': paginator.count,
'num_pages': paginator.num_pages,
'current_page': paginated_data.number,
'data': list(paginated_data)
}

return Response(response_data, status=status.HTTP_200_OK)

except Calibration_data.DoesNotExist:
return Response({"Error": 'File does not exist in the database'}, status=status.HTTP_400_BAD_REQUEST)
except Exception as e:
return Response({"Error": 'something went wrong' + str(e)}, status=status.HTTP_400_BAD_REQUEST)

return Response({"Result": results}, status=status.HTTP_200_OK)
return Response({"Error": 'Something went wrong: ' + str(e)}, status=status.HTTP_400_BAD_REQUEST)

def list(self, request, *args, **kwargs):
ret = super().list(request, *args, **kwargs)
return ret


class GetCatalogsApiView(APIView):
authentication_classes = [TokenAuthentication]
permission_classes = [IsAuthenticated]
pagination_class = StandardResultsSetPagination

def get(self, request):
page_number = request.query_params.get('page', 1)

try:
instance = calibration_catalog.objects.all().values('filters')
queryset = calibration_catalog.objects.all().values('filters')

paginator = Paginator(queryset, self.pagination_class.max_page_size)

try:
paginated_data = paginator.page(page_number)
except PageNotAnInteger:

paginated_data = paginator.page(1)
except EmptyPage:
paginated_data = paginator.page(paginator.num_pages)

response_data = {
'count': paginator.count,
'num_pages': paginator.num_pages,
'current_page': paginated_data.number,
'data': list(paginated_data)
}

return Response(response_data, status=status.HTTP_200_OK)

except Exception as e:
return Response({"Error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
return Response({"Catalogs": instance}, status=status.HTTP_200_OK)


class GetCpcsArchiveDataApiView(APIView):
authentication_classes = [TokenAuthentication]
Expand Down
Loading

0 comments on commit edeba50

Please sign in to comment.