receiving and updating now works

This commit is contained in:
Matthias Seidl 2020-11-20 11:40:04 +01:00
parent 7298bc0c73
commit 41ddda6196
2 changed files with 31 additions and 24 deletions

53
app.py
View File

@ -1,20 +1,20 @@
from authlib.integrations.flask_client import OAuth, token_update, OAuthError from authlib.integrations.flask_client import OAuth, token_update, OAuthError
from flask import Flask, request, url_for, render_template, redirect, session from flask import Flask, request, url_for, render_template, redirect, session
from config import SETTINGS
import urllib import urllib
import base64 import base64
import hashlib
import json
def fetch_token(name): def fetch_token(name):
return session['token'] return session['token']
app = Flask(__name__) app = Flask(__name__)
app.secret_key = 'super secret key' app.secret_key = 'super secret key'
app.config.from_object(SETTINGS)
oauth = OAuth(app, fetch_token=fetch_token) oauth = OAuth(app, fetch_token=fetch_token)
oauth.register( oauth.register(
name='gitea', 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_url='https://git.seidlm.at/login/oauth/access_token',
access_token_params=None, access_token_params=None,
authorize_url='https://git.seidlm.at/login/oauth/authorize', authorize_url='https://git.seidlm.at/login/oauth/authorize',
@ -69,31 +69,36 @@ def home():
@app.route('/test') @app.route('/test')
def test(): def test():
resp = oauth.gitea.get(urllib.parse.quote('repos/seidl/blog/contents//recipes/recipes/30_knoblauchpüree.rst')) path = request.args.get('path')
content = base64.b64decode(resp.json()['content']).decode('UTF-8') if not path:
return render_template('test.html',content=content) 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']) @app.route('/update', methods=['POST'])
def update(): def update():
sample = {
"branch": "master",
"content": "",
"sha": "c7ef6a89fe8b888ad897479ac592f1e4841e4f18",
"message": "Web API Update"}
content = request.form.get('content') content = request.form.get('content')
content = base64.b64encode(content.encode('UTF-8')) 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": { sha = request.form.get('sha')
"email": "user@example.com", path = request.form.get('path')
"name": "string" sample['content'] = content.decode('UTF-8')
}, sample['sha'] = sha
"branch": "master",
"committer": { resp = oauth.gitea.request('PUT', 'repos/seidl/rstcms/contents/{}'.format(path), data=json.dumps(sample), headers={'Content-type': 'application/json'})
"email": "user@example.com", print(resp)
"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"})
return str(resp) return str(resp)

View File

@ -7,5 +7,7 @@
{{ content }} {{ content }}
{% endif %} {% endif %}
</textarea><br> </textarea><br>
<input type='hidden' id='sha' name='sha' value="{{ sha }}">
<input type='hidden' id='path' name='path' value="{{ path }}">
<button type="submit">Update in GIT!!</button> <button type="submit">Update in GIT!!</button>
</form> </form>