def definition_add(request): definition = Definition() definition.text = str(request.POST['definition']) env.add_definition(definition.text) definition.save() return HttpResponseRedirect(reverse(index))
def get_teams(self): groups_container = self.teams_soup.find("div", {"class": "groups-container"}) table = groups_container.find("table") rows = table.find_all("tr") for row in rows: if not row.has_attr("class"): continue anchor = row.find("a") dictTeam = {} dictTeam["id"] = int(re.search(self.regex_teamid, anchor["href"]).group(1)) dictTeam["name"] = anchor.text if dictTeam["id"] in self.teams.keys(): self.teams[dictTeam["id"]].set(**dictTeam) else: team = Team(**dictTeam) self.teams[dictTeam["id"]] = team self.session.add(team) if dictTeam["id"] in self.definitions.keys(): self.definitions[dictTeam["id"]].set(TEAM_ID=dictTeam["id"], DEF_HLTV=dictTeam["name"]) else: definiton = Definition(DEF_TYPE="team", TEAM_ID=dictTeam["id"], DEF_HLTV=dictTeam["name"], DEF_SHEET=dictTeam["name"]) self.definitions[dictTeam["id"]] = definiton self.session.add(definiton)
def add_definition(title): body = request.get_json() try: word = db.session.query(Word).filter_by(title=title).one() except NoResultFound as ex: return jsonify({'error': 'Word does not exist'}) posted_user_id = body.get('userId') if not posted_user_id: return jsonify({'error': 'User ID not supplied'}) try: user = db.session.query(User).filter_by(twitter_id=posted_user_id).one() except NoResultFound as ex: return jsonify({'error': 'Auth Error'}) posted_auth_token = body.get('authToken') token = encode_value( user.oauth_token, app.config.get('AUTH_SALT') ) if token != posted_auth_token: return jsonify({'error': 'Auth Error'}) try: definition = db.session.query(Definition)\ .filter_by(user_id=user.id).filter_by(word_id=word.id).one() return jsonify({'error': 'Definition already exists'}) except NoResultFound as ex: pass posted_definition = body.get('definition') if not posted_definition: return jsonify({'error': 'Definition not supplied'}) definition = Definition() definition.word_id = word.id definition.user_id = user.id definition.definition = posted_definition db.session.add(definition) db.session.commit() return jsonify({'definition': { 'id': definition.id}})
def set_definition_and_get_response(slash_command, command_params, user_name): ''' Set the definition for the passed parameters and return the approriate responses ''' set_components = command_params.split('=', 1) set_term = set_components[0].strip() set_value = set_components[1].strip() if len(set_components) > 1 else u'' # reject poorly formed set commands if u'=' not in command_params or not set_term or not set_value: return u'Sorry, but *{bot_name}* didn\'t understand your command. You can set definitions like this: *{command} EW = Eligibility Worker*'.format( bot_name=BOT_NAME, command=slash_command), 200 # reject attempts to set reserved terms if set_term.lower() in STATS_CMDS + RECENT_CMDS + HELP_CMDS: return u'Sorry, but *{}* can\'t set a definition for *{}* because it\'s a reserved term.'.format( BOT_NAME, set_term) # check the database to see if the term's already defined entry = query_definition(set_term) if entry: if set_term != entry.term or set_value != entry.definition: # update the definition in the database last_term = entry.term last_value = entry.definition entry.term = set_term entry.definition = set_value entry.user_name = user_name entry.creation_date = datetime.utcnow() try: db.session.add(entry) db.session.commit() except Exception as e: return u'Sorry, but *{}* was unable to update that definition: {}, {}'.format( BOT_NAME, e.message, e.args), 200 return u'*{}* has set the definition for *{}* to *{}*, overwriting the previous entry, which was *{}* defined as *{}*'.format( BOT_NAME, set_term, set_value, last_term, last_value), 200 else: return u'*{}* already knows that the definition for *{}* is *{}*'.format( BOT_NAME, set_term, set_value), 200 # save the definition in the database entry = Definition(term=set_term, definition=set_value, user_name=user_name) try: db.session.add(entry) db.session.commit() except Exception as e: return u'Sorry, but *{}* was unable to save that definition: {}, {}'.format( BOT_NAME, e.message, e.args), 200 return u'*{}* has set the definition for *{}* to *{}*'.format( BOT_NAME, set_term, set_value), 200
def new_definitions(): pl = json.loads(request.data) word = session.query(Word).filter_by(base_form=pl['base_form'], language=pl['language']).first() definitions = pl['definitions'] for d in definitions: definition = session.query(Definition).filter_by(shape=d['shape'], definition=d['definition']).first() if not definition: definition = Definition(d['shape'], d['definition'], word) session.add(definition) session.commit() output = {'Message': 'Definitions saved in database'} return json.dumps(output)
def add_word(form): session = DBSession() session.add( Definition(word=form['word'], definition=form['definition'], category_name=form['category'], created_by=get_username())) session.commit() flash('%s added!' % form['word'], 'success') word = session.query(Definition).filter( Definition.word == form['word']).one() return word
def create_new_definition(word, defns): """Creates a new word and definition in the database. :param word: (str) word being defined :param defns: (tuple) long definition, short definition """ longdef, shortdef = defns definition = Definition(word=word, long_definition=longdef, short_definition=shortdef) with session_scope() as session: session.add(definition)
DBSession = sessionmaker(bind=engine) session = DBSession() user1 = User(username='******') session.add(user1) session.commit() category1 = Category(name='slang') session.add(category1) session.commit() word1 = Definition(word='LOL', definition='Laugh out loud', created_by=user1.username, category_name=category1.name ) session.add(word1) session.commit() category2 = Category(name='udacious things') session.add(category2) session.commit() word2 = Definition(word='NDOP', definition='Nanodegree overview page', created_by=user1.username, category_name=category2.name ) session.add(word2) session.commit()