Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 233 lines (205 sloc) 8.489 kb
8cba617 merged the ridesharing code
null authored
1 from hashlib import sha256
2 from functools import wraps
3
6b31508 more cleanup
Justine Tunney authored
4 from django.conf import settings
8cba617 merged the ridesharing code
null authored
5 from django.core.cache import cache
6 from django.forms import ValidationError
dec165a pep8 / pyflakes
Justine Tunney authored
7 from django.template import RequestContext
8cba617 merged the ridesharing code
null authored
8 from django.contrib.gis.geos import Polygon
dec165a pep8 / pyflakes
Justine Tunney authored
9 from django.core.urlresolvers import reverse
10 from django.utils.translation import ugettext as _
11 from django.contrib.auth.decorators import login_required
12 from django.http import HttpResponse, HttpResponseRedirect
13 from django.shortcuts import render_to_response, get_object_or_404
8cba617 merged the ridesharing code
null authored
14
15 from rideshare import forms, models as db
dec165a pep8 / pyflakes
Justine Tunney authored
16 from occupywallst import api, utils, models as maindb
8cba617 merged the ridesharing code
null authored
17
18
19 def my_cache(mkkey, seconds=60):
20 def _my_cache(function):
21 @wraps(function)
22 def __my_cache(request, *args, **kwargs):
23 if request.user.is_authenticated():
24 response = function(request, *args, **kwargs)
25 else:
26 key = sha256(mkkey(request, *args, **kwargs) + ':' +
27 request.LANGUAGE_CODE).hexdigest()
28 response = cache.get(key)
29 if not response:
30 response = function(request, *args, **kwargs)
31 cache.set(key, response, seconds)
32 return response
33 return __my_cache
34 return _my_cache
35
36
37 @my_cache(lambda r: 'rides')
38 def rides(request):
39 rides = db.Ride.objects.all()
40 user_ride_request = None
dec165a pep8 / pyflakes
Justine Tunney authored
41 user_ride = None
8cba617 merged the ridesharing code
null authored
42 if request.user.is_authenticated():
43 try:
44 user_ride = rides.get(user=request.user)
45 except db.Ride.DoesNotExist:
46 try:
dec165a pep8 / pyflakes
Justine Tunney authored
47 user_ride_request = \
48 db.RideRequest.objects.get(user=request.user)
8cba617 merged the ridesharing code
null authored
49 except db.RideRequest.DoesNotExist:
dec165a pep8 / pyflakes
Justine Tunney authored
50 pass
51 return render_to_response('rides.html', {
52 'rides': rides,
53 'user_ride': user_ride,
54 'user_ride_request': user_ride_request,
55 }, context_instance=RequestContext(request))
56
8cba617 merged the ridesharing code
null authored
57
58 @login_required
dec165a pep8 / pyflakes
Justine Tunney authored
59 def ride_request_view(request, request_id):
60 ride_request = db.RideRequest.objects.get(pk=int(request_id))
61 return render_to_response('request_view.html', {
62 "ride_request": ride_request,
63 "ride": ride_request.ride,
64 }, context_instance=RequestContext(request))
65
8cba617 merged the ridesharing code
null authored
66
67 @login_required
dec165a pep8 / pyflakes
Justine Tunney authored
68 def ride_delete(request, ride_id):
69 ride = db.Ride.objects.select_related('requests').get(
70 user=request.user, pk=int(ride_id))
8cba617 merged the ridesharing code
null authored
71 requests = ride.requests.all()
72 for req in requests:
73 maindb.Notification.send(
15f9368 pep8
mjbecze@gmail.com authored
74 req.user, reverse('rides'),
75 '%s has deleted there ride' % (ride.user.username))
8cba617 merged the ridesharing code
null authored
76 requests.delete()
77 ride.delete()
78 return HttpResponseRedirect(reverse(rides))
79
dec165a pep8 / pyflakes
Justine Tunney authored
80
8cba617 merged the ridesharing code
null authored
81 @login_required
82 def ride_create(request):
83 return ride_create_or_update(request)
84
85
86 @login_required
87 def ride_edit(request, ride_id):
88 ride = get_object_or_404(db.Ride, pk=int(ride_id))
89 return ride_create_or_update(request, ride)
90
91
92 @login_required
93 def ride_create_or_update(request, instance=None):
94 if request.method == "POST":
95 form = forms.RideForm(request.POST, instance=instance)
96 if form.is_valid():
97 ride = form.save(commit=False)
98 try:
99 # this should maybe go into some work queue instead of
100 # being run sync
101 ride.user = request.user
102 ride.retrieve_route_from_google()
103 #ride.full_clean()
104 ride.save()
105 return HttpResponseRedirect(ride.get_absolute_url())
dec165a pep8 / pyflakes
Justine Tunney authored
106 except ValidationError:
8cba617 merged the ridesharing code
null authored
107 #stupid hack
108 from django.forms.util import ErrorList
109 form._errors["title"] = ErrorList([
110 "You have already created a ride with that title",
111 ])
112 else:
113 ride_requests = None
114 else:
115 userinfo = maindb.UserInfo.objects.get(user=request.user)
15f9368 pep8
mjbecze@gmail.com authored
116 if hasattr(settings, 'DEFAULT_END_ADDRESS'):
117 default_end_address = settings.DEFAULT_END_ADDRESS
118 else:
119 default_end_address = None
dec165a pep8 / pyflakes
Justine Tunney authored
120 form = forms.RideForm(initial={
121 'start_address': userinfo.formatted_address,
15f9368 pep8
mjbecze@gmail.com authored
122 'end_address': default_end_address,
dec165a pep8 / pyflakes
Justine Tunney authored
123 }, instance=instance)
8cba617 merged the ridesharing code
null authored
124 ride_requests = db.RideRequest.objects.filter(ride=instance)
dec165a pep8 / pyflakes
Justine Tunney authored
125 return render_to_response('ride_update.html', {
126 "form": form,
127 "ride": instance,
128 "requests": ride_requests,
129 }, context_instance=RequestContext(request))
130
8cba617 merged the ridesharing code
null authored
131
132 def ride_info(request, ride_id):
133 ride = db.Ride.objects.get(pk=int(ride_id))
dec165a pep8 / pyflakes
Justine Tunney authored
134 CHOICES = [(i, i) for i in range(1, ride.seats_avail + 1)]
8cba617 merged the ridesharing code
null authored
135 ride_request = None
136 requests = None
137 form = None
c00dc7f nag at people to log in when using ride share feature
Justine Tunney authored
138 if request.user.is_authenticated():
0b141f0 don't require login for viewing a ride
Justine Tunney authored
139 if request.user == ride.user:
8cba617 merged the ridesharing code
null authored
140 requests = db.RideRequest.objects.filter(ride=ride)
141 else:
c00dc7f nag at people to log in when using ride share feature
Justine Tunney authored
142 if db.RideRequest.objects.filter(user=request.user).count():
143 ride_request = db.RideRequest.objects.get(user=request.user)
144 form = forms.RideRequestForm(instance=ride_request)
145 form.fields['seats_wanted'].widget.choices = CHOICES
146 else:
147 form = forms.RideRequestForm()
148 form.fields['seats_wanted'].widget.choices = CHOICES
dec165a pep8 / pyflakes
Justine Tunney authored
149 return render_to_response('ride_info.html', {
150 "ride": ride,
151 "ride_request": ride_request,
152 "requests": requests,
153 "form": form,
154 }, context_instance=RequestContext(request))
155
156
8cba617 merged the ridesharing code
null authored
157 @login_required
158 def ride_request_add_update(request, ride_id):
159 ride = get_object_or_404(db.Ride, pk=int(ride_id))
160 try:
161 ride_request = db.RideRequest.objects.get(user=request.user, ride=ride)
162 except db.RideRequest.DoesNotExist:
dec165a pep8 / pyflakes
Justine Tunney authored
163 ride_request = db.RideRequest(user=request.user, ride_id=ride_id)
8cba617 merged the ridesharing code
null authored
164 if request.method == 'POST':
dec165a pep8 / pyflakes
Justine Tunney authored
165 form = forms.RideRequestForm(request.POST, instance=ride_request)
8cba617 merged the ridesharing code
null authored
166 form.save()
dec165a pep8 / pyflakes
Justine Tunney authored
167 msg = "Ride Request From %s -- %s" % (
168 ride_request.user.username, request.POST['info'])
169 api.message_send(request.user, ride.user, msg)
170 msg = '%s has request a seat on your ride' % (
171 ride_request.user.username)
172 url = ride_request.ride.get_absolute_url()
173 maindb.Notification.send(ride.user, url, msg)
174 if request.is_ajax():
8cba617 merged the ridesharing code
null authored
175 return HttpResponse("{test:tes}", mimetype="application/json")
176 else:
177 return HttpResponseRedirect(ride.get_absolute_url())
178
179
180 @login_required
181 def ride_request_delete(request, ride_id):
dec165a pep8 / pyflakes
Justine Tunney authored
182 ride_request = db.RideRequest.objects.get(
183 ride__pk=ride_id, user__id=request.user.id)
8cba617 merged the ridesharing code
null authored
184 if request.user == ride_request.user:
185 ride_request.delete()
dec165a pep8 / pyflakes
Justine Tunney authored
186 msg = '%s has canceled their ride request' % (
187 ride_request.user.username)
188 url = ride_request.ride.get_absolute_url()
189 maindb.Notification.send(ride_request.ride.user, url, msg)
8cba617 merged the ridesharing code
null authored
190 return HttpResponseRedirect(reverse(rides))
191
dec165a pep8 / pyflakes
Justine Tunney authored
192
8cba617 merged the ridesharing code
null authored
193 def _str_to_bbox(val):
194 swlat, swlng, nwlat, nwlng = [float(s) for s in val.split(',')]
195 return Polygon.from_bbox([swlng, swlat, nwlng, nwlat])
196
dec165a pep8 / pyflakes
Justine Tunney authored
197
8cba617 merged the ridesharing code
null authored
198 def rides_get(bounds=None, **kwargs):
199 """Find all rides within visible map area"""
200 if bounds:
201 bbox = _str_to_bbox(bounds)
202 qset = (db.Ride.objects
203 .filter(route__isnull=False,
204 route__bboverlaps=bbox))
205 else:
206 qset = (db.Ride.objects
207 .filter(route__isnull=False))
208 for ride in qset:
209 yield {'id': ride.id,
15f9368 pep8
mjbecze@gmail.com authored
210 'title': ride.title,
8cba617 merged the ridesharing code
null authored
211 'address': ride.waypoint_list[0],
212 'route': ride.route}
dec165a pep8 / pyflakes
Justine Tunney authored
213
8cba617 merged the ridesharing code
null authored
214 def ride_request_update(request_id, status, user=None, **kwargs):
dec165a pep8 / pyflakes
Justine Tunney authored
215 ride_request = (db.RideRequest.objects
216 .select_related("ride", "ride__user")
217 .filter(id=request_id))
8cba617 merged the ridesharing code
null authored
218 try:
219 req = ride_request[0]
220 except IndexError:
dec165a pep8 / pyflakes
Justine Tunney authored
221 raise utils.APIException(_("request not found"))
8cba617 merged the ridesharing code
null authored
222 if req.ride.user == user:
223 req.status = status
224 req.save()
dec165a pep8 / pyflakes
Justine Tunney authored
225 # msg = '%s %s your ride request' % (req.ride.user.username, status)
226 # message_send(req.ride.user, req.user, msg)
8cba617 merged the ridesharing code
null authored
227 maindb.Notification.send(
dec165a pep8 / pyflakes
Justine Tunney authored
228 req.user, req.ride.get_absolute_url(),
229 '%s %s your ride request' % (req.ride.user.username, status))
8cba617 merged the ridesharing code
null authored
230 return [{"id": req.id, "status": req.status}]
231 else:
dec165a pep8 / pyflakes
Justine Tunney authored
232 raise utils.APIException(_("insufficient permissions"))
Something went wrong with that request. Please try again.