SciPost Code Repository

Skip to content
Snippets Groups Projects
views.py 2.33 KiB
Newer Older
__copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)"
__license__ = "AGPL v3"


from django.contrib.auth.decorators import permission_required
from django.shortcuts import get_object_or_404, render, redirect
from django.views.generic.detail import DetailView
from django.views.generic.list import ListView

from profiles.models import Profile
from profiles.forms import ProfileSelectForm, ProfileDynSelForm
from .models import Series, Collection


class SeriesListView(ListView):
    """
    List view for Series.
    """
    model = Series


class SeriesDetailView(DetailView):
    """
    Detail view for a Series.
    """
    model = Series


class CollectionDetailView(DetailView):
    """
    Detail view for a Collection.
    """
    model = Collection

    def get_context_data(self, *args, **kwargs):
        context = super().get_context_data(*args, **kwargs)
        context['expected_author_form'] = ProfileSelectForm()
        return context


@permission_required('scipost.can_manage_series')
def _hx_collection_expected_authors(request, slug):
    collection = get_object_or_404(Collection, slug=slug)
    form = ProfileDynSelForm(
        initial={
            'action_url_name': 'series:_hx_collection_expected_author_action',
            'action_url_base_kwargs': {'slug': collection.slug, 'action': 'add'}
        }
    )
    context = {
        'collection': collection,
        'profile_search_form': form
    }
    return render(request, 'series/_hx_collection_expected_authors.html', context)


@permission_required('scipost.can_manage_series')
def _hx_collection_expected_author_action(request, slug, profile_id, action):
    collection = get_object_or_404(Collection, slug=slug)
    profile = get_object_or_404(Profile, pk=profile_id)
    if action == 'add':
        collection.expected_authors.add(profile)
    if action == 'remove':
        # If this person already has a Publication, abort
        if collection.publications.filter(authors__profile=profile).exists():
            messages.error(
                request,
                f'{profile} is author of a Publication; removal aborted.'
            )
        else:
            collection.expected_authors.remove(profile)
    return redirect(reverse('series:_hx_collection_expected_authors',
                            kwargs={'slug': collection.slug}))