Newer
Older
__copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)"
__license__ = "AGPL v3"
from dal import autocomplete
from django.contrib.auth.models import Group
from django.contrib.auth.decorators import (
login_required,
permission_required,
user_passes_test,
)
from django.core.paginator import Paginator
from django.urls import reverse, reverse_lazy
from django.http import HttpResponse, Http404
from django.shortcuts import get_object_or_404, render, redirect
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.views.generic.list import ListView
from colleges.permissions import (
is_edadmin_or_senior_fellow,
is_edadmin_or_advisory_or_active_regular_or_senior_fellow,
from colleges.utils import check_profile_eligibility_for_fellowship
from invitations.constants import INVITATION_EDITORIAL_FELLOW
from invitations.models import RegistrationInvitation
from scipost.constants import TITLE_DR
from scipost.permissions import HTMXPermissionsDenied, HTMXResponse
POTENTIAL_FELLOWSHIP_STATUSES,
POTENTIAL_FELLOWSHIP_EVENT_STATUSUPDATED,
POTENTIAL_FELLOWSHIP_INVITED,
POTENTIAL_FELLOWSHIP_ACTIVE_IN_COLLEGE,
potential_fellowship_statuses_dict,
POTENTIAL_FELLOWSHIP_EVENT_VOTED_ON,
POTENTIAL_FELLOWSHIP_EVENT_EMAILED,
)
from .forms import (
FellowshipNominationSearchForm,
FellowshipNominationVotingRoundStartForm,
FellowshipDynSelForm,
FellowshipForm,
FellowshipRemoveSubmissionForm,
FellowshipAddSubmissionForm,
SubmissionAddFellowshipForm,
FellowshipRemoveProceedingsForm,
FellowshipAddProceedingsForm,
PotentialFellowshipForm,
PotentialFellowshipStatusForm,
PotentialFellowshipEventForm,
FellowshipNominationForm,
FellowshipNominationSearchForm,
FellowshipNominationDecisionForm,
FellowshipInvitationResponseForm,
College,
Fellowship,
PotentialFellowship,
PotentialFellowshipEvent,
FellowshipNomination,
FellowshipNominationVotingRound,
FellowshipNominationVote,
FellowshipNominationDecision,
FellowshipInvitation,
from scipost.forms import EmailUsersForm, SearchTextForm
from scipost.mixins import PermissionsMixin, PaginationMixin, RequestViewMixin
from scipost.models import Contributor
from common.utils import Q_with_alternative_spellings
from mails.views import MailView, MailEditorSubviewHTMX
from ontology.models import Branch
from profiles.models import Profile
from profiles.forms import ProfileDynSelForm
class CollegeListView(ListView):
model = College
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context["branches"] = Branch.objects.all()
class CollegeDetailView(DetailView):
model = College
template_name = "colleges/college_detail.html"
def get_object(self, queryset=None):
"""
Bypass django.views.generic.detail.SingleObjectMixin:
since CollegeSlugConverter already found the College as a kwarg, just pass that object on.
"""
return self.kwargs["college"]
class FellowshipAutocompleteView(autocomplete.Select2QuerySetView):
"""
View to feed the Select2 widget.
"""
def get_queryset(self):
qs = Fellowship.objects.all()
if self.q:
qs = qs.filter(
Q(contributor__profile__first_name__icontains=self.q)
| Q(contributor__profile__last_name__icontains=self.q)
).distinct()
class FellowshipCreateView(PermissionsMixin, CreateView):
"""
Create a new Fellowship instance for an existing Contributor.
A new Fellowship can be created only for:
* an existing Fellow who is renewed
* out of an existing PotentialFellowship (elected, or named by Admin)
If the elected/named Fellow does not yet have a Contributor object,
this must be set up first.
"""
permission_required = "scipost.can_manage_college_composition"
form_class = FellowshipForm
template_name = "colleges/fellowship_form.html"
def get_initial(self):
initial = super().get_initial()
contributor = get_object_or_404(
Contributor, pk=self.kwargs.get("contributor_id")
)
initial.update(
{
"contributor": contributor.id,
"start_date": datetime.date.today(),
"until_date": datetime.date.today()
+ datetime.timedelta(days=int(5 * 365.25)),
}
)
return initial
def form_valid(self, form):
"""
Save the new Fellowship, add College rights and update the status of any PotentialFellowship and FellowshipNomination.
"""
self.object = form.save()
group = Group.objects.get(name="Editorial College")
self.object.contributor.user.groups.add(group)
potfels = PotentialFellowship.objects.filter(
profile=self.object.contributor.profile
)
for potfel in potfels:
potfelevent = PotentialFellowshipEvent(
potfel=potfel,
event=POTENTIAL_FELLOWSHIP_EVENT_STATUSUPDATED,
comments="Fellowship created for this Potential Fellow",
noted_on=timezone.now(),
noted_by=self.request.user.contributor,
)
potfelevent.save()
potfel.status = POTENTIAL_FELLOWSHIP_ACTIVE_IN_COLLEGE
potfel.save()
nomination = FellowshipNomination.objects.filter(
profile=self.object.contributor.profile
).first()
if nomination:
nomination.save()
nomination.add_event(
description="Fellowship created",
by=self.request.user.contributor,
)
return redirect(self.get_success_url())
class FellowshipUpdateView(PermissionsMixin, UpdateView):
"""
Update an existing Fellowship.
"""
permission_required = "scipost.can_manage_college_composition"
model = Fellowship
form_class = FellowshipForm
Loading
Loading full blame...