def add_new(request, nparent=None, form_class=None, success_url=None, template_name='convo/add_new.html', extra_context=None): """ Create a new Entry """ if form_class is None: form_class = Convo.getForm(request.user) if success_url is None: success_url = "/" if request.method == 'POST': form = form_class(data=request.POST) if form.is_valid(): en = form.save(commit=False) if request.user.is_authenticated(): en.owner = request.user en.parent = nparent if nparent is None: en.original = None else: if nparent.original is None: en.original = nparent else: en.original = nparent.original en.save() return HttpResponseRedirect(success_url) else: form = form_class() if nparent is None: action_url = reverse('add_new_convo_entry') else: action_url = reverse('add_child_convo_entry', kwargs={"parent_id":nparent.id}) context = getContext(request, extra_context) return render_to_response(template_name, { 'form': form, "action_url" : action_url }, context_instance=context)
def edit_entry(request, e_id, form_class=None, success_url=None, template_name='convo/edit.html', extra_context = None): """ Edit an Entry """ e = Entry.objects.get(pk=e_id) if e.userCanEdit(request.user): if form_class is None: form_class = Convo.getForm(request.user) if success_url is None: success_url = "/" if request.method == 'POST': # have to check if e is published here before e gets its update from the form addEdit = e.published form = form_class(data=request.POST, instance=e) if form.is_valid(): en = form.save() if addEdit: edit = Edit() edit.entry = en edit.edit_by = request.user edit.save() return HttpResponseRedirect(success_url) else: form = form_class(instance=e) context = getContext(request, extra_context) return render_to_response(template_name, { 'form': form, 'e' : e, }, context_instance=context) else: return HttpResponseRedirect("/")
def show_convo(request, e_id, template_name='convo/show_convo.html', single_template_name=None, extra_context = None): """ Display a threaded conversation """ e = Entry.objects.get(pk=e_id) if e.published: from convo import Convo es, title = Convo.getConvoWithTitle(e) if es: entries = [] for en in es: if single_template_name and en.level==1: entries.append(get_entry_template(request, en, single_template_name)) else: entries.append(get_entry_template(request, en)) t = loader.get_template(template_name) if extra_context is None: extra_context = {} extra_context['entries'] = entries extra_context['title'] = title c = getContext(request, extra_context) return HttpResponse(t.render(c)) raise Http404
def main(): global server_ip global name global caller global exten global partner_ip global partner_name # get my ip s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(("8.8.8.8", 80)) my_ip = s.getsockname()[0] s.close() # get login from user while True: name = raw_input("Hi! What's your name? ") if len(name) > 0: final_endpoint = URL + ENDPOINT_NEW_USER + str(name) req = requests.post(final_endpoint) if req.status_code != 200: print "Sorry, that name is already taken." else: break client = Client(name, server_ip) # main program loop while True: want = raw_input("Press Enter when you're ready to talk, " + name + '!' + '\n Type "quit" if you want to quit.') if want == 'quit': print "Bye!" break # let server know you're ready to talk. final_endpoint = URL + ENDPOINT_USER_STATUS_RDY + str(name) req = requests.put(final_endpoint) if req.status_code != 200: print "Sorry, couldn't let the server know you're ready." break # find a partner to talk to final_endpoint = URL + ENDPOINT_PEER + str(name) while True: req = requests.get(final_endpoint, verify=False) if req.status_code == 226: time.sleep(1) continue if req.status_code == 208: time.sleep(1) continue # you'll be making SIP call if req.status_code == 202: caller = 1 info_dict = json.loads(req.text) partner_name = info_dict["call"] exten = info_dict["exten"] partner_ip = info_dict["ip_to_send_data"] enc_endpoint = Enc_Endpoint(1, my_ip, partner_ip) # see if your partner is waiting for your call while True: status = get_user_status_value(partner_name) if int(status) == 3: break time.sleep(1) # set Diffie-Hellman key for encrypted communication enc_endpoint.exchange_keys() break # you'll be waiting for SIP call if req.status_code == 230: info_dict = json.loads(req.text) partner_ip = info_dict["ip_to_send_data"] partner_name = info_dict["call"] # set Diffie-Hellman key for encrypted communication enc_endpoint = Enc_Endpoint(0, my_ip, partner_ip) key_exchange_thread = threading.Thread( target=enc_endpoint.exchange_keys) key_exchange_thread.start() # let server know you're waiting for the call final_endpoint = URL + ENDPOINT_USER_STATUS_RDY_TO_TALK + str( name) req = requests.put(final_endpoint) if req.status_code != 200: print "Sorry, couldn't communicate with server." break break time.sleep(1) client.set_exten(str(exten)) client.set_caller(caller) # ***************************** CALL ********************************* call = threading.Thread(target=client.start_call()) call.start() while client.CALL_ACTIVE == 0: time.sleep(1) print "Waiting for partner..." # start text chat chat = Convo(my_ip, partner_ip, partner_name, enc_endpoint) chat.start_convo() # wait for call to end while True: if client.CALL_ACTIVE == 0: # someone hang up on us chat.end_convo() break elif chat.DONE_STATUS == 1: # we want to hang up client.hangup() break final_endpoint = URL + ENDPOINT_USER_STATUS_BUSY + str(name) requests.put(final_endpoint) # main loop repeats # kill the client final_endpoint = URL + "/delete-user/" + str(name) requests.delete(final_endpoint) client.end()