Exemple #1
0
def edit_page(request, id):
    c = get_common_context(request)
    f = Work.get_by_id(id)
    if not ((f.author == request.user) or (f.is_collaborator(request.user))):
        return HttpResponseRedirect('/funfic/%s/' % id)
    c['title'] = u'Редактировать фанфик'
    if request.method == 'POST':
        form = AddWorkForm(request.POST, instance=f)
        collaborator = None
        if request.POST.get('collaborator'):
            try:
                collaborator = UserProfile.objects.get(nick=request.POST.get('collaborator')).user
            except:
                messages.error(request, u'Необходимо ввести правильный ник соавтора.')
        if form.is_valid():
            ff = form.save()
            if collaborator:
                ff.add_collaborator(collaborator)
            messages.success(request, u'Фанфик успешно отредактирован.')
            return HttpResponseRedirect('/cabinet/edit/%s/' % id)
        else:
            messages.error(request, u'Фанфик не отредактирован. Исправьте ошибки ниже.')
            print form.errors
    else:
        form = AddWorkForm(instance=f)
    c['form'] = form
    c['collaborator'] = f.get_collaborator()
    return render_to_response('cabinet/add.html', c, context_instance=RequestContext(request))
Exemple #2
0
def add_page(request):
    c = get_common_context(request)
    c['title'] = u'Добавить фанфик'
    if request.method=='POST':
        form = AddWorkForm(request.POST)
        collaborator = None
        if request.POST.get('collaborator'):
            try:
                collaborator = UserProfile.objects.get(nick=request.POST.get('collaborator')).user
            except:
                messages.error(request, u'Необходимо ввести правильный ник соавтора.')
        if form.is_valid():
            ff = form.save(commit=False)
            ff.author = request.user
            ff.commetns = 0
            ff.save()
            form.save_m2m()
            if collaborator:
                ff.add_collaborator(collaborator)
            messages.success(request, u'Фанфик успешно добавлен.')
            return HttpResponseRedirect('/cabinet/add_part/%s/' % ff.id)
        else:
            messages.error(request, u'Фанфик не добавлен. Исправьте ошибки ниже.')
        c['collaborator'] = request.POST.get('collaborator', '')

    else:
        form = AddWorkForm()
    c['form'] = form

    return render_to_response('cabinet/add.html', c, context_instance=RequestContext(request))