Exemplo n.º 1
0
Arquivo: note.py Projeto: filipp/Servo
def create_escalation(request):
    esc = Escalation()
    form = EscalationForm()
    title = _('Edit Escalation')

    if request.method == 'POST':
        data = request.POST.copy()
        data['created_by'] = request.user
        form = EscalationForm(data, request.FILES, instance=esc)
        if form.is_valid():
            note = form.save()
            #esc.submit(request.user)
            return redirect(view_note, 'escalations', note.pk)

    return render(request, 'notes/edit_escalation.html', locals())
Exemplo n.º 2
0
def create_escalation(request):
    esc = Escalation()
    form = EscalationForm()
    title = _('Edit Escalation')

    if request.method == 'POST':
        data = request.POST.copy()
        data['created_by'] = request.user
        form = EscalationForm(data, request.FILES, instance=esc)
        if form.is_valid():
            note = form.save()
            #esc.submit(request.user)
            return redirect(view_note, 'escalations', note.pk)

    return render(request, 'notes/edit_escalation.html', locals())
Exemplo n.º 3
0
Arquivo: note.py Projeto: filipp/Servo
    title = note.subject
    form = NoteForm(instance=note)

    if note.escalation:
        contexts = json.loads(note.escalation.contexts)

    escalation_form = EscalationForm(prefix='escalation', instance=note.escalation)

    if request.method == "POST":
        escalation_form = EscalationForm(request.POST,
                                         prefix='escalation',
                                         instance=note.escalation)

        if escalation_form.is_valid():
            note.escalation = escalation_form.save()

        form = NoteForm(request.POST, instance=note)

        if form.is_valid():

            note = form.save()
            formset = AttachmentFormset(request.POST, request.FILES)

            if formset.is_valid():

                files = formset.save(commit=False)

                for f in files:
                    f.content_object = note
                    try:
Exemplo n.º 4
0
def edit(request,
         pk=None,
         order_id=None,
         parent=None,
         recipient=None,
         customer=None):
    """
    Edit a note.

    @FIXME: Should split this up into smaller pieces
    """
    to = []
    order = None
    command = _('Save')
    note = Note(order_id=order_id)
    excluded_emails = note.get_excluded_emails()

    if recipient is not None:
        to.append(recipient)
        command = _('Send')

    if order_id is not None:
        order = get_object_or_404(Order, pk=order_id)

        if order.user and (order.user != request.user):
            note.is_read = False
            if order.user.email not in excluded_emails:
                to.append(order.user.email)

        if order.customer is not None:
            customer = order.customer_id

    if customer is not None:
        customer = get_object_or_404(Customer, pk=customer)
        note.customer = customer

        if order_id is None:
            to.append(customer.email)

    tpl = template.Template(note.subject)
    note.subject = tpl.render(template.Context({'note': note}))

    note.recipient = ', '.join(to)
    note.created_by = request.user
    note.sender = note.get_default_sender()

    fields = escalations.CONTEXTS

    try:
        note.escalation = Escalation(created_by=request.user)
    except Exception as e:
        messages.error(request, e)
        return redirect(request.META['HTTP_REFERER'])

    AttachmentFormset = modelformset_factory(Attachment,
                                             fields=('content', ),
                                             can_delete=True,
                                             extra=3,
                                             exclude=[])
    formset = AttachmentFormset(queryset=Attachment.objects.none())

    if pk is not None:
        note = get_object_or_404(Note, pk=pk)
        formset = AttachmentFormset(queryset=note.attachments.all())

    if parent is not None:
        parent = get_object_or_404(Note, pk=parent)
        note.parent = parent
        note.body = parent.quote()

        if parent.subject:
            note.subject = _(u'Re: %s') % parent.clean_subject()
        if parent.sender not in excluded_emails:
            note.recipient = parent.sender
        if parent.order:
            order = parent.order
            note.order = parent.order

        note.customer = parent.customer
        note.escalation = parent.escalation
        note.is_reported = parent.is_reported

    title = note.subject
    form = NoteForm(instance=note)

    if note.escalation:
        contexts = json.loads(note.escalation.contexts)

    escalation_form = EscalationForm(prefix='escalation',
                                     instance=note.escalation)

    if request.method == "POST":
        escalation_form = EscalationForm(request.POST,
                                         prefix='escalation',
                                         instance=note.escalation)

        if escalation_form.is_valid():
            note.escalation = escalation_form.save()

        form = NoteForm(request.POST, instance=note)

        if form.is_valid():

            note = form.save()
            formset = AttachmentFormset(request.POST, request.FILES)

            if formset.is_valid():

                files = formset.save(commit=False)

                for f in files:
                    f.content_object = note
                    try:
                        f.save()
                    except ValueError as e:
                        messages.error(request, e)
                        return redirect(note)

                note.attachments.add(*files)

                if form.cleaned_data.get('attach_confirmation'):
                    from servo.views.order import put_on_paper
                    response = put_on_paper(request, note.order_id, fmt='pdf')
                    filename = response.filename
                    content = response.render().content
                    content = ContentFile(content, filename)
                    attachment = Attachment(content=content,
                                            content_object=note)
                    attachment.save()
                    attachment.content.save(filename, content)
                    note.attachments.add(attachment)

                note.save()

                try:
                    msg = note.send_and_save(request.user)
                    messages.success(request, msg)
                except ValueError as e:
                    messages.error(request, e)

            return redirect(note)

    return render(request, "notes/form.html", locals())
