Skip to content

Commit

Permalink
Merge branch 'bhtom2-dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurii Purdenko committed Nov 29, 2024
2 parents a70e18a + 96b5264 commit a36e8b7
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 278 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,5 @@ __pycache__/
bhtom2/.DS_Store
*.env

/data
/data
.gitattributes
4 changes: 3 additions & 1 deletion Documentation/DocumentationAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -1302,8 +1302,10 @@ API for DataProduct list, lists uploaded instrumental or fits files and tracks t
```json
{
"data_product_type": "photometry",
"id": 1,
"status": "Dataproduct status",
"fits_data": "test",
"fits_data": "fits file name",
"photometry_data": "photometry file name",
"camera" : "BIALKOW_ANDOR-DW432",
"created_start": "2024-01-01",
"created_end": "2024-01-02",
Expand Down
59 changes: 0 additions & 59 deletions bhtom2/bhtom_calibration/migrations/0001_initial.py

This file was deleted.

This file was deleted.

4 changes: 4 additions & 0 deletions bhtom2/bhtom_calibration/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ def post(self, request):
serialized_data = serializers.serialize('json', [instance])
data = json.loads(serialized_data)[0]
result = data["fields"]
file_url = request.build_absolute_uri(settings.DATA_MEDIA_PATH + str(instance.dataproduct.photometry_data))
result["file_download_link"] = file_url
if getPlot:
if instance.calibration_plot:
plot_path = settings.DATA_PLOTS_PATH + str(instance.calibration_plot)
Expand All @@ -101,6 +103,8 @@ def post(self, request):
serialized_data = serializers.serialize('json', [instance])
data = json.loads(serialized_data)[0]
result = data["fields"]
file_url = request.build_absolute_uri(settings.DATA_MEDIA_PATH + str(instance.dataproduct.photometry_data))
result["file_download_link"] = file_url
if getPlot:
if instance.calibration_plot:
plot_path = settings.DATA_PLOTS_PATH + str(instance.calibration_plot)
Expand Down
3 changes: 1 addition & 2 deletions bhtom2/bhtom_catalogs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ def form_valid(self, form):
form.add_error('term', ValidationError('Object not found'))
return self.form_invalid(form)
except Exception as e:
# Handle other exceptions, log the error, etc.
form.add_error('term', ValidationError('Object not found'))
form.add_error('term', ValidationError("Error while searching for target"))
logger.error("Oops something went wrong: " + str(e))
return self.form_invalid(form)

Expand Down
82 changes: 42 additions & 40 deletions bhtom2/bhtom_common/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from rest_framework import serializers
from bhtom_base.bhtom_dataproducts.models import DataProduct, CCDPhotJob
from bhtom2.bhtom_calibration.models import Calibration_data
from bhtom_base.bhtom_targets.models import Target
from django_comments.models import Comment

Expand All @@ -11,61 +12,62 @@ class DataProductSerializer(serializers.ModelSerializer):
camera = serializers.SerializerMethodField()
observatory_name = serializers.SerializerMethodField()
observatory = serializers.SerializerMethodField()
fits_filter = serializers.SerializerMethodField()
calibration_data = serializers.SerializerMethodField()

class Meta:
model = DataProduct
fields = '__all__'

def get_user_name(self, obj):
user_name = obj.user.first_name + " " + obj.user.last_name
return user_name

return f"{obj.user.first_name} {obj.user.last_name}"

def get_user(self, obj):
user = obj.user.username
return user

return obj.user.username

def get_camera(self, obj):
camera = None
try:
camera = obj.observatory.camera.prefix
except Exception as e:
camera = None
return camera

return obj.observatory.camera.prefix
except AttributeError:
return None

def get_target_name(self, obj):
target_name = obj.target.name if obj.target else None
return target_name

return obj.target.name if obj.target else None

def get_target(self, obj):
target = obj.target.id if obj.target else None
return target


return obj.target.id if obj.target else None

def get_observatory_name(self, obj):
observatory_name = None
try:
observatory_name = obj.observatory.camera.observatory.name
except Exception as e:
observatory_name = None
return observatory_name

return obj.observatory.camera.observatory.name
except AttributeError:
return None

def get_observatory(self, obj):
observatory = None
try:
observatory = obj.observatory.camera.observatory.id
except Exception as e:
observatory = None
return observatory

def get_fits_filter(self, obj):
try:
ccdphotjob = CCDPhotJob.objects.get(dataProduct=obj.id)
fits_filter = ccdphotjob.fits_filter
except Exception as e:
fits_filter = None
return fits_filter

return obj.observatory.camera.observatory.id
except AttributeError:
return None


def get_calibration_data(self, obj):

calibration_data = Calibration_data.objects.filter(dataproduct=obj)
return [
{
'id': cal.id,
'time_photometry': cal.modified,
'mjd': cal.mjd,
'calib_survey_filter': f"{cal.use_catalog.survey}/{cal.use_catalog.filters}",
'standardised_to': f"{cal.survey}/{cal.best_filter}" if cal.survey and cal.best_filter else None,
'magnitude': cal.mag,
'zp': cal.zeropoint,
'scatter': cal.scatter,
'number of datapoints used for calibration': cal.npoints,
'outlier fraction': cal.outlier_fraction,
'matching radius[arcsec]': cal.match_distans
}
for cal in calibration_data
]



Expand Down
27 changes: 16 additions & 11 deletions bhtom2/bhtom_common/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from bhtom2.bhtom_common.serializers import DataProductSerializer,CommentSerializer
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger


logger: BHTOMLogger = BHTOMLogger(__name__, 'Bhtom: bhtom_common.views')


Expand Down Expand Up @@ -721,9 +722,11 @@ class GetDataProductApi(views.APIView):
request_body=openapi.Schema(
type=openapi.TYPE_OBJECT,
properties={
'id': openapi.Schema(type=openapi.TYPE_INTEGER),
'data_product_type': openapi.Schema(type=openapi.TYPE_STRING),
'status': openapi.Schema(type=openapi.TYPE_STRING),
'fits_data': openapi.Schema(type=openapi.TYPE_STRING),
'photometry_data': openapi.Schema(type=openapi.TYPE_STRING),
'camera': openapi.Schema(type=openapi.TYPE_STRING),
'created_start': openapi.Schema(type=openapi.TYPE_STRING, format=openapi.FORMAT_DATETIME),
'created_end': openapi.Schema(type=openapi.TYPE_STRING, format=openapi.FORMAT_DATETIME),
Expand Down Expand Up @@ -751,24 +754,29 @@ class GetDataProductApi(views.APIView):
)
def post(self, request):
query = Q()

id = request.data.get('id', None)
data_product_type = request.data.get('data_product_type', None)
status = request.data.get('status', None)
dp_status = request.data.get('status', None)
fits_data = request.data.get('fits_data', None)
photometry_data = request.data.get('photometry_data', None)
camera = request.data.get('camera', None)
created_start = request.data.get('created_start', None)
created_end = request.data.get('created_end', None)
mjd = request.data.get('mjd', None)
page = request.data.get('page', 1)

if id is not None:
query &= Q(id=id)
if photometry_data is not None:
query &= Q(data__contains=photometry_data)
if data_product_type is not None:
query &= Q(data_product_type=data_product_type)
if status is not None:
query &= Q(status=status)
if dp_status is not None:
query &= Q(status=dp_status)
if camera is not None:
query &= Q(observatory__camera__prefix=camera)
if fits_data is not None:
query &= Q(fits_data=fits_data)
query &= Q(fits_data__contains=fits_data)
if created_start is not None:
query &= Q(created__gte=created_start)
if created_end is not None:
Expand All @@ -778,10 +786,8 @@ def post(self, request):

queryset = DataProduct.objects.filter(query).distinct().order_by('-created')

# Determine page size based on user role
page_size = 500 if not request.user.is_staff else 1000

paginator = Paginator(queryset, page_size) # Set page size based on user role
paginator = Paginator(queryset, page_size)

try:
data_products = paginator.page(page)
Expand All @@ -790,16 +796,15 @@ def post(self, request):
except EmptyPage:
data_products = paginator.page(paginator.num_pages)

# Serialize the data
serialized_queryset = self.serializer_class(data_products, many=True).data

return Response({
'count': paginator.count,
'num_pages': paginator.num_pages,
'current_page': data_products.number,
'data': serialized_queryset
}, status=200)


}, status= status.HTTP_200_OK)


class NewsletterView(LoginRequiredMixin, TemplateView):
Expand Down
Loading

0 comments on commit a36e8b7

Please sign in to comment.