Exemplo n.º 1
0
    def from_email(cls, msg, user):
        """
        Creates a new Note from an email message
        """
        sender = decode_header(msg['From'])
        detected = chardet.detect(sender[0][0]).get('encoding')
        sender = [i[0].decode(i[1] or detected) for i in sender]
        sender = ' '.join(sender)

        note = cls(sender=sender, created_by=user)

        note.is_read = False
        note.is_reported = False
        note.recipient = msg['To']

        subject = decode_header(msg['Subject'])[0]
        detected = chardet.detect(subject[0]).get('encoding')
        note.subject = subject[0].decode(subject[1] or detected)

        note.find_parent(note.subject)

        for part in msg.walk():
            t, s = part.get_content_type().split('/', 1)
            charset = part.get_content_charset() or "latin1"

            if t == "text":
                payload = part.get_payload(decode=True)
                note.body = unicode(payload, str(charset), "ignore")
                if s == "html":
                    h = html2text.HTML2Text()
                    h.ignore_images = True
                    note.body = h.handle(note.body)
            else:
                note.save()
                if part.get_filename():
                    filename = unicode(part.get_filename())
                    payload = part.get_payload()
                    content = base64.b64decode(payload)
                    content = ContentFile(content, filename)
                    attachment = Attachment(content=content,
                                            content_object=note)
                    attachment.save()
                    attachment.content.save(filename, content)
                    note.attachments.add(attachment)

        if not note.parent:
            # cookie not found in the subject, let's try the body...
            note.find_parent(note.body)

        note.save()

        return note
Exemplo n.º 2
0
Arquivo: note.py Projeto: fpsw/Servo
    def from_email(cls, msg, user):
        """
        Creates a new Note from an email message
        """
        sender = decode_header(msg['From'])
        detected = chardet.detect(sender[0][0]).get('encoding')
        sender = [i[0].decode(i[1] or detected) for i in sender]
        sender = ' '.join(sender)

        note = cls(sender=sender, created_by=user)

        note.is_read = False
        note.is_reported = False
        note.recipient = msg['To']

        subject = decode_header(msg['Subject'])[0]
        detected = chardet.detect(subject[0]).get('encoding')
        note.subject = subject[0].decode(subject[1] or detected)

        note.find_parent(note.subject)

        for part in msg.walk():
            t, s = part.get_content_type().split('/', 1)
            charset = part.get_content_charset() or "latin1"

            if t == "text":
                payload = part.get_payload(decode=True)
                note.body = unicode(payload, str(charset), "ignore")
                if s == "html":
                    h = html2text.HTML2Text()
                    h.ignore_images = True
                    note.body = h.handle(note.body)
            else:
                note.save()
                if part.get_filename():
                    filename = unicode(part.get_filename())
                    payload = part.get_payload()
                    content = base64.b64decode(payload)
                    content = ContentFile(content, filename)
                    attachment = Attachment(content=content, content_object=note)
                    attachment.save()
                    attachment.content.save(filename, content)
                    note.attachments.add(attachment)

        if not note.parent:
            # cookie not found in the subject, let's try the body...
            note.find_parent(note.body)

        note.save()

        return note
Exemplo n.º 3
0
def save(req):
    mimetypes.init()

    if 'id' in req.POST:
        doc = Attachment.objects.get(pk=req.POST['id'])
    else:
        doc = Attachment()

    f = req.FILES['content']

    type, encoding = mimetypes.guess_type(f.name)
    doc.content = f

    doc.name = req.POST.get('name', f.name)
    doc.content_type = type
    doc.is_template = req.POST.get('is_template', False)

    if 'tags' in req.POST:
        doc.tags = req.POST.getlist('tags')
        
    doc.save()
    
    messages.add_message(req, messages.INFO, _('Tiedosto tallennettu'))
    return redirect('/admin/files/')

    return HttpResponse(json.dumps({'name': doc.name, 'id': doc.id}),
        content_type='application/json')
Exemplo n.º 4
0
Arquivo: note.py Projeto: filipp/Servo
    def from_email(cls, msg, user):
        """
        Creates a new Note from an email message
        """
        note = cls(sender=msg['From'], created_by=user)

        note.is_read = False
        note.is_reported = False
        note.recipient = msg['To']
        note.subject = msg['Subject']

        note.find_parent(note.subject)

        for part in msg.walk():
            t, s = part.get_content_type().split('/', 1)
            charset = part.get_content_charset() or "latin1"

            if t == "text":
                payload = part.get_payload(decode=True)
                note.body = unicode(payload, str(charset), "ignore")
                if s == "html":
                    note.body = strip_tags(note.body)
            else:
                note.save()
                if part.get_filename():
                    filename = unicode(part.get_filename())
                    content = base64.b64decode(part.get_payload())
                    content = ContentFile(content, filename)
                    attachment = Attachment(content=content, content_object=note)
                    attachment.save()
                    attachment.content.save(filename, content)
                    note.attachments.add(attachment)

        if not note.parent:
            # cookie not found in the subject, let's try the body...
            note.find_parent(note.body)

        note.save()

        return note
Exemplo n.º 5
0
        form = NoteForm(data, instance=note)
    else:
        form = NoteForm(data)
    
    if not form.is_valid():
        print form.errors
        return render(request, 'notes/form.html', {'form': form})

    try:
        note = form.save()
    except Exception, e:
        messages.add_message(request, messages.ERROR, e)
        return render(request, 'notes/form.html', {'form': form})

    if 'content' in request.FILES:
        a = Attachment(uploaded_by=request.user)
        a.from_file(request.FILES['content'])
        note.attachments.add(a)
        note.save()

    msg = _(u'Merkintä tallennettu')

    if 'order' in data:
        messages.add_message(request, messages.INFO, msg)
        return redirect(note.order)
    else:
        msg = _(u'Viesti lähetetty') if note.mailto else msg
        messages.add_message(request, messages.INFO, msg)
        return redirect('/accounts/messages/%d/' % note.id)

def templates(request, template_id=None):