Example #1
0
def delete(id):

    db = get_db()
    db.execute('DELETE FROM video WHERE id=?', (id, ))
    db.commit()

    return redirect(url_for('video.index', page=0))
Example #2
0
def edit(id):

    db = get_db()

    if request.method == 'POST':
        topic = request.form['topic']
        body = request.form['body']
        link = request.form['link']

        error = None
        if not topic or not body or not link:
            error = "All information are required"

        if not error:
            db.execute('UPDATE video SET topic=?, body=?, link=? WHERE id=?',
                       (topic, body, link, id))
            db.commit()

            return redirect(url_for('video.index', page=0))

        flash(error)

    data = db.execute('SELECT * FROM video WHERE id=?', (id, )).fetchone()

    return render_template('video/form.html', data=data)
Example #3
0
def unfollow(id):
    db = get_db()
    db.execute('DELETE FROM follow WHERE follower_id=? AND followed_id=?',
               (g.user['id'], id))
    db.commit()

    return redirect(url_for('profile.index', id=id))
Example #4
0
def create_log(tag):
    db = get_db()
    if g.user:
        db.execute('INSERT INTO log(user_id,tag) VALUES (?,?)',(g.user['id'],tag))
    else:
        db.execute('INSERT INTO log(tag) VALUES (?)',(tag,))
    db.commit()
Example #5
0
def edit_reply(id):

    db = get_db()
    
    if request.method == 'POST':

        body = request.form['body']

        error = None
        if not body:
            error = "Body is required"

        if not error:
            db.execute('UPDATE reply SET body=? WHERE id=?',(body,id))
            db.commit()

            post_id = db.execute('SELECT post_id FROM reply WHERE id=?',(id,)).fetchone()['post_id']

            return redirect(url_for('forum.post',id=post_id))

        flash(error)

    data = db.execute('SELECT * FROM reply WHERE id=?',(id,)).fetchone()

    return render_template('forum/form.html',isPost=False,data=data)
Example #6
0
def index(page):
    db = get_db()

    video_per_page = 3

    count = db.execute('SELECT COUNT(*) AS num FROM video').fetchone()
    count = int(count['num'])

    page = int(page)

    back = False
    if page > 0:
        back = True

    next = False
    if count / video_per_page > page + 1:
        next = True

    videos = db.execute(
        'SELECT * FROM video ORDER BY created DESC LIMIT ? OFFSET ?',
        (video_per_page, page * video_per_page)).fetchall()

    return render_template('video/index.html',
                           videos=videos,
                           back=back,
                           next=next,
                           now=page)
Example #7
0
def page(title):

    db = get_db()
    articles = db.execute('SELECT * FROM article WHERE title=? ORDER BY turn ASC',(title,)).fetchall()
    

    return render_template('article/article.html',articles=articles)
Example #8
0
def graph(tag):
    
    db = get_db()
    
    tags = db.execute('SELECT DISTINCT tag FROM log ORDER BY tag ASC').fetchall()
 
    sameday = db.execute("SELECT strftime('%Y-%m-%d', created) AS day, COUNT(*) AS count"
                         " FROM log"
                         " WHERE tag = ? AND created > (SELECT DATETIME('now', '-6 day'))"
                         " GROUP BY day"
                         " ORDER BY day DESC",(tag,)               
    ).fetchall()


    today = date.today()
    datas = [0, 0, 0, 0, 0, 0, 0]

    for sd in sameday:
        sd_date = datetime.strptime(sd['day'], '%Y-%m-%d').date()
        between = (today-sd_date).days
        index = 6 - between
        datas[index] = int(sd['count'])


    return render_template('log.html',datas=datas,title=tag,tags=tags)
Example #9
0
def register():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        repassword = request.form['repassword']

        db = get_db()
        error = None

        if not username:
            error = 'Username is required.'
        elif not password:
            error = 'Password is required.'
        elif password != repassword:
            error = 'Password do not match'
        elif db.execute('SELECT id FROM user WHERE username = ?',
                        (username, )).fetchone() is not None:
            error = 'User {} is already registered.'.format(username)

        if error is None:
            db.execute('INSERT INTO user (username, password) VALUES (?, ?)',
                       (username, generate_password_hash(password)))
            db.commit()

            create_log("register")
            return redirect(url_for('auth.login'))

        flash(error)

    return render_template('auth/register.html')
