def create(request, template='contacts/mailtemplate/create.html'):
    """Create a mailtemplate.

    :param template: A custom template.
    """

    if not request.user.is_authenticated():
        return HttpResponseRedirect('/login/?next=%s' % request.path)
        
    user = request.user
    if not user.has_perm('add_mailtemplate'):
        return HttpResponseForbidden()

    if request.method == 'POST':
        form = MailTemplateForm(request.POST)

        if form.is_valid():
            p = form.save(commit=False)            
            p.user_add = user
            p.user_modify = user  
            p.save()
            return HttpResponseRedirect(p.get_update_url())
    else:
        form = MailTemplateForm()

    kwvars = {
        'form': form
    }

    return render_to_response(template, kwvars, RequestContext(request))
def update(request, code, template='contacts/mailtemplate/update.html'):
    """Update a mailtemplate.

    :param template: A custom template.
    """
    
    if not request.user.is_authenticated():
        return HttpResponseRedirect('/login/?next=%s' % request.path)

    user = request.user
    if not user.has_perm('change_mailtemplate'):
        #todo: posar al missatge que no es pot realitzar l'accio si no es te permis
        return detail(request,code)
        
    try:
        mailtemplate = MailTemplate.objects.get(code__iexact=code)
    except MailTemplate.DoesNotExist:
        raise Http404

    if request.method == 'POST':
        form = MailTemplateForm(request.POST, instance=mailtemplate)
        
        if form.is_valid():            
            mailtemplate.user_modify = user              
            form.save()
            return HttpResponseRedirect(mailtemplate.get_absolute_url())            
    else:
        form = MailTemplateForm(instance=mailtemplate)

    kwvars = {
        'form': form,
        'object': mailtemplate,
    }

    return render_to_response(template, kwvars, RequestContext(request))
def copy(request, code, template='contacts/mailtemplate/create.html'):
    """Copia una mailtemplate.

    :param template: Add a custom template.
    """

    if not request.user.is_authenticated():
        return HttpResponseRedirect('/login/?next=%s' % request.path)
        
    user = request.user    
    if not user.has_perm('add_mailtemplate'):
        return HttpResponseForbidden()

    if request.method == 'POST':
        form = MailTemplateForm(request.POST)

        if form.is_valid():
            p = form.save(commit=False)            
            p.user_add = user
            p.user_modify = user  
            p.save()
            return HttpResponseRedirect(p.get_update_url())
    else:
        try:
            mailtemplate = MailTemplate.objects.get(code__iexact=code)
        except MailTemplate.DoesNotExist:
            raise Http404
        
        new_mailtemplate = deepcopy(mailtemplate)
        new_mailtemplate.id = None
        new_mailtemplate.code = "%s_cp" % mailtemplate.code
        
        form = MailTemplateForm(instance=new_mailtemplate)
    
    kwvars = {
        'form': form,
    }

    return render_to_response(template, kwvars, RequestContext(request))