def edit_website(web_id): """ Edit website endpoint :return: POST "redirect(url_for('index'))" :return: GET "render_template('edit_website.html')" """ user = _get_user() website = Website.query.get_or_404(web_id) if request.method == 'POST': data = {} for key, value in dict(request.form).items(): data[key] = value[0] Website.query.filter_by(id=web_id).update(data) db.session.commit() flash('Website {} updated successfully'.format(website.title)) return redirect(url_for('index')) else: auth = (user.livechat_login, user.livechat_api_key) url = 'https://api.livechatinc.com/groups/' headers = {"X-API-Version": "2"} request_data = requests.get(url, headers=headers, auth=auth).json() return render_template('edit_website.html', error={}, groups=request_data, website=website)
def code(): u = _get_user() if u and u.is_authenticated and u.user_role == 0: categories = InfraredDeviceCategory.get_all_categories() return render_template('admin_code.html', categories = categories) else: return abort(404)
def create_website(): """ Create website endpoint :return: POST "redirect(url_for('index'))" :return: GET "render_template('create_website.html')" """ user = _get_user() if request.method == 'POST': data = {} for key, value in dict(request.form).items(): data[key] = value[0] website = Website(**data) website.user_id = user.id db.session.add(website) db.session.commit() flash('Website created successfully') return redirect(url_for('index')) else: auth = (user.livechat_login, user.livechat_api_key) url = 'https://api.livechatinc.com/groups/' headers = {"X-API-Version": "2"} request_data = requests.get(url, headers=headers, auth=auth).json() return render_template('create_website.html', error={}, groups=request_data)
def get_data(): event = request.json['event'] # print request.headers if event == 'random_user': # Exclude me, my like users, users that don't show and my subscriptions if current_user.is_authenticated() and current_user.is_active(): exclude_list = [current_user.login] exclude_list.extend(current_user.users_like) exclude_list.extend(current_user.following) fields = ['sid', 'login', 'birthday', 'description'] fields_from_base = {field: '$' + field for field in fields} all_users = mongo.db.users.aggregate([{ "$group": { "_id": fields_from_base } }]) clear_users = [ user[u'_id'] for user in all_users['result'] if user[u'_id'][u'login'] not in exclude_list ] random_user = random.choice(clear_users) random_user['age'] = age(random_user['birthday']) return jsonify(result='OK', user=random_user) else: return jsonify(result='None', data="User is not authenticated", event=event, user=str(_get_user())) return jsonify(status="No response", event=event)
def update_profile(): """ Update profile endpoint :return: POST "redirect(url_for('index'))" :return: GET "render_template('update_profile.html')" """ user = _get_user() if request.method == 'POST': user.update_user_data(request.form) flash('Profile updated successfully') return redirect(url_for('index')) else: request.form = ImmutableMultiDict([ ('livechat_login', user.livechat_login or ''), ('livechat_api_key', user.livechat_api_key or '') ]) return render_template('update_profile.html', error={})
def user_context_processor(): """ Insert a special variable named `user` into all templates. This makes it easy for developers to add users and their data into templates without explicitly passing the user each each time. With this, you can now write templates that do stuff like this:: {% if user %} <p>Hi, {{ user.given_name }}!</p> <p>Your email is: {{ user.email }}</p> {% endif %} This lets you do powerful stuff, since the User object is nothing more than a Stormpath Account behind the scenes. See the Python SDK documentation for more information about Account objects: https://github.com/stormpath/stormpath-sdk-python """ return {"user": _get_user()}
def user_context_processor(): """ Insert a special variable named `user` into all templates. This makes it easy for developers to add users and their data into templates without explicitly passing the user each each time. With this, you can now write templates that do stuff like this:: {% if user %} <p>Hi, {{ user.given_name }}!</p> <p>Your email is: {{ user.email }}</p> {% endif %} This lets you do powerful stuff, since the User object is nothing more than a Stormpath Account behind the scenes. See the Python SDK documentation for more information about Account objects: https://github.com/stormpath/stormpath-sdk-python """ return {'user': _get_user()}
def login(): """ Log in an existing Stormpath user. This view will render a login template, then redirect the user to the next page (if authentication is successful). The fields that are asked for, the URL this view is bound to, and the template that is used to render this page can all be controlled via Flask-Stormpath settings. """ form = LoginForm() # If a user is already logged in, skip this page u = _get_user() if u.is_authenticated: return redirect(request.args.get('next') or current_app.config['STORMPATH_REDIRECT_URL']) # If we received a POST request with valid information, we'll continue # processing. if form.validate_on_submit(): try: # Try to fetch the user's account from Stormpath. If this # fails, an exception will be raised. account = User.from_login(form.login.data, form.password.data) # If we're able to successfully retrieve the user's account, # we'll log the user in (creating a secure session using # Flask-Login), then redirect the user to the ?next=<url> # query parameter, or the STORMPATH_REDIRECT_URL setting. login_user(account, remember=True) return redirect(request.args.get('next') or current_app.config['STORMPATH_REDIRECT_URL']) except StormpathError as err: flash(err.message) return render_template( current_app.config['STORMPATH_LOGIN_TEMPLATE'], form = form, )
from .decorators import groups_required from .models import User from .settings import check_settings, init_settings from .views import ( google_login, facebook_login, forgot, forgot_change, login, logout, register, ) # A proxy for the current user. user = LocalProxy(lambda: _get_user()) class StormpathManager(object): """ This object is used to hold the settings used to communicate with Stormpath. Instances of :class:`StormpathManager` are not bound to specific apps, so you can create one in the main body of your code and then bind it to your app in a factory function. """ def __init__(self, app=None): """ Initialize this extension. :param obj app: (optional) The Flask app. """
def get_data(): event = request.json['event'] # print request.headers if event == 'random_user': # Exclude me, my like users, users that don't show and my subscriptions if current_user.is_authenticated() and current_user.is_active(): exclude_list = [current_user.login] exclude_list.extend(current_user.users_like) exclude_list.extend(current_user.following) fields = ['sid', 'login', 'birthday', 'description'] fields_from_base = {field: '$' + field for field in fields} all_users = mongo.db.users.aggregate([ {"$group": {"_id": fields_from_base}} ]) clear_users = [user[u'_id'] for user in all_users['result'] if user[u'_id'][u'login'] not in exclude_list] random_user = random.choice(clear_users) random_user['age'] = age(random_user['birthday']) return jsonify(result='OK', user=random_user) else: return jsonify(result='None', data="User is not authenticated", event=event, user=str(_get_user())) return jsonify(status="No response", event=event)