def process_item(self, item, spider): session = self.Session() phrase = Phrase(**item) try: session.add(phrase) session.commit() except: session.rollback() raise finally: session.close() return item
def add_phrase_to_db(phrase, translation): """Takes in phrase and its translation and adds it to the DB""" #Both user_id and article_id are stored in the flask session. user_id = session.get('user_id') article_id = session.get('article_id') #Instantiated an instance of the class Phrase. phrase_pair = Phrase(user_id=user_id, article_id=article_id, phrase=phrase, translation=translation) #Add phrase_pair instance to the database and commits it. . db.session.add(phrase_pair) db.session.commit()
def post(self): """Create a new phrase""" if not users.is_current_user_admin(): self.error(403) else: text = self.request.get('text') phrase = Phrase(text=text) phrase.put() for lang in Language.all(): translation = Translation(phrase=phrase, text='') translation.save() lang.translations.append(translation.key()) lang.save()
def create_phrase_and_score(phrase_date, phrase_city, phrase_state_abbr, phrase_state, phrase_region, job_at_phrase, age_at_phrase, phrase_text, user_id, US_or_no=True): """Create and return a new Phrase, which includes a '0' or '1' score.""" # open the csv in the seed_database.py, not here # pass in a phrase that is string as argument # phrase_date is a string of form %Y-%m-%d – ex. '2021-02-18' polar_score = swn_polarity(phrase_text) # test that the above works; it does # print(f'Score is {polar_score}.') #make sure phrase has no trailing white space phrase_text = phrase_text.rstrip() # variable name from model.py in class Phrase = variable name used in this funct new_phrase_and_score = Phrase(phrase_date=phrase_date, US_or_no=True, phrase_city=phrase_city, phrase_state_abbr=phrase_state_abbr, phrase_state=phrase_state, phrase_region=phrase_region, job_at_phrase=job_at_phrase, age_at_phrase=age_at_phrase, phrase_text=phrase_text, polar_score=polar_score, user_id=user_id) db.session.add(new_phrase_and_score) db.session.commit() return new_phrase_and_score