Esempio n. 1
0
def change_password():
    old_pass = request.form['old_pass']
    new_pass = request.form['new_pass']
    u = current_user()
    if u.password == u.salted_password(old_pass):
        # u.password = u.salted_password(new_pass)
        # u.save()
        password = u.salted_password(new_pass)
        User.update(u.id, password=password)
        msg = '密码更新成功'
    else:
        msg = '密码更新失败'
    return render_template('settings.html', user=u, msg=msg)
Esempio n. 2
0
def current_user():
    # 从 session 中找到 user_id 字段, 找不到就 -1
    # 然后 User.find_by 来用 id 找用户
    # 找不到就返回 None
    if 'user_id' in session:
        uid = int(session['user_id'])
        e = User.exist(id=uid)
        # e = User.find_by(id=uid)
        if e:
            # return e
            return User.one(id=uid)
        else:
            return None
    else:
        return None
Esempio n. 3
0
def user_detail(id):
    # u = User.find_by(id=id)
    u = User.one(id=id)
    if u is None:
        abort(404)
    else:
        return render_template('profile.html', user=u)
Esempio n. 4
0
def image_add():
    file = request.files['avatar']

    # ../../root/.ssh/authorized_keys
    # filename = secure_filename(file.filename)
    suffix = file.filename.split('.')[-1]
    filename = '{}.{}'.format(str(uuid.uuid4()), suffix)
    path = os.path.join('images', filename)
    file.save(path)

    u = current_user()
    u.image = '/images/{}'.format(filename)
    # u.save()
    User.update(u.id, image=u.image)

    return redirect(url_for('.profile'))
Esempio n. 5
0
def current_user():
    # 从 session 中找到 user_id 字段, 找不到就 -1
    # 然后 User.find_by 来用 id 找用户
    # 找不到就返回 None
    uid = session.get('user_id', -1)
    # u = User.find_by(id=uid)
    u = User.one(id=uid)
    return u
Esempio n. 6
0
    def add_mail(self):
        receivers = []
        content = ''
        for tag in self.content.split(' '):
            if tag.startswith('@'):
                receivers.append(tag[1:])
            else:
                content += tag

        for r in receivers:
            if User.exist(username=r):
                receiver = User.one(username=r)
                form = dict(title='你被{} @ 了'.format(self.user().username),
                            content='<a href="/topic/{}">{} </a>查看'.format(
                                self.topic_id, content),
                            sender_id=self.user_id,
                            receiver_id=receiver.id)
                Mail.new(**form)
Esempio n. 7
0
def users(count=5):
    fake = Faker()
    i = 0
    while i < count:
        u = User.new(username=fake.user_name(), password='******')
        User.session.add(u)
        try:
            User.session.commit()
            i += 1
        except IntegrityError:
            User.session.rollback()
Esempio n. 8
0
def login():
    form = request.form
    u = User.validate_login(form)
    if u is None:
        # 转到 topic.index 页面
        log('登录失败')
        return redirect(url_for('topic.index'))
    else:
        log('登录成功')
        # session 中写入 user_id
        session['user_id'] = u.id
        # 设置 cookie 有效期为 永久
        session.permanent = True
        return redirect(url_for('topic.index'))
Esempio n. 9
0
def user_profile(username):
    # u = User.find_by(username=username)
    u = User.one(username=username)
    if u is None:
        abort(404)
    else:
        # topics = Topic.find_all(user_id=u.id)
        topics = Topic.all(user_id=u.id)
        reply_topic = Topic.user_participated(u)
        return render_template('user.html',
                               user=u,
                               reply_topic=reply_topic,
                               topics=topics,
                               hots=Topic.hots(),
                               u=current_user())
Esempio n. 10
0
 def user(self):
     u = User.one(id=self.user_id)
     return u
Esempio n. 11
0
 def user(self):
     u = User.find_by(id=self.user_id)
     return u
Esempio n. 12
0
def register():
    # form = request.args
    form = request.form.to_dict()
    # 用类函数来判断
    u = User.register(form)
    return redirect(url_for('.index'))
Esempio n. 13
0
def change_info():
    form = request.form.to_dict()
    u = current_user()
    # u.update(form)
    User.update(u.id, **form)
    return redirect(url_for('.settings'))
Esempio n. 14
0
 def receiver(self):
     if User.exist(id=self.receiver_id):
         u = User.one(id=self.receiver_id)
         return u.username
     return 'Anoymous'
Esempio n. 15
0
 def sender(self):
     if User.exist(id=self.sender_id):
         u = User.one(id=self.sender_id)
         return u.username
     return 'Anoymous'