-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforms.py
More file actions
25 lines (21 loc) · 1.08 KB
/
forms.py
File metadata and controls
25 lines (21 loc) · 1.08 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
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import Length, ValidationError
from datetime import datetime
import requests
class SearchForm(FlaskForm):
user = StringField('user',
validators=[Length(min=1, message="Enter valid username")],
render_kw={"placeholder": "Enter reddit username to search comment history",
"novalidate": "novalidate",
})
submit = SubmitField('Submit', render_kw={"onclick": "loading();"})
def validate_user(self, user):
r = requests.get(f"https://www.reddit.com/user/{user.data}.json",
headers={'User-agent': f"reHistory:v0.0 (by /u/r48patel at {datetime.now()})"})
data = r.json()
# print(f'user: {user.data}, r: {r}({r.url}), data: {data}')
if r.status_code != requests.codes.okay:
raise ValidationError("Enter valid username")
if 'error' in data.keys():
raise ValidationError("Enter valid username")