Пример #1
0
def post_details(pid, related=None):
    p = db.query("SELECT * FROM posts p join forums as f on f.fid = p.forum_id join users u on u.id = p.user_id where p.pid = %s" , (pid))
    if p.__len__() > 0:
        post = {
            'parent': p[0]['parent'],
            'isApproved': bool(p[0]['approved']),
            'isHighlighted': bool(p[0]['highlighted']),
            'isEdited': bool(p[0]['edited']),
            'isSpam': bool(p[0]['spam']),
            'isDeleted': bool(p[0]['deleted']),
            'date': str(p[0]['date']),
            'thread': p[0]['thread_id'],
            'message': p[0]['message'],
            'id': p[0]['pid'],
            'likes': p[0]['likes'],
            'dislikes': p[0]['dislikes'],
            'points': p[0]['likes'] - p[0]['dislikes']
        }
        if related is not None:
            if 'user' in related:
                post['user'] = user_details(p[0]['user_id'], 'id', p)
            if 'thread' in related:
                post['thread'] = thread_details(post['thread'])
            if 'forum' in related:
                post['forum'] = forum_details(p[0]['forum_id'], 'fid', p)

        if 'user' not in post:
            post['user'] = p[0]['email']
        if 'forum' not in post:
            post['forum'] = p[0]['shortname']
        return post
    return {}
Пример #2
0
def thread_details(thread, related=None, src=None):
    query = "SELECT * from threads "
    query += " JOIN users u ON user_id=u.id"
    query += " JOIN forums f ON forum_id=f.fid"
    query += " WHERE tid=%s"
    thr = db.query(query, thread) if src is None else src
    resp = {}
    if thr.__len__() != 0:
        if related is not None:
            if 'user' in related:
                resp['user'] = user_details(thr[0]['user_id'], 'id', thr)
            if 'forum' in related:
                resp['forum'] = forum_details(thr[0]['forum_id'], 'fid', thr)
        if 'user' not in resp:
            resp['user'] = thr[0]['email']
        if 'forum' not in resp:
            resp['forum'] = thr[0]['shortname']
        resp['date'] = str(thr[0]['date'])
        resp['title'] = thr[0]['title']
        resp['message'] = thr[0]['message']
        resp['dislikes'] = thr[0]['dislikes']
        resp['likes'] = thr[0]['likes']
        resp['points'] = thr[0]['likes'] - thr[0]['dislikes']
        resp['slug'] = thr[0]['slug']
        resp['id'] = thr[0]['tid']
        resp['isClosed'] = bool(thr[0]['closed'])
        resp['isDeleted'] = bool(thr[0]['deleted'])
        resp['posts'] = count_posts(thr[0]['tid'])
    return resp
Пример #3
0
def forum_details(forum, how, src=None):
    details = {}
    res = db.query("SELECT * from forums where %s=%%s" % how, forum) if(src is None) else src
    if res.__len__() > 0:
        details['id'] = res[0]['fid']
        details['short_name'] = res[0]['shortname']
        details['name'] = res[0]['fname']
        details['user'] = email_by_id(res[0]['founder_id'])
    return details
Пример #4
0
def all():
    tasks = db.query('SELECT id, name, description, active FROM tasks')
    all_tasks = []
    for task in tasks:
        if task['active'] == '0':
            active = 'Inativa'
        else:
            active = 'Ativa'
        all_tasks += [(task['id'], task['name'], task['description'], active)]

    return jsonify(tasks=all_tasks)
Пример #5
0
def all():
    tasks = db.query("SELECT id, name, description, active FROM tasks")
    all_tasks = []
    for task in tasks:
        if task["active"] == "0":
            active = "Inativa"
        else:
            active = "Ativa"
        all_tasks += [(task["id"], task["name"], task["description"], active)]

    return jsonify(tasks=all_tasks)
Пример #6
0
def all():
    tasks = db.query('SELECT id, name, description, active FROM tasks')
    all_tasks = []
    for task in tasks:
        if task['active'] == '0':
            active = 'done'
        elif task['active'] == '1':
            active = 'in progress'
        else:
            active = 'not started'
        all_tasks += [(task['id'], task['name'], task['description'], active)]

    return jsonify(tasks=all_tasks)