Example #10
0
def delete_article(title, id):
    db = get_db()

    count = len(
        db.execute('SELECT id FROM article WHERE title=?',
                   (title, )).fetchall())

    if count > 1:
        turn = int(
            db.execute('SELECT turn FROM article WHERE id=?',
                       (id, )).fetchone()['turn'])

        db.execute('DELETE FROM article WHERE title = ? AND id = ?',
                   (title, id))

        db.execute(
            'UPDATE article SET turn = turn-1 WHERE title = ? AND turn > ?',
            (title, turn))

        db.commit()

        id = db.execute('Select id FROM article WHERE title=? AND turn=0',
                        (title, )).fetchone()

        create_log("delete article")
        return redirect(url_for('admin.edit_article', title=title,
                                id=id['id']))

    return "At least one article"
Example #11
0
def edit_post(id):

    db = get_db()
    
    if request.method == 'POST':
        topic = request.form['topic']
        body = request.form['body']

        error = None

        if not topic:
            error = "Topic is required"
        elif not body:
            error = "Body is required"

        if not error:
            db.execute('UPDATE post SET topic=?, body=? WHERE id=?',(topic,body,id,))
            db.commit()
            return redirect(url_for('forum.post',id=id))

        flash(error)

    data = db.execute('SELECT * FROM post WHERE id=?',(id,)).fetchone()

    return render_template('forum/form.html',isPost=True,data=data)
Example #12
0
def load_logged_in_user():
    user_id = session.get('user_id')

    if user_id is None:
        g.user = None
    else:
        g.user = get_db().execute('SELECT * FROM user WHERE id = ?',
                                  (user_id, )).fetchone()
Example #13
0
def index():
    db = get_db()
    posts = db.execute('SELECT post.id, user.username, created, topic, body'
                       ' FROM post' 
                       ' LEFT JOIN user ON post.user_id = user.id'
                       ' ORDER BY created DESC').fetchall()


    return render_template('forum/index.html',posts=posts)
Example #14
0
def follow(id):

    if id != g.user['id']:
        db = get_db()
        db.execute('INSERT INTO follow (follower_id,followed_id) VALUES (?,?)',
                   (g.user['id'], id))
        db.commit()

    return redirect(url_for('profile.index', id=id))
Example #15
0
def delete_page(title):

    if title != 'home':
        db = get_db()

        db.execute("DELETE FROM article WHERE title=?", (title, ))
        db.execute("DELETE FROM book WHERE title=?", (title, ))

        db.commit()

        create_log("delete page")
        return redirect(url_for('admin.edit_page'))

    return "You cannot delete home page"
Example #16
0
def login():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        remember = request.form.getlist('remember')

        db = get_db()
        error = None
        user = db.execute('SELECT * FROM user WHERE username = ?',
                          (username, )).fetchone()

        if user is None:
            error = 'Incorrect username.'
        elif not check_password_hash(user['password'], password):
            error = 'Incorrect password.'

        if error is None:
            session.clear()
            session['user_id'] = user['id']

            create_log("login")
            resp = make_response(redirect(url_for('index')))
            if not remember:
                resp.set_cookie(key='username', value='', expires=0)
                resp.set_cookie(key='password', value='', expires=0)
                return resp
            else:
                resp.set_cookie(key='username',
                                value=username,
                                expires=time.time() + 7 * 60 * 60 * 24)
                resp.set_cookie(key='password',
                                value=password,
                                expires=time.time() + 7 * 60 * 60 * 24)
                return resp

            return redirect(url_for('index'))

        flash(error)

    if request.method == 'GET':
        username = request.cookies.get('username')
        password = request.cookies.get('password')

        if username is not None and password is not None:
            return render_template('auth/login.html',
                                   username=username,
                                   password=password)

    return render_template('auth/login.html')
