示例#1
0
文件: views.py 项目: kmbn/chronoflask
def register():
    ''' Register user and create pagination table with one page
    and no entries.'''
    details = get_details()
    if details:
        flash('A user is already registered. Log in.')
        return redirect(url_for('auth.login'))
    details = {'chronofile_name': current_app.config['DEFAULT_NAME'], \
               'author_name': current_app.config['DEFAULT_AUTHOR']}
    register = True
    form = RegistrationForm()
    if form.validate_on_submit():
        password_hash = pwd_context.hash(form.password.data)
        # Create account and get creator id
        creator_id = insert_record('auth', {'email': form.email.data, \
                                   'password_hash': password_hash})
        insert_record('admin', {'chronofile_name': \
                                current_app.config['DEFAULT_NAME'], \
                                'author_name': \
                                current_app.config['DEFAULT_AUTHOR'], \
                                'creator_id': creator_id})
        insert_record('pagination', {'page': 1, 'entries': None})
        flash('Registration successful. You can login now.')
        return redirect(url_for('auth.login'))
    return render_template('register.html', form=form, details=details, \
                           register=register)
示例#2
0
文件: views.py 项目: kmbn/chronoflask
def change_password():
    details = get_details()
    form = ChangePasswordForm()
    if form.validate_on_submit():
        new_password_hash = pwd_context.hash(form.new_password.data)
        get_table('auth').update({'password_hash': new_password_hash}, \
                                 eids=[session.get('user_id')])
        flash('Your password has been updated.')
        return redirect(url_for('admin.view_admin'))
    return render_template('change_password.html', form=form, details=details)
示例#3
0
文件: views.py 项目: kmbn/chronoflask
def change_email():
    details = get_details()
    form = ChangeEmailForm()
    if form.validate_on_submit():
        new_email = form.new_email.data
        user_id = session.get('user_id')
        get_table('auth').update({'email': new_email}, eids=user_id)
        flash('Your email address has been updated.')
        return redirect(url_for('admin.view_admin'))
    return render_template('change_email.html', form=form, details=details)
示例#4
0
def view_single_entry(timestamp):
    '''Return a single entry based on given timestamp.'''
    entry = get_record('entries', Query().timestamp == timestamp)
    if not entry:
        return abort(404)
    form = RawEntryForm()
    if form.validate_on_submit():
        return parse_input(form.raw_entry.data, datetime.utcnow())
    details = get_details()
    return render_template('entry.html', form=form, timestamp=timestamp, \
                           entry=entry, details=details)
示例#5
0
def view_entries_for_day(day):
    '''Returns entries for given day in chronological order.'''
    details = get_details()
    form = RawEntryForm()
    if form.validate_on_submit():
        return parse_input(form.raw_entry.data, datetime.utcnow())
    entries_for_day = search_records('entries', Query().timestamp.all([day]))
    if not entries_for_day:
        return abort(404)
    return render_template('day.html', form=form, day=day, \
                           entries_for_day=entries_for_day, details=details)
示例#6
0
def view_entries_for_tag(tag):
    '''Return entries for given tag in chronological order.'''
    details = get_details()
    form = RawEntryForm()
    if form.validate_on_submit():
        return parse_input(form.raw_entry.data, datetime.utcnow())
    entries_for_tag = search_records('entries', Query().tags.all([tag]))
    if not entries_for_tag:
        return abort(404)
    return render_template('tag.html', form=form, tag=tag, \
                           entries_for_tag=entries_for_tag, details=details)
示例#7
0
def view_all_days():
    details = get_details()
    form = RawEntryForm()
    if form.validate_on_submit():
        return parse_input(form.raw_entry.data, datetime.utcnow())
    all_entries = search_records('entries', \
                                 Query().creator_id == session.get('user_id'))
    all_days = list()
    for entry in all_entries:
        if entry['timestamp'][:10] not in all_days:
            all_days.append(entry['timestamp'][:10])
    return render_template('days.html', all_days=all_days, form=form, \
                           details=details)
示例#8
0
def view_all_tags():
    details = get_details()
    form = RawEntryForm()
    if form.validate_on_submit():
        return parse_input(form.raw_entry.data, datetime.utcnow())
    all_entries = search_records('entries', \
                                 Query().creator_id == session.get('user_id'))
    all_tags = list()
    for entry in all_entries:
        for tag in entry['tags']:
            if tag not in all_tags:
                all_tags.append(tag)
    all_tags.sort()
    return render_template('tags.html', all_tags=all_tags, form=form, \
                           details=details)
