-
Notifications
You must be signed in to change notification settings - Fork 1
/
oldApp.py
136 lines (105 loc) · 4.11 KB
/
oldApp.py
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
from flask import Flask, render_template, request, redirect, url_for, flash, session, get_flashed_messages, jsonify
import random
import string
from datetime import datetime
app = Flask(__name__)
app.secret_key = 'VerySecret' # Replace with your own secret key
# User login details
users = {
'admin': 'password',
'user1': 'pass123',
'user2': 'pass456'
}
Students = []
@app.route('/')
def home():
if 'username' in session:
return render_template('home.html')
else:
return redirect(url_for('login'))
@app.route("/studentCreate", methods=["POST"])
def studentCreate():
# Get data from the form using request.form.get
name = request.form.get("name")
surname = request.form.get("surname")
age = request.form.get("age")
joiningYear = request.form.get("joiningYear")
# Create a dictionary representing the student and add it to the Students list
student = {
"name": name,
"surname": surname,
"age": age,
"joiningYear": joiningYear
}
Students.append(student)
response_message = f"Received: {name} {surname}, Age: {age}, Joining Year: {joiningYear}"
# Return a JSON response indicating success and the student record
return jsonify({"message": "Student record created successfully.", "student": student})
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
# Validate username and password
if username in users and users[username] == password:
session['username'] = username # Store username in session
flash('Login successful!', 'success')
return redirect(url_for('home'))
else:
flash('Invalid username or password!', 'error')
return render_template('login.html')
@app.route('/logout')
def logout():
session.pop('username', None) # Remove username from session
with app.test_request_context(): # Create a temporary request context
# Clear all flash messages
flashes = list(get_flashed_messages())
for message in flashes:
flash(message, 'message')
flash('Logged out successfully!', 'success') # Add flash message for successful logout
return redirect(url_for('login'))
# student
class Student:
def __init__(self, name, surname, age, joiningYear):
self.student_id = self.generate_student_id(name)
self.name = name
self.surname = surname
self.age = age
self.joiningYear = joiningYear
def name_checker(name):
if name.isalpha():
return True
else:
return False
def number_checker(age):
try:
float(age)
return True
except ValueError:
return False
def generate_student_id(self, name):
random_suffix = ''.join(random.choices(string.digits, k=4))
name_without_spaces = name.replace(" ", "").lower()
student_id = name_without_spaces[:3] + random_suffix
return student_id
def create_student(name, surname, age, joiningYear):
while not name_checker(name) or len(name) > 15:
invalid_Name = "Invalid input. Please enter letters only and a maximum of 15 characters."
name = input("Enter student's name: ")
while not name_checker(surname) or len(surname) > 15:
print("Invalid input. Please enter letters only and a maximum of 15 characters.")
surname = input("Enter student's surname: ")
while not number_checker(age) or not 11 <= int(age) <= 17:
print("Invalid input. Please enter numbers only between 11 and 17.")
age = input("Enter student's age: ")
while True:
try:
joiningYear = datetime.strptime(joiningYear, "%Y-%m-%d").date()
break
except ValueError:
print("Invalid date format. Please enter the date in the specified format.")
joiningYear = input("Enter school start date (YYYY-MM-DD): ")
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)
# if __name__ == '__main__':
# app.run(ssl_context=('certificate.pem', 'private_key.pem'), host='0.0.0.0', port=443)