def test_unless_sqla(self): from sqlalchemy import (MetaData, Table, Column, Integer, String) from sqlalchemy.orm import create_session, mapper metadata = MetaData('sqlite:///:memory:') testtable = Table('test1', metadata, Column('id', Integer, primary_key=True), Column('val', String(8))) metadata.create_all() class Test(object): pass mapper(Test, testtable) testtable.insert().execute({'id': 1, 'val': 'bob'}) testtable.insert().execute({'id': 2, 'val': 'bobby'}) testtable.insert().execute({'id': 3, 'val': 'alberto'}) sess = create_session() getunless = unless(sess.query(Test).get) x = getunless(1) assert x.val == 'bob', x x = getunless(2) assert x.val == 'bobby', x assert_raises(ValueError, getunless, 5) assert_raises(TGValidationError, Convert(getunless).to_python, '5') x = Convert(getunless).to_python('1') assert x.val == 'bob', x
class DataSetController(BaseController): @expose('etl.templates.datasets.index') @require(predicates.not_anonymous()) def index(self): datasets = DBSession.query(DataSet).all() return dict(datasets=datasets) @expose('etl.templates.datasets.view') @expose(content_type="text/csv") @expose(content_type='application/json') @require(predicates.Any(predicates.not_anonymous(), is_api_authenticated())) @validate({'dataset': Convert(lambda v: DBSession.query(DataSet).filter_by(uid=v).one())}, error_handler=abort(404, error_handler=True)) def view(self, dataset, **kw): try: result = dataset.fetch() except Exception as e: log.exception('Failed to Retrieve Data') flash('ERROR: %s' % e, 'error') return dict(dataset=dataset, columns=[], results=[], count=0) if request.response_type == 'text/csv': return dateframe_to_csv(result) elif request.response_type == 'application/json': return dateframe_to_json(result) return dict( dataset=dataset, columns=list(result.columns), results=list(result.itertuples()), count=len(result), py2=py_version < 3 )
class BasicTGController(TGController): @expose() @validate(ColonLessGenericValidator()) def validator_without_columns(self, **kw): return tg.request.validation['errors']['_the_form'] @expose('json:') @validate(validators={"some_int": validators.Int()}) def validated_int(self, some_int): assert isinstance(some_int, int) return dict(response=some_int) @expose('json:') @validate(validators={"a": validators.Int()}) def validated_and_unvalidated(self, a, b): assert isinstance(a, int) assert isinstance(b, unicode_text) return dict(int=a, str=b) @expose() @controller_based_validate() def validate_controller_based_validator(self, *args, **kw): return 'ok' @expose('json:') @validate(validators={ "a": validators.Int(), "someemail": validators.Email() }) def two_validators(self, a=None, someemail=None, *args): errors = tg.request.validation['errors'] values = tg.request.validation['values'] return dict(a=a, someemail=someemail, errors=str(errors), values=str(values)) @expose('json:') @validate(validators={"a": validators.Int()}) def with_default_shadow(self, a, b=None): """A default value should not cause the validated value to disappear""" assert isinstance(a, int), type(a) return { 'int': a, } @expose('json:') @validate(validators={"e": ColonValidator()}) def error_with_colon(self, e): errors = tg.request.validation['errors'] return dict(errors=str(errors)) @expose('json:') @validate( validators={ "a": validators.Int(), "b": validators.Int(), "c": validators.Int(), "d": validators.Int() }) def with_default_shadow_long(self, a, b=None, c=None, d=None): """A default value should not cause the validated value to disappear""" assert isinstance(a, int), type(a) assert isinstance(b, int), type(b) assert isinstance(c, int), type(c) assert isinstance(d, int), type(d) return { 'int': [a, b, c, d], } @expose() def display_form(self, **kwargs): return str(myform.render(values=kwargs)) @expose('json:') @validate(form=myform) def process_form(self, **kwargs): kwargs['errors'] = tg.request.validation['errors'] return dict(kwargs) @expose('json:') @validate(form=myform, error_handler=process_form) def send_to_error_handler(self, **kwargs): kwargs['errors'] = tg.request.validation['errors'] return dict(kwargs) @expose('json') def tw2form_error_handler(self, **kwargs): return dict(errors=tg.request.validation['errors']) @expose('json:') @validate(form=movie_form, error_handler=tw2form_error_handler) def send_tw2_to_error_handler(self, **kwargs): return 'passed validation' @expose() @validate({'param': tw2c.IntValidator()}, error_handler=validation_errors_response) def tw2_dict_validation(self, **kwargs): return 'NO_ERROR' @expose() @validate({'param': validators.Int()}, error_handler=validation_errors_response) def formencode_dict_validation(self, **kwargs): return 'NO_ERROR' @expose('text/plain') @validate(form=FormWithFieldSet, error_handler=tw2form_error_handler) def tw2_fieldset_submit(self, **kwargs): return 'passed validation' @expose() def set_lang(self, lang=None): tg.session['tg_lang'] = lang tg.session.save() return 'ok' @expose() @validate(validators=Pwd()) def password(self, pwd1, pwd2): if tg.request.validation['errors']: return "There was an error" else: return "Password ok!" @expose('json:') @before_render( lambda rem, params, output: output.update({'GOT_ERROR': 'HOOKED'})) def hooked_error_handler(self, *args, **kw): return dict(GOT_ERROR='MISSED HOOK') @expose() @validate({'v': validators.Int()}, error_handler=hooked_error_handler) def with_hooked_error_handler(self, *args, **kw): return dict(GOT_ERROR='NO ERROR') @expose('json') @validate({'v': validators.Int()}) def check_tmpl_context_compatibility(self, *args, **kw): return dict(tmpl_errors=str(tg.tmpl_context.form_errors), errors=str(tg.request.validation['errors'])) @expose() def error_handler(self, *args, **kw): return 'ERROR HANDLER!' @expose('json:') @validate(validators={"some_int": validators.Int()}, error_handler=error_handler) def validate_other_error_handler(self, some_int): return dict(response=some_int) def unexposed_error_handler(self, uid, **kw): return 'UID: %s' % uid @expose() @validate({ 'uid': validators.Int(), 'num': validators.Int() }, error_handler=unexposed_error_handler) def validate_unexposed(self, uid, num): return 'HUH' @expose() @validate({'num': validators.Int()}, error_handler=partial(unexposed_error_handler, uid=5)) def validate_partial(self, num): return 'HUH' @expose() @validate({ 'uid': tw2c.IntValidator(), 'num': tw2c.IntValidator() }, error_handler=error_handler_function) def validate_function(self, uid, num): return 'HUH' @expose() @validate({ 'uid': validators.Int(), 'num': validators.Int() }, error_handler=ErrorHandlerCallable()) def validate_callable(self, uid, num): return 'HUH' @expose() @validate({'uid': validators.Int()}, error_handler=ErrorHandlerCallable()) @validate({'num': validators.Int()}, error_handler=abort(412, error_handler=True)) def validate_multi(self, uid, num): return str(uid + num) @expose() @validate({'uid': validators.Int()}, error_handler=abort(412, error_handler=True)) def abort_error_handler(self): return 'HUH' @expose() @validate({'uid': validators.Int()}, error_handler=validation_errors_response) def validate_json_errors(self): return 'HUH' @expose() def validate_json_errors_complex_types(self, date): tg.request.validation.values = {'date': datetime.datetime.utcnow()} return validation_errors_response() @expose() @before_call(lambda remainder, params: params.setdefault('num', 5)) def hooked_error_handler(self, uid, num): return 'UID: %s, NUM: %s' % (uid, num) @expose() @validate(ThrowAwayValidationIntentValidator(), error_handler=abort(412, error_handler=True)) def throw_away_intent(self, uid): if tg.request.validation.exception: return 'ERROR' return 'UHU?' @expose() @validate(error_handler=hooked_error_handler) def passthrough_validation(self, uid): return str(uid) @expose() @validate({'uid': validators.Int()}, error_handler=hooked_error_handler) def validate_hooked(self, uid): return 'HUH' # Decorate validate_hooked with a controller wrapper Decoration.get_decoration(hooked_error_handler)\ ._register_controller_wrapper(ControllerWrapperForErrorHandler) @expose() def manually_handle_validation(self): # This is done to check that we don't break compatibility # with external modules that perform custom validation like tgext.socketio controller = self.__class__.validate_function args = (2, 'NaN') try: output = '' validate_params = get_params_with_argspec(controller, {}, args) params = DecoratedController._perform_validate( controller, validate_params) except validation_errors as inv: handler, output = DecoratedController._handle_validation_errors( controller, args, {}, inv, None) return output @expose(content_type='text/plain') @validate({'num': Convert(int, l_('This must be a number'))}, error_handler=validation_errors_response) def post_pow2(self, num=-1): return str(num * num) @expose(content_type='text/plain') @validate({'num': Convert(int, l_('This must be a number'), default=0)}, error_handler=validation_errors_response) def post_pow2_opt(self, num=-1): return str(num * num)
class ExtractionFilterController(RestController): @expose('json') @decode_params('json') @validate( { 'extraction': Convert( lambda v: DBSession.query(Extraction).filter_by(uid=v).one()) }, error_handler=abort(404, error_handler=True)) def post(self, filter=None, extraction=None, **kwargs): e_filter = ExtractionFilter(extraction=extraction, name=filter.get('name')) if filter.get('default'): ExtractionFilter.set_default(e_filter) DBSession.add( ExtractionStep(extraction_filter=e_filter, function='query', priority=0, options=json.dumps( {'expression': filter.get('query')}))) DBSession.flush() return dict(filter=e_filter) @expose() def delete(self, uid=None): e_filter = DBSession.query(ExtractionFilter).get(uid) if not e_filter: abort(404) extraction_id = e_filter.extraction_id DBSession.delete(e_filter) return redirect('/extractions/view/%s' % extraction_id) @expose('json') @decode_params('json') @validate( { 'extraction': Convert( lambda v: DBSession.query(Extraction).filter_by(uid=v).one()) }, error_handler=abort(404, error_handler=True)) def put(self, filter=None, extraction=None, **kwargs): e_filter = DBSession.query(ExtractionFilter).get(filter.get('uid')) if not e_filter: return abort(404) step = DBSession.query(ExtractionStep).get( filter.get('steps')[0]['uid']) if not step: return abort(404) e_filter.extraction = extraction e_filter.name = filter.get('name') if filter.get('default'): ExtractionFilter.set_default(e_filter) else: e_filter.default = False exp = {'expression': filter.get('query')} step.options = json.dumps(exp) return dict(filter=e_filter) @expose('json') @decode_params('json') @require(predicates.not_anonymous()) @validate( { 'extraction': Convert( lambda v: DBSession.query(Extraction).filter_by(uid=v).one()) }, error_handler=abort(404, error_handler=True)) def filters_from_template(self, template=None, field=None, extraction=None): if template == 'alphabetical': for i in range(ord('A'), ord('Z') + 1): c = chr(i) next_c = chr(i + 1) e_filter = ExtractionFilter(extraction=extraction, name=c) expr = '({} >= "{}" and {} <= "{}") or ({} >= "{}" and {} <= "{}")'.format( field, c, field, next_c, field, c.lower(), field, next_c.lower()) DBSession.add( ExtractionStep(extraction_filter=e_filter, function='query', priority=0, options=json.dumps({'expression': expr}))) expr = '({} < "0") or ({} >= ":" and {} <= "A") or ({} >= "[" and {} <= "a") or ({} >= "{")'.replace( '{}', field) e_filter = ExtractionFilter(extraction=extraction, name='Symbols') DBSession.add( ExtractionStep(extraction_filter=e_filter, function='query', priority=0, options=json.dumps({'expression': expr}))) expr = '({} >= "0" and {} <= ":")'.replace('{}', field) e_filter = ExtractionFilter(extraction=extraction, name='0-9') DBSession.add( ExtractionStep(extraction_filter=e_filter, function='query', priority=0, options=json.dumps({'expression': expr}))) else: return dict(extraction=extraction, error=dict( template='unknown template: {}'.format(template), field=None, )) DBSession.flush() extraction = DBSession.merge(extraction) return dict(extraction=extraction, error=None)
class RootController(BaseController): admin = AdminController(model, None, config_type=TGAdminConfig) qa = QaController() precondition = PreconditionController() workspace = WorkspaceController() output = OutputController() document = DocumentController() questionary = FormController() resolve = ResolveController() output_plus = OutputPlusController() error = ErrorController() @expose('ksweb.templates.index') def index(self): if predicates.has_any_permission('manage', 'lawyer'): redirect('/start') return dict() @expose('ksweb.templates.questionary.index') @paginate('entities', items_per_page=int(config.get('pagination.items_per_page'))) def dashboard(self, share_id): user = model.User.query.find({'_id': ObjectId(share_id)}).first() if not user: response.status_code = 403 flash(_('You are not allowed to operate in this page')) redirect('/start') entities = model.Questionary.query.find({ '$or': [{ '_user': ObjectId(user._id) }, { '_owner': ObjectId(user._id) }] }).sort('title') if not entities.count(): flash( _('You don\'t have any form associated to %s' % user.email_address)) redirect('/start') auth_force_login(user.user_name) return dict( page='questionary-index', fields={ 'columns_name': [ _('Title'), _('Owner'), _('Shared with'), _('Created on'), _('Completion %') ], 'fields_name': ['title', '_owner', '_user', 'creation_date', 'completion'] }, entities=entities, actions=False, actions_content=[_('Export')], workspace=None, show_sidebar=False, share_id=share_id) @decode_params('json') @expose('json') def become_editor_from_user(self, **kw): user = model.User.query.find({'_id': ObjectId(kw['share_id'])}).first() if user and not user.password: user.password = kw['password'] user.user_name = kw['username'] group = model.Group.query.find({'group_name': 'lawyer'}).first() user.groups = [group] return dict() @expose('ksweb.templates.start') @require( predicates.has_any_permission('manage', 'lawyer', msg=l_('Only for admin or lawyer'))) def start(self): user = request.identity['user'] categories = model.Workspace.per_user(user._id) return dict(page='index', user=user, workspaces=categories, show_sidebar=False) @expose('ksweb.templates.welcome') @require( predicates.has_any_permission('manage', 'lawyer', msg=l_('Only for admin or lawyer'))) @validate({'workspace': Convert(ObjectId, 'must be a valid ObjectId')}, error_handler=ksweb_error_handler) def welcome(self, workspace): user = request.identity['user'] ws = model.Workspace.query.find({'_id': ObjectId(workspace)}).first() return dict(page='welcome', user=user, workspace=workspace, ws=ws, show_sidebar=True) @expose('json') @require( predicates.has_any_permission('manage', 'lawyer', msg=l_('Only for admin or lawyer'))) def entity(self, _id): entity = entity_from_hash(_id) if not entity: entity = entity_from_id(_id) if entity: redirect(entity.url) abort(404) @expose('ksweb.templates.terms') def terms(self): return dict() @expose('ksweb.templates.privacy') def privacy(self): return dict() @expose('ksweb.templates.legal') def legal(self): return dict() @expose('ksweb.templates.source') def source(self): return dict() @expose('ksweb.templates.login') def login(self, came_from=lurl('/'), failure=None, login=''): if failure is not None: if failure == 'user-not-found': flash(_('User not found'), 'error') elif failure == 'invalid-password': flash(_('Invalid Password'), 'error') elif failure == 'user-created': flash(_('User successfully created')) login_counter = request.environ.get('repoze.who.logins', 0) if failure is None and login_counter > 0: flash(_('Wrong credentials'), 'warning') return dict(page='login', login_counter=str(login_counter), came_from=came_from, login=login) @expose() def post_login(self, came_from=lurl('/')): if not request.identity: login_counter = request.environ.get('repoze.who.logins', 0) + 1 redirect('/login', params=dict(came_from=came_from, __logins=login_counter)) userid = request.identity['repoze.who.userid'] flash(_('Welcome back, %s!') % userid) return HTTPFound(location=came_from) @expose() def post_logout(self, came_from=lurl('/')): flash(_('We hope to see you soon!')) return HTTPFound(location=came_from)
class ExtractionsController(BaseController): filter = ExtractionFilterController() @expose('etl.templates.extractions.index') @require(predicates.not_anonymous()) def index(self, **kw): categories = DBSession.query(app_model.Category).all() uncategorised = DBSession.query(Extraction).filter_by( category_id=None).all() categories += [Bunch(extractions=uncategorised, name="No Category")] return dict(categories=categories, has_validation_errors=request.validation.errors, new_form=CreateExtractionForm) @expose() @require(predicates.in_group('managers')) @validate(CreateExtractionForm, error_handler=index) def create(self, name, **kw): DBSession.add(Extraction(name=name)) flash('New Extraction successfully created', 'ok') return redirect('./index') @expose() @require(predicates.in_any_group('manager', 'admin')) def delete(self, uid): extraction = DBSession.query(Extraction).get(uid) or abort(404) DBSession.delete(extraction) flash('Extraction correctly deleted') return redirect(tg.url('/extractions')) @expose('etl.templates.extractions.view') @expose(content_type="text/csv") @expose(content_type='application/json') @require(predicates.Any(predicates.not_anonymous(), is_api_authenticated())) @validate( { 'extraction': Convert( lambda v: DBSession.query(Extraction).filter_by(uid=v).one()) }, error_handler=abort(404, error_handler=True)) def view(self, extraction, extraction_filter=None, **kw): try: result = extraction.perform() except Exception as e: log.exception('Failed to Retrieve Data') flash('ERROR RETRIEVING DATA: %s' % e, 'error') return redirect('/error') e_filter = None try: if extraction_filter: if int(extraction_filter ) != -1: # -1 = original extraction requested by user e_filter = DBSession.query(ExtractionFilter).get( extraction_filter) if not e_filter: return abort(404) result = e_filter.perform(result) else: default = DBSession.query(ExtractionFilter).filter( ExtractionFilter.default == True, ExtractionFilter.extraction_id == extraction.uid).first() if default: e_filter = default result = default.perform(result) except Exception as e: log.exception('Failed to Retrieve Data') flash('ERROR RETRIEVING DATA: %s' % e, 'error') result = DataFrame() if request.response_type == 'text/csv': return dateframe_to_csv(result) elif request.response_type == 'application/json': return dateframe_to_json(result) visualizations = dict( (name, None) for name in extraction.visualization.split('+')) axis = [] if extraction.graph_axis: axis = [x.strip() for x in extraction.graph_axis.split(',')] visualizations = get_graph(result, axis, visualizations) if config.get("extraction.max_elements") is None: log.warn( "Cannot find max elements to render in config file. Using default 10000" ) if len(result) * len(result.columns) > int( config.get("extraction.max_elements", 10000)): flash( "There are too many data to extract, please add some filters", "error") filters = DBSession.query(ExtractionFilter).filter_by( extraction_id=extraction.uid).all() return dict(extraction=extraction, visualizations=visualizations, columns=result.columns, results=result.itertuples(), count=len(result), filters=filters, extraction_filter=e_filter, py2=py_version < 3) @expose() def reload_data(self, extraction): extraction = DBSession.query(Extraction).get(extraction) or abort(404) for dts in extraction.datasets: empty_cache(dts.dataset.cache_key()) empty_cache(dts.dataset.cache_key(DEFAULT_LIMIT_FOR_PERFORMANCE)) flash('Data reloaded') return redirect('/extractions/view/' + str(extraction.uid))
class EditorController(RestController): allow_only = predicates.not_anonymous() function = StepFunctionController() datasets = DatasetsEditorController() steps = StepsEditorController() def _visit(self, extraction=None, *args, **kwargs): try: tmpl_context.extraction = DBSession.query(Extraction).filter_by( uid=extraction).one() except: abort(404) @expose('etl.templates.editor.index') @validate( { 'extraction': Convert( lambda v: DBSession.query(Extraction).filter_by(uid=v).one()) }, error_handler=abort(404, error_handler=True)) def get_one(self, extraction=None, *args, **kw): available_datasets = [] datasets_columns = {} for d in DBSession.query(DataSet): try: cols = [str(c) for c in d.sample.columns] except Exception as ex: log.exception(str(ex)) cols = [] available_datasets.append((d.uid, d.name)) datasets_columns[d.uid] = cols # Documentation of all the functions available to build extractions docstring = { key: value.__doc__ for (key, value) in list(FUNCTIONS.items()) } docstring = escape_string_to_js(docstring) from tgext.pluggable import app_model categories = DBSession.query(app_model.Category) return dict(extraction=extraction, stepsformfields=dict( (fname, f.form_fields) for fname, f in list(FUNCTIONS.items())), available_datasets=available_datasets, datasets_columns=datasets_columns, visualization_types=VISUALIZATION_TYPES, docstring=json.dumps(docstring), category_list=categories) @expose('json') @decode_params('json') @validate( { 'extraction': Convert( lambda v: DBSession.query(Extraction).filter_by(uid=v).one()), 'visualization': VisualizationTypeValidator() }, error_handler=abort(404, error_handler=True)) def post(self, extraction=None, visualization=None, *args, **kw): if visualization['axis']: validate_axis_against_extraction_visualization( visualization['type'], visualization['axis'], extraction) extraction.visualization = visualization['type'] extraction.graph_axis = visualization['axis'] return dict(extraction=extraction) @expose('json') @validate( { 'extraction': Convert( lambda v: DBSession.query(Extraction).filter_by(uid=v).one()) }, error_handler=abort(404, error_handler=True)) def test_pipeline(self, extraction=None): try: data = extraction.fetch() except Exception as e: return dict( result=[dict(errors=stringfy_exc(e)[0], columns=[], data=[])]) result = [] for step in extraction.steps: try: data = step.apply(data) result.append( dict(errors=None, columns=(str(col) for col in data.columns), data=list( (truncate(force_text(d), 50) for d in r) for r in itertools.islice(data.itertuples(), 3)))) except Exception as e: # the shown error should be enough to understand the issue, # if it isn't then the full exception is logged, # the user should open a ticket or call you to fix it shown, logged = stringfy_exc(e) log.exception(logged) result.append(dict(errors=shown, columns=[], data=[])) return dict(results=result) @expose('json') def save_category(self, extraction, category): tmpl_context.extraction.category_id = category if category != "-1" else None flash(_('Category correctly updated')) return redirect(tg.url(['/editor', str(tmpl_context.extraction.uid)])) @expose() @validate( { 'extraction': Convert( lambda v: DBSession.query(Extraction).filter_by(uid=v).one()) }, error_handler=abort(404, error_handler=True)) def reload_data(self, extraction, **kw): for dts in extraction.datasets: empty_cache(dts.dataset.cache_key()) empty_cache(dts.dataset.cache_key(DEFAULT_LIMIT_FOR_PERFORMANCE)) flash('Data reloaded') return redirect('/extractions/view', params=dict(extraction=extraction.uid, **kw))
class StepsEditorController(RestController): @expose('json') def get_all(self): return dict(steps=tmpl_context.extraction.steps) @expose('json') @decode_params('json') def put(self, uid=None, function=None, priority=None, options=None, **kwargs): step = DBSession.query(ExtractionStep).get(uid) or abort(404) if function is not None: step.function = function if priority is not None: step.priority = priority if options is not None: try: step.options = json.dumps( ExtractionStep.formfor(function or step.function).validate(options)) except Exception as e: response.status = 412 return dict(errors=str(e)) return dict(step=step) @expose('json') @decode_params('json') def post(self, function=None, priority=None, options=None, **kwargs): try: if options is None: options = {} options = ExtractionStep.formfor(function).validate(options) except Exception as e: response.status = 412 return dict(errors=str(e)) DBSession.add( ExtractionStep(extraction_id=tmpl_context.extraction.uid, function=function, priority=priority, options=json.dumps(options))) return dict() @expose('json') @decode_params('json') def delete(self, uid=None, **kwargs): step = DBSession.query(ExtractionStep).get(uid) or abort( 404, passthrough='json') DBSession.delete(step) return dict() @expose('json') @decode_params('json') @validate({'enabled': Convert(int, default=0)}, error_handler=validation_errors_response) def toggle(self, uid=None, enabled=False, **kwargs): step = DBSession.query(ExtractionStep).get(uid) or abort(404) step.enabled = enabled return dict()
class RootController(BaseController): """ The root controller for the rubrica application. All the other controllers and WSGI applications should be mounted on this controller. For example:: panel = ControlPanelController() another_app = AnotherWSGIApplication() Keep in mind that WSGI applications shouldn't be mounted directly: They must be wrapped around with :class:`tg.controllers.WSGIAppController`. """ secc = SecureController() admin = AdminController(model, DBSession, config_type=TGAdminConfig) error = ErrorController() @expose('rubrica.templates.index') def index(self): return dict(page="index") @expose('rubrica.templates.home') @expose('json') @require(not_anonymous(msg='Only logged user can access thi page')) def home(self, *args): contact = DBSession.query(Rubrica).\ filter_by(parent_id=request.identity['user'].user_id).\ order_by(Rubrica.user_name).all() return dict(page='home', contact=contact) @expose('json') @require(not_anonymous(msg='Only logged user can access thi page')) def jp(self): contact = DBSession.query(Rubrica.user_name, Rubrica.user_Number).\ filter_by(parent_id=request.identity['user'].user_id).all() return dict(contact=contact) @expose('rubrica.templates.home') @require(not_anonymous(msg='Only logged user can access this page')) @validate(validators=dict({ 'search': validators.String(max=80, not_empty=True), })) def search(self, search): if not search: redirect('/home') else: try: contact = DBSession.query(Rubrica).\ filter(Rubrica.parent_id==request.identity['user'].user_id). \ filter(Rubrica.user_name.like(f"%{search}%")).\ order_by(Rubrica.user_name).all() return dict(page='home', contact=contact) except: redirect('/home') @expose('rubrica.templates.nuovo') @require(not_anonymous(msg='Only logged user can access thi page')) def new_contact(self): return dict(page='nuovo') @expose() def errore(self, **kwargs): flash('An error occurred: %s' % request.validation['errors']) @expose() @require(not_anonymous(msg='Only logged user can access thi page')) @validate( { 'name': validators.String(max=80, not_empty=True), 'number': Convert(int, 'Must be a number') }, error_handler=errore) def add(self, name, number): r = model.Rubrica() r.user_name = name r.user_Number = number r.parent_id = request.identity['user'].user_id model.DBSession.add(r) model.DBSession.flush() redirect('/home') @expose() @require(not_anonymous(msg='Only logged user can access thi page')) def delete(self, id): obj = DBSession.query(Rubrica).filter_by(user_id=id).one() model.DBSession.delete(obj) model.DBSession.flush() redirect('/home') # funzione non funzionante poichè devo trovare un modo per veficare # se l'email che l'utente decide magari di cambiare # se esista o meno, perciò per ora la seguente pagina # si limita solo a far vedere i dati dell'user # e la password mostrata è quella hashata @expose('rubrica.templates.settings') @require(not_anonymous(msg='Only logged user can access thi page')) def setting(self, **kw): try: user = DBSession.query(User)\ .filter_by(user_id=request.identity['user'].user_id).one() return dict(page='setting', user=user) except: return dict(page='setting') @expose('rubrica.templates.login') def login(self, came_from=lurl('/home'), failure=None, login=''): """Start the user login.""" if failure is not None: if failure == 'user-not-found': flash(_('User not found'), 'error') elif failure == 'invalid-password': flash(_('Invalid Password'), 'error') login_counter = request.environ.get('repoze.who.logins', 0) if failure is None and login_counter > 0: flash(_('Wrong credentials'), 'warning') return dict(page='login', login_counter=str(login_counter), came_from=came_from, login=login) @expose() def post_login(self, came_from=lurl('/home')): """ Redirect the user to the initially requested page on successful authentication or redirect her back to the login page if login failed. """ if not request.identity: login_counter = request.environ.get('repoze.who.logins', 0) + 1 redirect('/login', params=dict(came_from=came_from, __logins=login_counter)) userid = request.identity['repoze.who.userid'] flash(_('Welcome back, %s!') % userid) # Do not use tg.redirect with tg.url as it will add the mountpoint # of the application twice. return HTTPFound(location=came_from) @expose() def post_logout(self, came_from=lurl('/h')): """ Redirect the user to the initially requested page on logout and say goodbye as well. """ flash(_('We hope to see you soon!')) return HTTPFound(location=came_from)
class RootController(BaseController): admin = AdminController(model, None, config_type=TGAdminConfig) qa = QaController() precondition = PreconditionController() workspace = WorkspaceController() output = OutputController() document = DocumentController() questionary = FormController() resolve = ResolveController() output_plus = OutputPlusController() error = ErrorController() @expose("ksweb.templates.index") def index(self): if predicates.has_any_permission("manage", "lawyer"): redirect("/start") return dict() @expose("ksweb.templates.questionary.index") @paginate("entities", items_per_page=int(config.get("pagination.items_per_page"))) def dashboard(self, share_id): user = model.User.query.find({"_id": ObjectId(share_id)}).first() if not user: response.status_code = 403 flash(_("You are not allowed to operate in this page")) redirect("/start") entities = model.Questionary.query.find({ "$or": [{ "_user": ObjectId(user._id) }, { "_owner": ObjectId(user._id) }] }).sort("title") if not entities.count(): flash( _("You don't have any form associated to %s" % user.email_address)) redirect("/start") auth_force_login(user.user_name) return dict( page="questionary-index", fields={ "columns_name": [ _("Title"), _("Owner"), _("Shared with"), _("Created on"), _("Completion %"), ], "fields_name": [ "title", "_owner", "_user", "creation_date", "completion", ], }, entities=entities, actions=False, actions_content=[_("Export")], workspace=None, show_sidebar=False, share_id=share_id, ) @decode_params("json") @expose("json") def become_editor_from_user(self, **kw): user = model.User.query.find({"_id": ObjectId(kw["share_id"])}).first() if user and not user.password: user.password = kw["password"] user.user_name = kw["username"] group = model.Group.query.find({"group_name": "lawyer"}).first() user.groups = [group] return dict() @expose("ksweb.templates.start") @require( predicates.has_any_permission("manage", "lawyer", msg=l_("Only for admin or lawyer"))) def start(self): user = request.identity["user"] categories = model.Workspace.per_user(user._id) return dict(page="index", user=user, workspaces=categories, show_sidebar=False) @expose("ksweb.templates.welcome") @require( predicates.has_any_permission("manage", "lawyer", msg=l_("Only for admin or lawyer"))) @validate( {"workspace": Convert(ObjectId, "must be a valid ObjectId")}, error_handler=ksweb_error_handler, ) def welcome(self, workspace): user = request.identity["user"] ws = model.Workspace.query.find({"_id": ObjectId(workspace)}).first() return dict(page="welcome", user=user, workspace=workspace, ws=ws, show_sidebar=True) @expose("json") @require( predicates.has_any_permission("manage", "lawyer", msg=l_("Only for admin or lawyer"))) def entity(self, _id): entity = entity_from_hash(_id) if not entity: entity = entity_from_id(_id) if entity: redirect(entity.url) abort(404) @expose("ksweb.templates.terms") def terms(self): return dict() @expose("ksweb.templates.privacy") def privacy(self): return dict() @expose("ksweb.templates.legal") def legal(self): return dict() @expose("ksweb.templates.source") def source(self): return dict() @expose("ksweb.templates.login") def login(self, came_from=lurl("/"), failure=None, login=""): if failure is not None: if failure == "user-not-found": flash(_("User not found"), "error") elif failure == "invalid-password": flash(_("Invalid Password"), "error") elif failure == "user-created": flash(_("User successfully created")) login_counter = request.environ.get("repoze.who.logins", 0) if failure is None and login_counter > 0: flash(_("Wrong credentials"), "warning") return dict( page="login", login_counter=str(login_counter), came_from=came_from, login=login, ) @expose() def post_login(self, came_from=lurl("/")): if not request.identity: login_counter = request.environ.get("repoze.who.logins", 0) + 1 redirect("/login", params=dict(came_from=came_from, __logins=login_counter)) userid = request.identity["repoze.who.userid"] flash(_("Welcome back, %s!") % userid) return HTTPFound(location=came_from) @expose() def post_logout(self, came_from=lurl("/")): flash(_("We hope to see you soon!")) return HTTPFound(location=came_from)