|
49 | 49 | from sqlalchemy.orm.exc import NoResultFound
|
50 | 50 | from werkzeug.local import LocalProxy
|
51 | 51 |
|
| 52 | +from cap.modules.auth.ext import _fetch_token |
52 | 53 | from cap.modules.deposit.errors import DisconnectWebhookError, FileUploadError
|
53 | 54 | from cap.modules.deposit.validators import NoRequiredValidator
|
54 | 55 | from cap.modules.experiments.permissions import exp_need_factory
|
|
75 | 76 | UpdateDepositPermission)
|
76 | 77 |
|
77 | 78 | from .review import Reviewable
|
| 79 | +from .tasks import upload_to_zenodo |
| 80 | +from .utils import create_zenodo_deposit |
78 | 81 |
|
79 | 82 | _datastore = LocalProxy(lambda: current_app.extensions['security'].datastore)
|
80 | 83 |
|
@@ -254,53 +257,82 @@ def upload(self, pid, *args, **kwargs):
|
254 | 257 | _, rec = request.view_args.get('pid_value').data
|
255 | 258 | record_uuid = str(rec.id)
|
256 | 259 | 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.') |
| 260 | + target = data.get('target') |
| 261 | + |
| 262 | + if target == 'zenodo': |
| 263 | + # check for token |
| 264 | + token = _fetch_token('zenodo') |
| 265 | + if not token: |
| 266 | + raise FileUploadError( |
| 267 | + 'Please connect your Zenodo account ' |
| 268 | + 'before creating a deposit.') |
| 269 | + |
| 270 | + files = data.get('files') |
| 271 | + bucket = data.get('bucket') |
| 272 | + zenodo_data = data.get('zenodo_data', {}) |
| 273 | + |
| 274 | + if files and bucket: |
| 275 | + zenodo_deposit = create_zenodo_deposit(token, zenodo_data) # noqa |
| 276 | + self.setdefault('_zenodo', []).append(zenodo_deposit) |
| 277 | + self.commit() |
| 278 | + |
| 279 | + # upload files to zenodo deposit |
| 280 | + upload_to_zenodo.delay( |
| 281 | + record_uuid, files, bucket, token, |
| 282 | + zenodo_deposit['id'], |
| 283 | + zenodo_deposit['links']['bucket']) |
| 284 | + else: |
| 285 | + raise FileUploadError( |
| 286 | + 'You cannot create an empty Zenodo deposit. ' |
| 287 | + 'Please add some files.') |
| 288 | + else: |
| 289 | + webhook = data.get('webhook', False) |
| 290 | + event_type = data.get('event_type', 'release') |
287 | 291 |
|
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.') |
| 292 | + try: |
| 293 | + url = data['url'] |
| 294 | + except KeyError: |
| 295 | + raise FileUploadError('Missing url parameter.') |
293 | 296 |
|
294 |
| - create_webhook(record_uuid, api, event_type) |
295 |
| - 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) |
| 297 | + try: |
| 298 | + host, owner, repo, branch, filepath = parse_git_url(url) # noqa |
| 299 | + api = create_git_api(host, owner, repo, branch, |
| 300 | + current_user.id) |
| 301 | + |
| 302 | + if filepath: |
| 303 | + if webhook: |
| 304 | + raise FileUploadError( |
| 305 | + 'You cannot create a webhook on a file') |
| 306 | + |
| 307 | + download_repo_file( |
| 308 | + record_uuid, |
| 309 | + f'repositories/{host}/{owner}/{repo}/{api.branch or api.sha}/{filepath}', # noqa |
| 310 | + *api.get_file_download(filepath), |
| 311 | + api.auth_headers, |
| 312 | + ) |
| 313 | + elif webhook: |
| 314 | + if event_type == 'release': |
| 315 | + if branch: |
| 316 | + raise FileUploadError( |
| 317 | + 'You cannot create a release webhook' |
| 318 | + ' for a specific branch or sha.') |
| 319 | + |
| 320 | + if event_type == 'push' and \ |
| 321 | + api.branch is None and api.sha: |
| 322 | + raise FileUploadError( |
| 323 | + 'You cannot create a push webhook' |
| 324 | + ' for a specific sha.') |
| 325 | + |
| 326 | + create_webhook(record_uuid, api, event_type) |
| 327 | + else: |
| 328 | + download_repo.delay( |
| 329 | + record_uuid, |
| 330 | + f'repositories/{host}/{owner}/{repo}/{api.branch or api.sha}.tar.gz', # noqa |
| 331 | + api.get_repo_download(), |
| 332 | + api.auth_headers) |
301 | 333 |
|
302 |
| - except GitError as e: |
303 |
| - raise FileUploadError(str(e)) |
| 334 | + except GitError as e: |
| 335 | + raise FileUploadError(str(e)) |
304 | 336 |
|
305 | 337 | return self
|
306 | 338 |
|
@@ -584,16 +616,15 @@ def validate(self, **kwargs):
|
584 | 616 |
|
585 | 617 | validator = NoRequiredValidator(schema, resolver=resolver)
|
586 | 618 |
|
587 |
| - result = {} |
588 |
| - result['errors'] = [ |
| 619 | + errors = [ |
589 | 620 | FieldError(
|
590 | 621 | list(error.path)+error.validator_value,
|
591 | 622 | str(error.message))
|
592 | 623 | for error in validator.iter_errors(self)
|
593 | 624 | ]
|
594 | 625 |
|
595 |
| - if result['errors']: |
596 |
| - raise DepositValidationError(None, errors=result['errors']) |
| 626 | + if errors: |
| 627 | + raise DepositValidationError(None, errors=errors) |
597 | 628 | except RefResolutionError:
|
598 | 629 | raise DepositValidationError('Schema {} not found.'.format(
|
599 | 630 | self['$schema']))
|
|
0 commit comments