def import_(request, callback=None): clazz = request.context.__model__ _ = request.translate renderer = ImportDialogRenderer(request, clazz) imported_items = [] form = renderer.form if (request.method == 'POST' and request.ringo.params.confirmed and form.validate(request.params)): try: items = _import(request) except (ValueError, AttributeError) as e: err_title = _("Import failed") err_msg = _( "Bad news! The import could not be finished and " "returns with an error." "No data has been modified in this operation and no " "items has been imported or updated. " "The last message we heard from the importer was: %s") % e renderer = ErrorDialogRenderer(request, err_title, err_msg) rvalue = {} ok_url = request.ringo.history.pop(2) rvalue['dialog'] = renderer.render(ok_url) rvalue['clazz'] = clazz return rvalue imported_items = _handle_save(request, items, callback) invalidate_cache() redirect = _handle_redirect(request) if redirect: return redirect rvalue = {} rvalue['dialog'] = renderer.render(imported_items) rvalue['clazz'] = clazz return rvalue
def handle_caching(request): """Will handle invalidation of the cache :request: TODO """ # Invalidate cache invalidate_cache() clazz = request.context.__model__ if request.session.get("%s.form" % clazz): del request.session["%s.form" % clazz] request.session.save()
def handle_caching(request): """Will handle invalidation of the cache :request: TODO """ # Invalidate cache invalidate_cache() clazz = request.context.__model__ if request.session.get('%s.form' % clazz): del request.session['%s.form' % clazz] request.session.save()
def _handle_delete_request(request, items, callback): clazz = request.context.__model__ _ = request.translate if request.method == 'POST' and is_confirmed(request): item_label = get_item_modul(request, clazz).get_label(plural=True) item_label_log = get_item_modul(request, clazz).get_label() mapping = {'item_type': item_label, 'num': len(items)} for item in items: if callback: item = callback(request, item) request.db.delete(item) # Invalidate cache invalidate_cache() try: request.db.flush() except (sa.exc.CircularDependencyError, sa.exc.IntegrityError) as e: mapping["error"] = e.message.decode("utf-8") title = _("Can not delete ${item_type} items.", mapping=mapping) body = _("There has been an integrity error which prevents " "the request to be fulfilled. There are still " "depended items on the item to be deleted. Please " "remove all depended relations to this item before " "deleting it and try again. Hint: ${error}", mapping=mapping) request.db.rollback() renderer = InfoDialogRenderer(request, title, body) rvalue = {} ok_url = request.session['history'].pop(2) rvalue['dialog'] = renderer.render(ok_url) return rvalue msg = _('Deleted ${num} ${item_type} successfully.', mapping=mapping) log_msg = u'User {user.login} deleted {item_label} {item.id}' \ .format(item_label=item_label, item=item, user=request.user) log.info(log_msg) request.session.flash(msg, 'success') # Handle redirect after success. return _handle_redirect(request) else: renderer = ConfirmDialogRenderer(request, clazz, 'delete') rvalue = {} rvalue['dialog'] = renderer.render(items) rvalue['clazz'] = clazz rvalue['item'] = items return rvalue
def _handle_delete_request(request, items, callback): clazz = request.context.__model__ _ = request.translate if request.method == 'POST' and request.ringo.params.confirmed: item_label = get_item_modul(request, clazz).get_label(plural=True) mapping = {'item_type': item_label, 'num': len(items)} for item in items: handle_callback(request, callback, item=item, mode="pre,default") request.db.delete(item) handle_callback(request, callback, item=item, mode="post") # Invalidate cache invalidate_cache() try: request.db.flush() except (sa.exc.CircularDependencyError, sa.exc.IntegrityError) as e: mapping["error"] = e.message.decode("utf-8") title = _("Can not delete ${item_type} items.", mapping=mapping) body = _( "There has been an integrity error which prevents " "the request to be fulfilled. There are still " "depended items on the item to be deleted. Please " "remove all depended relations to this item before " "deleting it and try again. Hint: ${error}", mapping=mapping) request.db.rollback() renderer = InfoDialogRenderer(request, title, body) rvalue = {} ok_url = request.ringo.history.pop(2) rvalue['dialog'] = renderer.render(ok_url) return rvalue msg = _('Deleted ${num} ${item_type} successfully.', mapping=mapping) log_msg = u'User {user.login} deleted {item_label} {item.id}' \ .format(item_label=item_label, item=item, user=request.user) log.info(log_msg) request.session.flash(msg, 'success') # Handle redirect after success. return _handle_redirect(request) else: renderer = ConfirmDialogRenderer(request, clazz, 'delete') rvalue = {} rvalue['dialog'] = renderer.render(items) rvalue['clazz'] = clazz rvalue['item'] = items return rvalue
def import_(request, callback=None): handle_history(request) handle_params(request) clazz = request.context.__model__ _ = request.translate renderer = ImportDialogRenderer(request, clazz) imported_items = [] form = renderer.form if request.method == "POST" and is_confirmed(request) and form.validate(request.params): try: items = _import(request) except (ValueError, AttributeError) as e: err_title = _("Import failed") err_msg = ( _( "Bad news! The import could not be finished and " "returns with an error." "No data has been modified in this operation and no " "items has been imported or updated. " "The last message we heard from the importer was: %s" ) % e ) renderer = ErrorDialogRenderer(request, err_title, err_msg) rvalue = {} ok_url = request.session["history"].pop(2) rvalue["dialog"] = renderer.render(ok_url) rvalue["clazz"] = clazz return rvalue imported_items = _handle_save(request, items, callback) invalidate_cache() redirect = _handle_redirect(request) if redirect: return redirect rvalue = {} rvalue["dialog"] = renderer.render(imported_items) rvalue["clazz"] = clazz return rvalue
def changepassword(request): """Method to change the users password by the user. The user user musst provide his old and the new pasword. Users are only allowed to change their own password.""" # Check authentification # As this view has now security configured it is # generally callable by all users. For this reason we first check if # the user is authenticated. If the user is not authenticated the # raise an 401 (unauthorized) exception. if not request.user: raise HTTPUnauthorized clazz = User _ = request.translate rvalue = {} # Load the item return 400 if the item can not be found. id = request.matchdict.get('id') factory = clazz.get_item_factory() try: item = factory.load(id, request.db) # Check authorisation # User are only allowed to set their own password. if item.id != request.user.id: raise HTTPForbidden() except sa.orm.exc.NoResultFound: raise HTTPBadRequest() form = Form(get_form_config(item, 'changepassword'), item, request.db, translate=_, renderers={}, change_page_callback={'url': 'set_current_form_page', 'item': clazz.__tablename__, 'itemid': id}, request=request, csrf_token=request.session.get_csrf_token()) if request.POST: mapping = {'item': item} # Do extra validation which is not handled by formbar. # Is the provided old password correct? validator = Validator('oldpassword', _('The given password is not correct'), check_password) pw_len_validator = Validator('password', _('Password must be at least 12 ' 'characters long.'), password_minlength_validator) pw_nonchar_validator = Validator('password', _('Password must contain at least 2 ' 'non-letters.'), password_nonletter_validator) form.add_validator(validator) form.add_validator(pw_len_validator) form.add_validator(pw_nonchar_validator) if form.validate(request.params): form.save() # Actually save the password. This is not done in the form # as the password needs to be encrypted. encrypt_password_callback(request, item) msg = _('Changed password for "${item}" successfull.', mapping=mapping) log.info(msg) request.session.flash(msg, 'success') route_name = get_action_routename(item, 'changepassword') url = request.route_path(route_name, id=item.id) # Invalidate cache invalidate_cache() return HTTPFound(location=url) else: msg = _('Error on changing the password for ' '"${item}".', mapping=mapping) log.info(msg) request.session.flash(msg, 'error') rvalue['clazz'] = clazz rvalue['item'] = item rvalue['form'] = form.render(page=get_current_form_page(clazz, request)) return rvalue
def changepassword(request): """Method to change the users password by the user. The user user musst provide his old and the new pasword. Users are only allowed to change their own password.""" # Check authentification # As this view has now security configured it is # generally callable by all users. For this reason we first check if # the user is authenticated. If the user is not authenticated the # raise an 401 (unauthorized) exception. if not request.user: raise HTTPUnauthorized clazz = User handle_history(request) handle_params(request) _ = request.translate rvalue = {} # Load the item return 400 if the item can not be found. id = request.matchdict.get('id') factory = clazz.get_item_factory() try: item = factory.load(id, request.db) # Check authorisation # User are only allowed to set their own password. if item.id != request.user.id: raise HTTPForbidden() except sa.orm.exc.NoResultFound: raise HTTPBadRequest() form = Form(get_form_config(item, 'changepassword'), item, request.db, translate=_, renderers={}, change_page_callback={'url': 'set_current_form_page', 'item': clazz.__tablename__, 'itemid': id}, request=request, csrf_token=request.session.get_csrf_token()) if request.POST: mapping = {'item': item} # Do extra validation which is not handled by formbar. # Is the provided old password correct? validator = Validator('oldpassword', _('The given password is not correct'), check_password) pw_len_validator = Validator('password', _('Password must be at least 12 ' 'characters long.'), password_minlength_validator) pw_nonchar_validator = Validator('password', _('Password must contain at least 2 ' 'non-letters.'), password_nonletter_validator) form.add_validator(validator) form.add_validator(pw_len_validator) form.add_validator(pw_nonchar_validator) if form.validate(request.params): form.save() # Actually save the password. This is not done in the form # as the password needs to be encrypted. encrypt_password_callback(request, item) msg = _('Changed password for "${item}" successfull.', mapping=mapping) log.info(msg) request.session.flash(msg, 'success') route_name = get_action_routename(item, 'changepassword') url = request.route_path(route_name, id=item.id) # Invalidate cache invalidate_cache() return HTTPFound(location=url) else: msg = _('Error on changing the password for ' '"${item}".', mapping=mapping) log.info(msg) request.session.flash(msg, 'error') rvalue['clazz'] = clazz rvalue['item'] = item rvalue['form'] = form.render(page=get_current_form_page(clazz, request)) return rvalue