Esempio n. 1
0
def unsubscribe():
	requestBody = request.json

	user = requestBody.get('user')
	thread = requestBody.get('thread')
	db.execute("""DELETE FROM Subscription WHERE subscriber = %(subscriber)s AND thread = %(thread)s;""",
		   {'subscriber': user, 'thread': thread}, True)

	resultDict = {'thread': thread, 'user': str_to_json(user)}
	return json.dumps({"code": 0, "response": resultDict}, indent=4)
Esempio n. 2
0
def listFollowers(isFollowing):
	qs = get_json(request)

	email = qs.get('user')
	if not email:
		return json.dumps({"code": 2, "response": "No 'user' key"}, indent=4)

	# Since part
	since_id = qs.get('since_id', -1)
	if since_id != -1:
		sinceSql = """AND User.user >= {}""".format(since_id)
	else:
		sinceSql = ""

	# Order part
	order_sql = """ORDER BY User.name {}""".format(qs.get('order', 'desc'))

	# Limit part
	limit = qs.get('limit', -1)
	if limit != -1:
		try:
			limit = int(limit)
		except ValueError:
			return json.dumps({"code": 3, "response": "Wrong limit value"}, indent=4)
		if limit < 0:
			return json.dumps({"code": 3, "response": "Wrong limit value"}, indent=4)
		limit_sql = """LIMIT {}""".format(limit)
	else:
		limit_sql = ""

	sql = """SELECT about, email, user, isAnonymous, name, username FROM User JOIN Follower ON """
	if not isFollowing:
		sql += """Follower.follower = User.email WHERE Follower.following"""
	else:
		sql += """Follower.following = User.email WHERE Follower.follower"""

	sql += """ = %(email)s {since_value} {order_value} {limit_value};""".format(
		since_value=sinceSql, order_value=order_sql, limit_value=limit_sql)

	userListSql = db.execute(sql, {'email': email})
	if not userListSql:
		return json.dumps({"code": 1, "response": "Empty set"}, indent=4)

	user_list = list()
	for userSql in userListSql:
		followerEmail = str_to_json(userSql[1])
		user_list.append({'about': str_to_json(userSql[0]),
						  'email': followerEmail,
						  'id': str_to_json(userSql[2]),
						  'isAnonymous': str_to_json(userSql[3]),
						  'name': str_to_json(userSql[4]),
						  'username': str_to_json(userSql[5]),
						  'followers': get_followers_list(followerEmail),
						  'following': get_following_list(followerEmail),
						  'subscriptions': get_subscribed_threads_list(followerEmail)})

	return json.dumps({"code": 0, "response": user_list}, indent=4)
Esempio n. 3
0
def listUsers():
	qs = get_json(request)

	if not qs.get('forum'):
		return json.dumps({"code": 2, "response": "No 'forum' key"}, indent=4)

	# Since id part
	since_id = qs.get('since_id')
	if since_id:
		try:
			since_id = int(since_id)
		except ValueError:
			return json.dumps({"code": 3, "response": "Wrong since_id value"}, indent=4)
		since_id_sql = """AND User.user >= {}""".format(since_id)
	else:
		since_id_sql = ''

	# Limit part
	if qs.get('limit'):
		limit = qs.get('limit')[0]
		try:
			limit = int(limit)
		except ValueError:
			return json.dumps({"code": 3, "response": "Wrong limit value"}, indent=4)
		if limit < 0:
			return json.dumps({"code": 3, "response": "Wrong limit value"}, indent=4)
		limit_sql = """LIMIT {}""".format(limit)
	else:
		limit_sql = ''

	# Order part
	order = qs.get('order', 'desc')
	order_sql = """ORDER BY User.name {}""".format(order)

	sql = """SELECT User.user, User.email, User.name, User.username, User.isAnonymous, User.about FROM User \
		WHERE User.email IN (SELECT DISTINCT user FROM Post WHERE forum = %(forum)s) {snc_sql} {ord_sql} \
		{lim_sql};""".format(snc_sql=since_id_sql, lim_sql=limit_sql, ord_sql=order_sql)

	userListSql = db.execute(sql, {'forum': qs.get('forum')})

	userList = list()
	for userSql in userListSql:
		email = str_to_json(userSql[1])
		userList.append({'id': str_to_json(userSql[0]),
						  'email': email,
						  'name': str_to_json(userSql[2]),
						  'username': str_to_json(userSql[3]),
						  'isAnonymous': str_to_json(userSql[4]),
						  'about': str_to_json(userSql[5]),
						  'subscriptions': get_subscribed_threads_list(email)})

	return json.dumps({"code": 0, "response": userList}, indent=4)
