2019-04-29 01:49:40 +02:00
|
|
|
from app import app, db
|
2019-04-29 15:53:18 +02:00
|
|
|
from flask import Flask, request, redirect, session, render_template, url_for
|
2019-04-29 01:49:40 +02:00
|
|
|
from flask_login import current_user, login_user, logout_user, login_required
|
2019-04-29 15:53:18 +02:00
|
|
|
from app.forms import LoginForm, RegistrationForm, TrelloAPIForm
|
|
|
|
from app.models import User, TwitterAPI, Tweets, TrelloAPI, Boards
|
2019-04-29 01:49:40 +02:00
|
|
|
from werkzeug.urls import url_parse
|
2019-05-01 16:59:30 +02:00
|
|
|
from config import basedir
|
|
|
|
import tweepy, trello, twitter_credentials, trello_credentials, subprocess
|
2019-04-29 01:49:40 +02:00
|
|
|
|
|
|
|
@app.route("/login", methods=["GET", "POST"])
|
|
|
|
def login():
|
|
|
|
if current_user.is_authenticated:
|
|
|
|
return redirect(url_for("home"))
|
|
|
|
form = LoginForm()
|
|
|
|
if form.validate_on_submit():
|
2019-04-29 15:53:18 +02:00
|
|
|
user = User.query.filter_by(username = form.username.data).first()
|
|
|
|
login_user(user, remember = form.remember_me.data)
|
2019-04-29 01:49:40 +02:00
|
|
|
next_page = request.args.get("next")
|
|
|
|
if not next_page or url_parse(next_page).netloc != "":
|
|
|
|
next_page = url_for("home")
|
|
|
|
return redirect(next_page)
|
2019-04-29 15:53:18 +02:00
|
|
|
return render_template("login.html", form = form)
|
2019-04-29 01:49:40 +02:00
|
|
|
|
|
|
|
@app.route("/register", methods=["GET", "POST"])
|
|
|
|
def register():
|
|
|
|
if current_user.is_authenticated:
|
|
|
|
return redirect(url_for("index"))
|
|
|
|
form = RegistrationForm()
|
|
|
|
if form.validate_on_submit():
|
2019-04-29 15:53:18 +02:00
|
|
|
u = User(username=form.username.data, email=form.email.data)
|
|
|
|
u.set_password(form.password.data)
|
|
|
|
db.session.add(u)
|
2019-04-29 01:49:40 +02:00
|
|
|
db.session.commit()
|
|
|
|
return redirect(url_for("login"))
|
|
|
|
return render_template("register.html", form=form)
|
|
|
|
|
|
|
|
@app.route("/logout")
|
|
|
|
def logout():
|
|
|
|
logout_user()
|
|
|
|
return redirect(url_for("home"))
|
|
|
|
|
|
|
|
@app.route("/twlogin")
|
|
|
|
@login_required
|
|
|
|
def twlogin():
|
2019-04-29 15:53:18 +02:00
|
|
|
if current_user.twitter_api.first():
|
|
|
|
return "Already an api connected."
|
2019-04-29 01:49:40 +02:00
|
|
|
auth = tweepy.OAuthHandler(twitter_credentials.consumer_key, twitter_credentials.consumer_secret_key, "https://cyberplanificateur.flifloo.fr/twlogin")
|
|
|
|
if request.args.get("oauth_token") and request.args.get("oauth_verifier"):
|
|
|
|
auth.request_token = {"oauth_token" : request.args.get("oauth_token"), "oauth_token_secret" : request.args.get("oauth_verifier")}
|
|
|
|
try:
|
|
|
|
auth.get_access_token(request.args.get("oauth_verifier"))
|
|
|
|
except:
|
|
|
|
return "Error ! Failed to get access token"
|
|
|
|
else:
|
2019-04-29 15:53:18 +02:00
|
|
|
db.session.add(TwitterAPI(access_token = auth.access_token, access_token_secret = auth.access_token_secret, user = current_user))
|
2019-04-29 01:49:40 +02:00
|
|
|
db.session.commit()
|
|
|
|
elif not TwitterAPI.query.filter_by(user=current_user).first():
|
|
|
|
return redirect(auth.get_authorization_url())
|
|
|
|
return redirect(url_for("settings"))
|
|
|
|
|
|
|
|
@app.route("/twlogout")
|
|
|
|
@login_required
|
|
|
|
def twlogout():
|
|
|
|
twapi = TwitterAPI.query.filter_by(user=current_user).first()
|
|
|
|
if twapi:
|
|
|
|
db.session.delete(twapi)
|
|
|
|
db.session.commit()
|
|
|
|
return redirect(url_for("settings"))
|
|
|
|
|
|
|
|
@app.route("/trlogin")
|
|
|
|
@login_required
|
|
|
|
def trlogin():
|
2019-04-30 19:53:01 +02:00
|
|
|
if current_user.trello_api.first():
|
2019-04-29 15:53:18 +02:00
|
|
|
return "Already an api connected."
|
2019-04-29 01:49:40 +02:00
|
|
|
return redirect(f"https://trello.com/1/authorize?expiration=never&name=Cyberplanificateur&scope=read,write&response_type=token&key={trello_credentials.api_key}&return_url=https://cyberplanificateur.flifloo.fr/settings")
|
|
|
|
|
|
|
|
@app.route("/trlogout")
|
|
|
|
@login_required
|
|
|
|
def trlogout():
|
|
|
|
trapi = TrelloAPI.query.filter_by(user=current_user).first()
|
|
|
|
if trapi:
|
|
|
|
db.session.delete(trapi)
|
|
|
|
db.session.commit()
|
|
|
|
return redirect(url_for("settings"))
|
|
|
|
|
|
|
|
@app.route("/")
|
|
|
|
def home():
|
|
|
|
return render_template("index.html")
|
|
|
|
|
|
|
|
@app.route("/settings", methods = ["POST", "GET"])
|
|
|
|
@login_required
|
|
|
|
def settings():
|
2019-04-29 15:53:18 +02:00
|
|
|
form = TrelloAPIForm()
|
|
|
|
if form.validate_on_submit():
|
|
|
|
if current_user.trello_api.first():
|
|
|
|
return "Already an api connected."
|
|
|
|
db.session.add(TrelloAPI(token = form.token.data, user = current_user))
|
|
|
|
db.session.commit()
|
|
|
|
return render_template("settings.html", form = form)
|
2019-04-29 01:49:40 +02:00
|
|
|
|
|
|
|
@app.route("/dashboard", methods = ["POST", "GET"])
|
2019-04-29 15:53:18 +02:00
|
|
|
@login_required
|
2019-04-29 01:49:40 +02:00
|
|
|
def dashboard():
|
2019-04-29 15:53:18 +02:00
|
|
|
twapi = current_user.twitter_api.first()
|
|
|
|
trapi = current_user.trello_api.first()
|
2019-04-29 01:49:40 +02:00
|
|
|
tweets = list()
|
|
|
|
timeline = list()
|
2019-04-29 15:53:18 +02:00
|
|
|
boards = list()
|
|
|
|
columns = list()
|
2019-04-29 01:49:40 +02:00
|
|
|
|
2019-04-29 15:53:18 +02:00
|
|
|
if twapi:
|
|
|
|
twapi = twapi.api_login()
|
|
|
|
|
|
|
|
if request.args.get("twrm"):
|
2019-05-01 16:23:17 +02:00
|
|
|
t = Tweets.query.filter_by(user = current_user, statu_id = request.args.get("twrm")).first()
|
|
|
|
for c in t.commissions.all():
|
|
|
|
db.session.delete(c)
|
|
|
|
db.session.delete(t)
|
2019-04-29 15:53:18 +02:00
|
|
|
db.session.commit()
|
|
|
|
if request.args.get("delet"):
|
2019-05-21 20:51:59 +02:00
|
|
|
twapi.destroy_status(request.args.get("twrm"))
|
2019-05-01 16:32:00 +02:00
|
|
|
return redirect(url_for("dashboard"))
|
2019-04-29 15:53:18 +02:00
|
|
|
elif "tweet" in request.form and "slots" in request.form and "keywords" in request.form:
|
|
|
|
try:
|
|
|
|
slots = int(request.form["slots"])
|
|
|
|
tweet = int(request.form["tweet"])
|
|
|
|
except:
|
|
|
|
formerror = True
|
|
|
|
else:
|
|
|
|
db.session.add(Tweets(user = current_user, statu_id = tweet, slots = 0, slots_max = slots, keywords = str(request.form["keywords"].split(","))))
|
|
|
|
db.session.commit()
|
2019-05-21 20:51:59 +02:00
|
|
|
if str(subprocess.check_output(["screen -list; exit 0"], stderr=subprocess.STDOUT, shell=True)).find(f"stream-{str(current_user.id)}") == -1:
|
2019-05-01 16:59:30 +02:00
|
|
|
subprocess.check_call(["screen", "-S", f"stream-{str(current_user.id)}", "-d", "-m", "python3.7", "stream.py", str(current_user.id)], cwd = basedir)
|
2019-05-01 16:32:00 +02:00
|
|
|
return redirect(url_for("dashboard"))
|
2019-04-29 15:53:18 +02:00
|
|
|
|
|
|
|
for t in Tweets.query.filter_by(user = current_user):
|
|
|
|
statu = twapi.get_status(t.statu_id)
|
|
|
|
keywords = "|"
|
|
|
|
for text in eval(t.keywords):
|
|
|
|
keywords += f" {text} |"
|
|
|
|
tweets.append({"text": statu.text, "id": t.statu_id, "slots": f"{t.slots}/{t.slots_max}", "keywords": keywords})
|
|
|
|
|
2019-04-30 19:53:01 +02:00
|
|
|
for t in twapi.user_timeline(count = 200):
|
2019-04-29 15:53:18 +02:00
|
|
|
if not t.in_reply_to_status_id and not t.retweeted and not Tweets.query.filter_by(user = current_user, statu_id = t.id).first():
|
2019-04-30 19:53:01 +02:00
|
|
|
if len(timeline) >= 5:
|
|
|
|
break
|
2019-04-29 15:53:18 +02:00
|
|
|
timeline.append({"text": t.text, "id": t.id})
|
|
|
|
|
|
|
|
|
|
|
|
if trapi:
|
|
|
|
trapi = trapi.api_login()
|
2019-05-01 16:32:00 +02:00
|
|
|
|
|
|
|
if "board" in request.form:
|
|
|
|
db.session.add(Boards(user = current_user, board_id = request.form["board"]))
|
|
|
|
db.session.commit()
|
|
|
|
return redirect(url_for("dashboard"))
|
|
|
|
elif "column" in request.form:
|
|
|
|
current_user.boards.first().column_id = request.form["column"]
|
|
|
|
db.session.commit()
|
|
|
|
return redirect(url_for("dashboard"))
|
|
|
|
|
2019-04-29 01:49:40 +02:00
|
|
|
for b in trapi.list_boards():
|
2019-04-29 15:53:18 +02:00
|
|
|
select = False
|
2019-04-30 19:53:01 +02:00
|
|
|
if current_user.boards.first() and b.id == current_user.boards.first().board_id:
|
2019-04-29 15:53:18 +02:00
|
|
|
select = True
|
|
|
|
boards.append({"text": b.name, "id": b.id, "select": select})
|
2019-04-29 01:49:40 +02:00
|
|
|
|
2019-04-29 15:53:18 +02:00
|
|
|
if current_user.boards.first() and current_user.boards.first().board_id:
|
|
|
|
for c in trapi.get_board(current_user.boards.first().board_id).list_lists():
|
|
|
|
select = False
|
|
|
|
if c.id == current_user.boards.first().column_id:
|
|
|
|
select = True
|
|
|
|
columns.append({"text": c.name, "id": c.id, "select": select})
|
2019-04-29 01:49:40 +02:00
|
|
|
|
2019-04-29 15:53:18 +02:00
|
|
|
return render_template("dashboard.html", timeline = timeline, tweets = tweets, boards = boards, columns = columns)
|
2019-04-29 01:49:40 +02:00
|
|
|
|
|
|
|
@app.route("/test")
|
|
|
|
def test():
|
|
|
|
return render_template("elements.html")
|