Exemplo n.º 1
0
def new_complaint(request):
    geolocator = GoogleV3()
    fellows = []
    time = datetime.time(datetime.now())
    date = datetime.date(datetime.now())
    if request.method == 'POST' and request.is_ajax:
    	complaint = ComplaintForm(request.POST)
    	if complaint.is_valid():
    	    situation = complaint.cleaned_data['situation']
    	    neighborhood = complaint.cleaned_data['neighborhood']
    	    apt_number = complaint.cleaned_data['apt_number']
    	    street_address = complaint.cleaned_data['street_address']
    	    new_complaint = Complaint(situation=situation, neighborhood=neighborhood, street_address=street_address, date=date, time=time)
    	    if request.user.is_authenticated:
                new_complaint.owner = request.user
            if apt_number:
            	new_complaint.apt_number = apt_number
            	apts = Complaint.objects.filter(street_address=street_address).filter(apt_number=apt_number)
    	        numbers = [apt.apt_number for apt in apts]
    	        if apt_number in numbers:
    	        	new_complaint.others = True
    	    	if new_complaint.others:
    	    		for a in apts:
    	    			if a.owner not in fellows:
    	        			fellows.append(a.owner)	
    	    for fellow in fellows:
    	    	new_msg = Message(sent_from=request.user, send_to=fellow, subject="another complaint about %s" % new_complaint.street_address, message="you are not alone! Others have also complained about %s. Reply to this message so you guys can figure out what to do about it." % new_complaint.street_address)
    	    new_complaint.save()
    	    new_msg.complaint = new_complaint
    	    new_msg.save()
    	    data = json.dumps({"situation": situation, "neighborhood": neighborhood, "time": str(new_complaint.time)})
    	    if new_complaint.street_address:
    	    	nycaddress = new_complaint.street_address + " NYC"
    	    	geoAddress = nycaddress, (latitude, longitude) = geolocator.geocode(nycaddress)
                way = WayPoint(complaint=new_complaint)
                way.geometry = 'POINT(%s %s)' % (latitude, longitude)
                way.save()
                return HttpResponse(data, content_type='application/json')  
	return HttpResponse("...")