Example #17
0
def post(id):
    
    db = get_db()

    post = db.execute('SELECT post.id, post.user_id, user.username, created, topic, body'
                      ' FROM post' 
                      ' LEFT JOIN user ON post.user_id = user.id'
                      ' WHERE post.id = ?',(id,)).fetchone()

    replys = db.execute('SELECT reply.id, reply.user_id, user.username, created, body'
                      ' FROM reply' 
                      ' LEFT JOIN user ON reply.user_id = user.id'
                      ' WHERE reply.post_id = ?'
                      ' ORDER BY created ASC',(id,)).fetchall()

    return render_template('forum/post.html',post=post,replys=replys)
Example #18
0
def new_article(title):
    db = get_db()

    count = len(
        db.execute('SELECT id FROM article WHERE title = ?',
                   (title, )).fetchall())

    db.execute(
        "INSERT INTO article (title,topic,body,turn) VALUES (?,'New Article','Some Text',?)",
        (title, count))
    db.commit()

    id = db.execute('Select id FROM article WHERE title=? AND turn=?',
                    (title, count)).fetchone()

    create_log("new article")
    return redirect(url_for('admin.edit_article', title=title, id=id['id']))
Example #19
0
def new_reply(id):

    if request.method == 'POST':
        body = request.form['body']

        error = None
        if not body:
            error = "Body is required"

        if not error:
            db = get_db()
            db.execute('INSERT INTO reply (user_id, post_id, body) VALUES (?,?,?)',(g.user['id'],id,body))
            db.commit()

            create_log("reply")
            return redirect(url_for('forum.post',id=id))
        
        flash(error)

    return render_template('forum/form.html',isPost=False)
Example #20
0
def index(id):

    db = get_db()

    user = db.execute('SELECT * FROM user WHERE id=?', (id, )).fetchone()

    follow_list = db.execute(
        'SELECT * FROM follow WHERE follower_id=? AND followed_id=?',
        (g.user['id'], id)).fetchone()

    isfollow = False
    if follow_list:
        isfollow = True

    my_posts = db.execute(
        'SELECT * FROM post WHERE user_id=? ORDER BY created DESC LIMIT 5',
        (id, )).fetchall()
    my_replys = db.execute(
        'SELECT * FROM reply WHERE user_id=? ORDER BY created DESC LIMIT 5',
        (id, )).fetchall()

    follower_posts = db.execute(
        'SELECT post.id AS id, post.topic AS topic, post.created AS created'
        ' FROM post, follow'
        ' WHERE follow.follower_id = ? AND follow.followed_id = post.user_id'
        ' ORDER BY post.created DESC LIMIT 5', (id, )).fetchall()

    follower_replys = db.execute(
        'SELECT reply.post_id AS post_id, reply.body AS body, reply.created AS created'
        ' FROM reply, follow'
        ' WHERE follow.follower_id = ? AND follow.followed_id = reply.user_id'
        ' ORDER BY reply.created DESC LIMIT 5', (id, )).fetchall()

    return render_template('profile/index.html',
                           isfollow=isfollow,
                           user=user,
                           my_posts=my_posts,
                           my_replys=my_replys,
                           follower_posts=follower_posts,
                           follower_replys=follower_replys)
Example #21
0
def edit_page():

    db = get_db()

    if request.method == 'POST':
        domain = request.form['domain']

        error = None
        if not domain:
            error = 'Domain is required.'
        elif db.execute('SELECT * FROM book WHERE title = ?',
                        (domain, )).fetchone() is not None:
            error = 'Domain {} is already registered.'.format(domain)

        if not error:
            db.execute('INSERT INTO book (title,author_id) VALUES (?,?)',
                       (domain, g.user['id']))
            db.execute(
                "INSERT INTO article (title,topic,body,turn) VALUES (?,'New Article','Some Text',0)",
                (domain, ))
            db.commit()

            id = db.execute('Select id FROM article WHERE title=? AND turn=0',
                            (domain, )).fetchone()

            create_log("new page")
            create_log("new article")
            return redirect(
                url_for('admin.edit_article', title=domain, id=id['id']))

        flash(error)

    books = db.execute(
        'SELECT b.title AS title, a.topic AS topic, created, username, a.body AS body, a.id AS id'
        ' FROM book b'
        ' LEFT JOIN user u ON b.author_id = u.id'
        ' LEFT JOIN article a ON (b.title = a.title) AND (a.turn = 0)'
        ' ORDER BY created DESC').fetchall()

    return render_template('admin/editpage.html', books=books)
