def test_dictionary_multiple_create(self): dictionary_1 = Dictionary(user=self.user, native_lang='spanish', foreign_lang='english') dictionary_2 = Dictionary(user=self.user, native_lang='ukrainian', foreign_lang='french') self.refresh_db(dictionary_1, dictionary_2, self.user) yield db assert db.session.query(Dictionary) == 2
def test_follow_words(self): # create four users u1 = User(username='******', email='*****@*****.**') u2 = User(username='******', email='*****@*****.**') u3 = User(username='******', email='*****@*****.**') u4 = User(username='******', email='*****@*****.**') db.session.add_all([u1, u2, u3, u4]) # create four posts now = datetime.utcnow() w1 = Word(name="cool") w2 = Word(name="sadie") w3 = Word(name="new") db.session.add_all([w1, w2, w3]) db.session.commit() # users each have dictionary d1 = Dictionary(name='john dict', user_id=u1.id) d2 = Dictionary(name='susan dict', user_id=u2.id) d3 = Dictionary(name='mary dict', user_id=u3.id) d4 = Dictionary(name='david dict', user_id=u4.id) db.session.add_all([d1, d2, d3, d4]) db.session.commit() # users add words to their dictionaries uw1 = UserWord(word_id=w1.id, user_id=u1.id, dictionary_id=d1.id) uw2 = UserWord(word_id=w1.id, user_id=u2.id, dictionary_id=d2.id) uw3 = UserWord(word_id=w1.id, user_id=u3.id, dictionary_id=d3.id) uw4 = UserWord(word_id=w2.id, user_id=u4.id, dictionary_id=d4.id) uw5 = UserWord(word_id=w3.id, user_id=u4.id, dictionary_id=d4.id) uw6 = UserWord(word_id=w2.id, user_id=u3.id, dictionary_id=d3.id) db.session.add_all([uw1, uw2, uw3, uw4, uw5, uw6]) db.session.commit() # setup the followers u1.follow(u2) # john follows susan u1.follow(u4) # john follows david u2.follow(u3) # susan follows mary u3.follow(u4) # mary follows david db.session.commit() # check the followed posts of each user f1 = u1.friend_highlights().all() # john should see susan's and david dictionary words f2 = u2.friend_highlights().all() # susan should see mary words f3 = u3.friend_highlights().all() f4 = u4.friend_highlights().all() self.assertEqual(f1, [uw5, uw4, uw2]) self.assertEqual(f2, [uw6, uw3]) self.assertEqual(f3, [uw5, uw4]) self.assertEqual(f4, [])
def register(): if current_user.is_authenticated: return redirect(url_for('index')) form = RegistrationForm() form.my_language.choices = [(language.id, language.language) for language in Language.query.all()] #form.languages.choices = [(language.id, language.language) for language in Language.query.all()] form.languages1.choices = [(language.id, language.language) for language in Language.query.all()] if form.validate_on_submit(): if form.my_language.data in form.languages1.data: flash(_('Native language can not be a foreign language')) return redirect(url_for('register')) language = Language.query.filter_by(id=form.my_language.data).first() user = User(username=form.username.data, email=form.email.data, language_id=language.id) user.set_password(form.password.data) db.session.add(user) db.session.commit() foreign_languages = form.languages1.data for foreign_language in foreign_languages: language = Language.query.filter_by(id=foreign_language).first() dictionary = Dictionary(user_id=user.id, language_id=language.id) db.session.add(dictionary) db.session.commit() flash(_('Congratulations, you are now a registered user!')) return redirect(url_for('login')) return render_template('register.html', title='Register', form=form)
def test_dictionaries(self): """ Test dictionaries routes """ # Arrange user = User.query.filter_by(username='******').first() user_dictionary = Dictionary(dictionary_name='new_dictionary', user_id=user.id) db.session.add(user_dictionary) db.session.commit() # Act response = self.app_client.post( 'dicts/check_dictionary_name', data=dict(dictionary_name='new_dictionary')) # Assert res = response.data self.assertTrue('result' in res) self.assertFalse(res['result']) # Act response = self.app_client.post( 'dicts/check_dictionary_name', data=dict(dictionary_name='new_dictionary name')) res = response.data self.assertTrue('result' in res) self.assertFalse(res['result'])
def test_dictionary_single_create(self): dictionary = Dictionary(user=self.user, native_lang='ukrainian', foreign_lang='english') self.refresh_db(self.user, dictionary) yield db assert db.session.query(Dictionary).count() == 1
def test_dictionary_filds(self): dictionary = Dictionary(user=self.user, native_lang='ukrainian', foreign_lang='english') self.refresh_db(dictionary) yield db assert dictionary.native_lang == 'ukrainian' assert dictionary.foreign_lang == 'english'
def setUp(self): self.user = User(email='*****@*****.**', username='******', password='******') self.dictionary = Dictionary(user=self.user, native_lang='spanish', foreign_lang='english') super().setUp()
def create(): identity = get_jwt_identity() user = User.query.filter_by(username=identity['username']).first() g.current_user = user data = request.get_json() or {} if 'name' not in data: return bad_request('dictionary name cannot be blank') if Dictionary.query.filter_by(name=data['name'], user_id=g.current_user.id).first(): return bad_request('you already have a dictionary with that name') dictionary = Dictionary(data['name'], g.current_user.id) db.session.add(dictionary) db.session.commit() response = jsonify(dictionary.as_json()) response.status_code = 201 response.headers['Location'] = url_for('dictionaries.show', id=dictionary.id) return response
def post(self, user_id): json_data = request.get_json(force=True) dictionary = Dictionary(user=user_id, native_lang=json_data['native_lang'], foreign_lang=json_data['foreign_lang']) db.session.add(dictionary) db.session.commit() result = dictionary_schema.dump(dictionary).data return {"status": 'success', 'data': result}, 201
def test_delete_dictionary(self): dictionary = Dictionary(user=self.user, native_lang='spanish', foreign_lang='english') self.refresh_db(dictionary) db.session.delete(dictionary) db.session.commit() yield db assert db.session.query(Dictionary).count() == 0
def addword(): form = AddwordForm() if form.validate_on_submit(): word = Dictionary(word=form.word.data, definition=form.definition.data, example1=form.example1.data, example2=form.example2.data, synonyms=form.synonyms.data) db.session.add(word) db.session.commit() flash(f'"{form.word.data}" has been added to the Dictionary.') return redirect(url_for('addword')) return render_template('addword.html', title='Add Word', form=form)
def edit_profile(): form = EditProfileForm(current_user.username) languages = Language.query.all() form.languages.choices = [(language.id, language.language) for language in languages] if form.validate_on_submit(): languages_id = [dictionary.language_id for dictionary in current_user.languages] if form.languages.data not in languages_id: current_user.username = form.username.data language_id = form.languages.data user_new_dictionary = Dictionary(user_id = current_user.id, language_id = language_id) db.session.add(user_new_dictionary) db.session.commit() dictionary = Dictionary(user_id = current_user.id, language_id = form.languages.data) flash(_('Your changes have been saved!')) return redirect(url_for('edit_profile')) else: flash('The chosen language is alreay in your dictionary') return render_template('edit_profile.html', title='Edit Profile', form = form)
def fill_out_demo_data(): """ Create demo user, dictionary, words """ app = create_app() with app.app_context(): words = [] with open('demo.json') as json_file: data = json.load(json_file) for word in data['words']: words.append({ 'spelling': word['spelling'], 'definition': word['definition'], 'synonyms': word['synonyms'] }) # User demo_user = User.query.filter_by(username='******').first() if not demo_user: demo_user = User(username='******') demo_user.set_password(Config.DEMO_PASS) db.session.add(demo_user) db.session.commit() # Dictionary demo_dict = Dictionary.query.\ filter_by(user_id=demo_user.id).\ filter_by(dictionary_name='Demo').first() if not demo_dict: demo_dict = Dictionary(user_id=demo_user.id, dictionary_name='Demo') db.session.add(demo_dict) db.session.commit() # Words # words = Word.query.filter_by(dictionary_id=demo_dict.id).all() for word in words: demo_word = Word.query.\ filter_by(dictionary_id=demo_dict.id,).\ filter_by(spelling=word['spelling']).first() if not demo_word: demo_word = Word( dictionary_id=demo_dict.id, spelling=word['spelling'] ) demo_word.definition = word['definition'] db.session.add(demo_word) db.session.commit() for synonym in word['synonyms']: demo_synonym = WordSynonyms.query.\ filter_by(word_id=demo_word.id).\ filter_by(synonym=synonym).first() if not demo_synonym: demo_synonym = WordSynonyms(word_id=demo_word.id, synonym=synonym) db.session.add(demo_synonym) db.session.commit()
def add_word(): form = Add_Word() if form.validate_on_submit(): add_words = Dictionary(english_word=form.english_word.data.lower(), german_article=form.german_article.data.lower(), german_word=form.german_word.data.lower(), user_id=current_user.id) db.session.add(add_words) db.session.commit() return redirect(url_for('main.home')) else: flash('Error: enter a valid english and german word', 'warning') return render_template('add_word.html', title='New Word', form=form)
def edit_profile(): form = EditProfileForm(current_user.username) form.languages.choices = [(language.id, language.language) for language in Language.query.all()] if form.validate_on_submit(): if form.languages.data in current_user.languages: flash(_('The chosen language is already chosen')) else: current_user.username = form.username.data dictionary = Dictionary(user_id=current_user.id, language_id=form.languages.data) db.session.add(dictionary) db.session.commit() flash(_('Your changes have been saved!')) return redirect(url_for('edit_profile')) return render_template('edit_profile.html', title='Edit Profile', form=form)
def add_dictionary(): """ Add new dictionary into db """ db_user = User.check_request(request) if not db_user or not db_user.is_authenticated(): return {'message': 'Demo mode'} request_data = request.get_json() dictionary_name = request_data.get('dictionary_name').strip() dictionary_description = request_data.get('dictionary_description').strip() dictionary_entry = Dictionary( dictionary_name=dictionary_name, description=dictionary_description, user_id=db_user.id) db.session.add(dictionary_entry) db.session.commit() logger.info(f'Dictionary {dictionary_entry.dictionary_name} saved') return {'result': 'Dictionary added successfully'}
def add_dictionary(): data = request.form.to_dict() code = data.get('code') dicts = Dictionary.query.filter(Dictionary.code == code).first() if dicts == None: d = Dictionary(data.get('name')) d.code = data.get('code') d.order = data.get('oder') d.catalog_id = data.get('catalog_id') d.created_time = datetime.now() db.session.add(d) db.session.commit() else: return jsonify({'msg': 'dictionary code exist !'}) action_log(request, '添加字典') return jsonify({'msg': 'ok !'})
def setUp(self): """ Create infile sqllite db. Fill it with moth data """ self.app = create_app(TestConfig) self.app_context = self.app.app_context() self.app_context.push() self.app.testing = True self.app_client = self.app.test_client() self.words_number = 100 db.create_all() # Filling db with mock data user = User(username='******', id=1) user.set_password('Test') db.session.add(user) db.session.commit() user_dictionary = Dictionary(dictionary_name='dictionary', user_id=user.id) db.session.add(user_dictionary) db.session.commit() for i in range(self.words_number): word = Word(spelling=f'spelling{i}', definition=f'definition{i}', dictionary_id=user_dictionary.id) definition = Definitions(spelling=f'spelling{i}', definition=f'def{i}') synonym = Synonyms(spelling=f'spelling{i}', synonym=f'syn{i}') db.session.add(word) db.session.add(definition) db.session.add(synonym) db.session.commit() # Add synonyms to current word synonym = WordSynonyms(word_id=word.id, synonym=f'synonym{i}1') db.session.add(synonym) synonym = WordSynonyms(word_id=word.id, synonym=f'synonym{i}2') db.session.add(synonym) db.session.commit()
def test_unique_words_in_dict(self): u1 = User(username='******', email='*****@*****.**') d1 = Dictionary(name='john dict', user_id=u1.id) w1 = Word(name="cool") db.session.add_all([u1, d1, w1]) db.session.commit() uw1 = UserWord(word_id=w1.id, user_id=u1.id, dictionary_id=d1.id) db.session.add(uw1) db.session.commit() # trying to add the same word twice db.session.add(uw1) try: db.session.commit() except IntegrityError: db.session.rollback() words = db.session.query(UserWord).filter(UserWord.word_id == w1.id, UserWord.user_id == u1.id, UserWord.dictionary_id == d1.id) self.assertTrue(words.count() == 1)
def dictionaries(): if request.method == 'POST': dictionary_form = EditDictionaryForm(request.form['dictionary_name'], '') if dictionary_form.validate_on_submit(): dictionary_entry = Dictionary( dictionary_name=dictionary_form.dictionary_name.data.strip(), description=dictionary_form.description.data.strip(), user_id=current_user.id) db.session.add(dictionary_entry) db.session.commit() # TODO add logging logger.info(f'Dictionary {dictionary_entry.dictionary_name} saved') return redirect( url_for('main.edit_dictionary', dictionary_id=dictionary_entry.id)) dictionary_form = EditDictionaryForm('', '') dictionaries = Dictionary.query.filter_by( user_id=current_user.id).order_by('dictionary_name') return render_template('main/dictionaries.html', title='Dictionaries', form=dictionary_form, dictionaries=dictionaries)
def index(): identity = get_jwt_identity() user = User.query.filter_by(username=identity['username']).first() g.current_user = user dictionaries = g.current_user.dictionaries.all() return jsonify(Dictionary.as_json_collection(dictionaries))