Пример #7
0
def listing(json, what):
    how = ''
    id = -1
    if 'thread' in json and what in ['post']:
        how = 'thread_id'
        id = json['thread']
    if 'forum' in json and what in ['post', 'thread', 'user']:
        how = 'forum_id'
        id = id_by_sname(json['forum'])
    if 'user' in json and what in ['post', 'thread']:
        how = 'user_id'
        id = id_by_email(json['user'])
    if 'related' in json:
        related = json['related']
    else:
        related = []

    if id < 0 or how == '':
        return []

    query = "SELECT %s %s FROM %s where %s=%%s" % (tables[what][2], tables[what][0], tables[what][1], how)
    params = ()
    params += (id,)
    if 'since' in json:
        query += " AND date >= %s"
        params += (json['since'],)
    if 'since_id' in json:
        query += " AND user_id >= %s"
        params += (json['since_id'],)
    if 'order' in json:
        order = json['order']
    else:
        order = 'desc'
    type_order = 'date' if what is not 'user' else 'user_id'
    query += " ORDER BY %s %s" % (type_order, order)
    if 'limit' in json:
        query += " LIMIT %s" % (json['limit'])
    lst = db.query(query, params)
    result = []
    if lst.__len__() > 0:
        if what == 'user':
            for ids in lst:  # TODO FIX THIS
                thr = entity_handlers[what](ids[tables[what][0]], 'id')
                result.append(thr)
        else:
            for ids in lst:  # for every id get entity info, may be slow...
                thr = entity_handlers[what](ids[tables[what][0]], related)
                result.append(thr)
    return result
Пример #8
0
def user_details(ident, method, src=None):
    query = "SELECT * FROM users where %s=%%s" % method
    if src is not None:
        res = src
    else:
        res = db.query(query, ident)
    user = {}
    if res.__len__() != 0:
        user['id'] = uid = res[0]["id"]
        user["followers"] = get_followers(uid)
        user["following"] = get_following(uid)
        user["subscriptions"] = get_subscriptions(uid)
        user["isAnonymous"] = bool(res[0]["anonymous"])
        user["email"] = res[0]["email"]
        user["username"] = res[0]["username"]
        user["about"] = res[0]["about"]
        user["name"] = res[0]["name"]
    return user
Пример #9
0
def get_followers(id):
    res = db.query("SELECT email FROM followers INNER JOIN users on follower=id where followee=%s and active=1", id)
    result = []
    for followee in res:
        result.append(followee['email'])
    return result
Пример #10
0
def showall():
    tasks = db.query('SELECT id, name, description, active FROM tasks')
    return render_template('task/showall.html', tasks=tasks)
Пример #11
0
def showall():
    tasks = db.query("SELECT id, name, description, active FROM tasks")
    return render_template("task/showall.html", tasks=tasks)
Пример #12
0
def is_exist(what, id):
    thr = db.query("SELECT %s from %s WHERE %s=%%s" % (tables[what][0], tables[what][1], tables[what][0]), id)
    return 1 if (thr.__len__() > 0) else 0
Пример #13
0
def get_subscriptions(id):
    res = db.query("SELECT threads_id FROM subscriptions where users_id=%s AND active=1 order by threads_id desc", id)
    result = []
    for subs in res:
        result.append(subs['threads_id'])
    return result
Пример #14
0
def count_posts(thread):
    thr = db.query("SELECT COUNT(*) as cnt from posts WHERE thread_id=%s", thread)
    return thr[0]['cnt']
Пример #15
0
def email_by_id(id):
    res = db.query("SELECT email FROM users where id=%s", id)
    return res[0]['email']
Пример #16
0
def sname_by_id(id):
    name = db.query("SELECT shortname from forums where fid=%s", id)
    return name[0]['shortname']
Пример #17
0
def id_by_sname(name):
    id = db.query("SELECT fid from forums where shortname=%s", name)
    if id.__len__() > 0:
        return id[0]['fid']
    else:
        return -1
Пример #18
0
def user_by_email(email):
    res = db.query("SELECT id FROM users where email=%s", email)
    if res.__len__() > 0:
        return res[0]['id']
    return -1