Example #22
0
def graph(tag):
    
    db = get_db()
    
    tags = db.execute('SELECT DISTINCT tag FROM log ORDER BY tag ASC').fetchall()
 
    sameday = db.execute("SELECT strftime('%d', created) AS day, COUNT(*) AS count"
                         " FROM log"
                         " WHERE tag = ? AND created > (SELECT DATETIME('now', '-7 day'))"
                         " GROUP BY day"
                         " ORDER BY day ASC",(tag,)
    ).fetchall()

    today = int(date.today().strftime('%d'))

    datas = [0, 0, 0, 0, 0, 0, 0]
    for d in sameday:
        dday = int(d['day'])
        theday = 6 - (today - dday)
        datas[theday] += int(d['count'])

    return render_template('log.html',datas=datas,title=tag,tags=tags)
Example #23
0
def upload():

    if request.method == 'POST':
        topic = request.form['topic']
        body = request.form['body']
        link = request.form['link']

        error = None
        if not topic or not body or not link:
            error = "All information are required"

        if not error:
            db = get_db()
            db.execute('INSERT INTO video (topic,body,link) VALUES (?,?,?)',
                       (topic, body, link))
            db.commit()

            return redirect(url_for('video.index', page=0))

        flash(error)

    return render_template('video/form.html')
Example #24
0
def new_post():

    if request.method == 'POST':
        topic = request.form['topic']
        body = request.form['body']

        error = None

        if not topic:
            error = "Topic is required"
        elif not body:
            error = "Body is required"

        if not error:
            db = get_db()
            db.execute('INSERT INTO post (user_id, topic, body) VALUES (?,?,?)',(g.user['id'],topic,body))
            db.commit()

            create_log("post")
            return redirect(url_for('forum.index'))
        
        flash(error)

    return render_template('forum/form.html',isPost=True)
Example #25
0
def edit_article(title, id):

    db = get_db()

    topics = db.execute(
        'SELECT topic,id,turn'
        ' FROM article'
        ' WHERE title = ?'
        ' ORDER BY turn ASC', (title, )).fetchall()

    if request.method == 'POST':
        error = None

        f_topic = request.form['header']
        f_sequence = int(request.form['sequence'])
        f_body = request.form['body']
        f_button = request.form['button']
        f_link = request.form['link']
        f_image = request.form['image']

        if not f_topic:
            error = 'Topic is required.'
        elif not f_sequence and f_sequence != 0:
            error = 'Sequence is required.'
        elif not f_body:
            error = 'Body is required.'
        elif bool(f_button) != bool(f_link):
            error = 'Button Name and Link both required.'

        if error is None:
            biggest_turn = len(topics) - 1
            f_sequence = max(f_sequence, 0)
            f_sequence = min(f_sequence, biggest_turn)

            oturn = db.execute('SELECT turn FROM article WHERE id=?',
                               (id, )).fetchone()['turn']
            if oturn > f_sequence:
                db.execute(
                    'UPDATE article SET turn = turn+1 WHERE title=? AND turn>=? AND turn<?',
                    (title, f_sequence, oturn))
            elif oturn < f_sequence:
                db.execute(
                    'UPDATE article SET turn = turn-1 WHERE title=? AND turn>? AND turn<=?',
                    (title, oturn, f_sequence))

            db.execute(
                'UPDATE article'
                ' SET topic = ?,'
                ' body = ?,'
                ' button = ?,'
                ' link = ?,'
                ' turn = ?,'
                ' image = ?'
                ' WHERE id = ?',
                (f_topic, f_body, f_button, f_link, f_sequence, f_image, id))
            db.commit()

            create_log("edit article")
            return redirect(url_for('admin.edit_article', title=title, id=id))

        flash(error)

    images = os.listdir(os.path.join(bp.static_folder, 'icon'))

    article = db.execute('SELECT * FROM article WHERE id=?', (id, )).fetchone()

    domains = db.execute('SELECT title'
                         ' FROM book'
                         ' ORDER BY created DESC').fetchall()

    return render_template('admin/editarticle.html',
                           topics=topics,
                           domains=domains,
                           article=article,
                           images=images)