def setRoute(request, num):
	context = RequestContext(request)
        routeId = -1 
        user_id = num

        routes = getNotSetRoutes(Route.objects.all())

        notSet = True
        _id = request.POST.get("set", "")
        if _id:
            routeId = request.POST.get("_id")
        
        if MobileUserRoute.objects.filter(route_id = routeId):
            notSet = False
        else:
            mobileUser = MobileUser.objects.get(pk=num)

        if _id:
            date = request.POST['date']
            if date:
                if not validate(date):
                    return HttpResponseRedirect('passDate' + str(num))
                route = Route.objects.get(pk=routeId)	
                mobileUserRoute = MobileUserRoute(route = route, mobileUser = mobileUser, date = date)
                mobileUserRoute.save()
                return HttpResponseRedirect('mobileUsersRoutes' + str(num))
            else:
                return HttpResponseRedirect('passDate' + str(num))

	else:
            if request.method == 'POST':
                Id = request.POST['Id']
                Name = request.POST['Name']

                if Name == None:
                    Name = r".*"
                if Id:
                    routes =  Route.objects.filter(pk = Id, name__regex=Name)
                else:
                    routes =  Route.objects.filter(name__regex=Name)
                    routes = getNotSetRoutes(routes)

	return render_to_response("setRoute.html",
				locals(),
				context_instance=RequestContext(request))
def setUserToRoute(request, num):

        notSet = True
        if MobileUserRoute.objects.filter(route_id = num):
                notSet = False
        else:
                mobileUsers = MobileUser.objects.all()
                routeToSet = Route.objects.filter(id=num).values('name')[0]
                now = datetime.datetime.now()
                routeId = num
                _id = request.POST.get("set", "")
                if _id:
			date = request.POST['date']
                        if date:
                            if not validate(date):
                                return HttpResponseRedirect('passDate' + str(num))
                            route = Route.objects.get(pk=num)
                            mobileUser = MobileUser.objects.get(pk=int(_id.split('#')[1]))	
                            mobileUserRoute = MobileUserRoute(route = route, mobileUser = mobileUser, date = date)
                            mobileUserRoute.save()
                            return HttpResponseRedirect('mobileUsersRoutes' + str(mobileUser.id))
                
                else:
                        if request.method == 'POST':
                            Id = request.POST['id']
                            Login = request.POST['Login']

                            if Login == None:
                                Login = r".*"

                            if Id:
                                mobileUsers =  MobileUser.objects.filter(pk = Id, username__regex=Login)
                            else:
                                mobileUsers =  MobileUser.objects.filter(username__regex=Login)

        routeId = num
	return render_to_response("setUserToRoute.html",
								locals(),
								context_instance=RequestContext(request))