def userprofile(request, template_name): ''' Produce the user profile page. ''' valdict = {} if request.method == "GET": #tcform = forms.TCUserProfForm(instance = request.user.tcuserprofile) tcupform = forms.TCUserPrefsForm(instance = request.user.tcuserprofile.prefs) mhform = forms.MHUserProfForm(instance = request.user.userprofile) elif request.method == "POST" and request.is_ajax(): try: status = 200 reason = "OK" #tcform = forms.TCUserProfForm(request.POST, instance=request.user.tcuserprofile) #if tcform.is_valid(): # tcform.save() tcupform = forms.TCUserPrefsForm(request.POST, instance=request.user.tcuserprofile.prefs) mhform = forms.MHUserProfForm(request.POST, instance=request.user.userprofile) if tcupform.is_valid and mhform.is_valid(): mhform.save() tcupform.save() except Exception as e: status = 500 reason = str(e) finally: return HttpResponse(content="{}", status=status, reason=reason, content_type="application/json") else: raise Exception("Bad request") vplaces_json, tcupform.numrated, tcupform.ncvisited = travelcat.get_visited_places_json(request.user.tcuserprofile) tcupform.numvisited = len(vplaces_json) if not request.user.tcuserprofile.prefs.is_complete(): valdict[TC_NEWUSERFLAG] = True if request.user.tcuserprofile.home: tcupform.homename = request.user.tcuserprofile.home.get_map_desc() valdict.update({'tcupform': tcupform, 'mhform': mhform, 'google_api_key': settings.getGoogleKeySuffix(), 'revplaces_json': vplaces_json}) return render_to_response(template_name, RequestContext(request, valdict ))
def validateplace(request, id=None, template_name=None): place = None if id: place=Place.objects.get(id=id) if request.method == 'POST': pl = Place.objects.get(id=id) # if no id was passed, we should error here placeform = forms.ValidatePlaceForm(request.POST, instance = pl) revs = Review.objects.filter(place=pl, user=request.user.tcuserprofile) if revs.count() > 0: rev = revs[0] rat = rev.ratings else: rev = Review(user=request.user.tcuserprofile, place=pl) rat = None ratingsform = forms.RatingsForm(request.POST, instance = rat) revform = forms.ReviewForm(request.POST, instance = rev) if placeform.is_valid(): pl = placeform.save(commit = False) if 'parentkey' in placeform.changed_data: pl.parent_id = placeform.cleaned_data['parentkey'] pl.save() placeform.save_m2m() if ratingsform.is_valid() and revform.is_valid(): save_review(revform, ratingsform) if pl.verified: return HttpResponseRedirect(reverse('travelcat:validateplace')) else: return HttpResponseRedirect(reverse('travelcat:validateplace', args=(id,))) else: # get unvplaces = Place.objects.filter(verified=False) if not place: # no specific place requested; grab first available nonverified # place below continent level, that has a parent if not unvplaces.exists(): # wooohooo, no unverified places in system! # route back to travelcat page with message request.session[TC_MESSAGE] = "No more unverified places! :)" return HttpResponseRedirect(reverse('travelcat:meow')) place = unvplaces.earliest('id') place.unv_remaining = unvplaces.count() place.populate_links() placeform = forms.ValidatePlaceForm(instance = place, initial={'parentkey': place.parent.id if place.parent else None}) revs = Review.objects.filter(user=request.user.tcuserprofile, place=place) rat = None if revs.count() > 0: rev = revs[0] rat = rev.ratings else: rev = Review(user=request.user.tcuserprofile, place=place) rat = Ratings() revform = forms.ReviewForm(instance = rev) ratingsform = forms.RatingsForm(instance=rat) placeform.instance.nrevs = place.review_set.count() vardict = {'placeform':placeform, 'revform':revform, 'google_api_key': settings.getGoogleKeySuffix(), 'ratingsform':ratingsform} resp = render_to_response(template_name, RequestContext(request,vardict)) return resp
def placedetail(request, placeid=None, template_name=None): vardict = {} place = None if placeid == "0" and TC_NEWPLACEDATA in request.session: # check session data for information about new places to add newplaces = request.session[TC_NEWPLACEDATA] parent = Place.objects.get(id=newplaces.pop()) del request.session[TC_NEWPLACEDATA] # add the place for npname, nplat, nplon, nprad in newplaces: np = Place(name=npname, wtlink=None, longitude = nplon, latitude = nplat, parent = parent, radiuskm = nprad) #logger.debug("Adding place: {}, parent: {}".format(np, parent)) np.save() parent = np place = parent elif placeid and placeid != "0": place = Place.objects.get(id=placeid) else: # no place supplied, or invalis place; route to Earth in general place = Place.objects.get(name="Earth") if request.method == 'GET': revalert = request.GET.get('revalert', False) revs = Review.objects.filter(user=request.user.tcuserprofile, place=place) rat = None if revs.count() > 0: rev = revs[0] rat = rev.ratings else: rev = Review(user=request.user.tcuserprofile, place=place) rat = Ratings() revform = forms.ReviewForm(instance = rev) ratingsform = forms.RatingsForm(instance=rat) elif request.method == 'POST' and request.is_ajax(): # user posting review info revs = Review.objects.filter(place=place, user=request.user.tcuserprofile) if revs.exists(): rev = revs[0] rat = rev.ratings else: rev = Review(user=request.user.tcuserprofile, place=place) rat = None try: resp = {} status = 200 reason = "OK" ratingsform = forms.RatingsForm(request.POST, instance = rat) revform = forms.ReviewForm(request.POST, instance = rev) #print ("saving rat/rev. revvalid:{} ratvalid:{}".format(revform.is_valid(), ratingsform.is_valid())) if ratingsform.is_valid() and revform.is_valid(): save_review(revform, ratingsform) if revform.data['home'] == 'true' and rev.place != request.user.tcuserprofile.home: request.user.tcuserprofile.home = rev.place request.user.tcuserprofile.save() resp['ratings_id'] = Review.objects.get(place=place, user=request.user.tcuserprofile).ratings.id resp['home_id'] = request.user.tcuserprofile.home.id except Exception as e: e.print_stack_trace() status = 500 reason = str(e) finally: return HttpResponse(content=json.dumps(resp), status=status, reason=reason, content_type="application/json") else: raise Exception("Bad request") place.populate_links() #vplaces_json, _ = travelcat.get_visited_places_json(request.user.tcuserprofile) # give the meowform/title the name of this place -- unless we weren't passed one. meowform= forms.MeowForm(initial={'destregion': place.get_map_desc() if placeid else ""}) vardict.update({'dest':place, 'meowform':meowform, 'rats':request.user.tcuserprofile.prefs, # used for labels; we grab a ratings object we know exists 'revform':revform, 'ratingsform':ratingsform, 'google_api_key': settings.getGoogleKeySuffix(), 'TC_WTLINKURL': TC_WTLINKURL, 'TC_HWLINKURL': TC_HWLINKURL, 'TC_CSLINKURL': TC_CSLINKURL, 'TC_HCLINKURL': TC_HCLINKURL, #'revplaces_json': vplaces_json }) if revalert != False: vardict.update({'revalert': True}) return render_to_response(template_name, RequestContext(request,vardict))