Skip to content
This repository was archived by the owner on Jun 9, 2021. It is now read-only.

Commit 6e9711b

Browse files
author
raylu
committed
unclaim mentorship
1 parent 871ab8c commit 6e9711b

File tree

3 files changed

+39
-16
lines changed

3 files changed

+39
-16
lines changed

db.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ def create_user(self, user):
1818
yield self.execute(query, user['id'], user['login'], user['access_token'], 0)
1919

2020
@tornado.gen.coroutine
21-
def create_mentorship(self, mentee, mentor):
22-
mentee_claimed = yield self.get_mentor(mentee['github_id'])
23-
if mentor['is_mentor'] and not mentee['is_mentor'] and not mentee_claimed:
24-
query = 'INSERT INTO mentorships(mentee_id, mentor_id) VALUES (%s, %s);'
25-
yield self.execute(query, mentee['github_id'], mentor['github_id'])
21+
def create_mentorship(self, mentee_id, mentor_id):
22+
query = 'INSERT INTO mentorships(mentee_id, mentor_id) VALUES (%s, %s);'
23+
yield self.execute(query, mentee_id, mentor_id)
24+
25+
@tornado.gen.coroutine
26+
def remove_mentorship(self, mentee_id, mentor_id):
27+
query = 'DELETE FROM mentorships WHERE mentee_id = %s AND mentor_id = %s;'
28+
yield self.execute(query, mentee_id, mentor_id)
2629

2730
@tornado.gen.coroutine
2831
def update_access_token(self, user):

server.py

+24-10
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,28 @@ def get(self):
9797
users = yield self.db.get_userlist()
9898
self.render('users.html', users=users)
9999

100+
class ClaimHandler(BaseHandler):
101+
@tornado.web.authenticated
102+
@tornado.gen.coroutine
103+
def post(self, username):
104+
if not self.current_user['is_mentor']:
105+
raise tornado.web.HTTPError(403)
106+
mentee = yield self.db.get_user_by('username', username)
107+
if mentee['is_mentor']:
108+
raise tornado.web.HTTPError(403)
109+
yield self.db.create_mentorship(mentee['github_id'], self.current_user['github_id'])
110+
self.redirect('/users/' + username)
111+
112+
class UnclaimHandler(BaseHandler):
113+
@tornado.web.authenticated
114+
@tornado.gen.coroutine
115+
def post(self, username):
116+
if not self.current_user['is_mentor']:
117+
raise tornado.web.HTTPError(403)
118+
mentee = yield self.db.get_user_by('username', username)
119+
yield self.db.remove_mentorship(mentee['github_id'], self.current_user['github_id'])
120+
self.redirect('/users/' + username)
121+
100122
class ProfileHandler(BaseHandler):
101123
@tornado.gen.coroutine
102124
def get(self, username):
@@ -111,16 +133,6 @@ def get(self, username):
111133
questions, answers = yield self.db.get_questionnaire(user['github_id'])
112134
self.render('profile.html', user=user, mentor=mentor, mentees=mentees, questions=questions, answers=answers)
113135

114-
@tornado.web.authenticated
115-
@tornado.gen.coroutine
116-
def post(self, username):
117-
if not self.current_user['is_mentor']:
118-
raise tornado.web.HTTPError(403)
119-
mentee = yield self.db.get_user_by('username', username)
120-
mentor = yield self.db.get_user(self.current_user['github_id'])
121-
yield self.db.create_mentorship(mentee, mentor)
122-
self.redirect('/users/' + username)
123-
124136
class AccountHandler(BaseHandler):
125137
@tornado.web.authenticated
126138
@tornado.gen.coroutine
@@ -166,6 +178,8 @@ def get(self, css_path):
166178
(r'/github_emails', GithubEmailsHandler),
167179
(r'/logout', LogoutHandler),
168180
(r'/users', UserListHandler),
181+
(r'/users/(.*)/claim', ClaimHandler),
182+
(r'/users/(.*)/unclaim', UnclaimHandler),
169183
(r'/users/(.*)', ProfileHandler),
170184
(r'/account', AccountHandler),
171185
(r'/account/contact_info', ContactInfoHandler),

templates/profile.html

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,14 @@ <h3>Mentees</h3>
1212
{% else %}
1313
{% if mentor %}
1414
<h3>Mentor: <a href="{{ mentor['username'] }}">{{ mentor['username'] }}</a></h3>
15+
{% if current_user and current_user['github_id'] == mentor['github_id'] %}
16+
<form action="/users/{{ user['username'] }}/unclaim" method="post">
17+
<input type="hidden" name="github_id" value="{{ user['github_id'] }}">
18+
<input type="submit" value="Unclaim {{ user['username'] }}">
19+
</form>
20+
{% end %}
1521
{% elif current_user['is_mentor'] %}
16-
<form method="post">
22+
<form action="/users/{{ user['username'] }}/claim" method="post">
1723
<input type="hidden" name="github_id" value="{{ user['github_id'] }}">
1824
<input type="submit" value="Claim {{ user['username'] }}">
1925
</form>

0 commit comments

Comments
 (0)