Пример #1
0
def edit_discussion(request, id):
    """edit a discussion"""
    # prepare messages
    mm = MessageManager(request)
    d = get_object_or_404(Discussion, pk=id)
    if request.user.is_authenticated() and request.user.username == d.user.username:
        if request.POST:
            form = CreateDiscussionForm(request.POST, request.FILES, user=request.user)
            if form.is_valid():
                form.update(request, d)
                mm.set_success("Your discussion has been updated")
                return redirect(d.get_absolute_url())
            else:
                # form had errors
                return render(request, 'main/discussion/edit.html', mm.messages(), {'form':form, 'discussion':d})
        else:
            # prepare new form for user
            dpk = None
            if d.group():
                dpk = d.group().pk
            term_str = delim_list(object_taxon(d), ',')
            form = CreateDiscussionForm(user=request.user, initial={'group': dpk, 'title':d.title, 'body':d.body, 'type':d.type, 'terms':term_str})
            
            return render(request, 'main/discussion/edit.html', mm.messages(), {'form':form, 'discussion':d})
    else:
        return redirect(request.META.get('HTTP_REFERER','/'))
Пример #2
0
def create_discussion(request, gid=None):
    """page to create a new discussion object"""
    # prepare messages
    mm = MessageManager(request)
    if request.user.is_authenticated():
        # TODO: allow unauthorized users to create temporary content, but require them to
        # sign up for an account before it can be saved. (html5 local db)
        form = None
        if request.POST:
            form = CreateDiscussionForm(request.POST, user=request.user)
            if form.is_valid(): 
                new = form.save(request, gid)
                mm.set_success("You successfully created a new discussion \"%s\"" % new.title)
                if new.group():
                    return redirect(new.group().get_absolute_url())
                else:
                    return redirect(request.user.get_absolute_url())
            else:
                # the form had errors
                data = request.POST.copy()
                formerr = form
                
                dgroup=None
                if gid:
                    form.fields['group'].widget.attrs['disabled'] = True # radio / checkbox
                    dgroup = get_object_or_404(cGroup, pk=gid)
                    form.fields['group'].widget.choices = ((gid,dgroup.name),)
                    
                return render(request, 'main/discussion/create.html', mm.messages(), {'form':form, 'group':dgroup})
        else:
            # prepare new form for user
            form = CreateDiscussionForm(user=request.user, initial={'group': gid})
            dgroup=None
            if gid:
                form.fields['group'].widget.attrs['disabled'] = True # radio / checkbox
                dgroup = get_object_or_404(cGroup, pk=gid)
                
            return render(request, 'main/discussion/create.html', mm.messages(), {'form':form, 'group':dgroup})
    else:
        return redirect(request.META.get('HTTP_REFERER','/'))