def do_login(): if request.method == 'POST': user = str(request.form['username']) password = str(request.form['password']) #app.logger.info('Check password:\t'+str(check_passwd(user, password))) # debug if check_passwd(user, password): # Set Cookies for knowing about user on other pages auth_hash = str(id_generator()) user_id = int(get_user_id_from_db(user)) app.logger.info('Set cookies '+str(user)+' '+str(user_id)+' '+auth_hash) # debug response = app.make_response(redirect(url_for('task'))) response.set_cookie('id', value=str(user_id)) response.set_cookie('hash', value=auth_hash) response.set_cookie('logged_at', value=str(datetime.now())) db.session.query(models.User).filter_by(id=user_id).update({'cookie':auth_hash}) db.session.commit() #sql = "UPDATE user SET cookie='{}' WHERE id='{}'".format(auth_hash, user_id) #app.logger.info('SQL:\t'+str(sql)) # debug #db.session.execute(sql) return response # need for set cookies finaly else: return 'login wrong' # if request.method == GET return render_template('login.html', user=None)
def do_login(): if request.method == 'POST': try: user = str(request.form['username']) password = str(request.form['password']) except UnicodeEncodeError: return "Wrong login or password charset" # app.logger.info('Check password:\t'+str(check_passwd(user, password))) # debug if check_passwd(user, password): # Set Cookies for knowing about user on other pages auth_hash = str(id_generator()) user_id = int(get_user_id_from_db(user)) app.logger.debug('Set cookies ' + str(user) + ' ' + str(user_id) + ' ' + auth_hash) # debug response = app.make_response(redirect(url_for('index'))) response.set_cookie('id', value=str(user_id)) response.set_cookie('hash', value=auth_hash) response.set_cookie('logged_at', value=str(datetime.now())) db.session.query(User).filter_by(id=user_id).update( {'cookie': auth_hash}) db.session.commit() # sql = "UPDATE user SET cookie='{}' WHERE id='{}'".format(auth_hash, user_id) # app.logger.info('SQL:\t'+str(sql)) # debug # db.session.execute(sql) return response # need for set cookies finaly else: return 'login wrong' # if request.method == GET return render_template('login.html', user=None)
def logout(): response = app.make_response(redirect(url_for('index'))) response.set_cookie('id', value=' ', expires=1) response.set_cookie('pass', value=' ', expires=1) return response
def project(action='list'): try: user_id = get_user_id() app.logger.info(' ### Project | logined user ID:\t'+str(user_id)) # debug if user_id == None: return redirect(url_for('do_login')) # if not logined go to login except: app.logger.error('Not logined') # debug return redirect(url_for('do_login')) # if not logined go to login ### Show Project List ### if action == 'list' or action == 'list_closed': if action == 'list_closed': project_status = False project_ids = get_projects_for_user(user_id, 'Disabled') else: project_status = True project_ids = get_projects_for_user(user_id, 'Active') # app.logger.debug('project_ids: '+str(project_ids)) # debug if project_ids: projects = [] project_users = [] for project_id in project_ids: project_name = db.session.query(Project.name).filter_by(id=project_id[0]).all()[0] projects.append([project_id[0], project_name[0]]) project_user_ids = db.session.query(Project_association.user_id).filter_by(project_id=project_id[0]).all() project_user_names = [] for user_id in project_user_ids: name = db.session.query(User.nickname).filter_by(id=user_id[0]).all()[0] project_user_names.append(name[0]) project_users.append(project_user_names) # app.logger.debug('project_users is: '+str(project_user_names)) # debug return render_template('project/list.html', title=u'Проекты', user=get_nick(), project_list=projects, project_status=project_status, project_users=project_users) ### Create new Project ### elif action == 'create': user_id = get_user_id() if request.method == 'POST': # Need: # User ID (upper) # Project Name # Project ID project_name = request.form.get('projectname') db.session.add(Project(name=project_name)) db.session.commit() project_id = db.session.query(Project.id).filter_by(name=project_name) db.session.add(Project_association(user_id=user_id, project_id=project_id[0][0])) db.session.commit() return redirect(url_for('project')) # got to project list else: # if GET request return render_template('project/create.html', title=u'Проекты', user=get_nick(), user_id=user_id) ### View Project ### elif action == 'view': project_id = str(request.args.get('id')) app.logger.debug('### Setting cookie ###\nProject ID for view: '+str(project_id)) # debug response = app.make_response(redirect(url_for('task', action='list', project_id=project_id))) response.set_cookie('project_id', value=project_id) return response # go to task list with cookie 'project_id' set ### Edit Project ### elif action == 'edit': project_id = request.args.get('id') if request.method == 'POST': project_name = request.form.get('projectname') need_add_user = request.form.getlist('addusertoggle') if need_add_user: user_to_add = request.form.get('adduser') try: userid_to_add = db.session.query(User.id).filter_by(nickname=user_to_add).first()[0] db.session.add(Project_association(user_id=userid_to_add, project_id=project_id)) db.session.commit() app.logger.info('User existed') app.logger.info('Checkbox is: Checked\n'+str(user_to_add)+'\n'+str(userid_to_add)) except: app.logger.info('User doesn\'t exist') return "Error: User doesn't exist" else: app.logger.info('Checkbox is: Unchecked\n Not need to add user\n'+str(need_add_user)) db.session.query(Project).filter_by(id=project_id).update({ 'name':project_name, 'status':'Active', 'owner': user_id}) db.session.commit() return redirect(url_for('project', action='list', project_id=project_id)) else: # if GET request # Need: # Project ID # Project Name # Project Users project_name = db.session.query(Project.name).filter_by(id=project_id).all()[0] project_user_ids = db.session.query(Project_association.user_id).filter_by(project_id=project_id).values('user_id') project_user_names = [] user_ids = [] for user_id in project_user_ids: name = db.session.query(User.nickname).filter_by(id=user_id[0]).all()[0] project_user_names.append(name[0]) user_ids.append(user_id[0]) project_user_ids = user_ids # app.logger.debug('### Project # Edit ### id from form: '+str(project_id[0])+'\n'+ \ # 'Name: '+str(project_name[0])+'\n'+ \ # 'User IDs: '+str(project_user_ids)+'\n'+ \ # 'Users in project: '+str(project_user_names) ) # debug project_full_data = [project_id, project_name[0], project_user_ids, project_user_names] response = app.make_response(render_template('project/edit.html', title=u'Проекты', user=get_nick(), project=project_full_data)) response.set_cookie('project_id', value=project_id) return response # go to project editor with cookie 'project_id' set ### Remove user from Project ### elif action == 'rmuser': project_id = str(request.cookies.get('project_id')) userid_to_del = str(request.args.get('id')) cur = db.session.query(Project_association).filter_by(user_id=userid_to_del).filter_by(project_id=project_id).delete() db.session.commit() app.logger.debug("### Trying to delete user: "******"\nFrom project: "+project_id+"\n"+str(cur)) return redirect(url_for('project', action='edit', id=project_id))
def project(action='list'): try: user_id = get_user_id() app.logger.info(' ### Project | logined user ID:\t' + str(user_id)) # debug if user_id is None: return redirect(url_for('do_login')) # if not logined go to login except: app.logger.error('Not logined') # debug return redirect(url_for('do_login')) # if not logined go to login ### Show Project List ### if action == 'list' or action == 'list_closed': if action == 'list_closed': project_status = False project_ids = get_projects_for_user(user_id, 'Disabled') else: project_status = True project_ids = get_projects_for_user(user_id, 'Active') # app.logger.debug('project_ids: '+str(project_ids)) # debug projects = [] project_users = [] if project_ids: for project_id in project_ids: project_name = db.session.query( Project.name).filter_by(id=project_id[0]).all()[0] projects.append([project_id[0], project_name[0]]) project_user_ids = db.session.query( ProjectAssociation.user_id).filter_by( project_id=project_id[0]).all() project_user_names = [] for user_id in project_user_ids: name = db.session.query( User.nickname).filter_by(id=user_id[0]).one_or_none() if name: project_user_names.append(name[0]) project_users.append(project_user_names) # app.logger.debug('project_users is: '+str(project_user_names)) # debug return render_template('project/list.html', title=u'Проекты', user=get_nick(), project_list=projects, project_status=project_status, project_users=project_users) ### Create new Project ### elif action == 'create': user_id = get_user_id() if request.method == 'POST': # Need: # User ID (upper) # Project Name # Project ID project_name = request.form.get('projectname') db.session.add( Project(name=project_name, status='Active', owner=user_id)) db.session.commit() project_id = db.session.query( Project.id).filter_by(name=project_name) db.session.add( ProjectAssociation(user_id=user_id, project_id=project_id[0][0])) db.session.commit() return redirect(url_for('project')) # got to project list else: # if GET request return render_template('project/create.html', title=u'Проекты', user=get_nick(), user_id=user_id) ### View Project ### elif action == 'view': project_id = str(request.args.get('id')) app.logger.debug('### Setting cookie ###\nProject ID for view: ' + str(project_id)) # debug response = app.make_response( redirect(url_for('task', action='list', project_id=project_id))) response.set_cookie('project_id', value=project_id) return response # go to task list with cookie 'project_id' set ### Edit Project ### elif action == 'edit': project_id = request.args.get('id') if request.method == 'POST': project_name = request.form.get('projectname') need_add_user = request.form.getlist('addusertoggle') if need_add_user: user_to_add = request.form.get('adduser') try: userid_to_add = db.session.query( User.id).filter_by(nickname=user_to_add).first()[0] db.session.add( ProjectAssociation(user_id=userid_to_add, project_id=project_id)) db.session.commit() app.logger.info('User existed') app.logger.info('Checkbox is: Checked\n' + str(user_to_add) + '\n' + str(userid_to_add)) except: app.logger.info('User doesn\'t exist') return "Error: User doesn't exist" else: app.logger.info( 'Checkbox is: Unchecked\n Not need to add user\n' + str(need_add_user)) db.session.query(Project).filter_by(id=project_id).update({ 'name': project_name, 'status': 'Active', 'owner': user_id }) db.session.commit() return redirect( url_for('project', action='list', project_id=project_id)) else: # if GET request # Need: # Project ID # Project Name # Project Users project_name = db.session.query( Project.name).filter_by(id=project_id).all()[0] project_user_ids = db.session.query( ProjectAssociation.user_id).filter_by( project_id=project_id).values('user_id') project_user_names = [] user_ids = [] for user_id in project_user_ids: name = db.session.query( User.nickname).filter_by(id=user_id[0]).one_or_none() if name: project_user_names.append(name[0]) user_ids.append(user_id[0]) project_user_ids = user_ids # app.logger.debug('### Project # Edit ### id from form: '+str(project_id[0])+'\n'+ \ # 'Name: '+str(project_name[0])+'\n'+ \ # 'User IDs: '+str(project_user_ids)+'\n'+ \ # 'Users in project: '+str(project_user_names) ) # debug project_full_data = [ project_id, project_name[0], project_user_ids, project_user_names ] response = app.make_response( render_template('project/edit.html', title=u'Проекты', user=get_nick(), project=project_full_data)) response.set_cookie('project_id', value=project_id) return response # go to project editor with cookie 'project_id' set ### Remove user from Project ### elif action == 'rmuser': project_id = str(request.cookies.get('project_id')) userid_to_del = str(request.args.get('id')) cur = db.session.query(ProjectAssociation).filter_by( user_id=userid_to_del).filter_by(project_id=project_id).delete() db.session.commit() app.logger.debug("### Trying to delete user: "******"\nFrom project: " + project_id + "\n" + str(cur)) return redirect(url_for('project', action='edit', id=project_id))