def create(req): if req.method == 'POST': title = req.POST.get('title') message_body = req.POST.get('message_body') job_post = JobPost() job_post.title = title job_post.initial_message = message_body job_post.save() workers = req.POST.get('workers') worker_list = json.loads(workers) print message_body for worker_id in worker_list: try: worker = Worker.objects.get(pk=int(worker_id)) worker.contacted = True worker.save() print 'sending message to {}'.format(worker.name) message_result = _send_sms(worker, message_body) message = Message() message.message_body = message_body message.outgoing = True job_post.messaged_workers.add(worker) message.worker = worker message.message_sid = message_result.id_key message.save() except Exception as e: print 'Caught exception ' + str(e) job_post.save() response_json = '{"job_post_id": %d}' % job_post.pk return HttpResponse(response_json, content_type='application/json') else: return render_to_response('mjapp/create.html')
def _handle_sms_response(phone_number, body, message_sid): print '{} ::: {}'.format(phone_number, body) # figure out what question the worker is answering worker, created = Worker.objects.get_or_create(phone_number=phone_number) # save their response response = SignupQuestionResponse() response.response_text = body response.message_sid = message_sid response.worker = worker #copy all messages as Message models too message = Message() message.message_body = body message.message_sid = message_sid message.outgoing = False message.worker = worker message.save() # if they have been contacted in a job post blast, don't send them a canned response. # we are letting the employer chat with them if worker.contacted: return {'question': None, 'params': None} if worker.current_question is None: response.question = SignupQuestion.objects.get(question_name='start') else: response.question = worker.current_question response.save() params = [] if worker.current_question is None: worker.current_question = SignupQuestion.objects.get(question_name='name') elif worker.current_question.question_name == 'name': # store the worker's name worker.name = body worker.current_question = SignupQuestion.objects.get(question_name='availability') params.append(_get_first_name(worker)) elif worker.current_question.question_name == 'availability': _set_availability(worker, body) worker.current_question = SignupQuestion.objects.get(question_name='location') elif worker.current_question.question_name == 'location': _set_location(worker, body) worker.current_question = SignupQuestion.objects.get(question_name='thanks') elif worker.current_question.question_name == 'thanks': worker.current_question = SignupQuestion.objects.get(question_name='wait') params.append(_get_first_name(worker)) elif worker.current_question.question_name == 'wait': # you can't advance past the 'please just wait for a job' message. params.append(_get_first_name(worker)) pass worker.save() return {'question': worker.current_question, 'params': params}
def send_outgoing_sms(req): worker_id = req.POST.get('worker_id') message_body = req.POST.get('message_body') try: worker = Worker.objects.get(pk=worker_id) message_model = Message() message_model.worker = worker message_model.message_body = message_body message_model.outgoing = True message = _send_sms(worker, message_body) message_model.message_sid = str(message.id_key) message_model.save() response_object = { 'message': serializers.serialize('json', [message_model]), 'worker': serializers.serialize('json', [worker]) } response_string = json.dumps(response_object) return HttpResponse(response_string, content_type='application/json') except Exception as e: return HttpResponse(e.message, status=500)