From 41ddda619637438f75256e4df2b19d5400910409 Mon Sep 17 00:00:00 2001 From: Matthias Seidl Date: Fri, 20 Nov 2020 11:40:04 +0100 Subject: [PATCH] receiving and updating now works --- app.py | 53 +++++++++++++++++++++++++-------------------- templates/test.html | 2 ++ 2 files changed, 31 insertions(+), 24 deletions(-) diff --git a/app.py b/app.py index c2a2b60..256aebf 100644 --- a/app.py +++ b/app.py @@ -1,20 +1,20 @@ from authlib.integrations.flask_client import OAuth, token_update, OAuthError from flask import Flask, request, url_for, render_template, redirect, session +from config import SETTINGS import urllib import base64 +import hashlib +import json def fetch_token(name): return session['token'] app = Flask(__name__) app.secret_key = 'super secret key' - +app.config.from_object(SETTINGS) oauth = OAuth(app, fetch_token=fetch_token) oauth.register( name='gitea', - client_id='950be675-a53d-4d3b-a3c2-d2f5331581aa', - client_secret='PuiKSkdR5GxLJGXo_A2irUGjL0ZQdwHLsrSRFUM0_8U=', - access_token_url='https://git.seidlm.at/login/oauth/access_token', access_token_params=None, authorize_url='https://git.seidlm.at/login/oauth/authorize', @@ -69,31 +69,36 @@ def home(): @app.route('/test') def test(): - resp = oauth.gitea.get(urllib.parse.quote('repos/seidl/blog/contents//recipes/recipes/30_knoblauchpüree.rst')) - content = base64.b64decode(resp.json()['content']).decode('UTF-8') - return render_template('test.html',content=content) + path = request.args.get('path') + if not path: + return 'no path given' + + resp = oauth.gitea.get(urllib.parse.quote('repos/seidl/rstcms/contents/{}'.format(path))) + jresp = resp.json() + if 'errors' in jresp: + error = {} + error['description'] = jresp['errors'] + return render_template('error.html', error=error) + content = base64.b64decode(jresp['content']).decode('UTF-8') + sha = jresp['sha'] + return render_template('test.html',content=content,sha=sha,path=path) @app.route('/update', methods=['POST']) def update(): + sample = { + "branch": "master", + "content": "", + "sha": "c7ef6a89fe8b888ad897479ac592f1e4841e4f18", + "message": "Web API Update"} content = request.form.get('content') content = base64.b64encode(content.encode('UTF-8')) - resp = oauth.gitea.put(urllib.parse.quote('repos/seidl/blog/contents//recipes/recipes/30_knoblauchpüree.rst'), json={"author": { - "email": "user@example.com", - "name": "string" - }, - "branch": "master", - "committer": { - "email": "user@example.com", - "name": "string" - },"content": content.decode('UTF-8'), - "dates": { - "author": "2020-11-17T22:59:16.418Z", - "committer": "2020-11-17T22:59:16.418Z" - }, - "from_path": "string", - "message": "string", - "new_branch": "master", - "sha": "string"}) + sha = request.form.get('sha') + path = request.form.get('path') + sample['content'] = content.decode('UTF-8') + sample['sha'] = sha + + resp = oauth.gitea.request('PUT', 'repos/seidl/rstcms/contents/{}'.format(path), data=json.dumps(sample), headers={'Content-type': 'application/json'}) + print(resp) return str(resp) diff --git a/templates/test.html b/templates/test.html index 9d26f81..54d3ffa 100644 --- a/templates/test.html +++ b/templates/test.html @@ -7,5 +7,7 @@ {{ content }} {% endif %}
+ +