def create(): identity = get_jwt_identity() user = User.query.filter_by(username=identity['username']).first() g.current_user = user data = request.get_json() or {} dictionary = Dictionary.query.get(data['dictionary_id']) if dictionary in g.current_user.dictionaries: dictionary_id = dictionary.id else: abort(403) if 'name' not in data: return bad_request('word cannot be blank') if Word.query.filter_by(name=data['name']).first(): word = Word.query.filter_by(name=data['name']).first() else: word = Word(data["name"], created_by=g.current_user.username) db.session.add(word) db.session.commit() user_word = UserWord(word.id, dictionary_id, g.current_user.id) db.session.add(user_word) try: db.session.commit() except IntegrityError: return bad_request('word already is in your dictionary') if 'description' in data: user_word.description = data['description'] db.session.add(user_word) db.session.commit() if 'translations' in data: for t in data['translations']: trns = Translation(word.id, dictionary.id, t) db.session.add(trns) db.session.commit() response = jsonify(word.as_json()) response.status_code = 201 response.headers['Location'] = url_for('words.show', id=word.id) return response