예제 #1
0
def edit_entry(request, entry_id=None, event_id=None):
    """
    Display an edit page for Entry object.
    """
    if not is_contributor(request):
        messages.warning(request, _('This page is for contributors only.'))
        return redirect('index')

    if entry_id is None:
        entry = Entry()
        entry.event = get_object_or_404(Event, pk=event_id)
        version = EntryVersion(entry=entry)
        version.entry_date = entry.event.date
        entry.created_by = request.user
        entry.set_order_last()
    else:
        entry = get_object_or_404(Entry, pk=entry_id)

    if entry_id is not None:
        snippets = list(Snippet.all_visible.filter(entry=entry))
        images = list(ImageSource.all_visible.filter(entry=entry))
    else:
        snippets = []
        images = []

    return render(
        request, 'palanaeum/staff/entry_edit_form.html', {
            'entry': entry,
            'event': entry.event,
            'snippets': snippets,
            'images': images
        })
예제 #2
0
def save_entry(request):
    """
    Save received entry data.
    """
    if not is_contributor(request):
        raise AjaxException(_('Only contributors can perform this action.'))

    if 'entry_id' not in request.POST or not request.POST['entry_id']:
        entry = Entry()
        event = get_object_or_404(Event, pk=request.POST['event_id'])
        entry.event = event
        entry.created_by = request.user
        entry.is_approved = False
        entry.set_order_last()
        entry.save()
        entry_version = EntryVersion()
        entry_version.entry = entry
        entry_version.entry_date = event.date
        entry_version.user = request.user
    else:
        entry_id = request.POST['entry_id']
        entry = get_object_or_404(Entry, pk=entry_id)
        event = entry.event
        entry_version = entry.versions.last()
        if entry_version is None:
            entry_version = EntryVersion()
            entry_version.entry = entry
            entry_version.entry_date = event.date
            entry_version.user = request.user

    entry_version.archive_version()
    entry_version.note = request.POST.get('note', '')
    entry_version.user = request.user
    entry_version.is_approved = False
    entry_version.approved_by = None
    entry_version.approved_date = None
    entry_version.paraphrased = bool(request.POST.get('paraphrased', False))
    entry_version.direct_entry = bool(request.POST.get('direct', False))
    entry_version.reported_by = request.POST.get('reported_by', '')

    if request.user.is_staff:
        entry.searchable = bool(request.POST.get('searchable', False))
        entry.save()

    date_str = request.POST.get('date', event.date.strftime("%Y-%m-%d"))
    if date_str:
        try:
            entry_version.entry_date = datetime.strptime(date_str, "%Y-%m-%d")
        except ValueError:
            raise AjaxException(
                _("Unsupported date format. Expected date format is: YYYY-MM-DD."
                  ))
    else:
        entry_version.entry_date = event.date

    entry_version.save()
    event.modified_date = timezone.now()
    event.save()

    lines_id_mapping, deleted_lines_ids = _save_entry_lines(
        request, entry_version)

    _save_entry_url_sources(request, entry_version)

    tags_str = ", ".join(request.POST.getlist('tags[]'))
    entry_version.update_tags(tags_str)

    # Save by staff member approves by default
    if request.user.is_staff:
        entry_version.approve(request.user)

    logging.getLogger('palanaeum.staff').info("Entry %s updated by %s",
                                              entry.id, request.user)

    return {
        'lines_id_mapping': lines_id_mapping,
        'deleted_lines': deleted_lines_ids,
        'entry_id': entry.id,
        'add_entry_url': reverse('event_add_entry',
                                 kwargs={'event_id': event.id})
    }