def answer(request, ask_slug): '''Response page. ''' logging.debug('In question.views::answer()') question = Question.all().filter('ask_slug = ', ask_slug).get() if question is None: question = Question.get(ask_slug) if not question.answer: d1 = datetime.datetime.now() d2 = question.asked if (abs(d1.minute-d2.minute) % 5) == 0 and d1.second == 0: question.asked = d1 question.save() _send_message_xmpp('*****@*****.**', '%s: %s' % (question.key().id(), question.ask)) if request.is_ajax(): return HttpResponse(simplejson.dumps(question.to_dict()), mimetype='application/json') initial = {} initial['ask'] = question.ask initial['ask_slug'] = question.ask_slug question_form = QuestionForm(initial=initial) return render_to_response('index.html', { 'question_form': question_form, 'recent_stupid_questions': _recent_stupid_questions(), 'ask_slug': question.slugify(), 'answer': question.answer, })
def _recent_stupid_questions(): '''The latest stupid questions. ''' logging.debug('In question.views::_recent_stupid_questions()') questions = Question.all() if not settings.DEBUG: logging.debug('Debug mode active') questions.filter('answered != ', None) questions = questions.order('-answered').fetch(30) return questions
def incoming_chat(request): '''Mounts a chat with the underguiz (clients). ''' logging.debug('In question.views::incoming_chat()') if request.method != 'POST': return HttpResponse(_('XMPP requires POST'), status=405) st = False sender = request.POST.get('from') toaddr = request.POST.get('to') message = request.POST.get('body') if not sender: logging.warn('Incoming chat without \'from\' key ignored') return HttpResponse(st) elif not message: logging.warning('Incoming chat without \'body\' key ignored') return HttpResponse(st) try: body = message.split(':') if len(body) <= 1 and not body[0].isdigit(): logging.warn('Message not format ID:MESSAGE: %s' % (body,)) return HttpResponse(st) id_question = int(body[0]) if body[0].isdigit() else 0 answer = ''.join(body[1:]).strip() question = Question.get_by_id(id_question) # If the answer already exists, then concatenates the responses # with HTML formatting if question.answer: space = '<br />' + ' '*16 space = '' question.answer = '%s; %s%s' % (question.answer, space, answer) else: question.answer = answer question.answered = datetime.datetime.now() question.save() # Send XMPP message toaddrs = [toaddr,] #'*****@*****.**'] sts = xmpp.send_message(toaddrs, answer) logging.debug('XMPP status %s', (str(sts),)) except Exception, e: logging.error('Error in send for xmpp: %s' % (str(e),)) return HttpResponse(st)
def new_ask(request, ask): '''Writes a new question and redirects to the response page. ''' logging.debug('In question.views::new_answer()') new_question = Question.all().filter('ask = ', ask).get() if not new_question.is_exists(): new_question = Question(ask=ask) new_question = datetime.datetime.now() new_question.save() _send_message_xmpp('*****@*****.**', '%s: %s' % (new_question.key().id(), new_question.ask)) return HttpResponseRedirect(new_question.get_absolute_url())
def index(request): '''Main page of the application. ''' logging.debug('In question.views::index()') if request.method == 'POST': question_form = QuestionForm(request.POST) if question_form.is_valid(): new_question = question_form.save(commit=False) if new_question.is_exists(): new_question = Question.all() \ .filter('ask =', new_question.ask).get() new_question.asked = datetime.datetime.now() new_question.save() _send_message_xmpp('*****@*****.**', '%s: %s' % (new_question.key().id(), new_question.ask)) return HttpResponseRedirect(new_question.get_absolute_url()) else: question_form = QuestionForm() return render_to_response('index.html', { 'question_form': question_form, 'recent_stupid_questions': _recent_stupid_questions(), })