def new_redirect(): """ Create a new redirect. Display the form to create a new redirect, and then process the results of the form. """ current_user = users.get_current_user() form = forms.NewRedirectForm(request.form) if form.validate_on_submit(): result = RedirectModel(target_url=form.target_url.data, display_name=form.display_name.data, enabled=True, user_id=current_user.user_id()) try: result.put() redirect_key = result.update_hashed_key() flash(u' '.join(['Redirect ', ROOT_URL+'r/'+redirect_key, 'to', result.target_url, 'is now active.'])) except (Error, CapabilityDisabledError): flash(u'Database error. Please try again in a few moments.') return redirect(url_for('list_redirects')) return render_template('new_redirect.html', form=form, nickname=current_user.nickname(), logout_url=users.create_logout_url( url_for('list_redirects')))
def put(): data = request.json if RedirectRepository().get_url(data["key"]): return "This url key is exist in database", 409 else: RedirectRepository().create( RedirectModel( url=data["url"], key=data["key"], timestamp=str(datetime.now().timestamp()), ) ) return data
def serve_redirects(redirect_code): """ Serve a redirect. If a valid redirect code is supplied, redirect the client to the target_url of the redirect, and then queue up (to keep out of request stream time) storing that visit. If an invalid code, 404. """ try: target_redirect = RedirectModel.all() \ .filter('hashed_key = ', redirect_code) \ .filter('enabled = ', True).fetch(1) except (Error, CapabilityDisabledError), e: logging.error("Database error when loading redirect - ", e) return render_template('500.html'), 500
def list_redirects(): """ List all redirects belonging to the current user. Search by user_id (as opposed to user), since the latter can change if someone changes their gmail address mid-session. """ current_user = users.get_current_user() try: redirects = RedirectModel.all() \ .order('-enabled').order('-modified') \ .filter("user_id =", current_user.user_id()) except (Error, CapabilityDisabledError), e: logging.error("Database error when loading list of redirects - ", e) return render_template('500.html'), 500
def edit_redirect(redirect_code): """ API endpoint to update information about a redirect. Changes some aspect of the redirect, and returns the success of that change by the response code (400 if trying to edit a redirect that doesn't exist or a bad change, 500 if there's a problem with editing, 200 if OK). """ try: target_redirect = RedirectModel.all().filter('hashed_key = ', \ redirect_code).fetch(1) except (Error, CapabilityDisabledError), e: logging.error("Database error when loading redirect - ", e) return render_template('none.html'), 500
def list_visits(redirect_code): """ List all visits to a redirect. Returns a JSON object that contains all information about visits to the redirect specified in the URL. By default returns default_results_returned results, but this can be overridden with the records query parameter. """ default_results_returned = 10 current_user = users.get_current_user() try: target_redirect = RedirectModel.all() \ .filter("user_id = ", current_user.user_id()) \ .filter("hashed_key = ", redirect_code).fetch(1) except (Error, CapabilityDisabledError), e: logging.error("Database error when loading list of visits - ", e) return render_template('none.html'), 500
from repositories import RedirectRepository from models import RedirectModel m = RedirectModel("dupa", "https://duck.com", "123") a = RedirectRepository() print(a.get_url("dupsa"))