forked from Dishti-Oberai/Get_Set_Hired
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrollers.py
More file actions
274 lines (225 loc) · 9.86 KB
/
controllers.py
File metadata and controls
274 lines (225 loc) · 9.86 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
from django.contrib import messages
def shellController():
from home.models import Tag, Skill
count = len(Tag.objects.all())
tag = Tag(title = 'tag' + str(count+1))
tag.save()
count = len(Skill.objects.all())
skill = Skill(title = 'skill' + str(count+1))
skill.save()
def loginController(req):
from django.contrib.auth import authenticate, login
username = req.POST['username']
password = req.POST['password']
user = authenticate(req, username=username, password=password)
if user is not None:
login(req, user)
messages.success(req, f"{username} successfully loggedin!")
context = {'status': True}
return context
messages.error(req, 'Please provide correct details.')
context = {'status': False}
return context
def userRegisterController(req):
from home.forms import UserRegisterForm
from django.contrib.auth.models import User
from utils import sendFormErrorMessages
form = UserRegisterForm(req.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
user = User.objects.get(username=username)
user.userprofile.isUser = True
user.userprofile.save()
messages.success(req, f"Account created for {username}!")
context = {"status": True}
return context
sendFormErrorMessages(req, form)
context = {"status": False}
return context
def companyRegisterController(req):
from home.forms import CompanyRegisterForm
from django.contrib.auth.models import User
from utils import sendFormErrorMessages
form = CompanyRegisterForm(req.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
user = User.objects.get(username=username)
user.companyprofile.isCompany = True
user.companyprofile.save()
messages.success(req, f"Account created for {username}!")
context = {"status": True, "form": form}
return context
sendFormErrorMessages(req, form)
context = {"status": False, "form": form}
return context
def indexController(req):
context = {"status": True}
from home.models import Notification
if req.user.userprofile.isUser:
context["image"] = req.user.userprofile.image
else:
from home.models import JobPosting
jobPostings_closed = JobPosting.objects.all().filter(status = 'closed')
jobPostings_open = JobPosting.objects.all().filter(status = 'open')
context["jobPostings_open"] = jobPostings_open
context["jobPostings_closed"] = jobPostings_closed
context["notifications"] = Notification.objects.filter(reciever = req.user).order_by('-time')
return context
def profileController(req, profileId):
from home.models import UserProfile, CompanyProfile, Feedback
import requests
user = True
profile = UserProfile.objects.get(id=profileId)
if profile.isUser == False:
user = False
profile = CompanyProfile.objects.get(id=profileId)
feedback = Feedback.objects.all().filter(user__in = [profile.user.id]).filter(company__in = [req.user]).first()
context = {"status": True, "user": profile.user, "profile": profile, "feedback": feedback}
if user == False:
context['website'] = requests.get(profile.website_link).text
if len(Feedback.objects.all().filter(user__in = [profile.user.id])):
from utils import findAverageRating
context['averageRating'] = findAverageRating(profile.user.id)
return context
def profileEditController(req, profileId):
from utils import profileCompleted, sendFormErrorMessages
if req.method == "POST":
from utils import tryExcept
from home.models import UserProfile, CompanyProfile
from home.forms import UpdateUserProfileForm
profile = UserProfile.objects.get(id=profileId)
if profile == None:
profile = CompanyProfile.objects.get(id=profileId)
user = profile.user
username = user.username
data = req.POST.dict()
for key in data:
tryExcept(req, user, key, data[key])
user.save()
data['user'] = user
print(data)
if user.userprofile.isUser:
form = UpdateUserProfileForm(data, req.FILES, instance = profile)
if form.is_valid():
form.save()
messages.success(req, f"Profile updated for {username}!")
context = {"status": True}
return context
sendFormErrorMessages(req, form)
context = {"status": False}
return context
else:
user.companyprofile.email = data['email']
user.companyprofile.name = data['name']
user.companyprofile.contact_num = data['contact_num']
user.companyprofile.number_of_employees = data['number_of_employees']
user.companyprofile.website_link = data['website_link']
user.companyprofile.save()
messages.success(req, f"Profile updated for {username}!")
context = {"status": True}
return context
context = {"status": True, "user": req.user}
if not profileCompleted(req):
context['showNav'] = False
return context
def jobPostingController(req, jobPostingId):
from home.models import JobPosting, UserProfile
from utils import findAverageRating, sendFormErrorMessages
jobposting = JobPosting.objects.get(id = jobPostingId)
users = UserProfile.objects.all().filter(isUser = True)
users_accepted = list(jobposting.users_accepted.all())
willing_to_hire_users = list(jobposting.willing_to_hire.all())
willing_to_hire_users = [user.userprofile for user in willing_to_hire_users]
rem_users = [user for user in users if user not in willing_to_hire_users]
users_accepted = [(user, findAverageRating(user.id)) for user in users_accepted]
willing_to_hire_users = [(user, findAverageRating(user.id)) for user in willing_to_hire_users]
rem_users = [(user, findAverageRating(user.id)) for user in rem_users]
context = {"status": True, "users_accepted":users_accepted, "jobPosting": jobposting, "willing_to_hire_users": willing_to_hire_users, 'rem_users': rem_users}
return context
def createJobPostingController(req):
from utils import sendFormErrorMessages
from home.forms import CreateJobPostingForm
if req.method == "POST":
data = req.POST.dict()
domain_tags = req.POST.getlist('domain_tags')
requirement_tags = req.POST.getlist('requirement_tags')
data['domain_tags'] = domain_tags
data['requirement_tags'] = requirement_tags
data['company'] = req.user
form = CreateJobPostingForm(data, req.FILES)
if form.is_valid():
jobposting = form.save()
req.user.companyprofile.jobpostings.add(jobposting)
messages.success(req, f"JobPosting created successfully!")
context = {"status": True, 'jobPostingId': jobposting.pk}
return context
sendFormErrorMessages(req, form)
context = {"status": False, "jobPostingForm": CreateJobPostingForm()}
return context
context = {"status": True, "jobPostingForm": CreateJobPostingForm()}
return context
def editJobPostingController(req, jobPostingId):
from home.models import JobPosting
from home.forms import EditJobPostingForm
from utils import sendFormErrorMessages
jobPosting = JobPosting.objects.get(id = jobPostingId)
if req.method == "POST":
data = req.POST.dict()
domain_tags = req.POST.getlist('domain_tags')
requirement_tags = req.POST.getlist('requirement_tags')
data['domain_tags'] = domain_tags
data['requirement_tags'] = requirement_tags
data['company'] = req.user
form = EditJobPostingForm(data, req.FILES, instance = jobPosting)
if form.is_valid():
form.save()
messages.success(req, f"JobPosting successfully updated!")
context = {"status": True}
return context
sendFormErrorMessages(req, form)
context = {"status": False}
return context
context = {"status": True, "jobPostingForm": EditJobPostingForm(instance = jobPosting)}
return context
def giveFeedbackController(req, companyId, userId):
pass
def acceptController(req, jobPostingId, userId):
from home.models import JobPosting
from django.contrib.auth.models import User
from utils import sendNotification
user = User.objects.get(id = userId)
jobposting = JobPosting.objects.get(id = jobPostingId)
jobposting.willing_to_hire.add(user)
jobposting.save()
sendNotification(jobposting, user, "Want this job?")
def rejectController(req, jobPostingId, userId):
from home.models import JobPosting
from django.contrib.auth.models import User
from utils import sendNotification
user = User.objects.get(id = userId)
jobposting = JobPosting.objects.get(id = jobPostingId)
jobposting.willing_to_hire.remove(user)
jobposting.save()
sendNotification(jobposting, user, "Want this job?")
def rateUserController(req, userId, rating):
from home.models import Feedback
from django.contrib.auth.models import User
from utils import sendReviewNotification
user = User.objects.get(id = userId)
company = req.user
old_feedback = Feedback.objects.all().filter(user__in = [userId]).filter(company__in = [req.user.id]).first()
if old_feedback == None:
feedback = Feedback(rating = rating, comments = "profile Feedback")
feedback.save()
feedback.user.add(user)
feedback.company.add(company)
feedback.save()
sendReviewNotification(req.user, user, "Reviewed", feedback)
else:
old_feedback.rating = rating
old_feedback.save()
sendReviewNotification(req.user, user, "Reviewed", old_feedback)
context = {"status": True}
return context