def setUpClass(cls): model.connect_string = 'sqlite:///:memory:' model.echo = True model.init() model.Task('task1', 'category1') model.Task('task2', 'category2') model.commit()
def oauth_authorized(resp): """Called after authorization. After this function finished handling, the OAuth information is removed from the session again. When this happened, the tokengetter from above is used to retrieve the oauth token and secret. Because the remote application could have re-authorized the application it is necessary to update the values in the database. If the application redirected back after denying, the response passed to the function will be `None`. Otherwise a dictionary with the values the application submitted. Note that Twitter itself does not really redirect back unless the user clicks on the application name. """ next_url = request.args.get('next') or url_for('index') if resp is None: #TODO: show friendly message #flash(u'You denied the request to sign in.') return redirect(next_url) screen_name = resp['screen_name'] oauth_token = resp['oauth_token'] oauth_token_secret = resp['oauth_token_secret'] user = User.select_by_screen_name(screen_name) if not user: app.logger.debug('User not found on database. Using the Twitter API') auth = tweepy.OAuthHandler(config.CONSUMER_KEY, config.CONSUMER_SECRET) auth.set_access_token(oauth_token, oauth_token_secret) api = tweepy.API(auth) twitter_user = api.get_user(screen_name=screen_name) user = User() user.id = twitter_user.id user.screen_name = twitter_user.screen_name user.blocked = 'N' user.name = twitter_user.name user.description = twitter_user.description user.created_at = twitter_user.created_at user.friends_count = twitter_user.friends_count user.followers_count = twitter_user.followers_count user.statuses_count = twitter_user.statuses_count user.profile_image_url = twitter_user.profile_image_url user.lang = twitter_user.lang user.location = twitter_user.location user.oauth_token = oauth_token user.oauth_token_secret = oauth_token_secret User.add(user) else: user.oauth_token = oauth_token user.oauth_token_secret = oauth_token_secret flask.session['screen_name'] = resp['screen_name'] flask.session['oauth_token'] = resp['oauth_token'] flask.session['oauth_token_secret'] = resp['oauth_token_secret'] commit() return flask.redirect(next_url)
def display_main_menu(err='', table=None): tables = list(model.TABLES.keys()) menu = SelectionMenu(tables + ['Make commit'], subtitle=err, title="Select a table to work with:") menu.show() index = menu.selected_option if index < len(tables): table = tables[index] display_secondary_menu(table) elif index == len(tables): model.commit() display_main_menu('Commit was made successful')
def show_start_menu(tname=None, err=''): tables = list(model.TABLES.keys()) menu = SelectionMenu(tables + ['commit'], subtitle=err, title="Select a table to work with:") menu.show() index = menu.selected_option if index < len(tables): tname = tables[index] show_table_menu(tname) elif index == len(tables): print('Trying to commit...') model.commit() show_start_menu(err='All chages were commited') else: print('Bye! Have a nice day!')
def commit(title, body): """ Write form content into database and jump to index page to avoid that a reload of the page creates a duplicate entry. """ log.debug("Wiki entry %r %r", title, body) # Using Elixir try: entry = model.Wiki.query.filter_by(title=title).order_by(desc(model.Wiki.created)).one() except: entry = model.Wiki() entry.title = title entry.body = body model.commit() # Redirect to index redirect(".")
def commit(message): """ Write form content into database and jump to index page to avoid that a reload of the page creates a duplicate entry. """ if GAE: # Google App Engine model.GuestBook(message=message).put() else: # Using Elixir model.GuestBook(name, message) model.commit() # Redirect to index redirect(".")
def commit(title, body): """ Write form content into database and jump to index page to avoid that a reload of the page creates a duplicate entry. """ log.debug("Wiki entry %r %r", title, body) # Using Elixir try: entry = model.Wiki.query.filter_by(title=title).order_by( desc(model.Wiki.created)).one() except: entry = model.Wiki() entry.title = title entry.body = body model.commit() # Redirect to index redirect(".")
import model class Job(model.Model): name = model.Field(model.String, primary_key=True) description = model.Field(model.String) def __repr__(self): return "<Job('%s','%s')>" % (self.name, self.description) class Style(model.Model): name = model.Field(model.String, primary_key=True) description = model.Field(model.String) def __repr__(self): return "<Job('%s','%s')>" % (self.name, self.description) Job(name="Testing", description="Just another story") Job(name="Toasting", description="Just another toast") model.commit() Style(name="Testing", description="Just another story") Style(name="Toasting", description="Just another toast") model.commit() print dir(Style)
def testTaskIsSaved(self): model.Task('task1', True) model.commit() retTask = model.Task.query.first() self.assertEqual('task1', retTask.name)
def testGetID(self): task = model.Task('task1') model.commit() self.assertEqual(1, task.id)
def do_adminEdit(): security.is_logged_on() username = request.forms.get('username') currentUserName = model.get_username(security.current_user())[1] reset = request.forms.get('reset') if username: userid = model.get_role(username)[0] #change user's name # check current user's privillege if model.get_role(currentUserName)[1] == 4: #reset database if reset: if reset == 'Y': model.reset_table() return fEngine.load_and_render("valid", reason="changes committed!") else: return fEngine.load_and_render("invalid", reason="invalid") usernameNew = request.forms.get('usernameNew') passwordNew = request.forms.get('passwordNew') roleNew = request.forms.get('role') if usernameNew: if model.username_exists(usernameNew): return fEngine.load_and_render("invalid", reason="invalid name") else: model.sql('''UPDATE USER SET username = ? WHERE id = ? ''', usernameNew, userid ) model.commit() #change password if passwordNew: userName1 = '' if usernameNew: valid_pwd, reason = security.secure_password(passwordNew, usernameNew) userName1 = usernameNew else: valid_pwd, reason = security.secure_password(passwordNew, username) userName1 = username if valid_pwd: salt = model.get_salt(userName1)[1] hashPass= security.password_hash(passwordNew,salt) model.sql('''UPDATE USER SET password = ? WHERE id = ? ''', hashPass, userid ) model.commit() else: return fEngine.load_and_render("invalid", reason="invalid") #change the role if roleNew: model.sql('''UPDATE USER SET role = ? WHERE id = ? ''', roleNew, userid ) model.commit() return fEngine.load_and_render("valid", reason="changes committed!") else: return fEngine.load_and_render("invalid", reason="you are not the admin")
def do_edituser(): security.is_logged_on() newUsername = request.forms.get('username') password = request.forms.get('password') password2 = request.forms.get('password2') role = request.forms.get('role') curpassword = request.forms.get('curpassword') # Check if required current password is provided if not curpassword: return fEngine.load_and_render("invalid", reason = "Please input your current password") # Retrieve username of current user username = model.get_username(security.current_user())[1] # use salt and password to get hashed password hashed = security.password_hash(curpassword, model.get_salt(username)[1]) # check database to see if user has input a valid password valid = model.check_password(username,hashed) # Check current password is matches if valid: password_filled = False # If password field is filled if password: # If password matches the confirmation password if password == pdoassword2: password_filled = True valid_pwd, reason = security.secure_password(password,username) if not valid_pwd: return fEngine.load_and_render("invalid", reason=reason) else: return fEngine.load_and_render("invalid", reason="New passwords do not match") # Hashing and storing new pass # If username field is filled if newUsername: #check if username already exists if model.username_exists(newUsername): return fEngine.load_and_render("invalid", reason="Username is already taken") else: # Update username model.sql('''UPDATE USER SET username = ? WHERE id = ? ''', newUsername, security.current_user() ) model.commit() if password_filled: # Check if new password is valid valid_pwd, reason = security.secure_password(password,username) if valid_pwd: # Salt and hash password salt = model.get_salt(username)[1] hashPass = security.password_hash(password, salt) # Update password model.sql('''UPDATE USER SET password = ? WHERE id = ? ''', hashPass, security.current_user() ) model.commit() # Updates role if one has been selected if role != "None": model.sql('''UPDATE USER SET role = ? WHERE id = ? ''', role, security.current_user() ) model.commit() return fEngine.load_and_render("valid",reason="Info updated!") else: return fEngine.load_and_render("invalid", reason="Current password does not match")
def when_i_commit_the_session(step): model.commit()