def signup(): """ User Sign-Up GET requests serve sign-up page POST requests validate form & user registration """ form = SignupForm() if form.validate_on_submit(): # prepare new user data data = {} data["name"] = form.name.data data["email"] = form.email.data data["password"] = form.password.data # create new user # new_user = User( # name = form.name.data, # email = form.email.data, # password = form.password.data #) user = User.create_from_json(data) login_user(user) # Log in as newly created user # remember login date and time user.last_login = "******" flash(u'Congratulation! You were successfully registered!', 'success') return redirect(url_for('views.index')) return render_template('auth/signup.html', title='New user registration', form=form)
def get(self, id): # check if requested user exists user = User.get_by_id(id=id) if not user: return {"message": "User was not found."}, 404 # return serialized user details return {"user": UserSchema().dump(user)}
def user(app): user = User(name="Test user", email='*****@*****.**', password='******') db.session.add(user) db.session.commit() yield user db.session.delete(user) db.session.commit()
def post(self): # extract new user data from request json_data = request.get_json() if not json_data: return {"message": "New user details were not provided."}, 400 # Validate and deserialize input try: data = UserSchema().load(json_data) except ValidationError as err: return err.messages, 422 # create new user result = User.create_from_json(json_data) if isinstance(result, User): return {"message": "New user was created.", "user": UserSchema().dump(result)}, 201 return result
def put(self, id): # extract user data from request json_data = request.get_json() if not json_data: return {"message": "User was not changes."}, 400 # Validate and deserialize input try: data = UserSchema().load(json_data, partial=True) except ValidationError as err: return err.messages, 422 # update user update_result = User.update_from_json(data, id) if isinstance(update_result, User): return {"message": "User details were updated.", "user": UserSchema().dump(update_result)}, 200 return update_result
def app(): """ Create app instance for tests,initiate, create and clean database instance. """ app = create_app("test") #app.config["TESTING"] = True #app.testing = True # This creates an in-memory sqlite db #app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite://" #db_fd, db_path = tempfile.mkstemp() #app.config.update({"SQLALCHEMY_DATABASE_URI" : db_path}) #client = app.test_client() with app.app_context(): # create test database db.create_all() # filters and variables for jinja2 templates @app.template_global() def site_name(): return app.config['SITE_NAME'] # initial setup from photoric.config.initial_setup import initial_setup app.before_first_request(initial_setup) # test user without admin rights for authenticated requests test_user = User(name="test_user", password="******") db.session.add(test_user) db.session.commit() yield app db.drop_all()
def signin(): """ User Sign-In GET requests serve sign-in page POST requests validate form & log user in """ # redirect user to home page if it is already logged in if current_user.is_authenticated: return redirect(url_for('views.index')) # check provided credentials and log user in login_form = LoginForm() if login_form.validate_on_submit(): #user = get_user_by_name(name=login_form.name.data) user = User.get_by_name(name=login_form.name.data) if user is None or not user.check_password(login_form.password.data): flash(u'Invalid username or password', 'danger') return redirect(url_for('auth.signin')) login_user(user, remember=login_form.remember_me.data) # remember login date and time user.last_login = "******" # return logged in user to the requested page or home page if not return_page = request.args.get('next') flash(u'You were successfully logged in as ' + current_user.name, 'success') if not is_safe_url(return_page): return redirect(url_for('views.index')) return redirect(return_page or url_for('views.index')) # load log in dialog if GET method return render_template('auth/signin.html', title='Sign In', form=login_form)
def initial_setup(): """ create admin user if not exist """ if not User.get_by_name('admin'): # create user and map it to respective groups and role admin_user = User(name='admin', password='******') # admin_user.password = '******' admin_user.roles = [{'name': 'admin', 'restrictions': {}}] admin_user.groups = [ # Group( { 'name': 'admins', 'allowances': '*' }, # ), # Group( { 'name': 'private', 'allowances': { 'albums': ['read'], 'images': ['read'] } }, #), #Group( { 'name': 'contributors', 'allowances': { 'albums': ['read', 'create', 'update', 'revoke'], 'images': ['read', 'create', 'update', 'revoke'] } } #) ] db.session.add(admin_user) db.session.commit() """ create base navigation elements """ if get_navbar_by_name('topbar') is None: # create top navbar topnavbar = Navbar(name='topbar', html_class='navbar navbar-expand pt-1 pb-1') # create top navbar items topnavbar.items = [ NavbarItem(name='site_logo', item_type='logo', item_target='views.index', icon_type='favicon', icon_src='favicon.ico'), NavbarItem(name='topmenu', item_type='menu'), NavbarItem(name='simple_search', item_type='form', item_target='search.simple_search') ] db.session.add(topnavbar) db.session.commit() if get_menu_by_name('topmenu') is None: # create top menu topmenu = Menu(name='topmenu', html_class='navbar-nav') # create topmenu items topmenu.items = [ MenuItem(name='home', desc='Go to home page', item_target='views.index', icon_type='svg', icon_src='house-door'), MenuItem(item_type='dropdown', name='account', desc='Account management', icon_type='svg', icon_src='person-square', children=[ MenuItem(name='profile', desc='Look/edit your profile', item_target='views.index', auth_req=True), MenuItem( name='sign up', desc='Sign Up to get access to all functions', item_target='auth.signup', anonym_only=True), MenuItem(name='sign in', desc='Sign in to your account', item_target='auth.signin', anonym_only=True), MenuItem(name='sign out', desc='Sign out from your account', item_target='auth.logout', auth_req=True) ]) ] db.session.add(topmenu) db.session.commit() if get_navbar_by_name('mainbar') is None: # create main navbar mainnavbar = Navbar( name='mainbar', html_class= 'navbar navbar-expand-sm pt-0 pb-0 bg-light navbar-light d-flex border border-left-0 border-right-0 border-success', ) # create main navbar items mainnavbar.items = [ NavbarItem(name='collapse_toggle_button', item_type='button', item_target='mainmenu'), NavbarItem( name='mainmenu', item_type='menu', ), NavbarItem(name='upload_button', item_type='button', item_target='#', icon_type='svg', icon_src='upload', auth_req=True, group_req='contributors'), NavbarItem(name='create_album_button', item_type='button', item_target='#create_album', icon_type='svg', icon_src='folder-plus', auth_req=True, group_req='contributors'), NavbarItem(name='settings_button', item_type='button', item_target='settings.manage_settings', icon_type='svg', icon_src='gear', auth_req=True, group_req='admins') ] db.session.add(mainnavbar) db.session.commit() if get_menu_by_name('mainmenu') is None: # create first part of main menu mainmenu = Menu( name='mainmenu', html_class= 'collapse navbar-collapse flex-grow-1 font-weight-bold') # create mainmenu items mainmenu.items = [ MenuItem(name='about', desc='Read about me and this web-site', item_target='views.about'), MenuItem(name='galleries', desc='Look through photo galleries', item_target='views.index'), MenuItem(name='contact', desc='Contact form', item_target='views.index', auth_req=True) ] db.session.add(mainmenu) db.session.commit() if get_navbar_by_name('actionbar') is None: # create action navbar actionnavbar = Navbar( name='actionbar', html_class= 'navbar navbar-expand-sm justify-content-center fixed-top', html_style='display:none;') # create action navbar items actionnavbar.items = [ NavbarItem(name='actionmenu', item_type='menu') ] db.session.add(actionnavbar) db.session.commit() if get_menu_by_name('actionmenu') is None: # create action menu actionmenu = Menu( name='actionmenu', html_class='justify-content-center', ) # create actionmenu items actionmenu.items = [ MenuItem(name='share', desc='Share selected albums/images', item_target='views.index', icon_type='svg', icon_src='share', auth_req=True), MenuItem( item_type='dropdown', name='album', desc='Manage album', icon_type='svg', icon_src='book', auth_req=True, group_req='contributors', children=[ MenuItem( name='add', desc= 'Add images / albums to a new or existing album', item_target='views.index', auth_req=True, group_req='contributors'), MenuItem( name='remove', desc='Remove images / albums from an album', item_target='views.index', auth_req=True, group_req='contributors'), MenuItem(name='set', desc='Set an image as an album icon', item_target='views.index', auth_req=True, group_req='contributors') ]), MenuItem(name='download', desc='Download images / albums', item_target='views.index', icon_type='svg', icon_src='download', auth_req=True, group_req='private'), MenuItem(name='delete', desc='Delete images / albums', item_target='views.index', icon_type='svg', icon_src='trash', auth_req=True, group_req='admins', role_req='admin') ] db.session.add(actionmenu) db.session.commit() if get_navbar_by_name('sidebar') is None: # create side navbar sidenavbar = Navbar( name='sidebar', html_class= 'navbar navbar-light d-none d-md-flex flex-column ml-4', html_style='max-width:25%;') # create side navbar items sidenavbar.items = [NavbarItem(name='sidemenu', item_type='menu')] db.session.add(sidenavbar) db.session.commit() if get_menu_by_name('sidemenu') is None: # create action menu sidemenu = Menu( name='sidemenu', html_class='navbar-nav align-self-start font-weight-bold', ) # create sidemenu items sidemenu.items = [ MenuItem(item_type='tree', name='albums', desc='Albums tree', item_target='albumsTree'), MenuItem(name='images', desc='Images not included in albums', item_target='views.index'), MenuItem(item_type='tree', name='shares', desc='Shared items tree', item_target='sharesTree') ] db.session.add(sidemenu) db.session.commit()
def delete(self, id): if not User.delete(id): return {"message": "User was not found."}, 404 return {"message": "User was deleted."}, 204