예제 #1
0
def chat_api(request):
    if request.method == 'POST':
        d = json.loads(request.body)
        msg = d.get('msg')
        user = request.user.username
        gravatar = request.user.profile.gravatar_url
        m = Message(user=user, message=msg, gravatar=gravatar)
        m.save()

        res = {
            'id': m.id,
            'msg': m.message,
            'user': m.user,
            'time': m.time.strftime('%I:%M:%S %p').lstrip('0'),
            'gravatar': m.gravatar
        }
        data = json.dumps(res)
        return HttpResponse(data, content_type="application/json")

    # get request
    r = Message.objects.order_by('-time')[:20]
    res = []
    for msgs in reversed(r):
        res.append({
            'id': msgs.id,
            'user': msgs.user,
            'msg': msgs.message,
            'time': msgs.time.strftime('%I:%M:%S %p').lstrip('0'),
            'gravatar': msgs.gravatar
        })

    data = json.dumps(res)

    return HttpResponse(data, content_type="application/json")
예제 #2
0
def chat_api(request):
	if request.method == 'POST':
		d = json.loads(request.body)
		msg =  d.get('msg')
		user = request.user.username
		gravatar = request.user.profile.gravatar_url
		m = Message(user=user,message=msg,gravatar=gravatar)
		m.save()


		res = {'id':m.id,'msg':m.message,'user':m.user,'time':m.time.strftime('%I:%M:%S %p').lstrip('0'),'gravatar':m.gravatar}
		data = json.dumps(res)
		return HttpResponse(data,content_type="application/json")


	# get request
	r = Message.objects.order_by('-time')[:70]
	res = []
	for msgs in reversed(r) :
		res.append({'id':msgs.id,'user':msgs.user,'msg':msgs.message,'time':msgs.time.strftime('%I:%M:%S %p').lstrip('0'),'gravatar':msgs.gravatar})
	
	data = json.dumps(res)

	
	return HttpResponse(data,content_type="application/json")
예제 #3
0
파일: views.py 프로젝트: zmikaya/behavecon
def chat_api(request):
	if request.method == 'POST':
		d = json.loads(request.body)
		msg =  d.get('msg')
		# user = request.user.username
		user = request.session["chat_user"]
		room = request.session["chat_room"]
		page = request.session["chat_page"]
		try:
			gravatar = request.user.profile.gravatar_url
		except:
			gravatar = "https://www.gravatar.com/avatar/d41d8cd98f00b204e9800998ecf8427e?d=identicon"
	
		m = Message(user=user, room=room, page=page, message=msg,gravatar=gravatar)
		m.save()

		res = {'id':m.id,'msg':m.message,'user':m.user,'time':m.time.strftime('%I:%M:%S %p').lstrip('0'),'gravatar':m.gravatar}
		data = json.dumps(res)
		return HttpResponse(data,content_type="application/json")


	# get request
	# r = Message.objects.order_by('-time')[:20]
	room = request.session["chat_room"]
	page = request.session["chat_page"]
	r = Message.objects.filter(room=room, page=page)
	res = []
	for msgs in reversed(r) :
		res.append({'id':msgs.id,'user':msgs.user,'msg':msgs.message,'time':msgs.time.strftime('%I:%M:%S %p').lstrip('0'),'gravatar':msgs.gravatar})
	
	data = json.dumps(res)

	
	return HttpResponse(data,content_type="application/json")
def chat_api(request):
	if request.method == 'POST':
		###Oauth system:userid for username
		auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
		auth.set_access_token(request.session.get('key'), request.session.get('secret'))
		api = tweepy.API(auth_handler=auth)
		##makes json
		d = json.loads(request.body)
		msg =  d.get('msg')
		if len(msg) > 120:
			print "error to much word"
			return 0
		u = api.get_user(request.user.username)
		user= u.screen_name
		gravatar = request.user.profile.gravatar_url
		m = Message(user=user,message=msg,gravatar=gravatar)
		m.save()
		res = {'id':m.id,'msg':m.message,'user':m.user,'time':m.time.strftime('%I:%M:%S %p').lstrip('0'),'gravatar':m.gravatar}
		data = json.dumps(res)
		try:
			auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
			auth.set_access_token(request.session.get('key'), request.session.get('secret'))
			api = tweepy.API(auth_handler=auth)
			""" test get tweet"""
			api.update_status(status=m.message+" "+"#test")
			#count=count+1
		except tweepy.TweepError,e:
			p = re.compile('^\[{u\'message\': u\'(.+)\', u\'code\': (\d+)}\]$')
			m = p.match(e.reason)
			if m:
				message =  m.group(1)
				code = m.group(2)
		return HttpResponse(data,content_type="application/json")
예제 #5
0
def post_messages(request):
    """ post messege """
    if request.method == 'POST':
        msg_json = json.loads(request.body)
        msg = msg_json.get('msg')
        userID = request.user.profile.u_id
        message = Message(user_ID=userID, message=msg)
        message.save()


        res = {
            'id':message.id,
            'msg':message.message,
            'user':request.user.profile.username,
            'time':message.time.strftime('%I:%M:%S %p').lstrip('0'),
            'gravatar':request.user.profile.gravatar_url
        }
        data = json.dumps(res)
        return HttpResponse(data,content_type="application/json")
예제 #6
0
def chat_api(request):
    """ chat api """
    if request.method == 'POST':


        msg_json = json.loads(request.body)
        msg = escape(msg_json.get('msg'))  #escape html
        userID = request.user.profile.u_id
        message = Message(user_ID=userID, message=msg)
        message.save()


        res = {
            'id':message.id,
            'msg':message.message,
            'user':request.user.profile.username,
            'time':message.time.strftime('%I:%M:%S %p').lstrip('0'),
            'gravatar':request.user.profile.gravatar_url
        }
        data = json.dumps(res)
        return HttpResponse(data, content_type="application/json")



    # get request
    r = Message.objects.order_by('-time')[:20]
    res = []
    for msgs in reversed(r) :

        chat_user = ChatUser.objects.get(u_id=msgs.user_ID)

        res.append({
            'id':msgs.id,
            'user':chat_user.username,
            'msg':msgs.message,
            'time':msgs.time.strftime('%I:%M:%S %p').lstrip('0'),
            'gravatar':chat_user.gravatar_url
        })
    
    data = json.dumps(res)

    
    return HttpResponse(data, content_type="application/json")
예제 #7
0
def chat_api(request):
    if request.method == "POST":
        d = json.loads(request.body)
        msg = d.get("msg")
        user = request.user.username
        gravatar = request.user.profile.gravatar_url
        m = Message(user=user, message=msg, gravatar=gravatar)
        m.save()

        res = {
            "id": m.id,
            "msg": m.message,
            "user": m.user,
            "time": m.time.strftime("%I:%M:%S %p").lstrip("0"),
            "gravatar": m.gravatar,
        }
        data = json.dumps(res)
        return HttpResponse(data, content_type="application/json")

        # get request
    r = Message.objects.order_by("-time")[:20]
    res = []
    for msgs in reversed(r):
        res.append(
            {
                "id": msgs.id,
                "user": msgs.user,
                "msg": msgs.message,
                "time": msgs.time.strftime("%I:%M:%S %p").lstrip("0"),
                "gravatar": msgs.gravatar,
            }
        )

    data = json.dumps(res)

    return HttpResponse(data, content_type="application/json")