示例#1
0
def update_profile():
    """
    Update a user's profile.
    """

    user_id = session.get('user_id')
    user_email = session.get('user_email')

    user = db_users.find_by_email(user_email)

    if user is None:
        log.error(
            'profile.py::update_profile',
            'ERROR: invalid state - no user record for user_email: ' +
            user_email)
        return redirect(url_for('home.index'))

    user_from_form: Optional[Dict[str,
                                  Union[str,
                                        Any]]] = get_user_from_request(request)

    # if the user is changing his/her email address,
    # check to make sure is doesn't already exist in the database

    if user_from_form['email'] != user_email:

        # check to see if the email address already exists
        check_email_user = db_users.find_by_email(user_from_form['email'])

        # if the email address is already taken, bail
        if check_email_user is not None:

            flash(
                "The email address '" + user_from_form['email'] +
                "' is already in our system. Please try another email address."
            )

            return render_template('my_profile.html', user=user_from_form)

    user['email'] = user_from_form['email']
    user['full_name'] = user_from_form['full_name']
    user['phone'] = user_from_form['phone']
    user['pref_show_page_help'] = user_from_form['pref_show_page_help']

    db.update('users', {'user_id': user_id}, user)

    flash('Your profile changes were successful.')

    # if the user has changed their email address, update it in their session
    if user_from_form['email'] != user_email:

        session['user_email'] = user_from_form['email']

    return redirect(url_for('profile.my_profile'))
示例#2
0
def sign_up():
    """
    Sign up a new user.
    """

    user_email = request.form['user_email']
    password = request.form['password']

    user = db_users.find_by_email(user_email)
    if user is not None:
        flash(
            'A user with that email address already exists. Hmmm. Not you? Try another email address.'
        )
        return redirect(url_for('home.index'))

    user = db_users.insert_user(user_email, password)

    if user is None:
        flash(
            'Oops! Looks like something went sideways. We have been notified of the problem. Carry on.'
        )
        return redirect(url_for('home.index'))

    session['authenticated'] = True
    session['user_email'] = user_email
    session['user_id'] = user['user_id']

    flash('Welcome to QuantumRocket!')

    return redirect(url_for('home.dashboard'))
示例#3
0
def edit_widget_complete():
    """
    Complete a widget edit.
    """

    widget_id = request.form['widget_id']
    widget_name = request.form['widget_name']
    description = request.form['description']

    user_id = session.get('user_id')
    user_email = session.get('user_email')

    user = db_users.find_by_email(user_email)

    if user is None:

        log.error(
            'widget.py::edit_widget_complete',
            'ERROR: invalid state - no user record for user_email: ' +
            user_email)

        flash('Please sign in. If you are not already a user, please sign up.')
        return redirect(url_for('home.index'))

    # check to see if the widget name already exists
    existing_widget = db_widgets.find_by_user_id_and_widget_name(
        user_id, widget_name)

    if existing_widget is not None:

        # are we looking at the same widget (database and form)?
        if existing_widget['widget_id'] != widget_id:

            flash("You already have a Widget with the name '" + widget_name +
                  "'. Please try a different name as Widget names are unique.")

            keep_widget = get_widget_from_request(request)
            return render_template('edit_widget.html', widget=keep_widget)

    widget = {}
    widget['widget_name'] = widget_name
    widget['user_id'] = user_id
    widget['user_email'] = user_email
    widget['description'] = description

    db.update('widgets', {'widget_id': widget_id}, widget)

    flash("Widget '" + widget_name + "' edit successful.")

    log.debug(
        'widget.py::edit_widget_complete',
        'Successful edit of existing widget, widget_id [' + widget_id + '].')

    return redirect(url_for('widget.my_widgets'))
示例#4
0
def add_widget_complete():
    """
    Complete the process of adding a new widget.
    """

    user_id = session.get('user_id')
    user_email = session.get('user_email')

    user = db_users.find_by_email(user_email)

    if user is None:

        log.error(
            'widget.py::add_widget_complete',
            'ERROR: invalid state - no user record for user_email: ' +
            user_email)

        flash(
            'Please sign in. If you are not already a QuantumRocket user, please sign up.'
        )
        return redirect(url_for('home.index'))

    widget_name = request.form['widget_name']

    # check to see if the widget name already exists
    existing_widget: Optional[Dict[Any, Any]] = \
        db_widgets.find_by_user_id_and_widget_name(user_id, widget_name)

    if existing_widget is not None:

        flash("You already have a Widget with the name '" + widget_name +
              "'. Please try a different name as Widget names are unique.")

        keep_widget: Optional[Dict[
            str, Optional[Any]]] = get_widget_from_request(request)
        return render_template('add_widget.html', widget=keep_widget)

    description = request.form['description']

    widget = {}
    widget['widget_name'] = request.form['widget_name']
    widget['user_id'] = user_id
    widget['user_email'] = user_email
    widget['description'] = description

    widget_id = db.insert('widgets', widget)

    flash("Widget '" + widget_name + "' added.")
    log.debug('widget.py::add_widget_complete',
              'Added new widget, widget_id [' + widget_id + '].')

    return redirect(url_for('widget.my_widgets'))
