mirror of https://github.com/itsmrval/somycal
index demo page
parent
bd4fe95215
commit
b409f20d41
140
app.py
140
app.py
|
|
@ -1,6 +1,8 @@
|
||||||
from flask import Flask, redirect, url_for, session
|
from flask import Flask, redirect, url_for, session, render_template, Response
|
||||||
from flask_oauthlib.client import OAuth
|
from flask_oauthlib.client import OAuth
|
||||||
from flask_sqlalchemy import SQLAlchemy
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
|
from datetime import datetime
|
||||||
|
from icalendar import Calendar, Event
|
||||||
import os
|
import os
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
@ -28,6 +30,39 @@ google = oauth.remote_app(
|
||||||
authorize_url='https://accounts.google.com/o/oauth2/auth',
|
authorize_url='https://accounts.google.com/o/oauth2/auth',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
teams_dict = {
|
||||||
|
1: "Lakers",
|
||||||
|
2: "Heat",
|
||||||
|
3: "Warriors",
|
||||||
|
4: "Celtics",
|
||||||
|
5: "Spurs",
|
||||||
|
6: "Knicks",
|
||||||
|
7: "Pistons",
|
||||||
|
8: "Magic",
|
||||||
|
9: "Suns",
|
||||||
|
10: "Pacers",
|
||||||
|
11: "Jazz",
|
||||||
|
12: "Trail Blazers",
|
||||||
|
13: "Raptors",
|
||||||
|
14: "Mavericks",
|
||||||
|
15: "Bucks",
|
||||||
|
16: "Thunder",
|
||||||
|
17: "Bulls",
|
||||||
|
18: "Pelicans",
|
||||||
|
19: "Rockets",
|
||||||
|
20: "Kings",
|
||||||
|
21: "Clippers",
|
||||||
|
22: "Cavaliers",
|
||||||
|
23: "Hawks",
|
||||||
|
24: "Grizzlies",
|
||||||
|
25: "Nuggets",
|
||||||
|
26: "Hornets",
|
||||||
|
27: "76ers",
|
||||||
|
28: "Wizards",
|
||||||
|
29: "Timberwolves",
|
||||||
|
30: "Nets"
|
||||||
|
}
|
||||||
|
|
||||||
class User(db.Model):
|
class User(db.Model):
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
email = db.Column(db.String(120), unique=True)
|
email = db.Column(db.String(120), unique=True)
|
||||||
|
|
@ -42,16 +77,67 @@ def addTeam(idUser, idTeam):
|
||||||
db.session.add(team)
|
db.session.add(team)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
def getTeamName(idTeam):
|
def deleteTeam(idTeam):
|
||||||
|
team = Team.query.filter_by(idTeam=idTeam).first()
|
||||||
|
db.session.delete(team)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
def getTeamName(idTeam):
|
||||||
|
return teams_dict[idTeam]
|
||||||
|
|
||||||
def getTeams(idUser):
|
def getTeams(idUser):
|
||||||
teams = Team.query.filter_by(idUser=idUser).all()
|
teams = Team.query.filter_by(idUser=idUser).all()
|
||||||
return teams
|
return teams
|
||||||
|
|
||||||
|
def assignTeam(idUser, idTeam):
|
||||||
|
team = Team.query.filter_by(idTeam=idTeam).first()
|
||||||
|
if team is None:
|
||||||
|
addTeam(idUser, idTeam)
|
||||||
|
else:
|
||||||
|
team.idUser = idUser
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
def createEvent(summary, start_time, end_time):
|
||||||
|
event = Event()
|
||||||
|
event.add('summary', summary)
|
||||||
|
event.add('dtstart', start_time)
|
||||||
|
event.add('dtend', end_time)
|
||||||
|
return event
|
||||||
|
|
||||||
|
def generateIcal(events):
|
||||||
|
cal = Calendar()
|
||||||
|
for event_data in events:
|
||||||
|
event = createEvent(
|
||||||
|
event_data['summary'],
|
||||||
|
event_data['start_time'],
|
||||||
|
event_data['end_time']
|
||||||
|
)
|
||||||
|
cal.add_component(event)
|
||||||
|
return cal.to_ical()
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
def index():
|
def index():
|
||||||
|
if 'google_token' in session:
|
||||||
|
me = google.get('userinfo')
|
||||||
|
if (not(me.data['email'])):
|
||||||
|
return redirect("/logout", code=302)
|
||||||
|
user = User.query.filter_by(email=me.data['email']).first()
|
||||||
|
if user is None:
|
||||||
|
user = User(email=me.data['email'])
|
||||||
|
db.session.add(user)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
otherTeams = []
|
||||||
|
|
||||||
|
for i in list(teams_dict.keys()):
|
||||||
|
if (Team.query.filter_by(idUser=user.id, idTeam=i).first()):
|
||||||
|
otherTeams.insert(i)
|
||||||
|
|
||||||
|
return render_template('index.html', userTeams=getTeams(user.id), otherTeams=otherTeams, getTeamName=getTeamName)
|
||||||
|
return redirect("/login", code=302)
|
||||||
|
|
||||||
|
@app.route('/add/<int:idTeam>')
|
||||||
|
def addTeam(idTeam):
|
||||||
if 'google_token' in session:
|
if 'google_token' in session:
|
||||||
me = google.get('userinfo')
|
me = google.get('userinfo')
|
||||||
user = User.query.filter_by(email=me.data['email']).first()
|
user = User.query.filter_by(email=me.data['email']).first()
|
||||||
|
|
@ -59,11 +145,56 @@ def index():
|
||||||
user = User(email=me.data['email'])
|
user = User(email=me.data['email'])
|
||||||
db.session.add(user)
|
db.session.add(user)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
return 'Logged in as: ' + me.data['email']
|
|
||||||
|
assignTeam(user.id, idTeam)
|
||||||
|
return redirect("/", code=302)
|
||||||
|
return redirect("/login", code=302)
|
||||||
|
|
||||||
|
@app.route('/delete/<int:idTeam>')
|
||||||
|
def deleteTeam(idTeam):
|
||||||
|
if 'google_token' in session:
|
||||||
|
me = google.get('userinfo')
|
||||||
|
user = User.query.filter_by(email=me.data['email']).first()
|
||||||
|
if user is None:
|
||||||
|
user = User(email=me.data['email'])
|
||||||
|
db.session.add(user)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
deleteTeam(idTeam)
|
||||||
|
return redirect("/", code=302)
|
||||||
|
return redirect("/login", code=302)
|
||||||
|
|
||||||
|
@app.route('/calendar')
|
||||||
|
def download_ical():
|
||||||
|
if 'google_token' in session:
|
||||||
|
me = google.get('userinfo')
|
||||||
|
user = User.query.filter_by(email=me.data['email']).first()
|
||||||
|
if user is None:
|
||||||
|
user = User(email=me.data['email'])
|
||||||
|
db.session.add(user)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
user_teams = getTeams(user.id)
|
||||||
|
|
||||||
|
events_data = []
|
||||||
|
for team in user_teams:
|
||||||
|
team_events = parseTeamEvents(team.events)
|
||||||
|
events_data.extend(team_events)
|
||||||
|
|
||||||
|
ical_data = generateIcal(events_data)
|
||||||
|
|
||||||
|
response = Response(ical_data, content_type='text/calendar')
|
||||||
|
response.headers['Content-Disposition'] = 'inline; filename=calendar.ics'
|
||||||
|
return response
|
||||||
|
|
||||||
return redirect("/login", code=302)
|
return redirect("/login", code=302)
|
||||||
|
|
||||||
@app.route('/login')
|
@app.route('/login')
|
||||||
def login():
|
def login():
|
||||||
|
return render_template('login.html')
|
||||||
|
|
||||||
|
@app.route('/login/google')
|
||||||
|
def google_redirect():
|
||||||
return google.authorize(callback=url_for('authorized', _external=True))
|
return google.authorize(callback=url_for('authorized', _external=True))
|
||||||
|
|
||||||
@app.route('/logout')
|
@app.route('/logout')
|
||||||
|
|
@ -89,4 +220,5 @@ def get_google_oauth_token():
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
db.create_all()
|
db.create_all()
|
||||||
app.run(debug=True)
|
|
||||||
|
app.run(debug=True, port=5050)
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 28 KiB |
|
|
@ -0,0 +1,26 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Calendar settings</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<h2>Your Teams:</h2>
|
||||||
|
<ul>
|
||||||
|
{% for team in userTeams %}
|
||||||
|
<li><a>{{ getTeamName(team.idTeam) }}</a> <a href="/del/{{ team.idTeam }}">Delete</a></li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<h2>Total Teams:</h2>
|
||||||
|
<ul>
|
||||||
|
{% for team in otherTeams %}
|
||||||
|
<li><a>{{ getTeamName(team) }}</a> <a href="/add/{{ team }}">Add</a></li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<a href="{{ url_for('download_ical') }}">Download iCal Calendar</a>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
@ -0,0 +1,30 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||||
|
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
|
||||||
|
<title>Login with Google</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<div class="container">
|
||||||
|
<div class="row justify-content-center align-items-center" style="height:100vh;">
|
||||||
|
<div class="col-md-4">
|
||||||
|
<div class="card">
|
||||||
|
<div class="card-body text-center">
|
||||||
|
<img src="static/logo/google.png" width="150px">
|
||||||
|
<h5 class="card-title">Login with Google</h5>
|
||||||
|
<p class="card-text">Click the button below to connect with Google</p>
|
||||||
|
<a href="/login/google" class="btn btn-danger btn-lg">Login with Google</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
|
||||||
|
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.10.2/dist/umd/popper.min.js"></script>
|
||||||
|
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Loading…
Reference in New Issue