Skip to content

Commit

Permalink
Delete old test url ; Fix search results
Browse files Browse the repository at this point in the history
  • Loading branch information
djcoin committed Nov 14, 2010
1 parent e1f78f0 commit 90fa317
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 81 deletions.
61 changes: 1 addition & 60 deletions src/bv/server/api/carpoolhandlers.py
Expand Up @@ -73,46 +73,6 @@
'geometry',
)

def validateTripForm(operation='POST'):
"""Validate if the given trip is valid"""
@decorator
def wrap(f, self, request, *args, **kwargs):
errordict = {}
errors = False
if len(request.REQUEST.items()) == 1 and request.REQUEST.get('alert', False):
return f(self, request, *args, **kwargs)
# should be removed ??
if 3 <= len(request.REQUEST.items()) <= 4:
return f(self, request, *args, **kwargs)

if 'dows' in request.REQUEST.items():
request.REQUEST.setlist(request.REQUEST.get('dows').split('-'))
def is_valid(form):
if not form.is_valid():
errors = False
for key,value in form.errors.items():
errordict.setdefault(key, value)
return False
return True
requestdict = request_to_dict(request)
tripform = EditTripForm(data=requestdict)
offerform = EditTripOfferOptionsForm(data=requestdict, prefix='offer')
demandform = EditTripDemandOptionsForm(data=requestdict, prefix='demand')

is_valid(tripform)
if int(request.REQUEST.get('trip_type')) in (Trip.OFFER, Trip.BOTH):
is_valid(offerform)

if int(request.REQUEST.get('trip_type')) in (Trip.DEMAND, Trip.BOTH):
is_valid(demandform)

if not errors or request.REQUEST.get('alert',false):
return f(self, request, *args, **kwargs)
else:
tripform.errors = errordict
raise FormValidationError(tripform)
return wrap

def request_to_dict(request):
"""Transform a request into a dict, and split the dows field into a list.
Expand Down Expand Up @@ -160,7 +120,7 @@ def read(self, request, **kwargs):
"""Make a search within the list of existing trips
"""
return self.lib.get_trip_results(*values)
return self.lib.get_trip_results(**kwargs)

class TripsSearchHandler(CarpoolHandler):
anonymous = AnonymousTripsSearchHandler
Expand Down Expand Up @@ -188,24 +148,6 @@ def read(self, request, trip_id=None, start=None, count=None):
return paginate_items(items, start, count, request, self.count)


class TripsUpdateHandler(CarpoolHandler):
"""Special handler to handle small trip update.
Otherwise, see TripsHandler.
"""
anonymous = AnonymousTripsHandler
# XXX remove some methods
allowed_methods = ('PUT')

# trip_{offer, demand} : radius ; trip : interval_{min, max}
def update(self, request, *args, **kwargs):
trip_id = kwargs.get('trip_id', None)
response = self.lib.reduced_update_trip(request.user, request_to_dict(request), trip_id)
if response['error']:
return rc.BAD_REQUEST
elif response['trip']:
return response['trip']


class TripsHandler(CarpoolHandler):
"""Handler for trips: CRUD for authenticated users.
Expand Down Expand Up @@ -234,7 +176,6 @@ def create(self, request):
elif (response['trip']):
return response['trip']

@validateTripForm()
def update(self, request, *args, **kwargs):
"""Update an existing trip
Expand Down
4 changes: 1 addition & 3 deletions src/bv/server/api/urls.py
Expand Up @@ -7,7 +7,7 @@

# API imports
from bv.server.api.resource import Resource
from bv.server.api.carpoolhandlers import TripsHandler, TripsSearchHandler, TripsUpdateHandler
from bv.server.api.carpoolhandlers import TripsHandler, TripsSearchHandler
from bv.server.api.ratinghandlers import RatingsHandler, TempRatingsHandler
from bv.server.api.ratinghandlers import MyRatingsHandler, RatingsAboutMeHandler
from bv.server.api.talkshandlers import TalksHandler, MessagesHandler
Expand All @@ -25,7 +25,6 @@
# trips
trips_handler = Resource(handler=TripsHandler, authentication=auth)
trip_search_handler = Resource(handler=TripsSearchHandler, authentication=auth)
trips_update_handler = Resource(handler=TripsUpdateHandler, authentication=auth)

# ratings
ratings_handler = Resource(handler=RatingsHandler, authentication=auth)
Expand All @@ -46,7 +45,6 @@
url(r'^trips/search/$', trip_search_handler),
url(r'^trips/(?P<trip_id>[0-9a-z\_]+)/$', trips_handler),
url(r'^trips/$', trips_handler),
url(r'^trips/reduced_update/(?P<trip_id>[0-9a-z\_]+)/$', trips_update_handler),


url(r'^temp-ratings/$', tempratings_handler),
Expand Down
2 changes: 1 addition & 1 deletion src/bv/server/carpool/models.py
Expand Up @@ -391,7 +391,7 @@ def get_demands(self, route, direction_route, radius):
Calculations arent made from the simplified projected route
"""
ogr = smart_transform(route,SRID_TRANSFORM, from_srid=SRID_DEFAULT).ogr
ogr = smart_transform(route, SRID_TRANSFORM, from_srid=SRID_DEFAULT).ogr
demands = self.filter(demand__isnull=False)
demands = demands.select_related('user', 'demand')
print "IN DEMANDS #################"
Expand Down
39 changes: 22 additions & 17 deletions src/bv/server/lib/libcarpool.py
Expand Up @@ -381,11 +381,6 @@ def update_trip(self, user, post_data, trip_id):
"""Update an existing trip
"""
return self._add_or_edit_trip(user, post_data, trip_id)

