Exemplo n.º 1
0
def edit_board_member(request, journal_id, member_id):
    """
    Handles only existing editorial board member
    """

    if request.is_ajax():
        template_name = 'board/board_member_edit_form.html'
    else:
        template_name = 'board/board_member_edit.html'

    # check if user have correct access to view the journal:
    if not Journal.userobjects.active().filter(pk=journal_id).exists():
        messages.error(request, _('The journal is not available for you.'))
        return HttpResponseRedirect(reverse('editorial.index'))

    board_member = get_object_or_404(models.EditorialMember, id=member_id)
    post_url = reverse('editorial.board.edit', args=[journal_id, member_id, ])
    board_url = reverse('editorial.board', args=[journal_id, ])
    context = {
        'board_member': board_member,
        'post_url': post_url,
        'board_url': board_url,
    }

    if request.method == "POST":
        old_role = board_member.role
        form = forms.EditorialMemberForm(request.POST, instance=board_member)
        # this view only handle existing editorial board member, so always exist previous values.
        audit_old_values = helpers.collect_old_values(board_member, form)

        if form.is_valid():
            board_member = form.save()
            if 'role' in form.changed_data:  # change role -> change order
                _update_member_order(board_member, old_role)

            audit_data = {
                'user': request.user,
                'obj': board_member,
                'message': helpers.construct_change_message(form),
                'old_values': audit_old_values,
                'new_values': helpers.collect_new_values(form),
            }
            # this view only handle existing editorial board member, so always log changes.
            helpers.log_change(**audit_data)
            # notify librarians
            notifications.board_members_send_email_by_action(board_member, request.user, audit_data['message'], 'board_edit_member')

            messages.success(request, _('Board Member updated successfully.'))
            return HttpResponseRedirect(board_url)
        else:
            messages.error(request, _('Check mandatory fields.'))
            context['form'] = form
            return render_to_response(template_name, context, context_instance=RequestContext(request))
    else:
        form = forms.EditorialMemberForm(instance=board_member)

    context['form'] = form
    return render_to_response(template_name, context, context_instance=RequestContext(request))
Exemplo n.º 2
0
def edit_journal(request, journal_id):
    """
    Handles only existing journals
    """
    user_profile = request.user.get_profile()
    if request.user.is_superuser or user_profile.is_librarian:
        # redirect to full edit view:
        return HttpResponseRedirect(reverse('journal.edit', args=[journal_id, ]))

    journal = _get_journal_or_404_by_user_access(request.user, journal_id)

    JournalMissionFormSet = inlineformset_factory(Journal, JournalMission, form=JournalMissionForm, extra=1, can_delete=True)

    if request.method == "POST":
        journalform = RestrictedJournalForm(request.POST, request.FILES, instance=journal, prefix='journal')
        missionformset = JournalMissionFormSet(request.POST, instance=journal, prefix='mission')

        # this view only handle existing journals, so always exist previous values.
        audit_old_values = helpers.collect_old_values(journal, journalform, [missionformset, ])

        if journalform.is_valid() and missionformset.is_valid():
            journal = journalform.save()
            missionformset.save()

            audit_data = {
                'user': request.user,
                'obj': journal,
                'message': helpers.construct_change_message(journalform, [missionformset, ]),
                'old_values': audit_old_values,
                'new_values': helpers.collect_new_values(journalform, [missionformset, ]),
            }
            # this view only handle existing journals, so always log changes.
            helpers.log_change(**audit_data)

            messages.success(request, _('Journal updated successfully.'))

            if request.user.get_profile().is_editor:
                return HttpResponseRedirect(reverse('editorial.index'))
            else:
                return HttpResponseRedirect(reverse('journal.dash', args=[journal.id]))
        else:
            messages.error(request, _('Check mandatory fields.'))

    else:
        journalform = RestrictedJournalForm(instance=journal, prefix='journal')
        missionformset = JournalMissionFormSet(instance=journal, prefix='mission')

    return render_to_response('journal/edit_journal.html', {
                              'add_form': journalform,
                              'missionformset': missionformset,
                              }, context_instance=RequestContext(request))
Exemplo n.º 3
0
def translate_role_type(request, journal_id, role_id):
    template_name = 'board/role_type_translate.html'
    # check if user have correct access to view the journal:
    if not Journal.userobjects.active().filter(pk=journal_id).exists():
        messages.error(request, _('The journal is not available for you.'))
        return HttpResponseRedirect(reverse('editorial.index'))

    role_type = get_object_or_404(models.RoleType, id=role_id)
    post_url = reverse('editorial.role.translate', args=[journal_id, role_id, ])
    board_url = reverse('editorial.board', args=[journal_id, ])

    context = {
        'journal_id': journal_id,
        'role_type': role_type,
        'post_url': post_url,
        'board_url': board_url,
    }

    RoleTypeTranslationFormSet = inlineformset_factory(
                                    models.RoleType, models.RoleTypeTranslation,
                                    form=forms.RoleTypeForm, formset=forms.RoleTypeTranslationFormSet,
                                    extra=3, can_delete=True)

    if request.method == "POST":
        formset = RoleTypeTranslationFormSet(request.POST, instance=role_type, prefix="role-translations-formset")
        audit_old_values = helpers.collect_old_values(role_type, form=None, formsets=[formset, ])

        if formset.is_valid():
            formset.save()

            # audit
            audit_data = {
                'user': request.user,
                'obj': role_type,
                'message': helpers.construct_change_message(form=None, formsets=[formset, ]),
                'old_values': audit_old_values,
                'new_values': helpers.collect_new_values(form=None, formsets=[formset, ]),
            }
            # this view only handle existing roles, so always log changes.
            helpers.log_change(**audit_data)

            messages.success(request, _('Role updated successfully.'))
            return HttpResponseRedirect(board_url)
        else:
            messages.error(request, _('Check mandatory fields.'))
    else:
        formset = RoleTypeTranslationFormSet(instance=role_type, prefix="role-translations-formset")

    context['formset'] = formset
    return render_to_response(template_name, context, context_instance=RequestContext(request))
