def form_valid(self, form): point = Point() point.longitude = self.request.session["point"]["longitude"] point.latitude = self.request.session["point"]["latitude"] point.save() form.instance.category = get_object_or_404(Category, name=self.kwargs.get("category")) form.instance.point = point self.object = form.save() return HttpResponseRedirect(self.get_success_url())
def login(request): ''' Create a new user or add the existing user to the session. Logged in users are kept in the session. This is called when a user has no session. Users are also kept in the database and can be looked up by a user_id the user keeps in long term storage in the case that the session ends. Each login will also update the users' coordinates. ''' user_id = request.POST['userId'] longitude = request.POST['lon'] latitude = request.POST['lat'] user = None # create the point point = Point() point.latitude = latitude point.longitude = longitude point.save() # get or create the user if is_valid_uuid(user_id): user_query = User.objects.filter(uuid=user_id) if user_query.count() > 0: user = user_query.get() if not user: user = User() # set the new point user.uuid = uuid4() # generate a new uuid every login user.point = point user.save() request.session['user'] = {'pk': user.pk, 'uuid': str(user.uuid)} # this one may not be necessary, but it may be better to keep this in the session as well request.session['point'] = {'pk': point.pk, 'longitude': point.longitude, 'latitude': point.latitude} return HttpResponse(user.uuid)