-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.py
167 lines (118 loc) · 4.41 KB
/
controller.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
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
"""
This is Our Controller. Basically this is the heart of the project because it holds all the models and view together.
All the routes and classes are defined in here.
"""
import web
from Models import RegisterModel, LoginModel, Posts
web.config.debug = False
# All the routes for our websites are defined here
urls = (
'/', 'Home',
'/register', 'Register',
'/login', 'Login',
'/logout', 'Logout',
'/postregistration', 'PostRegistration',
'/check-login', 'CheckLogin',
'/post-activity', 'PostActivity',
'/profile/(.*)/info', 'UserInfo',
'/profile/(.*)', 'UserProfile',
'/settings', 'UserSettings',
'/update-settings', 'UpdateSettings',
'/submit-comment', 'SubmitComment',
'/upload-image/(.*)', 'UploadImage'
)
# Start the instance of our web app
app = web.application(urls, globals())
# Creating a session for an emtpy user at start, once the user logs in sssion obejct will be updated
session = web.session.Session(app, web.session.DiskStore('sessions'), initializer={'user': None})
session_data = session._initializer
render = web.template.render('Views/Templates', base='MainLayout', globals={'session': session_data, 'current_user': session_data['user']})
class Home:
def GET(self):
# data = type('obj', (object,), {'username': 'attu', 'password': 'patil'})
# login = LoginModel.LoginModel()
# isCorrect = login.check_user(data)
# if isCorrect:
# session_data['user'] = isCorrect
post_model = Posts.Posts()
posts = post_model.get_all_posts()
return render.Home(posts)
class Register:
def GET(self):
return render.Register()
class Login:
def GET(self):
return render.Login()
class CheckLogin:
def POST(self):
# To take the input from html form
data = web.input()
# Here we will make a call to out model LoginModel.py inside that we will call LoginModel class
login = LoginModel.LoginModel()
# we will compare the entered password with the password which is stored inside th database
isCorrect = login.check_user(data)
# if password is correct then establish a login session and return it
if isCorrect:
session_data['user'] = isCorrect
return isCorrect
# otherwise return error
return 'error'
class UserProfile:
def GET(self, user):
login = LoginModel.LoginModel()
user_info = login.get_profile(user)
post_model = Posts.Posts()
posts = post_model.get_user_posts(user)
return render.Profile(posts, user_info)
class UserInfo:
def GET(self, user):
login = LoginModel.LoginModel()
user_info = login.get_profile(user)
return render.Info(user_info)
class UserSettings:
def GET(self):
return render.Settings()
class UpdatingSettings:
def POST(self):
data = web.input()
data.username = session_data["user"]["username"]
settings_model = LoginModel.LoginModel()
if settings_model.update_info(data):
return "success"
else:
return "A fatal error has occurred."
class SubmitComment:
def POST(self):
data = web.input()
data.username = session_data["user"]["username"]
post_model = Posts.Posts()
added_comment = post_model.add_comment(data)
if added_comment:
return added_comment
else:
return {"error": "403"}
class Logout:
def GET(self):
session['user'] = None
session_data['user'] = None
session.kill()
return 'success'
class PostRegistration:
def POST(self):
data = web.input()
# Here we will make a call to out model RegisterModel.py inside that we will call RegisterModel class
reg_model = RegisterModel.RegisterModel()
# Using insert user we will create a new entry to the data base for this user who registers
reg_model.insert_user(data)
return data.username
class PostActivity:
def POST(self):
data = web.input()
# We need to store posts with every user's name to tell who posted what
data.username = session_data['user']['username']
# Here we will make a call to out model Posts.py inside that we will call Posts class
post_model = Posts.Posts()
post_model._insert_post(data)
return 'success'
if __name__ == '__main__':
app.run()