示例#1
0
def message_ajax(request):
    if request.method == 'POST':
        message = request.POST.get('message', '')
        if len(message) > 0:
            username = request.user.username
            #now = timezone.now()
            now = datetime.now()
            m = MessageModel(username=username, message=message, creation_date=now)
            m.save()

            # create push event with the data of a new message
            pusher_client = pusher.Pusher(
                app_id='187082',
                key='5729b7b62f550fc1c6ff',
                secret='a267a54361c086c8107d',
                cluster='eu',
                ssl=True
            )
            pusher_client.trigger('message_channel', 'message_save_event', 
                    {'username': m.username,
                        'message': m.message,
                        'date_string': m.date_string
            })
            return HttpResponse('message sent through pusher')
    # old no real time shit
    #messages = MessageModel.objects.order_by('-creation_date')[:5]
    #return render(request, 'message_board/messages_template.html', {
    #    'messages': messages })
    return HttpResponse('message not created, not sent')
示例#2
0
文件: views.py 项目: wwjiang/lecture
def org_message(request,org_id):
    org_id = int(org_id)
    form = MessageForm()
    org = OrgModel.objects.get(id=org_id)
    #第一次访问,此时无表单
    if not request.method == "POST":
        return render_to_response('org_message_submit.html',
                {
                    'org':org,
                    'form':form,
                },
                context_instance=RequestContext(request)
            )
    
    #验证表单合法性
    form = MessageForm(request.POST)
    if not form.is_valid():
        return render_to_response('org_note_submit.html',
                {
                    'org':org,
                    'form':form,
                },
                context_instance=RequestContext(request)
            )

    #保存读书笔记
    data = form.cleaned_data
    message = MessageModel(
                    date=date.today(),
                    time=time.strftime('%H:%M:%S',
                        time.localtime(time.time())
                        ),
                    user=request.user.get_profile(),
                    org = org,
                    title = data['title'],
                    content = data['content'],
                    hasPass = False,
                    showNote = False,
                    score=0,
                    score_times=0,
                    click_number=0)
    message.save()
    return HttpResponseRedirect("/org/"+org_id+"/")