def generate_sample_gps_data(lat, lng, userPK, numMins, makeStop, heading, timeStamp): # Number of minute intervals to generate data. Initially set at every 1 minute generate a data point # This should be 1, normally. Changed to 0 to generate a LOT of data points at one time stamp timeInterval = 0 # A maxDisctanceInterval of 0.0001 is roughly 11 meters. # So, we're saying this data can't move faster than 11 meters/interval (initially set at 11m/minute) # This could change with car rides, etc. maxDistanceInterval = 0.0001 user = User.objects.get(pk = userPK) # If we need to make a stop (makeStop = True) then we need a boolean flag to keep track of whether # we've made that stop yet. We only want one stop per trip. stopMade = False # The stop will be anywhere from 10-60 minutes long stopLength = random.randint(10,60) while (numMins > 0): tmpLat = random.uniform(0.0, maxDistanceInterval) tmpLat = ceil(tmpLat*1000000)/1000000 lat = round(lat, 6) tmpLng = random.uniform(0.0, maxDistanceInterval) tmpLng = ceil(tmpLng*1000000)/1000000 lng = round(lng , 6) # Make next data point in the general direction of the heading chosen # The 'adjustInt' will be -1, 0 or 1. That will make the long/lat increase, # decrease or not change. if heading == 'N': lat = float(lat) + tmpLat adjustInt = random.randint(-1,1) lng = float(lng) - (adjustInt * tmpLng) elif heading == 'E': lng = float(lng) + tmpLng adjustInt = random.randint(-1,1) lat = float(lat) + (adjustInt * tmpLat) elif heading == 'S': lat = float(lat) - tmpLat adjustInt = random.randint(-1,1) lng = float(lng) - (adjustInt * tmpLng) elif heading == 'W': lng = float(lng) - tmpLng adjustInt = random.randint(-1,1) lat = float(lat) + (adjustInt * tmpLat) elif heading == 'A': adjustInt = random.randint(-1,1) lng = float(lng) + (adjustInt * tmpLng) lat = float(lat) + (adjustInt * tmpLat) timeStamp = timeStamp + datetime.timedelta(minutes = timeInterval) newLoc = Location(user = user, latitude = lat, longitude = lng, timeStamp = timeStamp) newLoc.save() numMins -= 1 return 0
def post(self, request): # input validation # if data does not pass validation we send response with errors validator = CustomValidator(self.validation_schema) if validator.request_validation(request): errors_dict = validator.request_validation(request) return JsonResponse(errors_dict, status=400) else: data = json.loads(request.body) # check if username or email already exists in our database if not (User.objects.filter( Q(username=data['username']) | Q(email=data['email'])).exists()): # if user doesn't exist we create him with data User.objects.create_user(username=data['username'], email=data['email'], password=data['password'], first_name=data['first_name'], last_name=data['last_name'], is_active=False) user = User.objects.get(username=data['username']) profile = Profile.objects.get(user_id=user.id) profile.location = Location(lat=data['location']['lat'], lng=data['location']['lng'], city=data['location']['city']) profile.save() # send email token_generator = TokenGenerator() confirmation_email = EmailSender() context = { 'uid': user.id, 'token': token_generator.make_token(user), } email = user.email mail_subject = 'Activate your HappyWalker account' html_email = get_template('acc_active_email.html') text_email = get_template('acc_active_email') confirmation_email.send_email(email, mail_subject, text_email, html_email, context) return JsonResponse( { "uid": user.id, "message": "user successfully created", }, status=201) # in case username or email already exists in database we return that message else: return JsonResponse( { "errors": [{ "message": "user with that credentials already exists", "code": "registration.user_exists" }] }, status=460)
longitude=loc.get('longitude', None), latitude=loc.get('latitude', None), address=loc.get('address', ' '), region=loc.get('region', ' '), locality=loc.get('locality', ' '), postal_code=loc.get('postal_code', ' '), country_name=loc.get('country_name', ' ')) if len(_locs) > 0: _loc = _locs[0] else: _loc = Location( raw_geodata=raw_geodata, longitude=loc.get('longitude', None), latitude=loc.get('latitude', None), address=loc.get('address', ' '), region=loc.get('region', ' '), locality=loc.get('locality', ' '), postal_code=loc.get('postal_code', ' '), country_name=loc.get('country_name', ' '), ) _loc.save() original.location = _loc else: original.location = None if 'working_locations' in org: for loc in org['working_locations']: raw_geodata = json.dumps(loc["raw_geodata"]) if isinstance( loc.get("raw_geodata"), dict) else loc.get("raw_geodata") if raw_geodata not in [ l.raw_geodata for l in original.working_locations.all()
def post(self, request, user_id): if user_id != 'me': return JsonResponse( {"message": "user_id is not 'me', access denied"}, status=401) # input validation # if data does not pass validation we send response with errors validator = CustomValidator(self.validation_schema) if validator.request_validation(request): errors_dict = validator.request_validation(request) return JsonResponse(errors_dict, status=400) else: data = json.loads(request.body) user = User.objects.get(id=request.user.id) profile = Profile.objects.get(user_id=request.user.id) user.first_name = data["first_name"] user.last_name = data["last_name"] user.save() if data['location']: profile.location = Location(lat=data['location']['lat'], lng=data['location']['lng'], city=data['location']['city']) profile.save() else: profile.location = Location() profile.save() if data["email"] != user.email: if User.objects.filter(email=data['email']).exists(): return JsonResponse( { "errors": [{ "message": "user with that credentials already exists", "code": "registration.user_exists" }] }, status=460) # sending confirmation letter to new email token_generator = TokenGenerator() confirmation_email = EmailSender() context = { 'uid': user.id, 'token': token_generator.make_token(user), 'new_email': data["email"] } email = data['email'] mail_subject = 'Email change confirmation' html_email = get_template('change_email.html') text_email = get_template('change_email') confirmation_email.send_email(email, mail_subject, text_email, html_email, context) return JsonResponse( {"message": "please confirm your new email"}, status=202) return JsonResponse({"message": "user successfully updated"}, status=201)
def update_user(request): if 'user' not in request.POST: return HttpResponseBadRequest() user = json.loads(request.POST['user']) if 'location' in user and user['location']: loc = user['location'] raw_geodata = json.dumps(loc["raw_geodata"]) if isinstance( loc.get("raw_geodata"), dict) else loc.get("raw_geodata") #Until we fix duplicate locations we have to do the following...lame. _locs = Location.objects.filter( raw_geodata=raw_geodata, longitude=loc.get('longitude', None), latitude=loc.get('latitude', None), address=loc.get('address', ' '), region=loc.get('region', ' '), locality=loc.get('locality', ' '), postal_code=loc.get('postal_code', ' '), country_name=loc.get('country_name', ' ')) if len(_locs) > 0: _loc = _locs[0] else: _loc = Location( raw_geodata=raw_geodata, longitude=loc.get('longitude', None), latitude=loc.get('latitude', None), address=loc.get('address', ' '), region=loc.get('region', ' '), locality=loc.get('locality', ' '), postal_code=loc.get('postal_code', ' '), country_name=loc.get('country_name', ' '), ) _loc.save() request.user.location = _loc else: request.user.location = None str_fields = [ 'first_name', 'last_name', 'email', 'gender', 'bio', 'url', 'twitter_id', 'flickr_id', 'youtube_id', 'vimeo_id', 'blog_url', ] settings_fields = [ 'enable_jumo_updates', 'email_stream_frequency', 'post_to_fb', ] int_fields = [ 'birth_year', ] if 'enable_followed_notification' in user: try: sub = request.user.subscriptions.get(id=NOTIFICATIONS_PUB) except Subscription.DoesNotExist: sub = Subscription.get_or_create(user=request.user, pub_id=NOTIFICATIONS_PUB) if sub.subscribed <> user['enable_follow_notification']: sub.subscribed = user['enable_follow_notification'] sub.save() for f in str_fields: if f in user and user[f] != getattr(request.user, f): setattr(request.user, f, user[f]) for f in settings_fields: settings = user['settings'] if f in settings: setattr(request.user, f, settings[f]) for f in int_fields: if f in user and user[f] != getattr(request.user, f): if user[f] == '': user[f] = None setattr(request.user, f, user[f]) if 'password' in user and user['password'] != '': request.user.password = hash_password(user['password']) if 'username' in user and user['username'] != request.user.username: _username = request.user.username request.user.username = create_handle(user['username']) cache.bust_on_handle(request.user, _username, False) request.user.save() cache.bust_on_handle(request.user, request.user.username) return json_response({'result': request.user.username})
def update_user(request): if 'user' not in request.POST: return HttpResponseBadRequest() user = json.loads(request.POST['user']) if 'location' in user and user['location']: loc = user['location'] raw_geodata = json.dumps(loc["raw_geodata"]) if isinstance(loc.get("raw_geodata"), dict) else loc.get("raw_geodata") #Until we fix duplicate locations we have to do the following...lame. _locs = Location.objects.filter(raw_geodata = raw_geodata, longitude = loc.get('longitude', None), latitude = loc.get('latitude', None), address = loc.get('address', ' '), region = loc.get('region', ' '), locality = loc.get('locality', ' '), postal_code = loc.get('postal_code', ' '), country_name = loc.get('country_name', ' ')) if len(_locs) > 0: _loc = _locs[0] else: _loc = Location(raw_geodata = raw_geodata, longitude = loc.get('longitude', None), latitude = loc.get('latitude', None), address = loc.get('address', ' '), region = loc.get('region', ' '), locality = loc.get('locality', ' '), postal_code = loc.get('postal_code', ' '), country_name = loc.get('country_name', ' '),) _loc.save() request.user.location = _loc else: request.user.location = None str_fields = [ 'first_name', 'last_name', 'email', 'gender', 'bio', 'url', 'twitter_id', 'flickr_id', 'youtube_id', 'vimeo_id', 'blog_url', ] settings_fields = [ 'enable_jumo_updates', 'email_stream_frequency', 'post_to_fb', ] int_fields = [ 'birth_year', ] if 'enable_followed_notification' in user: try: sub = request.user.subscriptions.get(id=NOTIFICATIONS_PUB) except Subscription.DoesNotExist: sub = Subscription.get_or_create(user=request.user, pub_id=NOTIFICATIONS_PUB) if sub.subscribed <> user['enable_follow_notification']: sub.subscribed = user['enable_follow_notification'] sub.save() for f in str_fields: if f in user and user[f] != getattr(request.user, f): setattr(request.user, f, user[f]) for f in settings_fields: settings = user['settings'] if f in settings: setattr(request.user, f, settings[f]) for f in int_fields: if f in user and user[f] != getattr(request.user, f): if user[f] == '': user[f] = None setattr(request.user, f, user[f]) if 'password' in user and user['password'] != '': request.user.password = hash_password(user['password']) if 'username' in user and user['username'] != request.user.username: _username = request.user.username request.user.username = create_handle(user['username']) cache.bust_on_handle(request.user, _username, False) request.user.save() cache.bust_on_handle(request.user, request.user.username) return json_response({'result' : request.user.username})
_locs = Location.objects.filter(raw_geodata = raw_geodata, longitude = loc.get('longitude', None), latitude = loc.get('latitude', None), address = loc.get('address', ' '), region = loc.get('region', ' '), locality = loc.get('locality', ' '), postal_code = loc.get('postal_code', ' '), country_name = loc.get('country_name', ' ')) if len(_locs) > 0: _loc = _locs[0] else: _loc = Location(raw_geodata = raw_geodata, longitude = loc.get('longitude', None), latitude = loc.get('latitude', None), address = loc.get('address', ' '), region = loc.get('region', ' '), locality = loc.get('locality', ' '), postal_code = loc.get('postal_code', ' '), country_name = loc.get('country_name', ' '),) _loc.save() original.location = _loc else: original.location = None if 'working_locations' in org: for loc in org['working_locations']: raw_geodata = json.dumps(loc["raw_geodata"]) if isinstance(loc.get("raw_geodata"), dict) else loc.get("raw_geodata") if raw_geodata not in [l.raw_geodata for l in original.working_locations.all()]: locs = Location.objects.filter(raw_geodata = raw_geodata, longitude = loc.get('longitude', None), latitude = loc.get('latitude', None),