· 5 years ago · Jun 09, 2020, 03:44 PM
1from flask import render_template, redirect, request, jsonify, Flask, request
2import json
3from flask_sqlalchemy import SQLAlchemy
4import datetime
5import configparser
6import random
7import os
8import requests
9
10config = configparser.ConfigParser()
11config.read('config.cfg')
12PORT = config.get("server", "port")
13DEBUG = config.get("main", "debug")
14CSRF = config.get("main", "csrf_enabled")
15SECRET_KEY = config.get("main", "secret_key")
16HOST = config.get("server", "host")
17SQLALCHEMY_TRACK_MODS = config.get("other", "sqlalchemy-track-modifications")
18
19app = Flask(__name__)
20app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.db'
21app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = SQLALCHEMY_TRACK_MODS
22db = SQLAlchemy(app)
23
24class Page(db.Model):
25 title = db.Column(db.String, nullable=False)
26 author = db.Column(db.String, primary_key=True)
27 text = db.Column(db.String, nullable=False)
28 date = db.Column(db.String, default=datetime.datetime.today)
29 highlighter = db.Column(db.String, nullable=False)
30
31 def __repr__(self):
32 return f'<Paste {self.id}>'
33
34# while True:
35# check()
36
37
38@app.route('/<Author>-<Title>', methods=['GET', 'POST'])
39def get_lel(Author, Title):
40 page = Page.query.filter_by(author=Author, title=Title).first()
41 if page is not None:
42 return f'''Hello, {Author}! Here's your title: {Title}. Of course, here your code goes: { page.text }. And your date is { page.date }'''
43 else:
44 data = Page(title=Title, author=Author, text=content, date=datetime.datetime.today, highlighter='Python')
45 db.session.add(data)
46 db.session.commit()
47 # return '''We're sorry, but it seems like you've missed a row or something else. Try again!'''
48 return f'''Hello, {Author}! Here's your title: {Title}. Of course, here your code goes: { page.text }. Date: { page.date }'''
49
50@app.route('/', methods=['GET', 'POST'])
51def home():
52 return render_template('home.html')
53
54@app.route('/postmethod', methods=['POST'])
55def get_post_javascript_data():
56 jsdata = request.form['javascript_data']
57 global content
58 content = jsdata
59 return jsdata
60
61
62if __name__ == "__main__":
63 app.run(
64 host=HOST,
65 debug=DEBUG,
66 port=PORT
67 )
68 init_db()