Exemplo n.º 4
0
def edit_role_type(request, journal_id, role_id):
    """
    Handle only existing editorial Role Types
    """
    if request.is_ajax():
        template_name = 'board/role_type_edit_form.html'
    else:
        template_name = 'board/role_type_edit.html'

    # check if user have correct access to view the journal:
    if not Journal.userobjects.active().filter(pk=journal_id).exists():
        messages.error(request, _('The journal is not available for you.'))
        return HttpResponseRedirect(reverse('editorial.index'))

    role_type = get_object_or_404(models.RoleType, id=role_id)
    post_url = reverse('editorial.role.edit', args=[journal_id, role_id, ])
    board_url = reverse('editorial.board', args=[journal_id, ])
    context = {
        'role_type': role_type,
        'post_url': post_url,
        'board_url': board_url,
    }

    if request.method == "POST":
        form = forms.RoleTypeForm(request.POST, instance=role_type)
        audit_old_values = helpers.collect_old_values(role_type, form)

        if form.is_valid():
            role_type = form.save()
            # audit
            audit_data = {
                'user': request.user,
                'obj': role_type,
                'message': helpers.construct_change_message(form),
                'old_values': audit_old_values,
                'new_values': helpers.collect_new_values(form),
            }
            # this view only handle existing roles, so always log changes.
            helpers.log_change(**audit_data)
            messages.success(request, _('Role updated successfully.'))
            return HttpResponseRedirect(board_url)
        else:
            messages.error(request, _('Check mandatory fields.'))
            context['form'] = form
            return render_to_response(template_name, context, context_instance=RequestContext(request))
    else:
        form = forms.RoleTypeForm(instance=role_type)

    context['form'] = form
    return render_to_response(template_name, context, context_instance=RequestContext(request))
Exemplo n.º 5
0
def add_role_type(request, journal_id):
    """
    Handles only NEW editorial Role Types
    """
    if request.is_ajax():
        template_name = 'board/role_type_edit_form.html'
    else:
        template_name = 'board/role_type_edit.html'

    board_url = reverse('editorial.board', args=[journal_id, ])
    post_url = reverse('editorial.role.add', args=[journal_id, ])
    context = {
        'post_url': post_url,
        'board_url': board_url,
    }

    if request.method == "POST":
        form = forms.RoleTypeForm(request.POST)
        if form.is_valid():
            new_role = form.save()
            audit_data = {
                'user': request.user,
                'obj': new_role,
                'message': helpers.construct_create_message(form),
                'old_values': None,
                'new_values': helpers.collect_new_values(form),
            }
            # this view only handle NEW editorial board member, so always log create.
            helpers.log_create(**audit_data)
            messages.success(request, _('Role created successfully.'))
            return HttpResponseRedirect(board_url)
        else:
            messages.error(request, _('Check mandatory fields.'))
            context['form'] = form
            return render_to_response(template_name, context, context_instance=RequestContext(request))
    else:
        form = forms.RoleTypeForm()

    context['form'] = form
    return render_to_response(template_name, context, context_instance=RequestContext(request))
Exemplo n.º 6
0
def add_board_member(request, journal_id, issue_id):
    """
    Handles only NEW editorial board member
    """
    if request.is_ajax():
        template_name = 'board/board_member_edit_form.html'
    else:
        template_name = 'board/board_member_edit.html'

    # check if user have correct access to view the journal:
    if not Journal.userobjects.active().filter(pk=journal_id).exists():
        messages.error(request, _('The journal is not available for you.'))
        return HttpResponseRedirect(reverse('editorial.index'))

    # get the issue, then the board if exist
    issue = Issue.userobjects.active().filter(pk=issue_id)
    if len(issue) != 1:
        messages.error(request, _('The issue is not available for you.'))
        return HttpResponseRedirect(reverse('editorial.index'))

    issue = issue[0]
    # get the related issue::board, if none: create one
    if not hasattr(issue, 'editorialboard'):
        board = models.EditorialBoard(issue=issue)
        board.save()

    board = issue.editorialboard

    board_url = reverse('editorial.board', args=[journal_id, ])
    post_url = reverse('editorial.board.add', args=[journal_id, issue_id, ])

    context = {
        'post_url': post_url,
        'board_url': board_url,
    }
    if request.method == "POST":
        form = forms.EditorialMemberForm(request.POST)
        if form.is_valid():
            new_member = form.save(commit=False)
            new_member.board = board
            new_member.save()
            new_member.order = _get_order_for_new_members(new_member.board, new_member.role.pk, new_member.pk)
            new_member.save()

            audit_data = {
                'user': request.user,
                'obj': new_member,
                'message': helpers.construct_create_message(form),
                'old_values': None,
                'new_values': helpers.collect_new_values(form),
            }
            # this view only handle NEW editorial board member, so always log create.
            helpers.log_create(**audit_data)
            # notify librarians
            notifications.board_members_send_email_by_action(new_member, request.user, audit_data['message'], 'board_add_member')

            messages.success(request, _('Board Member created successfully.'))
            return HttpResponseRedirect(board_url)
        else:
            messages.error(request, _('Check mandatory fields.'))
            context['form'] = form
            return render_to_response(template_name, context, context_instance=RequestContext(request))
    else:
        form = forms.EditorialMemberForm()

    context['form'] = form
    return render_to_response(template_name, context, context_instance=RequestContext(request))