From 3622c92f6c03ef21ea0fbb9a9e75598d2b2581cf Mon Sep 17 00:00:00 2001 From: VG Date: Fri, 28 Nov 2014 16:45:09 +0100 Subject: Add paste save and restore --- config.py | 3 +++ pasteme.py | 26 +++++++++++++++++++++++--- views/bad_format.tpl | 10 ++++++++++ views/not_fount.tpl | 16 ++++++++++++++++ views/paste.tpl | 8 ++++++++ views/root.tpl | 19 +++++++++++++++++++ views/welcome_page.tpl | 19 ------------------- 7 files changed, 79 insertions(+), 22 deletions(-) create mode 100644 config.py create mode 100644 views/bad_format.tpl create mode 100644 views/not_fount.tpl create mode 100644 views/paste.tpl create mode 100644 views/root.tpl delete mode 100644 views/welcome_page.tpl diff --git a/config.py b/config.py new file mode 100644 index 0000000..4454890 --- /dev/null +++ b/config.py @@ -0,0 +1,3 @@ +# configuration file of pastme service + +pastedir = '/tmp/pastes' diff --git a/pasteme.py b/pasteme.py index 37f4522..fce1655 100755 --- a/pasteme.py +++ b/pasteme.py @@ -2,20 +2,40 @@ import bottle import identigen +import config +from pathlib import Path + +pathbase = Path(config.pastedir) + +if not pathbase.exists(): + pathbase.mkdir(mode=0o700, parents=True) @bottle.route('/') def route_root(): - return bottle.template('welcome_page') + return bottle.template('root') @bottle.route('/', method='POST') def route_paste_post(): content = bottle.request.forms.get('content') - return content + ' ' + identigen.generate(content) + pid = identigen.generate(content) + path = pathbase / pid + with path.open(mode='wb') as fd: + fd.write(content.encode('utf8')) + bottle.redirect('/' + pid) @bottle.route('/') @bottle.route('//') def route_paste_get(pid, pformat='colored'): - return 'paste: {}, {}'.format(pid, pformat) + if pformat != 'colored' and pformat != 'raw': + return bottle.template('bad_format') + path = pathbase / pid + try: + with path.open() as fd: + content = fd.read() + except IOError: + # use this template for all file based exception + return bottle.template('not_found') + return bottle.template('paste', content=content) if __name__ == '__main__': print('I: Starting application with development server') diff --git a/views/bad_format.tpl b/views/bad_format.tpl new file mode 100644 index 0000000..6efdab8 --- /dev/null +++ b/views/bad_format.tpl @@ -0,0 +1,10 @@ + + +Bad paste format requested + + +

Hello

+

I do not understand the format you are trying to get for the past +requested. Please try again with either colored or raw

+ + diff --git a/views/not_fount.tpl b/views/not_fount.tpl new file mode 100644 index 0000000..50a41db --- /dev/null +++ b/views/not_fount.tpl @@ -0,0 +1,16 @@ + + +Paste not available + + +

Hello

+

I wanted to display you a paste, but I did not find it. There can be two +reason for this:

+
    +
  • Either the paste expired
  • +
  • Or the paste never existed (the URL might be wrong)
  • +
+ +

I'm sorry for you, but nonetheless I whish you a very good day.

+ + diff --git a/views/paste.tpl b/views/paste.tpl new file mode 100644 index 0000000..99fad8c --- /dev/null +++ b/views/paste.tpl @@ -0,0 +1,8 @@ + + +Paste snippets + + +{{content}} + + diff --git a/views/root.tpl b/views/root.tpl new file mode 100644 index 0000000..53c6393 --- /dev/null +++ b/views/root.tpl @@ -0,0 +1,19 @@ + + +Paste snippets + + + +You are on a simple paste deposit service. + +Please copy your text on the box below: + +
+ + + + +
+ + + diff --git a/views/welcome_page.tpl b/views/welcome_page.tpl deleted file mode 100644 index 53c6393..0000000 --- a/views/welcome_page.tpl +++ /dev/null @@ -1,19 +0,0 @@ - - -Paste snippets - - - -You are on a simple paste deposit service. - -Please copy your text on the box below: - -
- - - - -
- - - -- cgit v1.2.3