def create_variable(env): """ Page to create a variable. Handles both GET and POST. """ if not loggedin(env): tmpl = jinjaenv.get_template('loginform.html') return template_render(tmpl, { 'session' : env['beaker.session']} ) session = Session() user = session.query(User).filter(User.id == \ env['beaker.session']['loggedin_id']).first() if not user: return None if user.admin_level < 1: return str('Access denied') tmpl = jinjaenv.get_template('createvariable.html') if str(env['REQUEST_METHOD']) == 'POST': data = read_post_data(env) if data is None: return str('Error: Invalid POST data') if 'variable' in data: s = data['variable'] s = urllib.unquote_plus(s) else: return template_render(tmpl, { 'session' : env ['beaker.session'], 'error' : 'Error: Variable name not specified'}) if len(s) == 0 or len(s) > 60: return template_render(tmpl, { 'session' : env ['beaker.session'], 'error' : 'Error: Variable name has invalid length'}) # 'on' when checked; not in data when not clicked. XXX if 'is_var' in data: v = 1 else: v = 0 res = session.query(Variable).filter(Variable.name == s).first() if res: return template_render(tmpl, { 'session' : env ['beaker.session'], 'error' : 'Error: Variable already exists'}) variable = Variable(s, v) session.add(variable) try: session.commit() except sqlalchemy.exc.IntegrityError as e: session.rollback() print 'Rollback! create_variable' print 'Post data:', data print 'Exception:', e return template_render(tmpl, { 'session' : env ['beaker.session'], 'newvariable' : variable}) return template_render(tmpl, {'session' : env['beaker.session'] })
def manage_variable(env, variableid): """ Page to manage a variable. Handles both GET and POST. """ if not loggedin(env): tmpl = jinjaenv.get_template('loginform.html') return template_render(tmpl, { 'session' : env['beaker.session']} ) session = Session() user = session.query(User).filter(User.id == \ env['beaker.session']['loggedin_id']).first() if not user: return None if user.admin_level < 1: return str('Access denied') tmpl = jinjaenv.get_template('managevariable.html') variable = session.query(Variable).filter(Variable.id == \ variableid).first() if not variable: return None if str(env['REQUEST_METHOD']) == 'POST': data = read_post_data(env) if data is None: return str('Invalid POST data') if 'newname' not in data: return str('Invalid POST data') data['newname'] = urllib.unquote_plus(data['newname']) if len(data['newname']) == 0 or len(data['newname']) > 20: return template_render(tmpl, { 'session' : env ['beaker.session'], 'error' : 'Variable name too long.', }) res = session.query(Variable).filter(Variable.name == data['newname']).first() if res is None: variable.name = data['newname'] session.add(variable) try: session.commit() except sqlalchemy.exc.IntegrityError as e: session.rollback() print 'Rollback in manage_variable' print 'Post data:', data print 'Exception:', e else: return template_render(tmpl, { 'session' : env ['beaker.session'], 'error' : 'Name already exists in the system.', 'variable' : variable }) return template_render(tmpl, { 'session' : env['beaker.session'], 'variable' : variable })
def register_user(env): """ Page to register a user. Handles POST and GET data. """ tmpl = jinjaenv.get_template('registeruser.html') session = Session() if str(env['REQUEST_METHOD']) == 'POST': data = read_post_data(env) if data is None: return str('Error: Invalid post data') if 'user' not in data or 'pass' not in data: return template_render(tmpl, { 'session' : env['beaker.session'], 'registerfail' : True, 'error' : 'Post data not complete'} ) data['user'] = urllib.unquote_plus(data['user']) data['pass'] = urllib.unquote_plus(data['pass']) if 'mail' in data: data['mail'] = urllib.unquote_plus(data['mail']) if len(data['user']) > 20 or len(data['pass']) > 20 or \ len(data['user']) == 0 or len(data['pass']) == 0: return template_render(tmpl, { 'session' : env['beaker.session'], 'registerfail' : True, 'error' : 'Username or Password too long.'} ) data['pass'] = hashlib.sha256(data['pass']).hexdigest() if 'mail' in data: if len(data['mail']) > 40: return template_render(tmpl, { 'session' : env['beaker.session'], 'registerfail' : True, 'error' : 'Email address is too long'} ) log.log([], LVL_VERBOSE, PyLogger.INFO, 'Register POST data: %s' % str(data)) if 'mail' in data and data['mail']: if not emailre.match(data['mail']): return template_render(tmpl, { 'session' : env['beaker.session'], 'registerfail' : True, 'error': 'Invalid Email.'} ) # Does the user exist? res = session.query(User).filter(func.lower(User.name) == data['user'].lower()).first() if res: return template_render(tmpl, { 'session' : env['beaker.session'], 'registerfail' : True, 'error' : 'Username already exists'} ) user = User(data['user'], data['pass'], data['mail'] if 'mail' in data else None) session.add(user) try: session.commit() except sqlalchemy.exc.IntegrityError as e: session.rollback() print 'Rollback in register_user' print 'Post data:', data print 'Exception:', e return template_render(tmpl, { 'session' : env['beaker.session'], 'registersuccess' : True, 'user' : user} ) elif str(env['REQUEST_METHOD']) == 'GET': return template_render(tmpl, { 'session' : env['beaker.session']} ) else: return None
def create_script(env): """ Page to create a script. Handles both GET and POST. """ if not loggedin(env): tmpl = jinjaenv.get_template('loginform.html') return template_render(tmpl, { 'session' : env['beaker.session']} ) session = Session() user = session.query(User).filter(User.id == \ env['beaker.session']['loggedin_id']).first() if not user: return None tmpl = jinjaenv.get_template('createscript.html') if str(env['REQUEST_METHOD']) == 'POST': data = read_post_data(env) if data is None: return str('Error: Invalid POST data') if 'script' in data: s = data['script'] s = urllib.unquote_plus(s) else: return template_render(tmpl, { 'session' : env ['beaker.session'], 'error' : 'Error: Script contains invalid characters'}) if len(s) == 0 or len(s) > 20: return template_render(tmpl, { 'session' : env ['beaker.session'], 'error' : 'Error: Script name has invalid length'}) res = session.query(Script).filter(Script.name == s).all() if res: return template_render(tmpl, { 'session' : env ['beaker.session'], 'error' : 'Error: Script already exists'}) user = session.query(User).filter(User.id == \ env['beaker.session']['loggedin_id']).first() if not user: return template_render(tmpl, { 'session' : env ['beaker.session'], 'error' : 'Error: Invalid user in session?'}) script = Script(s) script.owner = user session.add(script) try: session.commit() except sqlalchemy.exc.IntegrityError as e: session.rollback() print 'Rollback! create_script.' print 'Post data:', data print 'Exception:', e return template_render(tmpl, { 'session' : env ['beaker.session'], 'newscript' : script }) return template_render(tmpl, { 'session' : env ['beaker.session'] })
from excel_parser import dayparser, excel from mptparser.time_parser import timeparse session = Session() sqlabspara = session.query(AbstractPara).all() week = [u'понедельник', u'вторник', u'среда', u'четверг', u'пятница', u'суббота'] now = datetime.date.today() if (int(datetime.datetime.strftime(now, '%W')) % 2) == 0: # числитель print 'Числитель' else: print 'Знаменатель' #print now.weekday() for abspara in sqlabspara: #Добавляем расписание на неделю в бд, откуда потем тащем в календарь for xday in xrange(0, 7): dateoffset = now + datetime.timedelta(days=xday) if dateoffset.weekday() == week.index(abspara.dayofweek): paradate = dateoffset start, end = timeparse(abspara.numberpara, paradate.weekday()) sqlpara = Para(abspara, paradate, start, end) #print sqlpara.abstractpara.dayofweek session.add(sqlpara) session.commit()
def create_variable(env): """ Page to create a variable. Handles both GET and POST. """ if not loggedin(env): tmpl = jinjaenv.get_template('loginform.html') return template_render(tmpl, {'session': env['beaker.session']}) session = Session() user = session.query(User).filter(User.id == \ env['beaker.session']['loggedin_id']).first() if not user: return None if user.admin_level < 1: return str('Access denied') tmpl = jinjaenv.get_template('createvariable.html') if str(env['REQUEST_METHOD']) == 'POST': data = read_post_data(env) if data is None: return str('Error: Invalid POST data') if 'variable' in data: s = data['variable'] s = urllib.unquote_plus(s) else: return template_render( tmpl, { 'session': env['beaker.session'], 'error': 'Error: Variable name not specified' }) if len(s) == 0 or len(s) > 60: return template_render( tmpl, { 'session': env['beaker.session'], 'error': 'Error: Variable name has invalid length' }) # 'on' when checked; not in data when not clicked. XXX if 'is_var' in data: v = 1 else: v = 0 res = session.query(Variable).filter(Variable.name == s).first() if res: return template_render( tmpl, { 'session': env['beaker.session'], 'error': 'Error: Variable already exists' }) variable = Variable(s, v) session.add(variable) try: session.commit() except sqlalchemy.exc.IntegrityError as e: session.rollback() print 'Rollback! create_variable' print 'Post data:', data print 'Exception:', e return template_render(tmpl, { 'session': env['beaker.session'], 'newvariable': variable }) return template_render(tmpl, {'session': env['beaker.session']})
def create_script(env): """ Page to create a script. Handles both GET and POST. """ if not loggedin(env): tmpl = jinjaenv.get_template('loginform.html') return template_render(tmpl, {'session': env['beaker.session']}) session = Session() user = session.query(User).filter(User.id == \ env['beaker.session']['loggedin_id']).first() if not user: return None tmpl = jinjaenv.get_template('createscript.html') if str(env['REQUEST_METHOD']) == 'POST': data = read_post_data(env) if data is None: return str('Error: Invalid POST data') if 'script' in data: s = data['script'] s = urllib.unquote_plus(s) else: return template_render( tmpl, { 'session': env['beaker.session'], 'error': 'Error: Script contains invalid characters' }) if len(s) == 0 or len(s) > 20: return template_render( tmpl, { 'session': env['beaker.session'], 'error': 'Error: Script name has invalid length' }) res = session.query(Script).filter(Script.name == s).all() if res: return template_render( tmpl, { 'session': env['beaker.session'], 'error': 'Error: Script already exists' }) user = session.query(User).filter(User.id == \ env['beaker.session']['loggedin_id']).first() if not user: return template_render( tmpl, { 'session': env['beaker.session'], 'error': 'Error: Invalid user in session?' }) script = Script(s) script.owner = user session.add(script) try: session.commit() except sqlalchemy.exc.IntegrityError as e: session.rollback() print 'Rollback! create_script.' print 'Post data:', data print 'Exception:', e return template_render(tmpl, { 'session': env['beaker.session'], 'newscript': script }) return template_render(tmpl, {'session': env['beaker.session']})
def register_user(env): """ Page to register a user. Handles POST and GET data. """ tmpl = jinjaenv.get_template('registeruser.html') session = Session() if str(env['REQUEST_METHOD']) == 'POST': data = read_post_data(env) if data is None: return str('Error: Invalid post data') if 'user' not in data or 'pass' not in data: return template_render( tmpl, { 'session': env['beaker.session'], 'registerfail': True, 'error': 'Post data not complete' }) data['user'] = urllib.unquote_plus(data['user']) data['pass'] = urllib.unquote_plus(data['pass']) if 'mail' in data: data['mail'] = urllib.unquote_plus(data['mail']) if len(data['user']) > 20 or len(data['pass']) > 20 or \ len(data['user']) == 0 or len(data['pass']) == 0: return template_render( tmpl, { 'session': env['beaker.session'], 'registerfail': True, 'error': 'Username or Password too long.' }) data['pass'] = hashlib.sha256(data['pass']).hexdigest() if 'mail' in data: if len(data['mail']) > 40: return template_render( tmpl, { 'session': env['beaker.session'], 'registerfail': True, 'error': 'Email address is too long' }) log.log([], LVL_VERBOSE, PyLogger.INFO, 'Register POST data: %s' % str(data)) if 'mail' in data and data['mail']: if not emailre.match(data['mail']): return template_render( tmpl, { 'session': env['beaker.session'], 'registerfail': True, 'error': 'Invalid Email.' }) # Does the user exist? res = session.query(User).filter( func.lower(User.name) == data['user'].lower()).first() if res: return template_render( tmpl, { 'session': env['beaker.session'], 'registerfail': True, 'error': 'Username already exists' }) user = User(data['user'], data['pass'], data['mail'] if 'mail' in data else None) session.add(user) try: session.commit() except sqlalchemy.exc.IntegrityError as e: session.rollback() print 'Rollback in register_user' print 'Post data:', data print 'Exception:', e return template_render( tmpl, { 'session': env['beaker.session'], 'registersuccess': True, 'user': user }) elif str(env['REQUEST_METHOD']) == 'GET': return template_render(tmpl, {'session': env['beaker.session']}) else: return None
def manage_variable(env, variableid): """ Page to manage a variable. Handles both GET and POST. """ if not loggedin(env): tmpl = jinjaenv.get_template('loginform.html') return template_render(tmpl, {'session': env['beaker.session']}) session = Session() user = session.query(User).filter(User.id == \ env['beaker.session']['loggedin_id']).first() if not user: return None if user.admin_level < 1: return str('Access denied') tmpl = jinjaenv.get_template('managevariable.html') variable = session.query(Variable).filter(Variable.id == \ variableid).first() if not variable: return None if str(env['REQUEST_METHOD']) == 'POST': data = read_post_data(env) if data is None: return str('Invalid POST data') if 'newname' not in data: return str('Invalid POST data') data['newname'] = urllib.unquote_plus(data['newname']) if len(data['newname']) == 0 or len(data['newname']) > 20: return template_render( tmpl, { 'session': env['beaker.session'], 'error': 'Variable name too long.', }) res = session.query(Variable).filter( Variable.name == data['newname']).first() if res is None: variable.name = data['newname'] session.add(variable) try: session.commit() except sqlalchemy.exc.IntegrityError as e: session.rollback() print 'Rollback in manage_variable' print 'Post data:', data print 'Exception:', e else: return template_render( tmpl, { 'session': env['beaker.session'], 'error': 'Name already exists in the system.', 'variable': variable }) return template_render(tmpl, { 'session': env['beaker.session'], 'variable': variable })