forked from Shreyas8905/EventZone
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
169 lines (142 loc) · 6.63 KB
/
app.py
File metadata and controls
169 lines (142 loc) · 6.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
from flask import Flask, render_template, redirect, url_for, request, flash, session
from flask_bcrypt import Bcrypt
from flask_login import LoginManager, login_user, logout_user, login_required, current_user
from flask_mail import Mail, Message
from models import db, User, Event, Participated
from datetime import datetime, timedelta
from dotenv import load_dotenv
import os
load_dotenv()
username = os.getenv('MYSQLUSERNAME')
password = os.getenv('MYSQLPASS')
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = f'mysql://{username}:{password}@localhost/eventdb'
app.config['SECRET_KEY'] = 'your_secret_key'
app.config['MAIL_SERVER'] = 'smtp.gmail.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = ''
app.config['MAIL_PASSWORD'] = ''
db.init_app(app)
bcrypt = Bcrypt(app)
login_manager = LoginManager(app)
login_manager.login_view = 'login'
mail = Mail(app)
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
@app.route('/')
def home():
return render_template('index.html')
# Login, Signup, and Authentication routes
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
email = request.form['email']
password = request.form['password']
user = User.query.filter_by(email=email).first()
if user and bcrypt.check_password_hash(user.password, password):
login_user(user)
return redirect(url_for('dashboard'))
flash('Invalid credentials!')
return render_template('login.html')
@app.route('/signup', methods=['GET', 'POST'])
def signup():
if request.method == 'POST':
name = request.form['name']
email = request.form['email']
phone_number = request.form['phone']
password = bcrypt.generate_password_hash(request.form['password']).decode('utf-8')
user = User(name=name, email=email, phone_number=phone_number, password=password)
db.session.add(user)
db.session.commit()
flash('Account created! Please log in.')
return redirect(url_for('login'))
return render_template('signup.html')
@app.route('/dashboard')
@login_required
def dashboard():
upcoming_events = Event.query.filter(Event.date >= datetime.now(), Event.id != current_user.id).all()
organized_events = Event.query.filter_by(organizer_id=current_user.id).all()
return render_template('dashboard.html', upcoming_events=upcoming_events, organized_events=organized_events)
@app.route('/create_event', methods=['GET', 'POST'])
@login_required
def create_event():
if request.method == 'POST':
name = request.form['name']
place = request.form['place']
date = datetime.strptime(request.form['date'], '%Y-%m-%d')
time = datetime.strptime(request.form['time'], '%H:%M').time()
event = Event(name=name, place=place, date=date, time=time, organizer_id=current_user.id)
db.session.add(event)
db.session.commit()
flash('Event created successfully!')
return redirect(url_for('dashboard'))
return render_template('create_event.html')
@app.route('/manage_event/<int:event_id>', methods=['GET', 'POST'])
@login_required
def manage_event(event_id):
event = Event.query.get_or_404(event_id)
if event.organizer_id != current_user.id:
flash('Unauthorized access!')
return redirect(url_for('dashboard'))
if request.method == 'POST':
# Update the event details
event.name = request.form['name']
event.place = request.form['place']
event.date = datetime.strptime(request.form['date'], '%Y-%m-%d')
# Extract hours and minutes, ignoring any extra seconds
time_str = request.form['time'].split(':')[0:2]
# Convert the time string back to a datetime object and store the time part
event.time = datetime.strptime(':'.join(time_str), '%H:%M').time()
db.session.commit()
flash('Event updated successfully!')
return redirect(url_for('dashboard'))
participants = User.query.join(Participated).filter(Participated.event_id == event_id).all()
return render_template('manage_event.html', event=event, participants=participants)
# @app.route('/participate/<int:event_id>')
# @login_required
# def participate(event_id):
# existing_participation = Participated.query.filter_by(user_id=current_user.id, event_id=event_id).first()
# if existing_participation:
# flash('You are already enrolled in this event!')
# return redirect(url_for('dashboard'))
# participation = Participated(user_id=current_user.id, event_id=event_id)
# db.session.add(participation)
# db.session.commit()
# flash('You have successfully enrolled in the event!')
# return render_template('participate.html')
@app.route('/participate/<int:event_id>')
@login_required
def participate(event_id):
existing_participation = Participated.query.filter_by(user_id=current_user.id, event_id=event_id).first()
if existing_participation:
flash('You are already enrolled in this event!', 'warning')
return render_template('participate.html', event_id=event_id)
participation = Participated(user_id=current_user.id, event_id=event_id)
db.session.add(participation)
db.session.commit()
flash('You have successfully enrolled in the event!', 'success')
return render_template('participate.html', event_id=event_id)
@app.before_request
def send_reminders():
events = Event.query.all()
for event in events:
if (event.date - timedelta(days=1)) == datetime.now().date():
participants = Participated.query.filter_by(event_id=event.id).all()
for participant in participants:
user = User.query.get(participant.user_id)
msg = Message('Event Reminder', sender='[email protected]', recipients=[user.email])
msg.body = f"Reminder for your event: {event.name} at {event.place} on {event.date} {event.time}."
mail.send(msg)
@app.route('/view_participants/<int:event_id>')
@login_required
def view_participants(event_id):
event = Event.query.get_or_404(event_id)
if event.organizer_id != current_user.id:
flash('Unauthorized access!')
return redirect(url_for('dashboard'))
participants = User.query.join(Participated).filter(Participated.event_id == event_id).all()
return render_template('view_participants.html', event=event, participants=participants)
if __name__ == '__main__':
app.run(debug=True)