Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional fields to api/target_experiment_uploads endpoint (issue 1627) #709

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion viewer/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,10 @@ class Meta:
class ExperimentUploadFilter(filters.FilterSet):
class Meta:
model = ExperimentUpload
fields = ("target", "project", "data_version_major", "data_version_minor")
fields = (
"target",
"project",
"committer",
"data_version_major",
"data_version_minor",
)
31 changes: 31 additions & 0 deletions viewer/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,3 +413,34 @@ def filter_qs(self):

def by_target(self, target):
return self.get_queryset().filter_qs().filter(target=target.id)


class ExperimentUploadQueryset(QuerySet):
def annotated_qs(self):
ExperimentUpload = apps.get_model("viewer", "ExperimentUpload")
qs = ExperimentUpload.objects.prefetch_related(
"target",
"project",
).annotate(
target_name=F("target__title"),
proposal_number=F("project__title"),
committer_name=F("committer__username"),
)

return qs


class ExperimentUploadDataManager(Manager):
def get_queryset(self):
return ExperimentUploadQueryset(self.model, using=self._db)

def annotated_qs(self):
return (
self.get_queryset()
.annotated_qs()
.order_by(
'project',
'target__title',
'upload_version',
)
)
4 changes: 4 additions & 0 deletions viewer/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
CompoundDataManager,
CompoundIdentifierDataManager,
ExperimentDataManager,
ExperimentUploadDataManager,
PoseDataManager,
QuatAssemblyDataManager,
SessionActionsDataManager,
Expand Down Expand Up @@ -199,6 +200,9 @@ class ExperimentUpload(models.Model):
data_version_major = models.PositiveSmallIntegerField(default=0)
data_version_minor = models.PositiveSmallIntegerField(default=0)

objects = models.Manager()
upload_manager = ExperimentUploadDataManager()

def __str__(self) -> str:
return f"{self.project}"

Expand Down
21 changes: 20 additions & 1 deletion viewer/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,16 +895,35 @@ class Meta:


class TargetExperimentReadSerializer(ValidateProjectMixin, serializers.ModelSerializer):
tarball = serializers.CharField()
tarball = serializers.SerializerMethodField()
target_name = serializers.CharField()
proposal_number = serializers.CharField()
committer_name = serializers.CharField()

def get_tarball(self, obj):
request = self.context.get('request')
path = (
Path(settings.MEDIA_URL)
.joinpath(settings.TARGET_LOADER_MEDIA_DIRECTORY)
.joinpath(obj.target.zip_archive.name)
.joinpath(obj.file.name)
)
if request:
return request.build_absolute_uri(path)
else:
return None

class Meta:
model = models.ExperimentUpload
fields = (
'target',
'target_name',
'project',
'proposal_number',
'tarball',
'commit_datetime',
'committer',
'committer_name',
'task_id',
'neighbourhood_transforms',
'conformer_site_transforms',
Expand Down
18 changes: 1 addition & 17 deletions viewer/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
from dateutil.parser import parse
from django.conf import settings
from django.contrib.auth import get_user_model
from django.db.models import F, Value
from django.db.models.fields import CharField
from django.db.models.functions import Concat
from django.http import FileResponse, HttpResponse, JsonResponse
from django.shortcuts import get_object_or_404, redirect, render
from django.urls import reverse
Expand Down Expand Up @@ -1837,20 +1834,7 @@ def create(self, request, *args, **kwargs):


class ExperimentUploadView(ISPyBSafeQuerySet):
queryset = models.ExperimentUpload.objects.annotate(
tarball=Concat(
Value(settings.TARGET_LOADER_MEDIA_DIRECTORY),
Value('/'),
F('target__zip_archive'),
Value('/'),
F('file'),
output_field=CharField(),
),
).order_by(
'project',
'target__title',
'upload_version',
)
queryset = models.ExperimentUpload.upload_manager.annotated_qs()
serializer_class = serializers.TargetExperimentReadSerializer
permission_class = [permissions.IsAuthenticated]
filterset_class = filters.ExperimentUploadFilter
Expand Down
Loading