|
75 | 75 | UpdateDepositPermission) |
76 | 76 |
|
77 | 77 | from .review import Reviewable |
| 78 | +from .tasks import upload_to_zenodo |
78 | 79 |
|
79 | 80 | _datastore = LocalProxy(lambda: current_app.extensions['security'].datastore) |
80 | 81 |
|
@@ -254,53 +255,65 @@ def upload(self, pid, *args, **kwargs): |
254 | 255 | _, rec = request.view_args.get('pid_value').data |
255 | 256 | record_uuid = str(rec.id) |
256 | 257 | data = request.get_json() |
257 | | - webhook = data.get('webhook', False) |
258 | | - event_type = data.get('event_type', 'release') |
259 | | - |
260 | | - try: |
261 | | - url = data['url'] |
262 | | - except KeyError: |
263 | | - raise FileUploadError('Missing url parameter.') |
264 | | - |
265 | | - try: |
266 | | - host, owner, repo, branch, filepath = parse_git_url(url) |
267 | | - api = create_git_api(host, owner, repo, branch, |
268 | | - current_user.id) |
269 | | - |
270 | | - if filepath: |
271 | | - if webhook: |
272 | | - raise FileUploadError( |
273 | | - 'You cannot create a webhook on a file') |
274 | | - |
275 | | - download_repo_file( |
276 | | - record_uuid, |
277 | | - f'repositories/{host}/{owner}/{repo}/{api.branch or api.sha}/{filepath}', # noqa |
278 | | - *api.get_file_download(filepath), |
279 | | - api.auth_headers, |
280 | | - ) |
281 | | - elif webhook: |
282 | | - if event_type == 'release': |
283 | | - if branch: |
284 | | - raise FileUploadError( |
285 | | - 'You cannot create a release webhook' |
286 | | - ' for a specific branch or sha.') |
287 | | - |
288 | | - if event_type == 'push' and \ |
289 | | - api.branch is None and api.sha: |
290 | | - raise FileUploadError( |
291 | | - 'You cannot create a push webhook' |
292 | | - ' for a specific sha.') |
| 258 | + target = data.get('target') |
293 | 259 |
|
294 | | - create_webhook(record_uuid, api, event_type) |
| 260 | + if target == 'zenodo': |
| 261 | + files = data.get('files') |
| 262 | + bucket = data.get('bucket') |
| 263 | + if files and bucket: |
| 264 | + upload_to_zenodo.delay(record_uuid, files, bucket) |
295 | 265 | else: |
296 | | - download_repo.delay( |
297 | | - record_uuid, |
298 | | - f'repositories/{host}/{owner}/{repo}/{api.branch or api.sha}.tar.gz', # noqa |
299 | | - api.get_repo_download(), |
300 | | - api.auth_headers) |
| 266 | + raise FileUploadError( |
| 267 | + 'You cannot create an empty Zenodo deposit. ' |
| 268 | + 'Please add some files.') |
| 269 | + else: |
| 270 | + webhook = data.get('webhook', False) |
| 271 | + event_type = data.get('event_type', 'release') |
| 272 | + |
| 273 | + try: |
| 274 | + url = data['url'] |
| 275 | + except KeyError: |
| 276 | + raise FileUploadError('Missing url parameter.') |
| 277 | + |
| 278 | + try: |
| 279 | + host, owner, repo, branch, filepath = parse_git_url(url) # noqa |
| 280 | + api = create_git_api(host, owner, repo, branch, |
| 281 | + current_user.id) |
| 282 | + |
| 283 | + if filepath: |
| 284 | + if webhook: |
| 285 | + raise FileUploadError( |
| 286 | + 'You cannot create a webhook on a file') |
| 287 | + |
| 288 | + download_repo_file( |
| 289 | + record_uuid, |
| 290 | + f'repositories/{host}/{owner}/{repo}/{api.branch or api.sha}/{filepath}', # noqa |
| 291 | + *api.get_file_download(filepath), |
| 292 | + api.auth_headers, |
| 293 | + ) |
| 294 | + elif webhook: |
| 295 | + if event_type == 'release': |
| 296 | + if branch: |
| 297 | + raise FileUploadError( |
| 298 | + 'You cannot create a release webhook' |
| 299 | + ' for a specific branch or sha.') |
| 300 | + |
| 301 | + if event_type == 'push' and \ |
| 302 | + api.branch is None and api.sha: |
| 303 | + raise FileUploadError( |
| 304 | + 'You cannot create a push webhook' |
| 305 | + ' for a specific sha.') |
| 306 | + |
| 307 | + create_webhook(record_uuid, api, event_type) |
| 308 | + else: |
| 309 | + download_repo.delay( |
| 310 | + record_uuid, |
| 311 | + f'repositories/{host}/{owner}/{repo}/{api.branch or api.sha}.tar.gz', # noqa |
| 312 | + api.get_repo_download(), |
| 313 | + api.auth_headers) |
301 | 314 |
|
302 | | - except GitError as e: |
303 | | - raise FileUploadError(str(e)) |
| 315 | + except GitError as e: |
| 316 | + raise FileUploadError(str(e)) |
304 | 317 |
|
305 | 318 | return self |
306 | 319 |
|
@@ -584,16 +597,15 @@ def validate(self, **kwargs): |
584 | 597 |
|
585 | 598 | validator = NoRequiredValidator(schema, resolver=resolver) |
586 | 599 |
|
587 | | - result = {} |
588 | | - result['errors'] = [ |
| 600 | + errors = [ |
589 | 601 | FieldError( |
590 | 602 | list(error.path)+error.validator_value, |
591 | 603 | str(error.message)) |
592 | 604 | for error in validator.iter_errors(self) |
593 | 605 | ] |
594 | 606 |
|
595 | | - if result['errors']: |
596 | | - raise DepositValidationError(None, errors=result['errors']) |
| 607 | + if errors: |
| 608 | + raise DepositValidationError(None, errors=errors) |
597 | 609 | except RefResolutionError: |
598 | 610 | raise DepositValidationError('Schema {} not found.'.format( |
599 | 611 | self['$schema'])) |
|
0 commit comments