コード例 #1
0
ファイル: views.py プロジェクト: ccarlos/code-samples
def edit(request, diary_id):
    """Edit an existing diary entry.

    Checks and adds errors for setting a non-draft diary to a draft diary.

    """
    entry = get_object_or_404(DiaryEntry, pk=diary_id)
    if entry.creator != request.user:
        raise PermissionDenied
    was_draft = entry.is_draft
    if request.method == 'POST':
        data = request.POST
        if request.is_ajax():
            data = data.copy()
            data['is_draft'] = True
        form = DiaryForm(creator=entry.creator, data=data,
                         instance=entry)
        if form.is_valid():
            entry = form.save()
            u = request.user
            entries = list(u.diaries.get_all(visitor=u, creator=u)[:10])
            u.diaries.invalidate(*entries)
            # Publishing a diary entry. Notify favorites.
            if was_draft and not entry.is_draft:
                process_diary(entry)
                u.profile.update(
                    coins=u.profile.coins +
                        ucs.ACTIONS_COINS['diary']['daily'])
                update_rewards(request.user, 'diary')
                process_mojo_action(request.user, 'diary_entry')
            if request.is_ajax():
                data = {'is_valid': True,
                        'timestamp': entry.updated.strftime("%s")}
                return JSONResponse(data)
            return HttpResponseRedirect(reverse(
                'diary.single', kwargs=get_kwargs_for_diary_id(entry.pk)))
    else:
        form = DiaryForm(creator=entry.creator, instance=entry)
    c_year, c_month = clean_diary_date(entry.created_day.year,
                                       entry.created_day.month)
    entries = request.user.diaries.get_for_month(visitor=request.user,
                                                 creator=request.user,
                                                 year=c_year, month=c_month)
    calendar_data = get_diary_calendar(entries, c_year, c_month)
    data = {'form': form, 'creator': request.user, 'entry': entry,
            'calendar_data': calendar_data}
    return jingo.render(request, 'diary/edit.html', data)
コード例 #2
0
ファイル: views.py プロジェクト: ccarlos/code-samples
def new(request):
    """Create a new diary entry for today."""
    diaries = request.user.diaries.filter(created_day=datetime.date.today())
    # If they already have a diary for today, redirect them to edit
    if diaries:
        return HttpResponseRedirect(reverse('diary.edit',
                                            args=[diaries[0].pk]))
    if request.method == 'POST':
        data = request.POST
        if request.is_ajax():
            data = data.copy()
            data['is_draft'] = True
        form = DiaryForm(creator=request.user, data=data)
        if form.is_valid():
            entry = form.save()
            u = request.user
            entries = list(u.diaries.get_all(visitor=u, creator=u)[:10])
            u.diaries.invalidate(*entries)
            # Register user to receive email when a comment is posted.
            NewCommentEvent.notify(request.user, entry)
            # Send news feed item to creator's favorites.
            if not entry.is_draft:
                process_diary(entry)
                u.profile.update(
                    coins=u.profile.coins +
                        ucs.ACTIONS_COINS['diary']['daily'])
                update_rewards(request.user, 'diary')
                process_mojo_action(request.user, 'diary_entry')
            if request.is_ajax():
                data = {'is_valid': True,
                        'is_new': True,
                        'url': reverse('diary.edit', args=[entry.pk]),
                        'timestamp': entry.updated.strftime("%s")}
                return JSONResponse(data)
            return HttpResponseRedirect(reverse('diary.list_all',
                                        args=[request.user.username]))
    else:   # request.method == 'GET'
        form = DiaryForm(creator=request.user)
    c_year, c_month = clean_diary_date()
    entries = request.user.diaries.get_for_month(visitor=request.user,
                                                 creator=request.user,
                                                 year=c_year, month=c_month)
    calendar_data = get_diary_calendar(entries, c_year, c_month)
    data = {'form': form, 'creator': request.user,
            'calendar_data': calendar_data}
    return jingo.render(request, 'diary/new.html', data)