def get_form_initial_data(self, **kwargs): """ Calls super and returns form's initial values. """ data = super().get_form_initial_data(**kwargs) if self.paper.abstract: data['abstract'] = kill_html(self.paper.abstract) else: self.paper.consolidate_metadata(wait=False) # We try to find an email, if we do not succed, that's ok up = UserPreferences.get_by_user(user=self.user) if up.email: data['email'] = up.email else: try: r = Researcher.objects.get(user=self.user) except ObjectDoesNotExist: pass except MultipleObjectsReturned: logger.warning("User with id {} has multiple researcher objects assigned".format(self.user.id)) else: if r.email: data['email'] = r.email return data
def get_form_kwargs(self): """ We pass an instance of the model """ form_kwargs = super().get_form_kwargs() form_kwargs['instance'] = UserPreferences.get_by_user(self.request.user) return form_kwargs
def start_view(request, pk): paper = get_object_or_404(Paper.objects.prefetch_related('oairecord_set'), pk=pk) repositories_protocol = get_all_repositories_and_protocols( paper, request.user) used_protocols = set([proto for repo, proto in repositories_protocol]) available_repositories = sorted( [repo for repo, proto in repositories_protocol], key=lambda r: r.name.lower()) # select the most appropriate repository userprefs = UserPreferences.get_by_user(request.user) preselected_repository = userprefs.get_preferred_or_last_repository() preselected_protocol = None if preselected_repository: preselected_protocol = { repo.id: proto for repo, proto in repositories_protocol }.get(preselected_repository.id, None) # If the preferred repository is not available for this paper, pick any if not preselected_protocol: for repo, protocol in repositories_protocol: if protocol is not None: preselected_repository = repo preselected_protocol = protocol break breadcrumbs = paper.breadcrumbs() breadcrumbs.append((_('Deposit'), '')) context = { 'paper': paper, 'max_file_size': settings.DEPOSIT_MAX_FILE_SIZE, 'available_repositories': available_repositories, 'selected_repository': preselected_repository, 'selected_protocol': preselected_protocol, 'is_owner': paper.is_owned_by(request.user, flexible=True), 'breadcrumbs': breadcrumbs, 'repositoryForm': None, 'paper_form': PaperDepositForm(initial={'radioUploadType': request.GET.get('type')}), 'collapse_doctype': request.GET.get('type') in ['preprint', 'postprint', 'pdfversion'], 'used_protocols': used_protocols, } return render(request, 'deposit/start.html', context)
def edit_global_preferences(request): context = { 'repositories': Repository.objects.filter(enabled=True), } prefs = UserPreferences.get_by_user(request.user) if request.method == 'POST': pref_form = UserPreferencesForm(request.POST, instance=prefs) pref_form.save() pref_form = UserPreferencesForm(instance=prefs) if not pref_form: raise Http404(_('This repository does not have any settings.')) context['preferences_form'] = pref_form return render(request, 'deposit/global_preferences.html', context)
def edit_global_preferences(request): context = { 'repositories': Repository.objects.all(), } prefs = UserPreferences.get_by_user(request.user) if request.method == 'POST': pref_form = UserPreferencesForm(request.POST, instance=prefs) pref_form.save() pref_form = UserPreferencesForm(instance=prefs) if not pref_form: raise Http404(_('This repository does not have any settings.')) context['preferences_form'] = pref_form return render(request, 'deposit/global_preferences.html', context)
def start_view(request, pk): paper = get_object_or_404(Paper, pk=pk) repositories = get_all_repositories_and_protocols(paper, request.user) repositories_protocol = {repo.id: proto for repo, proto in repositories} # select the most appropriate repository selected_repository = None selected_protocol = None # Try to get the preferred one userprefs = UserPreferences.get_by_user(request.user) if userprefs.preferred_repository: selected_repository = userprefs.preferred_repository selected_protocol = repositories_protocol.get(selected_repository.id) # Try to get the last one used by this user print userprefs.last_repository if selected_protocol is None and userprefs.last_repository: selected_repository = userprefs.last_repository selected_protocol = repositories_protocol.get(selected_repository.id) # If none of these are available, pick any if selected_protocol is None: for repo, protocol in repositories: if protocol is not None: selected_repository = repo selected_protocol = protocol break breadcrumbs = paper.breadcrumbs() breadcrumbs.append((_('Deposit'), '')) context = { 'paper': paper, 'max_file_size': DEPOSIT_MAX_FILE_SIZE, 'available_repositories': repositories, 'selected_repository': selected_repository, 'selected_protocol': selected_protocol, 'is_owner': paper.is_owned_by(request.user, flexible=True), 'breadcrumbs': breadcrumbs, 'repositoryForm': None, } if request.GET.get('type') not in [ None, 'preprint', 'postprint', 'pdfversion' ]: return HttpResponseForbidden() return render(request, 'deposit/start.html', context)
def start_view(request, pk): paper = get_object_or_404(Paper, pk=pk) repositories = get_all_repositories_and_protocols(paper, request.user) repositories_protocol = {repo.id :proto for repo, proto in repositories} # select the most appropriate repository selected_repository = None selected_protocol = None # Try to get the preferred one userprefs = UserPreferences.get_by_user(request.user) if userprefs.preferred_repository: selected_repository = userprefs.preferred_repository selected_protocol = repositories_protocol.get(selected_repository.id) # Try to get the last one used by this user logger.info(userprefs.last_repository) if selected_protocol is None and userprefs.last_repository: selected_repository = userprefs.last_repository selected_protocol = repositories_protocol.get(selected_repository.id) # If none of these are available, pick any if selected_protocol is None: for repo, protocol in repositories: if protocol is not None: selected_repository = repo selected_protocol = protocol break breadcrumbs = paper.breadcrumbs() breadcrumbs.append((_('Deposit'), '')) context = { 'paper': paper, 'max_file_size': settings.DEPOSIT_MAX_FILE_SIZE, 'available_repositories': repositories, 'selected_repository': selected_repository, 'selected_protocol': selected_protocol, 'is_owner': paper.is_owned_by(request.user, flexible=True), 'breadcrumbs': breadcrumbs, 'repositoryForm': None, } if request.GET.get('type') not in [None, 'preprint', 'postprint', 'pdfversion']: return HttpResponseForbidden() return render(request, 'deposit/start.html', context)
def get_preselected_repository(user, repositories): """ This returns the preselected repository of a user out of a given list We look in the user preferences, if no suitable repository is found, we try from shibboleth data, else we take the last, or finally, none :param user: User object :returns: Repository or None """ user_preferences = UserPreferences.get_by_user(user) preferred_repository = user_preferences.get_preferred_repository() if preferred_repository in repositories: return preferred_repository # Let's try via shibboleth identifier = 'shib:{}'.format(get_users_idp(user)) institutional_repository = Institution.objects.get_repository_by_identifier( identifier) if institutional_repository in repositories: return institutional_repository last_repository = user_preferences.get_last_repository() if last_repository in repositories: return last_repository
def submitDeposit(request, pk): paper = get_object_or_404(Paper, pk=pk) context = {'status': 'error'} form = PaperDepositForm(data=request.POST) if not form.is_valid(): context['form'] = form.errors return context, 400 # This validation could take place in the form (but we need access to the # paper and user?) repository = form.cleaned_data['radioRepository'] protocol = repository.protocol_for_deposit(paper, request.user) if protocol is None: context['radioRepository'] = _( "This repository cannot be used for this paper.") return context, 400 repositoryForm = protocol.get_bound_form(request.POST) if not repositoryForm.is_valid(): request_context = RequestContext(request) form_html = render_crispy_form(repositoryForm, context=request_context) context['form_html'] = form_html return context, 400 # Store the repository as in the user preferences userprefs = UserPreferences.get_by_user(request.user) userprefs.last_repository = repository userprefs.save(update_fields=['last_repository']) # Check that the paper has been uploaded by the same user pdf = form.cleaned_data['file_id'] if pdf.user != request.user: context['message'] = _('Access to the PDF was denied.') return context, 400 # Submit the paper to the repository path = os.path.join(settings.MEDIA_ROOT, pdf.file.name) # Create initial record d = DepositRecord(paper=paper, user=pdf.user, repository=repository, upload_type=form.cleaned_data['radioUploadType'], file=pdf) d.save() submitResult = protocol.submit_deposit_wrapper(path, repositoryForm) d.request = submitResult.logs if submitResult.status == 'failed': context['message'] = submitResult.message d.save() # Send the failed deposit as error to sentry msg = "Deposit failed for id %s for paper %s \n\n" % (d.pk, paper.pk) logger.error(msg + submitResult.logs) return context, 400 d.identifier = submitResult.identifier d.additional_info = submitResult.additional_info d.status = submitResult.status d.oairecord = submitResult.oairecord d.license = submitResult.license d.save() paper.update_availability() paper.save() paper.update_index() context['status'] = 'success' # TODO change this (we don't need it) context['upload_id'] = d.id return context
def submitDeposit(request, pk): paper = get_object_or_404(Paper, pk=pk) context = {'status': 'error'} form = PaperDepositForm(data=request.POST) if not form.is_valid(): context['form'] = form.errors return context, 400 # This validation could take place in the form (but we need access to the # paper and user?) repository = form.cleaned_data['radioRepository'] protocol = repository.protocol_for_deposit(paper, request.user) if protocol is None: context['radioRepository'] = _( "This repository cannot be used for this paper.") return context, 400 repositoryForm = protocol.get_bound_form(request.POST) if not repositoryForm.is_valid(): request_context = RequestContext(request) form_html = render_crispy_form(repositoryForm, context=request_context) context['form_html'] = form_html return context, 400 # Store the repository as in the user preferences userprefs = UserPreferences.get_by_user(request.user) userprefs.last_repository = repository userprefs.save(update_fields=['last_repository']) # Check that the paper has been uploaded by the same user pdf = form.cleaned_data['file_id'] if pdf.user != request.user: context['message'] = _('Access to the PDF was denied.') return context, 400 # Submit the paper to the repository path = os.path.join(settings.MEDIA_ROOT, pdf.file.name) # Create initial record d = DepositRecord( paper=paper, user=pdf.user, repository=repository, upload_type=form.cleaned_data['radioUploadType'], file=pdf) d.save() submitResult = protocol.submit_deposit_wrapper(path, repositoryForm) d.request = submitResult.logs if submitResult.status == 'failed': context['message'] = submitResult.message d.save() return context, 400 d.identifier = submitResult.identifier d.additional_info = submitResult.additional_info d.status = submitResult.status d.oairecord = submitResult.oairecord d.save() paper.update_availability() paper.save() paper.update_index() context['status'] = 'success' # TODO change this (we don't need it) context['upload_id'] = d.id return context