def texts_get_body(text_no): """Get the body of text, with the content type of the body set in the HTTP header. Useful for creating img-tags in HTML and specifying this URL as source. If the content type is text, the text will be recoded to UTF-8. For other types, the content type will be left untouched. .. rubric:: Request :: GET /<server_id>/texts/19680717/body HTTP/1.0 .. rubric:: Responses Text exists:: HTTP/1.0 200 OK Content-Type: text/x-kom-basic; charset=utf-8 räksmörgås Text does not exist:: HTTP/1.0 404 NOT FOUND { TODO: error stuff } .. rubric:: Example :: curl -v -X GET -H "Content-Type: application/json" \\ "http://localhost:5001/lyskom/texts/19680717/body" """ try: text = g.ksession.get_text(text_no) mime_type, encoding = parse_content_type(text.content_type) data = cStringIO.StringIO() if mime_type[0] == 'text': data.write(text.body.encode('utf-8')) else: data.write(text.body) data.flush() data.seek(0) response = send_file(data, mimetype=text.content_type, as_attachment=False) return response except komerror.NoSuchText as ex: return error_response(404, kom_error=ex)
def texts_get_body(text_no): """Get the body of text, with the content type of the body set in the HTTP header. Useful for creating img-tags in HTML and specifying this URL as source. If the content type is text, the text will be recoded to UTF-8. For other types, the content type will be left untouched. .. rubric:: Request :: GET /<server_id>/texts/19680717/body HTTP/1.0 .. rubric:: Responses Text exists:: HTTP/1.0 200 OK Content-Type: text/x-kom-basic; charset=utf-8 räksmörgås Text does not exist:: HTTP/1.0 404 NOT FOUND { TODO: error stuff } .. rubric:: Example :: curl -v -X GET -H "Content-Type: application/json" \\ "http://localhost:5001/lyskom/texts/19680717/body" """ try: text = g.ksession.get_text(text_no) mime_type, encoding = parse_content_type(text.content_type) if mime_type[0] == 'text': data = BytesIO(text.body.encode('utf-8')) else: data = BytesIO(text.body) response = send_file(data, mimetype=text.content_type, as_attachment=False) return response except komerror.NoSuchText as ex: return error_response(404, kom_error=ex)
def KomText_to_dict(komtext, lookups, session): d = dict( text_no=komtext.text_no, author=pers_to_dict(komtext.author, lookups, session), no_of_marks=komtext.no_of_marks, content_type=komtext.content_type, subject=komtext.subject) mime_type, encoding = parse_content_type(komtext.content_type) # Only add body if text if mime_type[0] == 'text': d['body'] = komtext.body elif mime_type[0] == 'x-kom' and mime_type[1] == 'user-area': d['body'] = komtext.body if komtext.recipient_list is None: d['recipient_list'] = None else: d['recipient_list'] = [ to_dict(r, lookups, session) for r in komtext.recipient_list ] if komtext.comment_to_list is None: d['comment_to_list'] = None else: d['comment_to_list'] = [ to_dict(ct, lookups, session) for ct in komtext.comment_to_list ] if komtext.comment_in_list is None: d['comment_in_list'] = None else: d['comment_in_list'] = [ to_dict(ci, lookups, session) for ci in komtext.comment_in_list ] if komtext.aux_items is None: d['aux_items'] = None else: aux_items = [] for ai in [ai for ai in komtext.aux_items if ai.tag in _ALLOWED_KOMTEXT_AUXITEMS]: aux_items.append(to_dict(ai, lookups, session)) d['aux_items'] = aux_items if komtext.creation_time is None: d['creation_time'] = None else: d['creation_time'] = Time_to_dict(komtext.creation_time, lookups, session) return d
def test_parse_content_type_from_androkom_image(): ct = 'image/jpeg; name=https://lh3.googleusercontent.com/0D_7y-M=s0-d' parsed = parse_content_type(ct) assert parsed == (('image', 'jpeg', dict(name='https://lh3.googleusercontent.com/0D_7y-M=s0-d')), None)
def test_parse_content_type_from_email(): ct = 'application/pdf; name="=?iso-8859-1?q?f=f6rslag=5fmedlemsavgifter=2epdf?="' parsed = parse_content_type(ct) assert parsed == (('application', 'pdf', dict(name='"=?iso-8859-1?q?f=f6rslag=5fmedlemsavgifter=2epdf?="')), None)
def test_parse_content_type(): ct = 'application/pdf; name=foobar.pdf' parsed = parse_content_type(ct) assert parsed == (('application', 'pdf', dict(name='foobar.pdf')), None)