|
8 | 8 | from django.views.decorators.csrf import csrf_protect
|
9 | 9 |
|
10 | 10 | from recommender_webapp.common import lightfm_manager, constant
|
11 |
| -from recommender_webapp.forms import ProfileForm, UserRegisterForm, SearchNearPlacesForm, DistanceRange, \ |
12 |
| - SearchRecommendationForm, FullProfileForm |
| 11 | +from recommender_webapp.forms import ProfileForm, UserRegisterForm, SearchNearPlacesForm, SearchPlacesDistanceRange, \ |
| 12 | + AddRatingForm, FullProfileForm |
13 | 13 | from recommender_webapp.models import Comune, Distanza, Place, Mood, Companionship, Rating, User, Profile, Event
|
14 | 14 |
|
15 | 15 |
|
@@ -115,26 +115,18 @@ def profile_configuration(request):
|
115 | 115 |
|
116 | 116 | close_places = []
|
117 | 117 | user_location = request.user.profile.location
|
118 |
| - distances_in_range = Distanza.objects.filter(cittaA=user_location, distanza__lte=constant.KM_RANGE_CONFIGURATION).order_by('distanza') |
119 |
| - |
120 |
| - user_location_places = Place.objects.filter(location=user_location) |
121 |
| - for place in user_location_places: |
122 |
| - place_dict = vars(place) |
123 |
| - place_dict['labels'] = place.labels() |
124 |
| - rated_place = Rating.objects.filter(place=place, user=request.user.profile) |
125 |
| - if rated_place: |
126 |
| - place_dict['rated'] = True |
127 |
| - rated_places.append(place_dict) |
128 |
| - else: |
129 |
| - close_places.append(place_dict) |
130 |
| - |
131 |
| - for distance in distances_in_range: |
132 |
| - places = Place.objects.filter(location=distance.cittaB) |
| 118 | + locations_in_range = [distance[0] for distance in |
| 119 | + Distanza.objects.filter( |
| 120 | + cittaA=user_location, |
| 121 | + distanza__lte=constant.KM_RANGE_CONFIGURATION).order_by('distanza').values_list('cittaB')] |
| 122 | + locations_in_range = [user_location] + locations_in_range |
| 123 | + |
| 124 | + for location in locations_in_range: |
| 125 | + places = Place.objects.filter(location=location) |
133 | 126 | for place in places:
|
134 | 127 | place_dict = vars(place)
|
135 | 128 | place_dict['labels'] = place.labels()
|
136 |
| - rated_place = Rating.objects.filter(place=place) |
137 |
| - if rated_place: |
| 129 | + if Rating.objects.filter(place=place, user=request.user.profile).exists(): |
138 | 130 | place_dict['rated'] = True
|
139 | 131 | rated_places.append(place_dict)
|
140 | 132 | else:
|
@@ -175,42 +167,31 @@ def add_rating_config(request, place_id, mood, companionship):
|
175 | 167 |
|
176 | 168 | def close_places(request):
|
177 | 169 | context = {}
|
178 |
| - rated_places = [] |
179 | 170 | close_places = []
|
180 | 171 |
|
181 | 172 | if request.user.is_authenticated:
|
182 | 173 |
|
183 | 174 | search_near_places_form = SearchNearPlacesForm(request.POST or None)
|
184 |
| - initial_km_range = (DistanceRange.km5.name, DistanceRange.km5.value) |
| 175 | + initial_km_range = (SearchPlacesDistanceRange.km5.name, SearchPlacesDistanceRange.km5.value) |
185 | 176 | search_near_places_form.fields['km_range'].initial = initial_km_range
|
186 | 177 |
|
187 | 178 | if search_near_places_form.is_valid():
|
188 | 179 | km_range = search_near_places_form.cleaned_data.get('km_range')
|
189 | 180 | user_location = request.user.profile.location
|
190 |
| - distances_in_range = Distanza.objects.filter(cittaA=user_location, distanza__lte=km_range).order_by('distanza') |
191 |
| - |
192 |
| - user_location_places = Place.objects.filter(location=user_location) |
193 |
| - for place in user_location_places: |
194 |
| - place_dict = vars(place) |
195 |
| - place_dict['labels'] = place.labels() |
196 |
| - rated_place = Rating.objects.filter(place=place, user=request.user.profile) |
197 |
| - if rated_place: |
198 |
| - place_dict['rated'] = True |
199 |
| - rated_places.append(place_dict) |
200 |
| - else: |
201 |
| - close_places.append(place_dict) |
202 |
| - |
203 |
| - for distance in distances_in_range: |
204 |
| - places = Place.objects.filter(location=distance.cittaB) |
| 181 | + locations_in_range = [distance[0] for distance in |
| 182 | + Distanza.objects.filter( |
| 183 | + cittaA=user_location, |
| 184 | + distanza__lte=km_range).order_by('distanza').values_list('cittaB')] |
| 185 | + locations_in_range = [user_location] + locations_in_range |
| 186 | + |
| 187 | + for distance in locations_in_range: |
| 188 | + places = Place.objects.filter(location=distance) |
205 | 189 | for place in places:
|
206 | 190 | place_dict = vars(place)
|
207 | 191 | place_dict['labels'] = place.labels()
|
208 |
| - rated_place = Rating.objects.filter(place=place) |
209 |
| - if rated_place: |
| 192 | + if Rating.objects.filter(place=place).exists(): |
210 | 193 | place_dict['rated'] = True
|
211 |
| - rated_places.append(place_dict) |
212 |
| - else: |
213 |
| - close_places.append(place_dict) |
| 194 | + close_places.append(place_dict) |
214 | 195 |
|
215 | 196 | context = {
|
216 | 197 | 'search_form': search_near_places_form,
|
@@ -253,7 +234,7 @@ def place_details(request, place_id):
|
253 | 234 | context = {}
|
254 | 235 | if request.user.is_authenticated:
|
255 | 236 |
|
256 |
| - search_rec_form = SearchRecommendationForm(request.POST or None) |
| 237 | + search_rec_form = AddRatingForm(request.POST or None) |
257 | 238 | initial_mood = (Mood.joyful.name, Mood.joyful.value)
|
258 | 239 | search_rec_form.fields['mood'].initial = initial_mood
|
259 | 240 |
|
|
0 commit comments