示例#5
0
def edit_widget():
    """
    Edit a widget.

    This endpoint is called by an 'edit' action button,
    from the 'widgets' table on the my_widgets.html page.
    """

    user_id = session.get('user_id')
    user_email = session.get('user_email')

    user = db_users.find_by_email(user_email)

    if user is None:
        log.error(
            'widget.py::edit_widget',
            'ERROR: invalid state - no user record for user_email: ' +
            user_email)
        flash('Please sign in. If you are not already a user, please sign up.')
        return redirect(url_for('home.index'))

    widget_id = request.args.get('widget_id')

    if widget_id is None:

        log.error(
            'widget.py::edit_widget',
            'ERROR: invalid state - no widget_id in request. user_id [' +
            user_id + ']')

        flash(
            'Oops! Something went sideways. We have been notified of the problem. Carry on.'
        )
        return redirect(url_for('widget.my_widgets'))

    log.debug('widget.py::edit_widget', 'widget_id [' + widget_id + ']')

    widget_to_edit: Optional[Dict[Any, Any]] = db_widgets.find_by_id(widget_id)

    if widget_to_edit is None:

        log.error(
            'widget.py::edit_widget',
            'ERROR: invalid state - no widget record for widget_id [' +
            widget_id + ']')

        flash(
            'Oops! Something went sideways. We have been notified of the problem. Carry on.'
        )
        return redirect(url_for('widget.my_widgets'))

    return render_template('edit_widget.html', widget=widget_to_edit)
示例#6
0
def my_profile():
    """
    Display a user's profile.
    """

    try:

        user_email = session.get('user_email')
        user = db_users.find_by_email(user_email)

        things = db_things.get('page.my_profile.help')

        if user['pref_show_page_help'] == 'yes':
            things['pref_show_page_help'] = 'yes'

        return render_template('my_profile.html', user=user, things=things)

    except Exception as e:
        log.error('profile.py::my_profile', str(e))
        return redirect(url_for('home.dashboard'))
示例#7
0
def index():
    """
    Home page.
    """

    things = None
    user = None

    try:

        things: Optional[Dict[Any, Any]] = db_things.get('page.index.help')

        user_email = session.get('user_email')
        user = None

        if user_email is None:

            # non-user: show help
            things['pref_show_page_help'] = 'yes'

        else:

            user = db_users.find_by_email(user_email)

            if user is None:
                things['pref_show_page_help'] = 'yes'
            else:
                if user['pref_show_page_help'] == 'yes':
                    things['pref_show_page_help'] = 'yes'

        return render_template('index.html', things=things, user=user)

    except Exception as e:

        log.error('home.py::index', 'Error on home page, [' + str(e) + '].')
        flash(
            'Oops! Something went sideways. We have been notified of the problem. Carry on.'
        )
        return render_template('index.html', things=things, user=user)
示例#8
0
def dashboard():
    """
    The Dashboard displays all stats at a glance.
    """

    user_email = session.get('user_email')
    user = db_users.find_by_email(user_email)

    if user is None:
        log.error('home.py::dashboard',
                  'No user record for email [' + user_email + '].')
        flash(
            'Oops! Looks like something went sideways. We have been notified of the problem. Carry on.'
        )
        return redirect(url_for('home.index'))

    widgets = db_widgets.find_by_user_id(user['user_id'])
    things: Optional[Dict[Any, Any]] = db_things.get('page.dashboard.help')

    if user['pref_show_page_help'] == 'yes':
        things['pref_show_page_help'] = 'yes'

    return render_template('dashboard.html', widgets=widgets, things=things)
示例#9
0
def my_widgets():
    """
    Display a user's widgets.
    """

    widgets = []
    user = None

    try:

        user_email = session.get('user_email')

        user = db_users.find_by_email(user_email)

        if user is None:
            log.error(
                'widget.py::my_widgets',
                'ERROR: invalid state - no user record for user_email: ' +
                user_email)
            flash(
                'Please sign in. If you are not already a QuantumRocket user, please sign up.'
            )
            return redirect(url_for('home.index'))

        widgets = db_widgets.find_by_user_id(user['user_id'])

    except Exception as e:

        log.error('widget.py::my_widgets', str(e))

    things = db_things.get('page.my_widgets.help')

    if user['pref_show_page_help'] == 'yes':
        things['pref_show_page_help'] = 'yes'

    return render_template('my_widgets.html', widgets=widgets, things=things)