示例#9
0
文件: views.py 项目: kmbn/chronoflask
def request_reset():
    details = get_details()
    if not details:
        return abort(404)
    form = ResetPasswordForm()
    if form.validate_on_submit():
        email = form.email.data
        user_id = get_element_id('auth', Query().email == email)
        token = generate_confirmation_token(user_id)
        send_email(email,
                   'Link to reset your password',
                   'email/reset_password',
                   token=token)
        flash('Your password reset token has been sent.')
        return redirect(url_for('auth.login'))
    return render_template('reset_password.html', form=form, details=details)
示例#10
0
文件: views.py 项目: kmbn/chronoflask
def login():
    details = get_details()
    if not get_record('auth', Query().email.exists()):
        flash('You need to register first.')
        return redirect(url_for('auth.register'))
    if session.get('logged_in'):
        return redirect(url_for('main.browse_all_entries'))
    form = LoginForm()
    if form.validate_on_submit():
        session['logged_in'] = True
        user_id = get_element_id('auth', Query().email == form.email.data)
        session['user_id'] = user_id
        if request.args.get('next'):
            return redirect(request.args.get('next'))
        else:
            return redirect(url_for('main.browse_all_entries'))
    return render_template('login.html', form=form, details=details)
示例#11
0
def view_entries_for_page(page):
    '''Returns entries for given page in reverse chronological order.'''
    try:
        int(page)
    except:
        TypeError
        return abort(404)
    page = int(page)
    if page == 1:
        return redirect(url_for('main.browse_all_entries'))
    details = get_details()
    form = RawEntryForm()
    if form.validate_on_submit():
        return parse_input(form.raw_entry.data, datetime.utcnow())
    # Get entries for the given page
    entries_for_page = get_entries_for_page(page)
    # Check if there's another page, returns None if not
    next_page = check_next_page(page)
    prev_page = page - 1
    return render_template('page.html', form=form, \
        entries_for_page=entries_for_page, details=details, \
        page=page, next_page=next_page, prev_page=prev_page)
示例#12
0
def edit_entry(timestamp):
    '''Edit an entry and return a view of the edited entry'''
    entry = get_record('entries', Query().timestamp == timestamp)
    if not entry:
        return abort(404)
    form = EditEntryForm()
    if form.validate_on_submit():
        # Split comma-delimted string of tags into a list
        # Delete spaces at the start of tags if necessary
        tags = form.new_tags.data.split(", ")
        update_record('entries', {'entry': form.new_entry.data, \
            'tags': tags}, (Query().creator_id == 1) & \
            (Query().timestamp == timestamp))
        flash('Entry updated.')
        update_pagination()
        return redirect(url_for('main.view_single_entry', timestamp=timestamp))
    form.new_entry.default = entry['entry']
    form.new_tags.default = ', '.join(entry['tags'])
    form.process()
    details = get_details()
    return render_template('edit_entry.html', form=form, timestamp=timestamp, \
        details=details)
示例#13
0
文件: views.py 项目: kmbn/chronoflask
def confirm_password_reset(token):
    details = get_details()
    if not details:
        return abort(404)
    s = Serializer(current_app.config['SECRET_KEY'])
    try:
        data = s.loads(token)
    except:
        flash('The password reset link is invalid or has expired.')
        return redirect(url_for('auth.request_reset'))
    if not data.get('confirm'):
        flash('The password reset link is invalid or has expired.')
        return redirect(url_for('auth.request_reset'))
    user_id = data.get('confirm')
    form = SetNewPasswordForm()
    if form.validate_on_submit():
        new_password_hash = pwd_context.hash(form.new_password.data)
        get_table('auth').update({'password_hash': new_password_hash}, \
                                 eids=[user_id])
        flash('Password updated—you can now log in.')
        return redirect(url_for('auth.login'))
    return render_template('set_new_password.html', form=form, token=token, \
                           details=details)
示例#14
0
def browse_all_entries():
    '''Returns all entries (most recent entry at the top of the page).'''
    details = get_details()
    if not session.get('logged_in'):
        if details:
            register = False
        else:
            register = True
            details = {'chronofile_name': current_app.config['DEFAULT_NAME'], \
                       'author_name': current_app.config['DEFAULT_AUTHOR']}
        return render_template('welcome.html', details=details, \
                               register=register)
    form = RawEntryForm()
    # Try to validate form and create a new entry
    if form.validate_on_submit():
        return parse_input(form.raw_entry.data, datetime.utcnow())
    # Otherwise, show the latest entries
    page = 1
    # Get entries for the given page
    entries_for_page = get_entries_for_page(page)
    # Check if there's another page, returns None if not
    next_page = check_next_page(page)
    return render_template('home.html', entries_for_page=entries_for_page, \
        form=form, details=details, next_page=next_page)
示例#15
0
def page_not_found(e):
    details = get_details()
    return render_template('404.html', details=details), 404
示例#16
0
def internal_server_error(e):
    details = get_details()
    return render_template('500.html', details=details), 500