-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
119 lines (86 loc) · 3.55 KB
/
app.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
from flask import Flask, render_template, request, redirect, session, jsonify
from pymongo import MongoClient
from pymongo.server_api import ServerApi
app = Flask(__name__)
app.secret_key = 'your_secret_key'
uri = 'mongodb+srv://Temp_User:9BH1EM6p6LWStCxt@mongodatabase.ytbk03l.mongodb.net/?retryWrites=true&w=majority'
# Create a new client and connect to the server
client = MongoClient(uri, server_api=ServerApi('1'))
# Send a ping to confirm a successful connection
try:
client.admin.command('ping')
print("Pinged your deployment. You successfully connected to MongoDB!")
except Exception as e:
print(e)
db = client['ai_buisness']
users_collection = db['users']
companies_collection = db['companies']
services_collection = db['services']
contact_collection = db['contact_form']
@app.route('/')
def home():
companies = companies_collection.find()
return render_template('home.html', companies=companies)
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
email = request.form['email']
password = request.form['password']
# Check if the user exists in the database
user = users_collection.find_one({'email': email, 'password': password})
if user:
# Convert ObjectId to string before storing in session
user['_id'] = str(user['_id'])
# Store user data in session
session['user'] = user
return redirect('/')
else:
message = 'Invalid email or password'
return render_template('login.html', message=message)
return render_template('login.html')
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
username = request.form['username']
email = request.form['email']
password = request.form['password']
# Check if the email is already registered
existing_user = users_collection.find_one({'email': email})
if existing_user:
message = 'Email already registered'
return render_template('login.html', message=message)
# Create a new user document
new_user = {'username': username, 'email': email, 'password': password}
result = users_collection.insert_one(new_user)
new_user['_id'] = str(result.inserted_id)
# Store user data in session
session['user'] = new_user
return redirect('/')
return render_template('login.html')
@app.route('/logout')
def logout():
session.pop('user', None)
return redirect('/')
@app.route('/services')
def services():
# Fetch all services from the database
services = services_collection.find()
return render_template('services.html', services=services)
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/contact', methods=['GET', 'POST'])
def contact():
if request.method == 'POST':
name = request.form['name']
email = request.form['email']
message = request.form['message']
# Create a new contact form document
contact_form_data = {'name': name, 'email': email, 'message': message}
result = contact_collection.insert_one(contact_form_data)
if result.inserted_id:
return jsonify({'status': 'success'})
return jsonify({'status': 'failed'})
return render_template('contact.html')
if __name__ == '__main__':
app.run(debug=True, host="192.168.0.104")