Esempio n. 4
0
def listFollowers(isFollowing):
    qs = get_json(request)

    email = qs.get('user')
    if not email:
        return json.dumps({"code": 2, "response": "No 'user' key"}, indent=4)

    # Since part
    since_id = qs.get('since_id', -1)
    if since_id != -1:
        sinceSql = """AND User.user >= {}""".format(since_id)
    else:
        sinceSql = ""

    # Order part
    order_sql = """ORDER BY User.name {}""".format(qs.get('order', 'desc'))

    # Limit part
    limit = qs.get('limit', -1)
    if limit != -1:
        try:
            limit = int(limit)
        except ValueError:
            return json.dumps({
                "code": 3,
                "response": "Wrong limit value"
            },
                              indent=4)
        if limit < 0:
            return json.dumps({
                "code": 3,
                "response": "Wrong limit value"
            },
                              indent=4)
        limit_sql = """LIMIT {}""".format(limit)
    else:
        limit_sql = ""

    sql = """SELECT about, email, user, isAnonymous, name, username FROM User JOIN Follower ON """
    if not isFollowing:
        sql += """Follower.follower = User.email WHERE Follower.following"""
    else:
        sql += """Follower.following = User.email WHERE Follower.follower"""

    sql += """ = %(email)s {since_value} {order_value} {limit_value};""".format(
        since_value=sinceSql, order_value=order_sql, limit_value=limit_sql)

    userListSql = db.execute(sql, {'email': email})
    if not userListSql:
        return json.dumps({"code": 1, "response": "Empty set"}, indent=4)

    user_list = list()
    for userSql in userListSql:
        followerEmail = str_to_json(userSql[1])
        user_list.append({
            'about':
            str_to_json(userSql[0]),
            'email':
            followerEmail,
            'id':
            str_to_json(userSql[2]),
            'isAnonymous':
            str_to_json(userSql[3]),
            'name':
            str_to_json(userSql[4]),
            'username':
            str_to_json(userSql[5]),
            'followers':
            get_followers_list(followerEmail),
            'following':
            get_following_list(followerEmail),
            'subscriptions':
            get_subscribed_threads_list(followerEmail)
        })

    return json.dumps({"code": 0, "response": user_list}, indent=4)
Esempio n. 5
0

@module.route("/subscribe/", methods=["POST"])
def subscribe():
	requestBody = request.json

	user = requestBody.get('user')
	thread = requestBody.get('thread')
	try:
		db.execute("""INSERT INTO Subscription (subscriber, thread) VALUES (%(subscriber)s, %(thread)s);""",
				   {'subscriber': user, 'thread': thread}, True)
	except MySQLdb.IntegrityError, message:
		if message[0] == MYSQL_DUPLICATE_ENTITY_ERROR:
			print "Already subscribed"
	
	resultDict = {'thread': thread, 'user': str_to_json(user)}
	return json.dumps({"code": 0, "response": resultDict}, indent=4)


@module.route("/unsubscribe/", methods=["POST"])
def unsubscribe():
	requestBody = request.json

	user = requestBody.get('user')
	thread = requestBody.get('thread')
	db.execute("""DELETE FROM Subscription WHERE subscriber = %(subscriber)s AND thread = %(thread)s;""",
		   {'subscriber': user, 'thread': thread}, True)

	resultDict = {'thread': thread, 'user': str_to_json(user)}
	return json.dumps({"code": 0, "response": resultDict}, indent=4)
Esempio n. 6
0
def listUsers():
    qs = get_json(request)

    if not qs.get('forum'):
        return json.dumps({"code": 2, "response": "No 'forum' key"}, indent=4)

    # Since id part
    since_id = qs.get('since_id')
    if since_id:
        try:
            since_id = int(since_id)
        except ValueError:
            return json.dumps({
                "code": 3,
                "response": "Wrong since_id value"
            },
                              indent=4)
        since_id_sql = """AND User.user >= {}""".format(since_id)
    else:
        since_id_sql = ''

    # Limit part
    if qs.get('limit'):
        limit = qs.get('limit')[0]
        try:
            limit = int(limit)
        except ValueError:
            return json.dumps({
                "code": 3,
                "response": "Wrong limit value"
            },
                              indent=4)
        if limit < 0:
            return json.dumps({
                "code": 3,
                "response": "Wrong limit value"
            },
                              indent=4)
        limit_sql = """LIMIT {}""".format(limit)
    else:
        limit_sql = ''

    # Order part
    order = qs.get('order', 'desc')
    order_sql = """ORDER BY User.name {}""".format(order)

    sql = """SELECT User.user, User.email, User.name, User.username, User.isAnonymous, User.about FROM User \
		WHERE User.email IN (SELECT DISTINCT user FROM Post WHERE forum = %(forum)s) {snc_sql} {ord_sql} \
		{lim_sql};""".format(snc_sql=since_id_sql,
                       lim_sql=limit_sql,
                       ord_sql=order_sql)

    userListSql = db.execute(sql, {'forum': qs.get('forum')})

    userList = list()
    for userSql in userListSql:
        email = str_to_json(userSql[1])
        userList.append({
            'id': str_to_json(userSql[0]),
            'email': email,
            'name': str_to_json(userSql[2]),
            'username': str_to_json(userSql[3]),
            'isAnonymous': str_to_json(userSql[4]),
            'about': str_to_json(userSql[5]),
            'subscriptions': get_subscribed_threads_list(email)
        })

    return json.dumps({"code": 0, "response": userList}, indent=4)