def update_details(urlid): content = get_content_or_404(urlid) title = request.forms.getunicode('title', '').strip() license = request.forms.get('license') partner = request.forms.getunicode('partner', '').strip() flags = request.forms.getall('flags') replaces = request.forms.get('replaces', '').strip() is_partner = 'is_partner' in flags errors = {} if not title: errors['title'] = _('Title cannot be blank') if not errors: content.populate(title=title, license=license, partner=partner or None, replaces=replaces or None, is_partner=is_partner) content.put() response.flash(_('Content details have been updated')) redirect(i18n_path(content.admin_path)) return dict(vals=request.forms, errors=errors, content=content)
def update_content_details(id): to_put = [] content = get_content_or_404(id) ref_path = i18n_path(request.forms.get('back', content.path)) if not content.is_editable: response.flash( _('Voting is disabled for content that is being ' 'broadcast')) redirect(ref_path) vote = request.forms.get('vote') if vote not in ['up', 'down']: response.flash( _('There was a problem with the request. Please try ' 'again later.')) redirect(ref_path) if vote == 'up': content.upvotes += 1 to_put.append(Event.create(Event.UPVOTE, content.key)) elif vote == 'down': content.downvotes += 1 to_put.append(Event.create(Event.DOWNVOTE, content.key)) to_put.append(content) ndb.put_multi(to_put) redirect(ref_path)
def update_content_details(id): to_put = [] content = get_content_or_404(id) ref_path = i18n_path(request.forms.get('back', content.path)) if not content.is_editable: response.flash(_('Voting is disabled for content that is being ' 'broadcast')) redirect(ref_path) vote = request.forms.get('vote') if vote not in ['up', 'down']: response.flash(_('There was a problem with the request. Please try ' 'again later.')) redirect(ref_path) if vote == 'up': content.upvotes += 1 to_put.append(Event.create(Event.UPVOTE, content.key)) elif vote == 'down': content.downvotes += 1 to_put.append(Event.create(Event.DOWNVOTE, content.key)) to_put.append(content) ndb.put_multi(to_put) redirect(ref_path)
def update_content_details(id): content = get_content_or_404(id) if not content.is_editable: # Translators, shown when content is not editable (it's on air, etc) response.flash(_('This content is not editable')) redirect(i18n_path(content.path)) errors = {} title = request.forms.getunicode('title', '').strip() license = request.forms.get('license') or None if not content.title and not title: errors['title'] = _('Title cannot be blank') if license and license not in Content.LICENSE_CHOICES: errors['license'] = _('Please select a license from provided choices') if not errors: to_put = [] if title and content.title != title: content.title = title to_put.append(Event.create(Event.TITLE, content.key)) if license and content.license != license: content.license = license to_put.append(Event.create(Event.LICENSE, content.key)) if to_put: # If we have events in to_put list, we also need to put the content to_put.append(content) ndb.put_multi(to_put) response.flash(_('Content has been updated')) redirect(content.path) return dict(vals=request.forms, errors=erorrs, content=content)
def update_content_details(id): content = get_content_or_404(id) if not content.is_editable: # Translators, shown when content is not editable (it's on air, etc) response.flash(_('This content is not editable')) redirect(i18n_path(content.path)) errors = {} title = request.forms.getunicode('title', '').strip() license = request.forms.get('license') or None if not content.title and not title: errors['title'] = _('Title cannot be blank') if license and license not in Content.LICENSE_CHOICES: errors['license'] = _('Please select a license from provided choices') if not errors: to_put = [] if title and content.title != title: content.title = title to_put.append(Event.create(Event.TITLE, content.key)) if license and content.license != license: content.license = license to_put.append(Event.create(Event.LICENSE, content.key)) if to_put: # If we have events in to_put list, we also need to put the content to_put.append(content) ndb.put_multi(to_put) response.flash(_('Content has been updated')) redirect(content.path) return dict(vals=request.forms, errors=erorrs, content=content)
def login(): username = request.forms.get('username') password = request.forms.get('password') user = check_login(username, password) if user: set_user_token(user) response.flash("Welcome, " + user.first_name) else: response.flash("No user found with that login") redirect('/')
def post_set_password(): user = get_user(request) if user: password = request.forms.get('password') password_check = request.forms.get('password_check') if password != password_check: the_html = open('password_change.html').read() response.flash("Those passwords don't match!") return template(the_html, user=user, request=request) set_user_password(user, password) response.flash("Your password has been changed") redirect('/')
def wrapper(session, db=None): params={ 'session': session } if db: params['db'] = db if(session['user']): return f(**params) else: response.flash({'message': 'Usuário não esta autorizado a acessa essa requisição.', 'code': 'danger'}) return redirect('login')
def do_change_password(db, session): token = request.csrf_token form = ChangePasswordForm(request.forms or None) if form.is_valid(): user_session = json.loads(session.get('user')) if form.data.get('new_password') == form.data.get('confirma_password'): new_password = form.data.get('new_password') user = db.query(User).filter_by(username=user_session.get('username')).first() user.change_password(new_password) db.add(user) response.flash({'message': 'Senha auterada com sucesso', 'code': 'success'}) return redirect('/change_password') else: return {'form': form, 'message': 'Senha não confirma', 'code': 'warning', 'token': token} return {'form': form, 'message': 'Corrija os erros e tente novamente', 'code': 'danger', 'token': token}
def handle_manual_add(): url = request.params.get('url', '').strip() title = request.params.getunicode('title', '').strip() license = request.params.get('license') or None archive = request.params.get('archive') or None errors = {} if not url: # Translators, used as error message on failure to submit content errors['url'] = _('Please type in a valid URL') if not errors: try: content = Content.create(url=url, license=license, title=title, archive=archive) logging.info("Created content for '%s' (real url: '%s')", url, content.url) response.flash(_('Content has been added')) redirect(i18n_path(PREFIX + '/')) except Content.InvalidURLError as err: logging.debug("URL error while parsing '%s': %s", url, err) # Translators, used as error message on failure submit suggestion errors['url'] = _('This URL is invalid') except Content.FetchError as err: logging.debug("Fetch error while parsing '%s': %s (%s)", url, err, err.error) # Translators, used as error message on failure submit suggestion errors['url'] = _('The page at specified URL does not exist') except Content.NotAllowedError as err: logging.debug("Access error while parsing '%s': %s", url, err) # Translators, used as error message on failure submit suggestion errors['url'] = _('The page must be accessible to robots') except Content.ContentError as err: logging.debug("Content error while parsing '%s': %s (%s)", url, err, err.error) # Translators, used as error message on failure submit suggestion errors['url'] = _('The content on the page could not be ' 'understood, please provide and URL to a valid ' 'web page') except Content.BotError as err: logging.exception("Error while fetching '%s': %s", url, err) # Translators, used as error message on failure submit suggestion errors['url'] = _('There was an unknown error with the URL') return get_common_context(dict(vals=request.forms, errors=errors))
def post_edit_profile(): user = get_user(request) if user: # Username shouldn't already exist username = request.forms.get('username') try: existing_user = User.get(username=username) if existing_user and existing_user != user: the_html = open('edit_profile.html').read() response.flash("That username is taken!") return template(the_html, user=user, request=request) except User.DoesNotExist: pass # Email should look vaguely legitimate # TODO: security thing - should we enforce confirmation # w/ the old email address? email = request.forms.get('email') if '@' not in email and '.' not in email.split('@')[1]: the_html = open('edit_profile.html').read() response.flash("That email is invalid!") return template(the_html, user=user, request=request) user.username = request.forms.get('username') user.first_name = request.forms.get('first_name') user.last_name = request.forms.get('last_name') user.email = request.forms.get('email') user.save() response.flash("Your profile has been updated") redirect('/')
def do_register(db): confirma_password = request.forms.get('confirma_password') form = LoginForm(request.forms) token = request.csrf_token usuario_exist = db.query(User).filter_by(username=form.data.get('username')).first() if usuario_exist: return {'form': form, 'message': 'Usuário exite.', 'code': 'danger', 'token': token} email_exist = db.query(User).filter_by(email=form.data.get('email')).first() if email_exist: return {'form': form, 'message': 'Email já esta sendo usado.', 'code': 'danger', 'token': token} if form.is_valid(): user = form.to_model() if not user.check_password(confirma_password): return {'form': form, 'message': 'Senha não confirma.', 'code': 'danger', 'token': token} db.add(user) response.flash({'message': 'Usuário registrado com sucesso', 'code': 'success'}) return redirect('/register') return {'form': form, 'message': 'Corrija os erros e tente novamente', 'code': 'danger', 'token': token}
def logout(): response.delete_cookie('token') response.flash("You have been logged out!") redirect('/')
def add_content_suggestion(): """ Handle a content suggestion request. """ # TODO: Handle Unicode URLs url = Content.validate_url(request.forms.get('url', '')) license = request.forms.get('license') or None errors = {} if not url: # Translators, used as error message on failure submit suggestion errors['url'] = _('This URL is invalid') if license: license = license.strip().upper() if license not in Content.LICENSE_CHOICES: # Translators, used as error message on failure to submit # suggestion errors['license'] = _('Please select a license from provided ' 'choices') if not url: # Translators, used as error message on failure to submit suggestion errors['url'] = _('Please type in a valid URL') if not errors: try: content = Content.create(url=url, license=license) logging.info("Created content for '%s' (real url: '%s')", url, content.url) response.flash(_('Your suggestion has been added')) redirect(i18n_path(content.path)) except Content.InvalidURLError as err: logging.debug("URL error while parsing '%s': %s", url, err) # Translators, used as error message on failure submit suggestion errors['url'] = _('This URL is invalid') except Content.FetchError as err: logging.debug("Fetch error while parsing '%s': %s (%s)", url, err, err.error) # Translators, used as error message on failure submit suggestion errors['url'] = _('The page at specified URL does not exist or ' 'the domain cannot be reached.') except Content.NotAllowedError as err: logging.debug("Access error while parsing '%s': %s", url, err) # Translators, used as error message on failure submit suggestion errors['url'] = _('The page must be accessible to robots') except Content.ContentError as err: logging.debug("Content error while parsing '%s': %s (%s)", url, err, err.error) # Translators, used as error message on failure submit suggestion errors['url'] = _('The content on the page could not be ' 'understood, please provide and URL to a valid ' 'web page') except Exception as err: logging.debug("Unknown error fetching '%s': %s", url, err) # Translators, used as error message on failure submit suggestion errors['url'] = _('There was an unknown error with the URL') return dict(vals=request.forms, errors=errors, Content=Content, content=get_content_list())
def finish_with_message(message): response.flash(message) redirect(i18n_path(PREFIX + '/'))
def add_content_suggestion(): """ Handle a content suggestion request. """ # TODO: Handle Unicode URLs url = Content.validate_url(request.forms.get('url', '')) license = request.forms.get('license') or None errors = {} if not url: # Translators, used as error message on failure submit suggestion errors['url'] = _('This URL is invalid') if license: license = license.strip().upper() if license not in Content.LICENSE_CHOICES: # Translators, used as error message on failure to submit # suggestion errors['license'] = _('Please select a license from provided ' 'choices') if not url: # Translators, used as error message on failure to submit suggestion errors['url'] = _('Please type in a valid URL') if not errors: try: content = Content.create(url=url, license=license) logging.info("Created content for '%s' (real url: '%s')", url, content.url) response.flash(_('Your suggestion has been added')) redirect(i18n_path(content.path)) except Content.InvalidURLError as err: logging.debug("URL error while parsing '%s': %s", url, err) # Translators, used as error message on failure submit suggestion errors['url'] = _('This URL is invalid') except Content.FetchError as err: logging.debug("Fetch error while parsing '%s': %s (%s)", url, err, err.error) # Translators, used as error message on failure submit suggestion errors['url'] = _('The page at specified URL does not exist or ' 'the domain cannot be reached.') except Content.NotAllowedError as err: logging.debug("Access error while parsing '%s': %s", url, err) # Translators, used as error message on failure submit suggestion errors['url'] = _('The page must be accessible to robots') except Content.ContentError as err: logging.debug("Content error while parsing '%s': %s (%s)", url, err, err.error) # Translators, used as error message on failure submit suggestion errors['url'] = _('The content on the page could not be ' 'understood, please provide and URL to a valid ' 'web page') except Exception as err: logging.debug("Unknown error fetching '%s': %s", url, err) # Translators, used as error message on failure submit suggestion errors['url'] = _('There was an unknown error with the URL') return dict(vals=request.forms, errors=errors, Content=Content, content=get_content_list())
def logout(session): if session.get('user'): session.destroy() response.flash({'message': 'Sessão encerrada com sucesso', 'code': 'success'}) return redirect('login')