-
Notifications
You must be signed in to change notification settings - Fork 0
/
forms.py
107 lines (77 loc) · 3.41 KB
/
forms.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
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, SelectField
from wtforms.validators import DataRequired, ValidationError
import requests
import psycopg2
HOST = "localhost"
PORT = "5432"
USERNAME = 'lcc'
PASSWORD = '12345678'
DATABASE = 'git_session'
db = psycopg2.connect(user=USERNAME, password=PASSWORD,
host=HOST, database=DATABASE)
class RegisterForm(FlaskForm):
name = StringField('Name', validators=[DataRequired()])
team = SelectField('Team Name', choices=[
('Git Knitters', 'Git Knitters'), ('DebuGIT',
'DebuGIT'), ('Goal Diggers', 'Goal Diggers'),
('GitWiz', 'GitWiz'), ('EPS', 'EPS'), ('FORK IT',
'FORK IT'), ('GitMaster', 'GitMaster'),
('Git Set Go', 'Git Set Go'), ('Git Up', 'Git Up'), ('GoRepo', 'GoRepo'), ('Git Ready', 'Git Ready'), ('Branch Managers', 'Branch Managers')])
github_link = StringField('Github Link', validators=[DataRequired()])
submit = SubmitField('Register')
def validate_github_link(self, github_link):
print("here\n")
user_name = github_link.data.split('/')[-1]
print('Username: '+user_name)
if user_name == '':
print("Username is None")
raise ValidationError(
'Please remove the trailing slash and try again')
query_url = github_link.data
print(query_url)
data = requests.get(query_url)
if data.status_code != 200:
raise ValidationError(
"User not Found on github! Please check the link or contact administrator")
sql_query = 'SELECT github_name FROM users where github_name=%s'
user_name = github_link.data.split('/')[-1]
cursor = db.cursor()
cursor.execute(sql_query, (user_name,))
res = cursor.fetchone()
cursor.close()
if res is not None:
raise ValidationError("You are already in the contest!")
def validate_name(self, name):
print("here\n")
sql_query = 'SELECT display_name FROM users where display_name=%s'
name = name.data.strip()
cursor = db.cursor()
cursor.execute(sql_query, (name,))
res = cursor.fetchone()
cursor.close()
if res is not None:
raise ValidationError("Name already taken!")
class LoginForm(FlaskForm):
github_link = StringField('Github Link', validators=[DataRequired()])
submit = SubmitField('Login')
def validate_github_link(self, github_link):
print("here\n")
user_name = github_link.data.split('/')[-1]
if user_name == '':
raise ValidationError(
'Please remove the trailing slash and try again')
query_url = github_link.data
print(query_url)
data = requests.get(query_url)
if data.status_code != 200:
raise ValidationError(
"User not Found on github! Please check the link or contact administrator")
sql_query = 'SELECT github_name FROM users where github_name=%s'
user_name = github_link.data.split('/')[-1]
cursor = db.cursor()
cursor.execute(sql_query, (user_name,))
res = cursor.fetchone()
cursor.close()
if res is None:
raise ValidationError("Please Register!")