Esempio n. 1
0
def add_edit_thread(request, model, object_slug, thread_id=None, template='blmessage/form_thread.html'):
    model = get_model(*model.split('.'))
    model_object = get_object_or_404(model, slug=object_slug, members=request.user)
    ctype = ContentType.objects.get_for_model(model_object)
    tags = Tag.objects.usage_for_queryset(Thread.objects.filter(content_type__pk=ctype.id,
                                                                object_id=model_object.id), counts=True)

    if isinstance(model_object, Group):
        redirect_reverse = 'group-thread-detail'
        extra_context = {'group': model_object}

    elif isinstance(model_object, Project):
        redirect_reverse = 'project-thread-detail'
        extra_context = {'project': model_object}

    if not thread_id:
        form = ThreadForm(request.POST or None)
    else:
        form = EditThreadForm(request.POST or None,
                              instance=thread_id and Thread.objects.get(pk=thread_id,
                                                                        content_type__pk=ctype.id))

    if request.method == "POST" and form.is_valid():
        thread = form.save(model_object, request.user)
        form.save_m2m()
        if not thread_id:
            Tag.objects.update_tags(thread, model_object.slug)
            message = _("Thread '%(thread)s' has been created" % {'thread': thread.title })
            thread.create_activity(request.user, Activity.CREATE)
        else:
            message = _("Thread '%(thread)s' has been updated" % {'thread': thread.title })

        # Notification
        request.user.message_set.create(message=message)

        return HttpResponseRedirect(reverse(redirect_reverse,
                                            kwargs = {'object_slug': object_slug,
                                                      'thread_id': thread.id}))
    extra_context['tags'] = tags
    extra_context['form'] = form
    return direct_to_template(request,
                              template=template,
                              extra_context=extra_context)
Esempio n. 2
0
def edit_thread(request, thread_id):
    thread = get_object_or_404(Thread, pk=thread_id)

    if not thread_id:
        form = ThreadForm(request.POST or None)
    else:
        form = EditThreadForm(request.POST or None,
                              instance=thread_id and Thread.objects.get(pk=thread_id))

    if request.method == "POST" and form.is_valid():
        thread = form.save(thread, request.user)

        # Notification
        request.user.message_set.create(message=_("Thread '%(thread)s' has been updated" % {'thread': thread.title }))

        return HttpResponseRedirect(reverse('message-detail',
                                            kwargs = {'thread_id': thread.id}))

    return direct_to_template(request,
                              template='blmessage/form_thread.html',
                              extra_context={'form': form})