-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiso_utils.py
More file actions
208 lines (177 loc) · 6.54 KB
/
iso_utils.py
File metadata and controls
208 lines (177 loc) · 6.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import os.path
from uuid import uuid4
from django.utils import timezone
from django.core.files.base import ContentFile
from repository import models as repo_models
from utils import setting_handler
from plugins.isolinear import convert, plugin_settings
from core import files
from identifiers import preprints
def get_pdf_path(journal, article, file):
# generate a cover sheet using the cover sheet xml setting.
plugin = plugin_settings.IsolinearPlugin.get_self()
isolinear_cover_sheet = setting_handler.get_plugin_setting(
plugin,
'isolinear_cover_sheet',
journal,
).value
repo_code = setting_handler.get_plugin_setting(
plugin,
'isolinear_repository_code',
journal,
).value
repository = repo_models.Repository.objects.get(
short_name=repo_code,
)
context = {
'repository': repository,
'article': article,
'authors': article.frozen_authors(),
}
cover_sheet_path = convert.generate_pdf_cover_sheet(
isolinear_cover_sheet,
context,
)
# check if the supplied file is a PDF, if it is, we only need to splice.
file_mime = files.file_path_mime(
file.get_file_path(article)
)
if not file_mime == 'application/pdf':
article_pdf_path = convert.generate_pdf_from_doc(
file,
)
else:
article_pdf_path = file.get_file_path(article)
# splice the two files together and return the saved path
spliced_file_path = convert.splice_pdf_files_together(
article_pdf_path,
cover_sheet_path,
)
return spliced_file_path
def publish_repository_object_from_journal_article(article, repository, file):
preprint, _ = repo_models.Preprint.objects.get_or_create(
repository=repository,
owner=article.owner,
title=article.title,
article=article,
defaults={
'date_submitted': article.date_submitted,
'date_accepted': timezone.now(),
'date_published': timezone.now(),
'date_updated': timezone.now(),
'abstract': article.abstract,
'stage': repo_models.STAGE_PREPRINT_PUBLISHED,
'license': article.license,
},
)
for keyword in article.keywords.all():
preprint.keywords.add(keyword)
# Create a dummy file to allow the PreprintVersion to be generated first,
# so the version ID can be used in the final file.
placeholder_file = ContentFile(b"")
placeholder_file.name = f"{uuid4()}_placeholder.pdf"
preprint_file, _ = repo_models.PreprintFile.objects.get_or_create(
preprint=preprint,
file=placeholder_file,
original_filename="placeholder.pdf",
mime_type="application/pdf",
size=0,
defaults={
'uploaded': timezone.now(),
},
)
version, _ = repo_models.PreprintVersion.objects.get_or_create(
preprint=preprint,
version=1,
title=preprint.title,
abstract=preprint.abstract,
file=preprint_file,
)
if article.preprint.repository.crossref_enable:
preprints.deposit_doi_for_preprint_version(
repository=article.preprint.repository,
preprint_versions=[version],
)
file_path = get_pdf_path(
article.journal,
article,
file,
)
with open(file_path, 'rb') as preprint_pdf:
file = ContentFile(preprint_pdf.read())
file.name = f"{uuid4()}.pdf"
preprint_file.file = file
preprint_file.original_filename = article.manuscript_files.all().first().original_filename
preprint_file.size = os.path.getsize(file_path)
preprint_file.save()
version.file = preprint_file
version.save()
preprint.submission_file = preprint_file
preprint.save()
for author in article.frozen_authors():
repo_models.PreprintAuthor.objects.get_or_create(
preprint=preprint,
account=author.author,
order=author.order,
affiliation=author.affiliation
)
def recreate_version_file(article, version, file):
file_path = get_pdf_path(article.journal, article, file)
with open(file_path, 'rb') as preprint_pdf:
file = ContentFile(preprint_pdf.read())
file.name = f"{uuid4()}.pdf"
preprint_file = repo_models.PreprintFile.objects.create(
preprint=article.preprint,
file=file,
original_filename=article.manuscript_files.all().first().original_filename,
mime_type='application/pdf',
size=os.path.getsize(file_path),
uploaded=timezone.now(),
)
version.file = preprint_file
version.save()
def publish_new_preprint_version(article, file):
"""
Publishes a new preprint version and attaches a generated PDF file.
Ensures the version ID is included in the cover sheet by creating the
version before generating the file.
"""
# Create a placeholder PreprintFile object
placeholder_file = ContentFile(b"")
placeholder_file.name = f"{uuid4()}_placeholder.pdf"
preprint_file, _ = repo_models.PreprintFile.objects.get_or_create(
preprint=article.preprint,
file=placeholder_file,
original_filename="placeholder.pdf",
mime_type="application/pdf",
size=0,
defaults={
"uploaded": timezone.now(),
}
)
# Create the version with the placeholder file
version, _ = repo_models.PreprintVersion.objects.get_or_create(
preprint=article.preprint,
file=preprint_file,
version=article.preprint.next_version_number(),
title=article.preprint.title,
abstract=article.preprint.abstract,
)
if article.preprint.repository.crossref_enable:
preprints.deposit_doi_for_preprint_version(
repository=article.preprint.repository,
preprint_versions=[version],
)
# Generate the actual PDF file with the version ID on the cover sheet
file_path = get_pdf_path(article.journal, article, file)
with open(file_path, "rb") as preprint_pdf:
generated_file = ContentFile(preprint_pdf.read())
generated_file.name = f"{uuid4()}.pdf"
# Update the PreprintFile with the generated file
preprint_file.file = generated_file
preprint_file.original_filename = article.manuscript_files.all().first().original_filename
preprint_file.size = os.path.getsize(file_path)
preprint_file.save()
# Update the version with the final file
version.file = preprint_file
version.save()