Skip to content

Commit 72eada3

Browse files
committed
fix: the return of the ribbon
Ribbon was missing because the 'template_protein' attribute was missing, because the path was compiled incorrectly and file was not found. Template protein file is not explicitly given in metadata, so trial-error method is used.
1 parent 99118e4 commit 72eada3

File tree

3 files changed

+44
-31
lines changed

3 files changed

+44
-31
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Generated by Django 3.2.25 on 2024-09-06 12:43
2+
3+
from django.db import migrations
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('viewer', '0061_auto_20240905_0756'),
10+
('viewer', '0062_experiment_code_prefix'),
11+
]
12+
13+
operations = [
14+
]

viewer/models.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,7 @@ def get_upload_path(self):
195195
return (
196196
Path(settings.MEDIA_ROOT)
197197
.joinpath(settings.TARGET_LOADER_MEDIA_DIRECTORY)
198-
.joinpath(self.task_id)
199-
.joinpath(Path(str(self.file)).stem)
198+
.joinpath(self.target.zip_archive.name)
200199
.joinpath(self.upload_data_dir)
201200
)
202201

@@ -205,7 +204,6 @@ def get_download_path(self):
205204
return (
206205
Path(settings.MEDIA_ROOT)
207206
.joinpath(settings.TARGET_LOADER_MEDIA_DIRECTORY)
208-
.joinpath(self.task_id)
209207
.joinpath(Path(str(self.file)))
210208
)
211209

viewer/serializers.py

+29-28
Original file line numberDiff line numberDiff line change
@@ -134,19 +134,8 @@ class TargetSerializer(serializers.ModelSerializer):
134134
zip_archive = serializers.SerializerMethodField()
135135
metadata = serializers.SerializerMethodField()
136136

137-
def get_template_protein(self, obj):
138-
exp_upload = (
139-
models.ExperimentUpload.objects.filter(
140-
target=obj,
141-
)
142-
.order_by('-commit_datetime')
143-
.first()
144-
)
145-
146-
yaml_path = exp_upload.get_upload_path()
147-
148-
# last components of path, need for reconstruction later
149-
comps = yaml_path.parts[-2:]
137+
def get_template_protein_path(self, experiment_upload) -> Path | None:
138+
yaml_path = experiment_upload.get_upload_path()
150139

151140
# and the file itself
152141
yaml_path = yaml_path.joinpath(XTALFORMS_FILE)
@@ -158,44 +147,56 @@ def get_template_protein(self, obj):
158147
assemblies = contents["assemblies"]
159148
except KeyError:
160149
logger.error("No 'assemblies' section in '%s'", XTALFORMS_FILE)
161-
return ''
150+
return None
162151

163152
try:
164153
first = list(assemblies.values())[0]
165154
except IndexError:
166155
logger.error("No assemblies in 'assemblies' section")
167-
return ''
156+
return None
168157

169158
try:
170159
reference = first["reference"]
171160
except KeyError:
172161
logger.error("No assemblies in 'assemblies' section")
173-
return ''
162+
return None
174163

175164
ref_path = (
176165
Path(settings.TARGET_LOADER_MEDIA_DIRECTORY)
177-
.joinpath(exp_upload.task_id)
178-
.joinpath(comps[0])
179-
.joinpath(comps[1])
166+
.joinpath(experiment_upload.target.zip_archive.name)
167+
.joinpath(experiment_upload.upload_data_dir)
180168
.joinpath("crystallographic_files")
181169
.joinpath(reference)
182170
.joinpath(f"{reference}.pdb")
183171
)
184172
logger.debug('ref_path: %s', ref_path)
185173
if Path(settings.MEDIA_ROOT).joinpath(ref_path).is_file():
186-
request = self.context.get('request', None)
187-
if request is not None:
188-
return request.build_absolute_uri(
189-
Path(settings.MEDIA_URL).joinpath(ref_path)
190-
)
191-
else:
192-
return ''
174+
return ref_path
193175
else:
194176
logger.error("Reference pdb file doesn't exist")
195-
return ''
177+
return None
196178
else:
197179
logger.error("'%s' missing", XTALFORMS_FILE)
198-
return ''
180+
return None
181+
182+
def get_template_protein(self, obj):
183+
# loop through exp uploads from latest to earliest, and try to
184+
# find template protein
185+
for exp_upload in models.ExperimentUpload.objects.filter(
186+
target=obj,
187+
).order_by('-commit_datetime'):
188+
path = self.get_template_protein_path(exp_upload)
189+
if path is None:
190+
continue
191+
else:
192+
request = self.context.get('request', None)
193+
if request is not None:
194+
return request.build_absolute_uri(
195+
Path(settings.MEDIA_URL).joinpath(path)
196+
)
197+
else:
198+
return None
199+
return None
199200

200201
def get_zip_archive(self, obj):
201202
# The if-check is because the filefield in target has null=True.

0 commit comments

Comments
 (0)