def reduced_update_trip(self, user, post_data, trip_id):
"""Only updates small part of the trip: radius and interval min/max"""

ff = {'trip' : DynamicEditTripForm,
'tripoffer' : DynamicEditTripOfferOptionsForm,
'tripdemand' : DynamicEditTripDemandOptionsForm,
Expand All @@ -399,7 +394,11 @@ def get_trip_results(self, is_offer=None, is_demand=None, offer_radius=None,
"""Return a list containing the trip object, trip_offers and
trip_demands matching given criterias.
:args: search criteria ;
if a trip_id is present it will be used to complete missing criteria
"""
# can be both a demand and an offer!

# vars initialisation
max_trips = max_trips or self.MAX_TRIPS
trip = None
Expand All @@ -412,15 +411,22 @@ def get_trip_results(self, is_offer=None, is_demand=None, offer_radius=None,
trip = Trip.objects.get(pk=int(trip_id), user=user)
except Trip.DoesNotExist:
raise InvalidUser(user)

if is_offer or trip.offer:
is_offer = is_offer or True
offer_radius = offer_radius or trip.offer.radius
offer_route = offer_route or trip.offer.route

# warning: if a trip exist ; the search type will be the inverse
is_offer = is_offer or not trip.offer
is_demand = is_demand or not trip.demand

# XXX: if no offer_ settings is set ; use the demand of the trip to get one.
# lookin for an offer
if is_offer:
offer_radius = offer_radius or trip.demand.radius
# offer_route = offer_route or trip.demand.route # not possible <- or demand AND offer

if (is_demand or trip.demand):
is_demand = is_demand or True
demand_radius = demand_radius or trip.demand.radius
# lookin for a demand
if is_demand:
demand_radius = demand_radius or trip.offer.radius
offer_route = offer_route or trip.offer.route

is_regular = is_regular or trip.regular
date = date or trip.date
dows = dows or trip.dows
Expand All @@ -429,7 +435,7 @@ def get_trip_results(self, is_offer=None, is_demand=None, offer_radius=None,
departure_point = departure_point or trip.departure_point
arrival_point = arrival_point or trip.arrival_point

if is_offer:
if is_demand:
offer_radius = offer_radius or self.OFFER_RADIUS
if trip_id:
if offer_route is None or offer_route.geom_type != 'MultiLineString':
Expand All @@ -454,7 +460,7 @@ def get_trip_results(self, is_offer=None, is_demand=None, offer_radius=None,
today = datetime.date.today()
date = date or today

if is_demand:
if is_offer:
trip_offers = Trip.objects.get_offers(departure_point, arrival_point, demand_radius)
if trip_id:
trip_offers = trip_offers.exclude(pk=trip_id)
Expand All @@ -472,7 +478,7 @@ def get_trip_results(self, is_offer=None, is_demand=None, offer_radius=None,
trip_offers = trip_offers.order_by('-pourcentage_rank')[:max_trips]
trip_offers = sort_offers(trip_offers, date, interval_min, interval_max, trip=trip)

if is_offer:
if is_demand:
trip_demands = Trip.objects.get_demands(offer_route, get_direction_route(offer_route), offer_radius)
if trip_id:
trip_demands = trip_demands.exclude(pk=trip_id)
Expand Down Expand Up @@ -527,4 +533,3 @@ def get_trip(self, trip_id):
"""
return get_object_or_404(Trip.objects.get_mark_details(), pk=trip_id)

0 comments on commit 90fa317

Please sign in to comment.