def add_word(): form = AddWordForm(request.form) if request.method == 'POST': if not form.validate(): abort(400) word_data = form.data del word_data['lang_name'] # make sure word doesn't exist word_check = model.Word.query.filter_by(**word_data).first() if word_check is None: # add the word to the database word = model.Word(current_user, word_data['orig_form'], word_data['language'], definition=word_data['definition'], latin_form=word_data['latin_form'], ipa_form=word_data['ipa_form']) model.db.session.add(word) model.db.session.commit() return redirect('/{}'.format(word.id)) else: abort(400) langs = sorted([model.names[code] for code in model.names]) lang_lookup = {} for code in model.names: lang_lookup[model.names[code]] = code return helpers.render_search_template('addword.html', form=form, langs=langs, lang_lookup=lang_lookup, title='Add Word')
def search(): # ET-5, ET-19 ''' Search for all words that match a particular substring usage: /search?q=<string> or frontend search bar ''' if 'q' not in request.args or request.args['q'] == '': abort(400) query = request.args['q'] if 'page' not in request.args: return redirect(url_for('web.search', q=query, page=1)) page = int(request.args['page']) if page < 1: abort(400) per_page = 20 results = helpers.search(query, page=page, per_page=per_page) return helpers.render_search_template('results.html', query=query, results=results[0], results_length=results[1], page=page, page_count=int(math.ceil(results[1]/per_page)), body_class="search", title='Results for {}'.format(query))
def show_graph(word_id): word = model.Word.query.get(word_id) if word is None: abort(404) else: word_roots = word.get_roots() word_descs = word.get_descs() return helpers.render_search_template('graph.html', roots=word_roots, descs=word_descs, form=AddRootForm(), body_class="graph", add_root_search=SearchForm(), title=word.orig_form)
def profile(user_id): user = get_user(user_id=user_id) is_me = (user == current_user) if user == None: return abort(400) results = [word.info() for word in user.created_words] return helpers.render_search_template('profile.html', user_name=user.name, body_class='index', is_me=is_me, added_words=results, title=user.name)
def login(): if current_user.is_authenticated: return redirect(url_for('web.index')) form = LoginForm(request.form) if request.method == 'POST': if not form.validate(): abort(400) user_data = form.data user = model.User.query.filter_by(username=user_data['username']).first() if user is not None and user.check_password(user_data['password']): login_user(user) return redirect(url_for('web.index')) else: print(user) abort(400) return helpers.render_search_template('login.html', form=form, title='Log in')
def edit_rel(root_id, desc_id): form = EditRelForm(request.form) #get dicts of info about this relationship rel = model.Rel.query.filter_by(root_id=root_id, desc_id=desc_id).first() if rel is None: abort(404) if request.method == 'POST': if not form.validate(): abort(400) source = form.source.data # edit the relationship the database rel.source = source model.db.session.commit() next_url = '/{}'.format(root_id) return redirect(next_url) my_URL = url_for('edit.edit_rel', root_id=root_id, desc_id=desc_id) return helpers.render_search_template('edit_rel.html', form=form, root=rel.root.info(), rel=rel.info(), desc=rel.desc.info(), my_URL=my_URL, title='Edit Relation')
def register(): form = RegisterForm(request.form) if request.method == 'POST': if not form.validate(): abort(400) user_data = form.data # make sure username isn't taken user_check = model.User.query.filter_by(username=user_data['username']).first() if user_check is None: # add the word to the database user = model.User(user_data['username'], user_data['password'], user_data['name']) model.db.session.add(user) model.db.session.commit() return redirect(url_for('user.login')) abort(400) return helpers.render_search_template('register.html', form=form, title='Register')
def edit_word(word_id): form = EditWordForm(request.form) if request.method == 'POST': if not form.validate(): abort(400) word_data = form.data del word_data['lang_name'] # edit the word in the database word = model.Word.query.get(word_id) word.orig_form = word_data['orig_form'] word.language = word_data['language'] word.definition = word_data['definition'] word.ipa_form = word_data['ipa_form'] word.latin_form = word_data['latin_form'] model.db.session.commit() return redirect('/{}'.format(word_id)) langs = sorted([model.names[code] for code in model.names]) lang_lookup = {} for code in model.names: lang_lookup[model.names[code]] = code word = model.Word.query.get(word_id).info() return helpers.render_search_template('editword.html', form=form, langs=langs, lang_lookup=lang_lookup, word=word, title='Edit Word')
def api_doc(): return helpers.render_search_template('api.html', title='API')
def about(): return helpers.render_search_template('about.html', title='About')
def show_flagged(): words = [flag.word.info() for flag in model.WordFlag.query.all()] rels = [flag.rel.info() for flag in model.RelFlag.query.all()] return helpers.render_search_template('flagged.html', words=words, rels=rels, title='Flagged')