-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
151 lines (132 loc) · 5.07 KB
/
app.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
import os
from flask import Flask, session, render_template, redirect, request, jsonify, g, flash
from flask_debugtoolbar import DebugToolbarExtension
from models import User, Search, db, connect_db
from forms import UserForm, NoteForm, LoginForm
# from secrets import merriam_webster, sketch
import requests
app = Flask(__name__)
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY',"carmelvalley")
app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get('DATABASE_URL', "postgres:///prag_dict")
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.config["SQLALCHEMY_ECHO"] = True
app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False
debug = DebugToolbarExtension(app)
connect_db(app)
db.create_all()
def check_login():
if 'user_id' in session:
user = User.query.get(session['user_id'])
g.user = user
@app.route('/')
def home_page():
check_login()
return render_template('home.html', form=NoteForm())
# Make a call to the Merriam Webster Dictionary API and get short definitions
@app.route('/api/dictionary/<word>', methods=['GET','POST'])
def return_dict(word):
url = f'https://dictionaryapi.com/api/v3/references/learners/json/{word}'
resp = requests.get(url, params={
'key': os.environ.get('mw_apikey', 'secret_api')
})
data = resp.json()
definition = data[0]['meta']['app-shortdef']['def']
return jsonify(definition)
# Make a call to the Sketch Engine API and get the corpus data
@app.route('/api/sketchengine/<word>/<pos>', methods=['GET','POST'])
def return_gramrels(word, pos):
USERNAME = 'thor.sawin'
API_KEY = os.environ.get('sketch_apikey', 'secret_api')
url = 'https://api.sketchengine.eu/bonito/run.cgi/wsketch'
data = requests.get(url, auth=(USERNAME, API_KEY), params={
'corpname': 'preloaded/bnc2',
'format': 'json',
'lemma': word,
'lpos': pos
}).json()
session['word'] = word
session['pos'] = pos
return jsonify(data['Gramrels'])
@app.route('/register', methods=['GET', 'POST'])
def register_page():
form = UserForm()
if form.validate_on_submit():
username = form.data['username']
email = form.data['email']
password = form.data['password']
first_name = form.data['first_name']
last_name = form.data['last_name']
new_user = User.register(username=username, email=email, pwd=password, first_name=first_name, last_name=last_name)
session['user_id'] = new_user.id
db.session.add(new_user)
db.session.commit()
return redirect('/')
else:
return render_template('register.html', form=form)
@app.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
username = form.data['username']
password = form.data['password']
user = User.authenticate(username=username, pwd=password)
if user:
session['user_id'] = user.id
g.user = user
return redirect('/')
else:
flash('Incorrect credentials')
return redirect('/login')
else:
return render_template('login.html', form=form)
# show user their notes page
@app.route('/<int:user_id>/notes', methods=['GET'])
def show_notes(user_id):
g.user = User.query.get_or_404(user_id)
form = NoteForm()
if user_id != session['user_id']:
flash('Access denied')
return redirect('/')
else:
notes = g.user.searches
return render_template('notes.html', notes=notes)
@app.route('/<int:user_id>/notes', methods=['POST'])
def save_notes(user_id):
g.user = User.query.get_or_404(user_id)
word, pos, user_id, note = [request.json[k] for k in ('word', 'pos', 'user_id', 'note')]
if len(Search.query.filter(Search.word==word, Search.user_id == user_id).all()) == 0:
search = Search(word=word, pos=pos, note=note, user_id=user_id)
db.session.add(search)
db.session.commit()
return 'Note created'
else:
search = Search.query.filter(Search.word==word, Search.user_id == user_id).one()
search.note = note
db.session.commit()
return 'Note saved'
@app.route('/<int:user_id>/notes/<int:search_id>')
def show_note(user_id, search_id):
g.user = User.query.get_or_404(user_id)
note = Search.query.get_or_404(search_id)
word = note.word
pos = note.pos.value
if user_id != session['user_id']:
flash('Access denied')
return redirect('/')
return render_template('home.html', word_to_search=word, pos=pos, form=NoteForm(obj=note))
@app.route('/<int:user_id>/notes/<int:search_id>/delete')
def delete_note(user_id, search_id):
g.user = User.query.get_or_404(user_id)
note = Search.query.get_or_404(search_id)
if note.user_id == g.user.id:
db.session.delete(note)
db.session.commit()
flash('Note deleted', 'success')
return redirect(f'/{user_id}/notes')
else:
flash('Permission denied', 'danger')
return redirect('/')
@app.route('/logout', methods=['GET'])
def logout():
del session['user_id']
return redirect('/')