Exemplo n.º 5
0
def edit(request, pk=None, order_id=None, parent=None, recipient=None,
         customer=None):
    """
    Edits a note
    @FIXME: Should split this up into smaller pieces
    """
    to = []
    order = None
    command = _('Save')
    note = Note(order_id=order_id)
    excluded_emails = note.get_excluded_emails()

    if recipient is not None:
        to.append(recipient)
        command = _('Send')

    if order_id is not None:
        order = get_object_or_404(Order, pk=order_id)

        if order.user and (order.user != request.user):
            note.is_read = False
            if order.user.email not in excluded_emails:
                to.append(order.user.email)

        if order.customer is not None:
            customer = order.customer_id

    if customer is not None:
        customer = get_object_or_404(Customer, pk=customer)
        note.customer = customer

        if order_id is None:
            to.append(customer.email)

    tpl = template.Template(note.subject)
    note.subject = tpl.render(template.Context({'note': note}))

    note.recipient = ', '.join(to)
    note.created_by = request.user
    note.sender = note.get_default_sender()

    fields = escalations.CONTEXTS

    try:
        note.escalation = Escalation(created_by=request.user)
    except Exception as e:
        messages.error(request, e)
        return redirect(request.META['HTTP_REFERER'])

    AttachmentFormset = modelformset_factory(Attachment,
                                             fields=('content',),
                                             can_delete=True,
                                             extra=3,
                                             exclude=[])
    formset = AttachmentFormset(queryset=Attachment.objects.none())

    if pk is not None:
        note = get_object_or_404(Note, pk=pk)
        formset = AttachmentFormset(queryset=note.attachments.all())

    if parent is not None:
        parent = get_object_or_404(Note, pk=parent)
        note.parent = parent
        note.body = parent.quote()

        if parent.subject:
            note.subject = _(u'Re: %s') % parent.clean_subject()
        if parent.sender not in excluded_emails:
            note.recipient = parent.sender
        if parent.order:
            order = parent.order
            note.order = parent.order

        note.customer = parent.customer
        note.escalation = parent.escalation
        note.is_reported = parent.is_reported

    title = note.subject
    form = NoteForm(instance=note)

    if note.escalation:
        contexts = json.loads(note.escalation.contexts)

    escalation_form = EscalationForm(prefix='escalation',
                                     instance=note.escalation)

    if request.method == "POST":
        escalation_form = EscalationForm(request.POST,
                                         prefix='escalation',
                                         instance=note.escalation)

        if escalation_form.is_valid():
            note.escalation = escalation_form.save()

        form = NoteForm(request.POST, instance=note)

        if form.is_valid():

            note = form.save()
            formset = AttachmentFormset(request.POST, request.FILES)

            if formset.is_valid():

                files = formset.save(commit=False)

                for f in files:
                    f.content_object = note
                    try:
                        f.save()
                    except ValueError as e:
                        messages.error(request, e)
                        return redirect(note)

                note.attachments.add(*files)
                note.save()

                try:
                    msg = note.send_and_save(request.user)
                    messages.success(request, msg)
                except ValueError as e:
                    messages.error(request, e)

            return redirect(note)

    return render(request, "notes/form.html", locals())