Пример #1
0
def new_user(l, p):
    c = get_db()
    count = \
        c.execute('select count(*) from users where username=(?)',
                  (l,)).fetchone()[
            0]
    if count != 0:
        return False
    c.execute("insert into users (username, password) VALUES (?, ?)", (l, p))
    c.commit()
    return True
Пример #2
0
def user():
    c = salmon.get_db()
    res = []
    comments = c.execute(
        "SELECT msg, photo, time FROM comments "
        "WHERE username=(?)", (current_user.username, )).fetchall()
    for e in comments:
        res.append((e[0], e[1], misc.timestamp_to_str(e[2])))
    return render_template('user.html',
                           seed=misc.generate_number_nonce(),
                           page_title="Профайл " + current_user.username,
                           comments=res)
Пример #3
0
def get_comments(ph):
    c = salmon.get_db().cursor()
    res = c.execute(
        "SELECT msg, time, id, username FROM comments "
        "WHERE photo=(?) ORDER BY TIME DESC", (ph, )).fetchall()
    for i in range(len(res)):
        res[i] = {
            'time': misc.timestamp_to_str(res[i][1]),
            'msg': str(res[i][0]),
            'id': str(res[i][2]),
            'username': res[i][3]
        }
    return res
Пример #4
0
def record_visit():
    c = salmon.get_db()
    ip = get_ip()
    visit_time = c.execute(
        "SELECT TIME FROM VISITS WHERE ip=(?) ORDER BY time DESC LIMIT 1",
        (ip, )).fetchone()
    now = misc.get_time()
    if visit_time is not None:
        visit_time = int(visit_time[0])
        if now - visit_time > THIRTY_MINUTES:
            c.execute("INSERT INTO VISITS (ip, time) "
                      "VALUES (?, ?)", (ip, now))
    else:
        c.execute("INSERT INTO VISITS (ip, time) " "VALUES (?, ?)", (ip, now))
    c.commit()
Пример #5
0
def get_views_data():
    c = salmon.get_db()
    raw = c.execute('SELECT * FROM views ORDER BY time DESC').fetchall()
    res = []
    for s in raw:
        res.append({
            'ip': s[1],
            'cookie': s[2],
            'path': s[3],
            'useragent': s[4],
            'simple_ug': s[5],
            'viewport': s[6],
            'time': misc.timestamp_to_str(s[7]),
            'username': s[8]
        })
    return res
Пример #6
0
def record_hit(cookie):
    c = salmon.get_db()
    ip = get_ip()
    path = request.path
    visit_time = misc.get_time()
    user_agent = request.headers.get('User-Agent')
    vp = ''
    username = current_user.username if current_user.is_authenticated else ''
    if 'res' in request.cookies:
        vp = request.cookies['res']
    c.execute(
        "INSERT INTO views (ip, path, useragent, "
        "time, cookie, viewport, simplified_ug, username) "
        "VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
        (ip, path, user_agent, visit_time, cookie, vp,
         misc.simplify_ug(user_agent), username))
    c.commit()
Пример #7
0
def build_xml():
    top = Element('gallery')
    c = salmon.get_db()
    for p in get_photos():
        likes = c.execute('SELECT count(*) FROM likes WHERE photo=(?)',
                          (p, )).fetchone()[0]
        photo_xml = SubElement(top, 'photo', {'name': p, 'likes': str(likes)})
        res = c.execute(
            'SELECT msg, username, time FROM comments '
            'WHERE photo=(?)', (p, )).fetchall()

        for r in res:
            comment = SubElement(
                photo_xml, 'comment', {
                    'user': r[1] if r[1] != '' else 'anon',
                    'time': timestamp_to_str(r[2])
                })
            comment.text = r[0]
    f = BytesIO()
    f.write(prettify(top).encode(encoding='utf-8'))
    return f
Пример #8
0
def like():
    c = salmon.get_db()
    form = LikeForm(request.form)
    if form.validate_on_submit():
        user_ = get_user()
        if user_ != '':
            count = c.execute(
                "SELECT count(*) FROM likes "
                "WHERE photo=(?) AND username=(?)",
                (form.photo.data, user_)).fetchone()[0]
            if count:
                c.execute(
                    "DELETE FROM  likes "
                    "WHERE photo=(?) AND username=(?)",
                    (form.photo.data, user_))
            else:
                c.execute("INSERT INTO likes (photo, username) VALUES (?, ?)",
                          (form.photo.data, user_))
    c.commit()
    return str(
        c.execute('SELECT count(*) FROM likes WHERE '
                  'photo=(?)', (form.photo.data, )).fetchone()[0])
Пример #9
0
def create_comment():
    form = CommentForm(request.form)
    if form.validate_on_submit():
        if not misc.is_photo(form.photo.data):
            msg = "Параметр имени фотографии указан неправильно!"
            return comments_template(form.photo.data, msg=msg)
        if not check_useragent():
            return ''
        user_ = get_user()
        c = salmon.get_db()
        clear_msg = clean_string(form.msg.data)
        if form.msg_id.data == '-1':  # new comment
            c.execute(
                "INSERT INTO comments (username, msg, version, time, photo) "
                "VALUES (?, ?, ?, ?, ?)",
                (user_, clear_msg, 1, misc.get_time(), form.photo.data))
        else:
            res = c.execute(
                "SELECT count(*) FROM comments WHERE id=(?) AND username=(?)",
                (form.msg_id.data, user_)).fetchone()[0]
            if res != 0:
                c.execute('UPDATE comments SET msg=(?) WHERE id=(?) ',
                          (clear_msg, form.msg_id.data))
            else:
                return comments_template(form.photo.data,
                                         msg='этот комментарий принадлежит '
                                         'не вам или не существует!')
        c.commit()
    else:
        msg = ''
        if form.errors:
            for errors in form.errors.values():
                for error in errors:
                    msg += error
        print(msg)
        return comments_template(form.photo.data, msg=msg)

    comments = get_comments(form.photo.data)
    return render_template('commentanderror.html', comments=comments)
Пример #10
0
 def is_valid(self, pswd):
     c = get_db()
     return c.execute(
         'select count(*) from users where username=(?) '
         'and password=(?)', (self.username, pswd)).fetchone()[0] == 1
Пример #11
0
def likes(file):
    c = salmon.get_db()
    return str(
        c.execute('SELECT count(*) FROM likes WHERE '
                  'photo=(?)', (file, )).fetchone()[0])
Пример #12
0
def get_last_visit_time():
    c = salmon.get_db()
    res = c.execute("SELECT TIME FROM visits ORDER BY time DESC LIMIT 1")
    return misc.timestamp_to_str(res.fetchone()[0])
Пример #13
0
def get_today_visits_count():
    c = salmon.get_db().cursor()
    res = c.execute("SELECT count(*) FROM visits WHERE time > (?)",
                    (misc.today_timestamp(), ))
    return res.fetchone()[0]
Пример #14
0
def get_visit_count():
    c = salmon.get_db().cursor()
    res = c.execute('SELECT count(*) FROM visits')
    return res.fetchone()[0]