Source

rankademy / www / users / user_view.py

from flask import Blueprint
from flask import render_template
from flask import g, url_for, request, jsonify, session, abort, redirect
from flask import make_response
from flasklogin import login_required

import simplejson as json

ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])


user = Blueprint("user", __name__)

ftype = lambda filename: filename.rsplit('.', 1)[1]
def is_allowed_file(filename):
    return '.' in filename and ftype(filename) in ALLOWED_EXTENSIONS

#define JSON messages
def buildJSONresponse(data):
	response = make_response(data)
	response.headers["content-type"] = "application/json";
	return response

def fail_message(msg):
	data = jsonify({"success": False, "msg": msg, "data": {}})
	return buildJSONresponse(data)

def success_message(data):
	data  = jsonify({"success": True, "msg": "OK", "data": data})
	return buildJSONresponse(data)

@user.route("/user", methods = ["GET", "POST"])
def index():
	'''page that will rendered if specific user id's are not given'''
	if request.method == "GET":
		cur = g.db.users.find({}, 
			{"_id", "first_name", "last_name", "specialities"})
		
		#TODO: fix that problem
		#transform object_id to string
		users = []
		for user in cur:
			user['_id'] = str(user["_id"])
			users.append(user)

		print "users: ", len(users)
		return render_template("users.html", 
			users = users)

	elif request.method == "POST":
		response = {"success": True}


		user = g.db.users.User()
		user["_id"] =  user.new_id()

		#build roles
		roles = []
		for rlstr in request.form["roles"]:
			role = g.db.roles.Role()
			role.update({
				"role": rlstr,
				"place": request.form["university"],
				"subject": user["_id"]
			})
			role.save()
			roles.append(role)

        #update user data
		user.update({
					"first_name": request.form["first_name"],
					"last_name": request.form["last_name"],
					"headline": request.form["headline"],
					"interests": set(request.form["interests"].split()),
					"roles": roles
		})

		if request.files.has_key("portrait") and\
			is_allowed_file(request.files["portrait"].filename):
			fp = request.files["portrait"]
			user.portrait = user.to_img_str(fp, ftype(fp.filename))

		print user
		user.save()
		return redirect(url_for('user.index'))

	else:
		abort(404)


@user.route("/user/<string:uid>", methods = ['GET', "POST"])
def user_page(uid):
	''' '''
	user_data = g.db.users.find_one({u"_id": uid}, {u"_id": False})
	if not user_data:
		abort(404)

	return render_template("user.html",
			user = user_data
			)


@user.route("/user/<uid>/status", methods = ['GET', "POST"])
@user.route("/user/<uid>/status/<sid>", methods = ['GET', "POST"])
def user_status(uid, sid = None):
	'''handles user achievement requests and appending new ones'''
	
	if request.method == "GET":
		if sid is None:
			#if wanted all user achievments
			print "returning statuses"
			data = []
			statuses = g.db.statuses.Status.find()
			for status in statuses:
				#replace user info just with uid, user_name, portrait
				user = status.pop("user")
				user_info = {
					"profile": "/user/{0}".format(user["_id"]),
					"name": "{0} {1}".format(user["first_name"], user["last_name"]),
					"portrait": user["portrait"]
				}
				status["user"] = user_info
				data.append(status)

			return success_message(data)
			
		else:
			#return specific achievment
			print "has sid:", sid
			return success_message("test")
		
	elif request.method == "POST":
		#if user wants to add new achievment
		print "I got data."
		data  = json.loads(request.form["body"])
		status = g.db.statuses.Status()
		status["_id"] = status.new_id()
		status["user"] = g.db.users.find_one({"_id": uid})
		try:
			status.update(data)
			status.save()
			print "Data saved."
			return success_message({"_id": status["_id"]})
		except:
			print "Not saved."
			return fail_message("Data was not correct:\n{0}".format(data))
		
	else:
		abort(404)

@user.route("/user/<uid>/bookmarks", methods = ["GET", "POST"])
@login_required
def bookmarks(uid):
	'''return list of given users bookmarks '''
	pass

@user.route("/user/<uid>/status/<sid>/chat", methods = ["GET", "POST"])
@login_required
def status_chats(uid, sid):
	'''handles user chats as one collection'''
	if request.method == "GET":
		status = g.db.statuses.Status.find({"_id": sid})
		return success_message(status["chats"])
	else:
		abort(404)

@user.route("/user/<uid>/status/<sid>/chat/<cid>", methods = ["GET", "POST", "PUT"])
@login_required
def status_chat(uid, sid, cid):
	'''handles only one specific comment - like editing, blocking, answering, linking'''
	if request.method == "GET":
		if cid is None:
			return fail_messsage("Missing comment id.")
		else:
			chat  = g.db.chats.Chat.find({"_id": cid, "user._id": uid})
			return success_message(chat)
	elif request.method == "POST":
		data = request.form["body"]
		new_chat = g.db.chats.Chat()
		new_chat.update(data)
		new_chat.save()

	elif request.method == "PUT":
		#if user wants update specific comment
		if cid is None:
			return fail_message("Missing comment id.")
		else:
			return success_message("OK")
	else:
		abort(404)

@user.route("/user/<uid>/status/<sid>/bookmark/<bid>", methods = ["GET", "POST", "PUT"])
@login_required
def status_bookmark(uid,sid, bid):
	'''handles user bookmark for given status posting, for example bookmarks shared link '''
	if request.method == "GET":
		#just returns true or false, which marks is user bookmarked or not
		if bid is None:
			return fail_message("Missing bookmark id")
		else:
			pass
	elif request.method == "POST":
		#user just bookmarked given posting
		pass
	elif request.method == "PUT":
		#user toggled change of bookmark status
		pass
	else:
		abort(404)

@user.route("/user/<uid>/status/<sid>/subscription/", methods = ["GET", "POST", "PUT"])
@login_required
def status_subscription(uid, sid):
	'''if user wanted to subscribe to event, todo list'''
	if request.method == "GET":
		#to check is person subscribed to event
		pass
	elif request.method == "POST":
		#add new subscription
		pass
	elif request.method == "PUT":
		#to change subscription
		pass
	else:
		abort(404)